Skip to content

Commit

Permalink
Log all redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 6, 2024
1 parent 05d10f0 commit 4dd9a43
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions messagix/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ func NewClient(cookies *cookies.Cookies, logger zerolog.Logger) *Client {
ResponseHeaderTimeout: 40 * time.Second,
ForceAttemptHTTP2: true,
},
CheckRedirect: checkHTTPRedirect,
Timeout: 60 * time.Second,
Timeout: 60 * time.Second,
},
cookies: cookies,
Logger: logger,
Expand All @@ -97,6 +96,7 @@ func NewClient(cookies *cookies.Cookies, logger zerolog.Logger) *Client {
activeTasks: make([]int, 0),
taskMutex: &sync.Mutex{},
}
cli.http.CheckRedirect = cli.checkHTTPRedirect

cli.configurePlatformClient()
cli.configs = &Configs{
Expand Down
13 changes: 12 additions & 1 deletion messagix/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"time"

"go.mau.fi/mautrix-meta/messagix/cookies"
Expand Down Expand Up @@ -94,10 +95,20 @@ func isPermanentRequestError(err error) bool {
errors.Is(err, ErrConsentRequired)
}

func checkHTTPRedirect(req *http.Request, _ []*http.Request) error {
func (c *Client) checkHTTPRedirect(req *http.Request, via []*http.Request) error {
if req.Response == nil {
return nil
}
if !strings.HasSuffix(req.URL.Hostname(), "fbcdn.net") && !strings.HasSuffix(req.URL.Hostname(), "facebookcooa4ldbat4g7iacswl3p2zrf5nuylvnhxn6kqolvojixwid.onion") {
var prevURL string
if len(via) > 0 {
prevURL = via[0].URL.String()
}
c.Logger.Warn().
Stringer("url", req.URL).
Str("prev_url", prevURL).
Msg("HTTP request was redirected")
}
if req.URL.Path == "/challenge/" {
return fmt.Errorf("%w: redirected to %s", ErrChallengeRequired, req.URL.String())
} else if req.URL.Path == "/consent/" || req.URL.Path == "/privacy/consent/" {
Expand Down

0 comments on commit 4dd9a43

Please sign in to comment.