Skip to content

Commit

Permalink
Send bad credentials state if cookies are invalid on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jan 25, 2024
1 parent 1586aa3 commit 2fae4ca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
34 changes: 21 additions & 13 deletions messagix/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -37,7 +38,7 @@ const SecCHMobile = "?0"
const SecCHModel = ""
const SecCHPrefersColorScheme = "light"

var ErrRedirectAttempted = errors.New("redirect attempted")
var ErrTokenInvalidated = errors.New("access token is no longer valid")

type EventHandler func(evt interface{})
type Client struct {
Expand All @@ -64,11 +65,28 @@ type Client struct {
stopCurrentConnection atomic.Pointer[context.CancelFunc]
}

// pass an empty zerolog.Logger{} for no logging
func NewClient(platform types.Platform, cookies cookies.Cookies, logger zerolog.Logger, proxy string) (*Client, error) {
cli := &Client{
http: &http.Client{
Transport: &http.Transport{},
Transport: &http.Transport{
DialContext: (&net.Dialer{Timeout: 5 * time.Second}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ForceAttemptHTTP2: true,
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if req.Response == nil {
return nil
}
respCookies := req.Response.Cookies()
for _, cookie := range respCookies {
if (cookie.Name == "xs" || cookie.Name == "sessionid") && cookie.MaxAge < 0 {
return fmt.Errorf("%w: %s cookie was deleted", ErrTokenInvalidated, cookie.Name)
}
}
return nil
},
Timeout: 60 * time.Second,
},
cookies: cookies,
Logger: logger,
Expand Down Expand Up @@ -309,16 +327,6 @@ func (c *Client) sendCookieConsent(jsDatr string) error {
return nil
}

func (c *Client) enableRedirects() {
c.http.CheckRedirect = nil
}

func (c *Client) disableRedirects() {
c.http.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return ErrRedirectAttempted
}
}

func (c *Client) getEndpoint(name string) string {
if endpoint, ok := c.endpoints[name]; ok {
return endpoint
Expand Down
8 changes: 0 additions & 8 deletions messagix/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package messagix

import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -88,13 +87,6 @@ func (c *Client) MakeRequest(url string, method string, headers http.Header, pay
newRequest.Header = headers

response, err := c.http.Do(newRequest)
if errors.Is(err, ErrRedirectAttempted) {
/*
can't read body on redirect
https://github.com/golang/go/issues/10069
*/
return response, nil, nil
}
defer func() {
if response != nil && response.Body != nil {
_ = response.Body.Close()
Expand Down
7 changes: 6 additions & 1 deletion user.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,13 @@ func (user *User) Connect() {
user.Client, err = user.unlockedConnectWithCookies(user.Cookies)
if err != nil {
user.log.Error().Err(err).Msg("Failed to connect")
stateEvt := status.StateUnknownError
if errors.Is(err, messagix.ErrTokenInvalidated) {
stateEvt = status.StateBadCredentials
// TODO clear cookies?
}
user.BridgeState.Send(status.BridgeState{
StateEvent: status.StateUnknownError,
StateEvent: stateEvt,
Error: "meta-connect-error",
Message: err.Error(),
})
Expand Down

0 comments on commit 2fae4ca

Please sign in to comment.