Skip to content

Commit

Permalink
initial ajax client implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
0xzer committed May 24, 2024
1 parent c06a4ea commit af1ee56
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 6 deletions.
46 changes: 46 additions & 0 deletions messagix/ajax.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package messagix

import (
"github.com/google/go-querystring/query"
"go.mau.fi/mautrix-meta/messagix/types"
)

type AjaxClient struct {
client *Client
}

func (c *Client) newAjaxClient() *AjaxClient {
return &AjaxClient{
client: c,
}
}

func (ac *AjaxClient) SendQMRequest(path string, payload interface{}) error {
form, err := query.Values(payload)
if err != nil {
return err
}
payloadBytes := []byte(form.Encode())

ac.client.Logger.Info().
Str("url_encoded_payload", string(payloadBytes)).
Str("url_path", path).
Msg("Ajax QM Request")

baseURL := ac.client.getEndpoint("base_url")
headers := ac.client.buildHeaders(true)
headers.Set("sec-fetch-dest", "empty")
headers.Set("sec-fetch-mode", "no-cors")
headers.Set("sec-fetch-site", "same-origin")
headers.Set("origin", baseURL)
headers.Set("referer", ac.client.getEndpoint("messages")+"/")

reqUrl := baseURL + path
resp, respData, err := ac.client.MakeRequest(reqUrl, "POST", headers, payloadBytes, types.FORM)
if err != nil {
return err
}

ac.client.Logger.Info().Str("response_body", string(respData)).Int("status_code", resp.StatusCode).Msg("Ajax QM Response")
return nil
}
23 changes: 22 additions & 1 deletion messagix/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ const SecCHMobile = "?0"
const SecCHModel = ""
const SecCHPrefersColorScheme = "light"

type ClientOptions struct {
WithAjaxClient bool
}
type EventHandler func(evt interface{})
type Client struct {
Instagram *InstagramMethods
Expand All @@ -53,6 +56,7 @@ type Client struct {
eventHandler EventHandler
configs *Configs
SyncManager *SyncManager
ajax *AjaxClient

cookies *cookies.Cookies
httpProxy func(*http.Request) (*url.URL, error)
Expand All @@ -77,7 +81,7 @@ type Client struct {
sendMessagesCond *sync.Cond
}

func NewClient(cookies *cookies.Cookies, logger zerolog.Logger) *Client {
func NewClient(cookies *cookies.Cookies, logger zerolog.Logger, opts *ClientOptions) *Client {
if cookies.Platform == types.Unset {
panic("messagix: platform must be set in cookies")
}
Expand Down Expand Up @@ -112,6 +116,12 @@ func NewClient(cookies *cookies.Cookies, logger zerolog.Logger) *Client {
}
cli.socket = cli.newSocketClient()

if opts != nil {
if opts.WithAjaxClient {
cli.ajax = cli.newAjaxClient()
}
}

return cli
}

Expand All @@ -137,6 +147,17 @@ func (c *Client) LoadMessagesPage() (types.UserInfo, *table.LSTable, error) {
} else {
currentUser = &c.configs.browserConfigTable.PolarisViewer
}

if c.ajax != nil {
err = c.ajax.SendQMRequest(
c.configs.Eqmc.AjaxURL,
c.configs.Eqmc.ToAjaxQMPayload(),
)
if err != nil {
return nil, nil, err
}
}

return currentUser, ls, nil
}

Expand Down
1 change: 1 addition & 0 deletions messagix/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Configs struct {
CometReq string
VersionId int64
Jazoest string
Eqmc *types.Eqmc
WebSessionId string
Bitmap *crypto.Bitmap
CsrBitmap *crypto.Bitmap
Expand Down
4 changes: 4 additions & 0 deletions messagix/methods/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ func NeedUpdateSyncGroups(data *table.LSTable) bool {
return len(data.LSExecuteFirstBlockForSyncTransaction) > 0 ||
len(data.LSUpsertSyncGroupThreadsRange) > 0
}

func SpoofMarkerPageTime() int64 {
return int64(rand.Intn(700-150+1) + 150)
}
1 change: 1 addition & 0 deletions messagix/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (m *ModuleParser) HandleRawJSON(data []byte, id string) error {
}
m.client.configs.Jazoest = ajaxData.Jazoest
m.client.configs.CometReq = ajaxData.CometReq
m.client.configs.Eqmc = &d
}
return nil
}
Expand Down
31 changes: 27 additions & 4 deletions messagix/types/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"

"go.mau.fi/mautrix-meta/messagix/methods"
"go.mau.fi/mautrix-meta/messagix/table"
)

Expand Down Expand Up @@ -202,11 +203,33 @@ type EnvJSON struct {

type Eqmc struct {
AjaxURL string `json:"u,omitempty"`
HasteSessionId string `json:"e,omitempty"`
S string `json:"s,omitempty"`
W int `json:"w,omitempty"`
// also HasteSessionId
EventId string `json:"e,omitempty"`
ScriptPath string `json:"s,omitempty"`
Weight int `json:"w,omitempty"`
FbDtsg string `json:"f,omitempty"`
L any `json:"l,omitempty"`
LsdToken any `json:"l,omitempty"`
}

type AjaxQMPayload struct {
EventId string `url:"event_id"`
MarkerPageTime int64 `url:"marker_page_time"`
ScriptPath string `url:"script_path"`
Weight int `url:"weight"`
// 0 or 1
ClientStart int `url:"client_start"`
FbDtsg string `url:"fb_dtsg"`
}

func (e *Eqmc) ToAjaxQMPayload() AjaxQMPayload {
return AjaxQMPayload{
EventId: e.EventId,
MarkerPageTime: methods.SpoofMarkerPageTime(),
ScriptPath: e.ScriptPath,
Weight: e.Weight,
ClientStart: 1, // hardcoded 1
FbDtsg: e.FbDtsg,
}
}

type AjaxQueryParams struct {
Expand Down
5 changes: 4 additions & 1 deletion user.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,10 @@ func (user *User) unlockedConnectWithCookies(cookies *cookies.Cookies) error {
log := user.log.With().Str("component", "messagix").Logger()
user.log.Debug().Msg("Connecting to Meta")
// TODO set proxy for media client?
cli := messagix.NewClient(cookies, log)
clientOpts := messagix.ClientOptions {
WithAjaxClient: true,
}
cli := messagix.NewClient(cookies, log, &clientOpts)
if user.bridge.Config.Meta.GetProxyFrom != "" || user.bridge.Config.Meta.Proxy != "" {
cli.GetNewProxy = user.getProxy
if !cli.UpdateProxy("connect") {
Expand Down

0 comments on commit af1ee56

Please sign in to comment.