Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP Proxy lookup function support #94

Merged
merged 9 commits into from
Oct 16, 2024
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func newClient(endpoint string, isProtobuf bool, config Config) *Client {
if config.Name == "" {
config.Name = "go"
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also set config.Proxy to http.ProxyFromEnvironment if it is nil, to maintain backwards compatible behaviour? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, yes to maintain backwards compatibility. Lemme make this change. This would mean that it would be impossible to disable proxies, but 🤷 that doesn't seem to be a complaint

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the default to http.ProxyFromEnvironment and updated the docs a bit in. 5a720a3

// We support setting multiple endpoints to try in round-robin fashion. But
// for now this feature is not documented and used for internal tests. In most
// cases there should be a single public server WS endpoint.
Expand Down Expand Up @@ -888,6 +889,7 @@ func (c *Client) startReconnecting() error {
c.mu.Unlock()

wsConfig := websocketConfig{
Proxy: c.config.Proxy,
NetDialContext: c.config.NetDialContext,
TLSConfig: c.config.TLSConfig,
HandshakeTimeout: c.config.HandshakeTimeout,
Expand Down
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"net"
"net/http"
"net/url"
"time"
)

Expand All @@ -30,6 +31,8 @@ type Config struct {
// Version allows setting client version. This is an application
// specific information. By default, no version set.
Version string
// Proxy specifies the function responsible for determining the proxy URL.
Proxy func(*http.Request) (*url.URL, error)
// NetDialContext specifies the dial function for creating TCP connections. If
// NetDialContext is nil, net.DialContext is used.
NetDialContext func(ctx context.Context, network, addr string) (net.Conn, error)
Expand Down
6 changes: 5 additions & 1 deletion transport_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net"
"net/http"
"net/url"
"sync"
"time"

Expand Down Expand Up @@ -62,6 +63,9 @@ type websocketTransport struct {

// websocketConfig configures Websocket transport.
type websocketConfig struct {
// Proxy specifies the function responsible for determining the proxy URL.
Proxy func(*http.Request) (*url.URL, error)

// NetDialContext specifies the dial function for creating TCP connections. If
// NetDialContext is nil, net.DialContext is used.
NetDialContext func(ctx context.Context, network, addr string) (net.Conn, error)
Expand Down Expand Up @@ -92,7 +96,7 @@ func newWebsocketTransport(url string, protocolType protocol.Type, config websoc
wsHeaders := config.Header

dialer := &websocket.Dialer{}
dialer.Proxy = http.ProxyFromEnvironment
dialer.Proxy = config.Proxy
dialer.NetDialContext = config.NetDialContext

dialer.HandshakeTimeout = config.HandshakeTimeout
Expand Down