Skip to content

Commit

Permalink
Merge pull request #54 from mautrix/fix/referer-for-uploads
Browse files Browse the repository at this point in the history
Fix referer header for upload requests
  • Loading branch information
javiercr authored May 2, 2024
2 parents 9ab9bb0 + f85d6e9 commit 19cb048
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 3 deletions.
4 changes: 4 additions & 0 deletions messagix/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ func (c *Client) getEndpoint(name string) string {
panic(fmt.Sprintf("messagix-client: endpoint %s not found", name))
}

func (c *Client) getEndpointForThreadID(threadID int64) string {
return c.getEndpoint("thread") + strconv.FormatInt(threadID, 10) + "/"
}

func (c *Client) IsAuthenticated() bool {
var isAuthenticated bool
if c.platform.IsMessenger() {
Expand Down
1 change: 1 addition & 0 deletions messagix/data/endpoints/facebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func makeFacebookEndpoints(host string) map[string]string {
"base_url": baseURL,
"login_page": baseURL + "/login",
"messages": baseURL + "/messages",
"thread": baseURL + "/t/",
"cookie_consent": baseURL + "/cookie/consent/",
"graphql": baseURL + "/api/graphql/",
"media_upload": baseURL + "/ajax/mercury/upload.php?",
Expand Down
1 change: 1 addition & 0 deletions messagix/data/endpoints/instagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var InstagramEndpoints = map[string]string{
"base_url": instaBaseUrl, //+ "/",
"login_page": instaBaseUrl + "/accounts/login/",
"messages": instaBaseUrl + "/direct/inbox/",
"thread": instaBaseUrl + "/direct/t/",
"graphql": instaBaseUrl + "/api/graphql",
"cookie_consent": "https://graphql.instagram.com/graphql/",
"default_graphql": "https://graphql.instagram.com/graphql/",
Expand Down
4 changes: 2 additions & 2 deletions messagix/mercury.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type WaveformData struct {
SamplingFrequency int `json:"sampling_frequency"`
}

func (c *Client) SendMercuryUploadRequest(ctx context.Context, media *MercuryUploadMedia) (*types.MercuryUploadResponse, error) {
func (c *Client) SendMercuryUploadRequest(ctx context.Context, threadID int64, media *MercuryUploadMedia) (*types.MercuryUploadResponse, error) {
urlQueries := c.NewHttpQuery()
queryValues, err := query.Values(urlQueries)
if err != nil {
Expand All @@ -46,7 +46,7 @@ func (c *Client) SendMercuryUploadRequest(ctx context.Context, media *MercuryUpl
h.Set("accept", "*/*")
h.Set("content-type", contentType)
h.Set("origin", c.getEndpoint("base_url"))
h.Set("referer", c.getEndpoint("messages"))
h.Set("referer", c.getEndpointForThreadID(threadID))
h.Set("sec-fetch-dest", "empty")
h.Set("sec-fetch-mode", "cors")
h.Set("sec-fetch-site", "same-origin") // header is required
Expand Down
3 changes: 2 additions & 1 deletion msgconv/from-matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (mc *MessageConverter) downloadMatrixMedia(ctx context.Context, content *ev
}

func (mc *MessageConverter) reuploadFileToMeta(ctx context.Context, evt *event.Event, content *event.MessageEventContent) (*types.MercuryUploadResponse, error) {
threadID := mc.GetData(ctx).ThreadID
data, mimeType, fileName, err := mc.downloadMatrixMedia(ctx, content)
if err != nil {
return nil, err
Expand All @@ -146,7 +147,7 @@ func (mc *MessageConverter) reuploadFileToMeta(ctx context.Context, evt *event.E
mimeType = "audio/mp4"
fileName += ".m4a"
}
resp, err := mc.GetClient(ctx).SendMercuryUploadRequest(ctx, &messagix.MercuryUploadMedia{
resp, err := mc.GetClient(ctx).SendMercuryUploadRequest(ctx, threadID, &messagix.MercuryUploadMedia{
Filename: fileName,
MimeType: mimeType,
MediaData: data,
Expand Down

0 comments on commit 19cb048

Please sign in to comment.