From 1d2a4aea4d8547886cc413a30fde0ca13b1c59d2 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 11 Oct 2024 18:45:54 +0300 Subject: [PATCH] messagix: fix some inconsistencies --- pkg/messagix/client.go | 6 +++--- pkg/messagix/facebook.go | 2 +- pkg/messagix/graphql.go | 2 +- pkg/messagix/http.go | 18 +++++++++++++----- pkg/messagix/instagram.go | 10 +++++----- pkg/messagix/js_module_parser.go | 8 +------- pkg/messagix/mercury.go | 2 +- pkg/messagix/socket.go | 8 ++++---- pkg/messagix/syncManager.go | 8 ++++---- 9 files changed, 33 insertions(+), 31 deletions(-) diff --git a/pkg/messagix/client.go b/pkg/messagix/client.go index abfa2f6..64aaf33 100644 --- a/pkg/messagix/client.go +++ b/pkg/messagix/client.go @@ -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{}) @@ -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") @@ -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") diff --git a/pkg/messagix/facebook.go b/pkg/messagix/facebook.go index 0339cfe..1d5ba25 100644 --- a/pkg/messagix/facebook.go +++ b/pkg/messagix/facebook.go @@ -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") diff --git a/pkg/messagix/graphql.go b/pkg/messagix/graphql.go index 1cec0b2..03cf575 100644 --- a/pkg/messagix/graphql.go +++ b/pkg/messagix/graphql.go @@ -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") diff --git a/pkg/messagix/http.go b/pkg/messagix/http.go index 111a3a5..4a3f222 100644 --- a/pkg/messagix/http.go +++ b/pkg/messagix/http.go @@ -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") @@ -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 { @@ -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 { diff --git a/pkg/messagix/instagram.go b/pkg/messagix/instagram.go index 171965a..f7ee776 100644 --- a/pkg/messagix/instagram.go +++ b/pkg/messagix/instagram.go @@ -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") @@ -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 @@ -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 != "" { @@ -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", "*/*") @@ -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") diff --git a/pkg/messagix/js_module_parser.go b/pkg/messagix/js_module_parser.go index 8c2bbee..23ef528 100644 --- a/pkg/messagix/js_module_parser.go +++ b/pkg/messagix/js_module_parser.go @@ -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 } diff --git a/pkg/messagix/mercury.go b/pkg/messagix/mercury.go index b649334..582b40c 100644 --- a/pkg/messagix/mercury.go +++ b/pkg/messagix/mercury.go @@ -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")) diff --git a/pkg/messagix/socket.go b/pkg/messagix/socket.go index 30bf210..0234b66 100644 --- a/pkg/messagix/socket.go +++ b/pkg/messagix/socket.go @@ -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} @@ -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 } diff --git a/pkg/messagix/syncManager.go b/pkg/messagix/syncManager.go index cfc0abd..b34bb94 100644 --- a/pkg/messagix/syncManager.go +++ b/pkg/messagix/syncManager.go @@ -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"} @@ -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"} },