Skip to content

Commit

Permalink
Add timeouts for HTTP requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 5, 2024
1 parent 96c09b4 commit 90b5346
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
20 changes: 12 additions & 8 deletions libgm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/sha256"
"fmt"
"io"
"net"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -77,8 +78,9 @@ type Client struct {
AuthData *AuthData
cfg *gmproto.Config

proxy Proxy
http *http.Client
httpTransport *http.Transport
http *http.Client
lphttp *http.Client
}

func NewAuthData() *AuthData {
Expand All @@ -92,11 +94,17 @@ func NewClient(authData *AuthData, logger zerolog.Logger) *Client {
sessionHandler := &SessionHandler{
responseWaiters: make(map[string]chan<- *IncomingRPCMessage),
}
transport := &http.Transport{
DialContext: (&net.Dialer{Timeout: 10 * time.Second}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 20 * time.Second,
}
cli := &Client{
AuthData: authData,
Logger: logger,
sessionHandler: sessionHandler,
http: &http.Client{},
http: &http.Client{Transport: transport, Timeout: 2 * time.Minute},
lphttp: &http.Client{Transport: transport, Timeout: 30 * time.Minute},

pingShortCircuit: make(chan struct{}),
}
Expand Down Expand Up @@ -127,11 +135,7 @@ func (c *Client) SetProxy(proxy string) error {
if err != nil {
c.Logger.Fatal().Err(err).Msg("Failed to set proxy")
}
proxyUrl := http.ProxyURL(proxyParsed)
c.http.Transport = &http.Transport{
Proxy: proxyUrl,
}
c.proxy = proxyUrl
c.httpTransport.Proxy = http.ProxyURL(proxyParsed)
c.Logger.Debug().Any("proxy", proxyParsed.Host).Msg("SetProxy")
return nil
}
Expand Down
10 changes: 7 additions & 3 deletions libgm/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ const ContentTypePBLite = "application/json+protobuf"

func (c *Client) makeProtobufHTTPRequest(url string, data proto.Message, contentType string) (*http.Response, error) {
ctx := c.Logger.WithContext(context.TODO())
return c.makeProtobufHTTPRequestContext(ctx, url, data, contentType)
return c.makeProtobufHTTPRequestContext(ctx, url, data, contentType, false)
}

func (c *Client) makeProtobufHTTPRequestContext(ctx context.Context, url string, data proto.Message, contentType string) (*http.Response, error) {
func (c *Client) makeProtobufHTTPRequestContext(ctx context.Context, url string, data proto.Message, contentType string, longPoll bool) (*http.Response, error) {
var body []byte
var err error
switch contentType {
Expand All @@ -48,7 +48,11 @@ func (c *Client) makeProtobufHTTPRequestContext(ctx context.Context, url string,
}
util.BuildRelayHeaders(req, contentType, "*/*")
c.AddCookieHeaders(req)
res, reqErr := c.http.Do(req)
client := c.http
if longPoll {
client = c.lphttp
}
res, reqErr := client.Do(req)
if reqErr != nil {
return res, reqErr
}
Expand Down
4 changes: 3 additions & 1 deletion libgm/longpoll.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package libgm
import (
"bufio"
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
Expand Down Expand Up @@ -214,6 +215,7 @@ func (c *Client) doLongPoll(loggedIn bool, onFirstConnect func()) {
defer func() {
log.Debug().Msg("Long polling stopped")
}()
ctx := log.WithContext(context.TODO())
log.Debug().Str("listen_uuid", listenReqID).Msg("Long polling starting")

dittoPing := make(chan struct{}, 1)
Expand Down Expand Up @@ -253,7 +255,7 @@ func (c *Client) doLongPoll(loggedIn bool, onFirstConnect func()) {
url = util.ReceiveMessagesURLGoogle
payload.Auth.Network = util.GoogleNetwork
}
resp, err := c.makeProtobufHTTPRequest(url, payload, ContentTypePBLite)
resp, err := c.makeProtobufHTTPRequestContext(ctx, url, payload, ContentTypePBLite, true)
if err != nil {
if loggedIn {
c.triggerEvent(&events.ListenTemporaryError{Error: err})
Expand Down
4 changes: 2 additions & 2 deletions libgm/pair_google.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *Client) signInGaiaInitial(ctx context.Context) (*gmproto.SignInGaiaResp
payload := c.baseSignInGaiaPayload()
payload.UnknownInt3 = 1
return typedHTTPResponse[*gmproto.SignInGaiaResponse](
c.makeProtobufHTTPRequestContext(ctx, util.SignInGaiaURL, payload, ContentTypePBLite),
c.makeProtobufHTTPRequestContext(ctx, util.SignInGaiaURL, payload, ContentTypePBLite, false),
)
}

Expand All @@ -82,7 +82,7 @@ func (c *Client) signInGaiaGetToken(ctx context.Context) (*gmproto.SignInGaiaRes
SomeData: key,
}
resp, err := typedHTTPResponse[*gmproto.SignInGaiaResponse](
c.makeProtobufHTTPRequestContext(ctx, util.SignInGaiaURL, payload, ContentTypePBLite),
c.makeProtobufHTTPRequestContext(ctx, util.SignInGaiaURL, payload, ContentTypePBLite, false),
)
if err != nil {
return nil, err
Expand Down

0 comments on commit 90b5346

Please sign in to comment.