Skip to content

Commit

Permalink
messagix: fix some inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Oct 11, 2024
1 parent 0b13219 commit 1d2a4ae
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 31 deletions.
6 changes: 3 additions & 3 deletions pkg/messagix/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const OSVersion = "6.8.0"
const SecCHPlatform = `"` + OSName + `"`
const SecCHPlatformVersion = `"` + OSVersion + `"`
const SecCHMobile = "?0"
const SecCHModel = ""
const SecCHModel = `""`
const SecCHPrefersColorScheme = "light"

type EventHandler func(evt interface{})
Expand Down Expand Up @@ -293,7 +293,7 @@ func (c *Client) IsConnected() bool {
func (c *Client) sendCookieConsent(jsDatr string) error {

var payloadQuery interface{}
h := c.buildHeaders(false)
h := c.buildHeaders(false, false)
h.Set("sec-fetch-dest", "empty")
h.Set("sec-fetch-mode", "cors")

Expand All @@ -319,7 +319,7 @@ func (c *Client) sendCookieConsent(jsDatr string) error {
IgDid: c.cookies.Get("ig_did"),
ThirdPartyTrackingOptIn: true,
Input: struct {
ClientMutationID int "json:\"client_mutation_id,omitempty\""
ClientMutationID int `json:"client_mutation_id,omitempty"`
}{0},
})
h.Del("x-csrftoken")
Expand Down
2 changes: 1 addition & 1 deletion pkg/messagix/facebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (fb *FacebookMethods) RegisterPushNotifications(endpoint string, keys PushK

payloadBytes := []byte(form.Encode())

headers := c.buildHeaders(true)
headers := c.buildHeaders(true, false)
headers.Set("Referer", c.getEndpoint("host"))
headers.Set("Sec-fetch-site", "same-origin")

Expand Down
2 changes: 1 addition & 1 deletion pkg/messagix/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c *Client) makeGraphQLRequest(name string, variables interface{}) (*http.R

payloadBytes := []byte(form.Encode())

headers := c.buildHeaders(true)
headers := c.buildHeaders(true, false)
headers.Set("x-fb-friendly-name", graphQLDoc.FriendlyName)
headers.Set("sec-fetch-dest", "empty")
headers.Set("sec-fetch-mode", "cors")
Expand Down
18 changes: 13 additions & 5 deletions pkg/messagix/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (c *Client) makeRequestDirect(url string, method string, headers http.Heade
return response, responseBody, nil
}

func (c *Client) buildHeaders(withCookies bool) http.Header {
func (c *Client) buildHeaders(withCookies, isSecFetchDocument bool) http.Header {
headers := http.Header{}
headers.Set("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
headers.Set("accept-language", "en-US,en;q=0.9")
Expand All @@ -221,9 +221,17 @@ func (c *Client) buildHeaders(withCookies bool) http.Header {
headers.Set("sec-ch-ua-model", SecCHModel)
headers.Set("sec-ch-ua-platform-version", SecCHPlatformVersion)

c.addFacebookHeaders(&headers)
if !c.Platform.IsMessenger() {
c.addInstagramHeaders(&headers)
if isSecFetchDocument {
headers.Set("sec-fetch-dest", "document")
headers.Set("sec-fetch-mode", "navigate")
headers.Set("sec-fetch-site", "none") // header is required, otherwise they dont send the csr bitmap data in the response. lets also include the other headers to be sure
headers.Set("sec-fetch-user", "?1")
headers.Set("upgrade-insecure-requests", "1")
} else {
c.addFacebookHeaders(&headers)
if !c.Platform.IsMessenger() {
c.addInstagramHeaders(&headers)
}
}

if c.cookies != nil && withCookies {
Expand Down Expand Up @@ -284,7 +292,7 @@ func (c *Client) sendLoginRequest(form url.Values, loginUrl string) (*http.Respo
}

func (c *Client) buildLoginHeaders() http.Header {
h := c.buildHeaders(true)
h := c.buildHeaders(true, false)
if c.Platform.IsMessenger() {
h = c.addLoginFacebookHeaders(h)
} else {
Expand Down
10 changes: 5 additions & 5 deletions pkg/messagix/instagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (ig *InstagramMethods) Login(identifier, password string) (*cookies.Cookies
if _, err := ig.client.configs.SetupConfigs(nil); err != nil {
return nil, err
}
h := ig.client.buildHeaders(false)
h := ig.client.buildHeaders(false, false)
h.Set("x-web-device-id", ig.client.cookies.Get(cookies.IGCookieDeviceID))
h.Set("sec-fetch-dest", "empty")
h.Set("sec-fetch-mode", "cors")
Expand Down Expand Up @@ -97,7 +97,7 @@ func (ig *InstagramMethods) Login(identifier, password string) (*cookies.Cookies
}

func (ig *InstagramMethods) FetchProfile(username string) (*responses.ProfileInfoResponse, error) {
h := ig.client.buildHeaders(true)
h := ig.client.buildHeaders(true, false)
h.Set("x-requested-with", "XMLHttpRequest")
h.Set("referer", ig.client.getEndpoint("base_url")+username+"/")
reqUrl := ig.client.getEndpoint("web_profile_info") + "username=" + username
Expand All @@ -119,7 +119,7 @@ func (ig *InstagramMethods) FetchProfile(username string) (*responses.ProfileInf
}

func (ig *InstagramMethods) FetchMedia(mediaID, mediaShortcode string) (*responses.FetchMediaResponse, error) {
h := ig.client.buildHeaders(true)
h := ig.client.buildHeaders(true, false)
h.Set("x-requested-with", "XMLHttpRequest")
referer := ig.client.getEndpoint("base_url")
if mediaShortcode != "" {
Expand All @@ -146,7 +146,7 @@ func (ig *InstagramMethods) FetchMedia(mediaID, mediaShortcode string) (*respons
}

func (ig *InstagramMethods) FetchReel(reelIds []string, mediaID string) (*responses.ReelInfoResponse, error) {
h := ig.client.buildHeaders(true)
h := ig.client.buildHeaders(true, false)
h.Set("x-requested-with", "XMLHttpRequest")
h.Set("referer", ig.client.getEndpoint("base_url"))
h.Set("Accept", "*/*")
Expand Down Expand Up @@ -205,7 +205,7 @@ func (ig *InstagramMethods) RegisterPushNotifications(endpoint string, keys Push

payloadBytes := []byte(form.Encode())

headers := c.buildHeaders(true)
headers := c.buildHeaders(true, false)
headers.Set("x-requested-with", "XMLHttpRequest")
headers.Set("Referer", c.getEndpoint("host"))
headers.Set("Referrer-Policy", "strict-origin-when-cross-origin")
Expand Down
8 changes: 1 addition & 7 deletions pkg/messagix/js_module_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,8 @@ func (m *ModuleParser) SetClientInstance(cli *Client) {
}

func (m *ModuleParser) fetchPageData(page string) ([]byte, error) { // just log.fatal if theres an error because the library should not be able to continue then
headers := m.client.buildHeaders(true)
headers.Set("connection", "keep-alive")
headers := m.client.buildHeaders(true, true)
//headers.Set("host", m.client.getEndpoint("host"))
headers.Set("sec-fetch-dest", "document")
headers.Set("sec-fetch-mode", "navigate")
headers.Set("sec-fetch-site", "none") // header is required, otherwise they dont send the csr bitmap data in the response. lets also include the other headers to be sure
headers.Set("sec-fetch-user", "?1")
headers.Set("upgrade-insecure-requests", "1")
_, responseBody, err := m.client.MakeRequest(page, "GET", headers, nil, types.NONE)
return responseBody, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/messagix/mercury.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *Client) SendMercuryUploadRequest(ctx context.Context, threadID int64, m
if err != nil {
return nil, err
}
h := c.buildHeaders(true)
h := c.buildHeaders(true, false)
h.Set("accept", "*/*")
h.Set("content-type", contentType)
h.Set("origin", c.getEndpoint("base_url"))
Expand Down
8 changes: 4 additions & 4 deletions pkg/messagix/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var (
//lint:ignore U1000 - alternatives for minimal*Sync
fbReconnectSync = []int64{1, 2, 5, 16, 95, 104}
//lint:ignore U1000 - alternatives for minimal*Sync
igInitialSync = []int64{1, 2, 6, 7, 16, 28, 198}
igInitialSync = []int64{1, 2, 6, 7, 16 /*28,*/, 89, 197, 198}
//lint:ignore U1000 - alternatives for minimal*Sync
fbInitialSync = []int64{1, 2 /*5,*/, 16, 26, 28, 89, 95, 104, 120, 140, 141, 142, 143, 145, 196, 197, 198, 202}

Expand Down Expand Up @@ -163,12 +163,12 @@ func (s *Socket) Connect() error {

func (s *Socket) BuildBrokerUrl() string {
query := &url.Values{}
query.Add("cid", s.client.configs.browserConfigTable.MqttWebDeviceID.ClientID)
query.Add("sid", strconv.FormatInt(s.sessionId, 10))
query.Add("cid", s.client.configs.browserConfigTable.MqttWebDeviceID.ClientID)

encodedQuery := query.Encode()
if !strings.HasSuffix(s.broker, "=") {
return s.broker + "&" + encodedQuery
if strings.HasSuffix(s.broker, "?") {
return s.broker + encodedQuery
} else {
return s.broker + "&" + encodedQuery
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/messagix/syncManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ func (c *Client) NewSyncManager() *SyncManager {
client: c,
store: map[int64]*socket.QueryMetadata{
1: {SendSyncParams: false, SyncChannel: socket.MailBox},
2: {SendSyncParams: true, SyncChannel: socket.Contact}, // FB sync params (previously null?: {"locale": "en_US"}
2: {SendSyncParams: true, SyncChannel: socket.Contact}, // FB/IG sync params (previously null?): {"locale": "en_US"}
//2: {SendSyncParams: false, SyncChannel: socket.Contact},
5: {SendSyncParams: true}, // FB sync params: {"locale": "en_US"} TODO may be removed
6: {SendSyncParams: true}, // IG sync params: {"locale": "en_US"}
7: {SendSyncParams: true}, // IG sync params: {"mnet_rank_types": [44]}
16: {SendSyncParams: true}, // FB/IG sync params: {"locale": "en_US"}
26: {SendSyncParams: true}, // FB sync params: {"locale": "en_US"}
28: {SendSyncParams: true}, // FB/IG sync params: {"locale": "en_US"}
89: {SendSyncParams: true}, // FB sync params: {"locale": "en_US"}
28: {SendSyncParams: true}, // FB sync params: {"locale": "en_US"}
89: {SendSyncParams: true}, // FB/IG sync params: {"locale": "en_US"}
95: {SendSyncParams: false, SyncChannel: socket.Contact},
104: {SendSyncParams: true}, // FB sync params: {"locale": "en_US"}
120: {SendSyncParams: true}, // FB sync params: {"locale": "en_US"}
Expand All @@ -45,7 +45,7 @@ func (c *Client) NewSyncManager() *SyncManager {
143: {SendSyncParams: true}, // FB sync params: {"locale": "en_US"}
145: {SendSyncParams: true}, // FB sync params: {"locale": "en_US"}
196: {SendSyncParams: true}, // FB sync params: {"locale": "en_US"}
197: {SendSyncParams: true}, // FB sync params: {"locale": "en_US"}
197: {SendSyncParams: true}, // FB/IG sync params: {"locale": "en_US"}
198: {SendSyncParams: true}, // FB/IG sync params: {"locale": "en_US"}
202: {SendSyncParams: true}, // FB sync params: {"locale": "en_US"}
},
Expand Down

0 comments on commit 1d2a4ae

Please sign in to comment.