Skip to content

Commit

Permalink
twittermeow: clean up some headers
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Dec 13, 2024
1 parent 9a28dc7 commit eabf965
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
1 change: 1 addition & 0 deletions pkg/connector/handlematrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (tc *TwitterClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.

uploadMediaParams := &payload.UploadMediaQuery{
MediaCategory: MSG_TYPE_TO_MEDIA_CATEGORY[content.MsgType],
MediaType: content.Info.MimeType,
}
if content.Info.MimeType == "image/gif" {
uploadMediaParams.MediaCategory = "dm_gif"
Expand Down
6 changes: 3 additions & 3 deletions pkg/twittermeow/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func uploadAndSendImageTest(initialInboxData *response.XInboxData) {
}

uploadQuery := &payload.UploadMediaQuery{
MediaType: payload.MEDIA_TYPE_IMAGE_JPEG,
MediaType: "image/jpeg",
MediaCategory: payload.MEDIA_CATEGORY_DM_IMAGE,
}

Expand Down Expand Up @@ -252,7 +252,7 @@ func uploadAndSendGifTest(initialInboxData *response.XInboxData) {

uploadQuery := &payload.UploadMediaQuery{
SourceURL: "https://media1.giphy.com/media/v1.Y2lkPWU4MjZjOWZjYzdkYTk5YWM3ODE2MjczYTlkYWFiZjY2MDkxYTIyZDJmMjVlMDAwYiZlcD12MV9naWZzX2NhdGVnb3JpZXNfY2F0ZWdvcnlfdGFnJmN0PWc/z3HFoEzXCMykr4L0TB/giphy.gif",
MediaType: payload.MEDIA_TYPE_IMAGE_GIF,
MediaType: "image/gif",
MediaCategory: payload.MEDIA_CATEGORY_DM_GIF,
}
mediaResult, err := cli.UploadMedia(uploadQuery, nil)
Expand Down Expand Up @@ -294,7 +294,7 @@ func uploadAndSendVideoTest(initialInboxData *response.XInboxData) {
}

uploadQuery := &payload.UploadMediaQuery{
MediaType: payload.MEDIA_TYPE_VIDEO_MP4,
MediaType: "video/mp4",
MediaCategory: payload.MEDIA_CATEGORY_DM_VIDEO,
}

Expand Down
10 changes: 1 addition & 9 deletions pkg/twittermeow/data/payload/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,6 @@ const (
MEDIA_CATEGORY_DM_GIF MediaCategory = "dm_gif"
)

type MediaType string

const (
MEDIA_TYPE_IMAGE_JPEG MediaType = "image/jpeg"
MEDIA_TYPE_IMAGE_GIF MediaType = "image/gif"
MEDIA_TYPE_VIDEO_MP4 MediaType = "video/mp4"
)

type UploadMediaQuery struct {
Command string `url:"command,omitempty"`
TotalBytes int `url:"total_bytes,omitempty"`
Expand All @@ -170,7 +162,7 @@ type UploadMediaQuery struct {
VideoDurationMS float32 `url:"video_duration_ms,omitempty"`
OriginalMD5 string `url:"original_md5,omitempty"`
SegmentIndex int `url:"segment_index,omitempty"`
MediaType MediaType `url:"media_type,omitempty"`
MediaType string `url:"media_type,omitempty"`
MediaCategory MediaCategory `url:"media_category,omitempty"`
}

Expand Down
29 changes: 15 additions & 14 deletions pkg/twittermeow/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@ const SecCHPrefersColorScheme = "light"
const UDID = OSName + "/" + BrowserName

var BaseHeaders = http.Header{
"accept": []string{"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"},
"accept-language": []string{"en-US,en;q=0.9"},
"user-agent": []string{UserAgent},
"sec-ch-ua": []string{SecCHUserAgent},
"sec-ch-ua-platform": []string{SecCHPlatform},
"sec-ch-ua-mobile": []string{SecCHMobile},
"referer": []string{endpoints.BASE_URL + "/"},
"origin": []string{endpoints.BASE_URL},
//"sec-ch-prefers-color-scheme": []string{SecCHPrefersColorScheme},
//"sec-ch-ua-full-version-list": []string{SecCHFullVersionList},
//"sec-ch-ua-model": []string{SecCHModel},
//"sec-ch-ua-platform-version": []string{SecCHPlatformVersion},
"Accept": []string{"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"},
"Accept-Language": []string{"en-US,en;q=0.9"},
"User-Agent": []string{UserAgent},
"Sec-Ch-Ua": []string{SecCHUserAgent},
"Sec-Ch-Ua-Platform": []string{SecCHPlatform},
"Sec-Ch-Ua-Mobile": []string{SecCHMobile},
"Referer": []string{endpoints.BASE_URL + "/"},
"Origin": []string{endpoints.BASE_URL},
//"Sec-Ch-Prefers-Color-Scheme": []string{SecCHPrefersColorScheme},
//"Sec-Ch-Ua-Full-Version-List": []string{SecCHFullVersionList},
//"Sec-Ch-Ua-Model": []string{SecCHModel},
//"Sec-Ch-Ua-Platform-Version": []string{SecCHPlatformVersion},
}

type HeaderOpts struct {
WithAuthBearer bool
WithNonAuthBearer bool
WithCookies bool
WithXGuestToken bool
WithXTwitterHeaders bool
Expand All @@ -60,11 +61,11 @@ func (c *Client) buildHeaders(opts HeaderOpts) http.Header {
opts.Extra["cookie"] = c.cookies.String()
}

if opts.WithAuthBearer {
if opts.WithAuthBearer || opts.WithNonAuthBearer {
authTokens := c.session.GetAuthTokens()
// check if client is authenticated here
var bearerToken string
if c.isAuthenticated() {
if c.isAuthenticated() && !opts.WithNonAuthBearer {
bearerToken = authTokens.authenticated
} else {
bearerToken = authTokens.notAuthenticated
Expand Down
16 changes: 11 additions & 5 deletions pkg/twittermeow/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ func (c *Client) UploadMedia(params *payload.UploadMediaQuery, mediaBytes []byte

url := fmt.Sprintf("%s?%s", endpoints.UPLOAD_MEDIA_URL, string(encodedQuery))
headerOpts := HeaderOpts{
WithAuthBearer: true,
WithXCsrfToken: true,
WithCookies: true,
Origin: endpoints.BASE_URL,
Referer: endpoints.BASE_URL + "/",
WithNonAuthBearer: true,
WithXCsrfToken: true,
WithCookies: true,
Origin: endpoints.BASE_URL,
Referer: endpoints.BASE_URL + "/",
Extra: map[string]string{
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"accept": "*/*",
},
}
headers := c.buildHeaders(headerOpts)

Expand Down

0 comments on commit eabf965

Please sign in to comment.