From 7a82bfa7444bebdc8d1604e49c6c85e4b49a1d43 Mon Sep 17 00:00:00 2001 From: Zach Larum Date: Thu, 5 Sep 2024 22:49:25 -0700 Subject: [PATCH 1/7] Updating config to contain proxy field --- client.go | 2 ++ config.go | 4 ++++ transport_websocket.go | 11 ++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index e2a4dd8..6e95108 100644 --- a/client.go +++ b/client.go @@ -873,6 +873,7 @@ func (c *Client) handleServerUnsub(channel string, _ *protocol.Unsubscribe) { func (c *Client) getReconnectDelay() time.Duration { return c.reconnectStrategy.timeBeforeNextAttempt(c.reconnectAttempts) } +type ProxyFunc = func(*http.Request) (*url.URL, error) func (c *Client) startReconnecting() error { c.mu.Lock() @@ -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, diff --git a/config.go b/config.go index 73114e6..ccba593 100644 --- a/config.go +++ b/config.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "net" "net/http" + "net/url" "time" ) @@ -30,6 +31,9 @@ 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. If + // Proxy is nil, http.ProxyFromEnvironment is used. + 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) diff --git a/transport_websocket.go b/transport_websocket.go index aa246c9..c0ca11f 100644 --- a/transport_websocket.go +++ b/transport_websocket.go @@ -8,6 +8,7 @@ import ( "io" "net" "net/http" + "net/url" "sync" "time" @@ -62,6 +63,10 @@ type websocketTransport struct { // websocketConfig configures Websocket transport. type websocketConfig struct { + // Proxy specifies the function responsible for determining the proxy URL. If + // Proxy is nil, http.ProxyFromEnvironment is used. + 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) @@ -91,8 +96,12 @@ type websocketConfig struct { func newWebsocketTransport(url string, protocolType protocol.Type, config websocketConfig) (transport, error) { wsHeaders := config.Header + if config.Proxy == nil { + config.Proxy = http.ProxyFromEnvironment + } + dialer := &websocket.Dialer{} - dialer.Proxy = http.ProxyFromEnvironment + dialer.Proxy = config.Proxy dialer.NetDialContext = config.NetDialContext dialer.HandshakeTimeout = config.HandshakeTimeout From 6f542c7cd9e26fc227a9d93d0e27afcffaca207a Mon Sep 17 00:00:00 2001 From: Zach Larum Date: Fri, 6 Sep 2024 09:26:33 -0700 Subject: [PATCH 2/7] removing unnecessary line --- client.go | 1 - 1 file changed, 1 deletion(-) diff --git a/client.go b/client.go index 6e95108..a2a6976 100644 --- a/client.go +++ b/client.go @@ -873,7 +873,6 @@ func (c *Client) handleServerUnsub(channel string, _ *protocol.Unsubscribe) { func (c *Client) getReconnectDelay() time.Duration { return c.reconnectStrategy.timeBeforeNextAttempt(c.reconnectAttempts) } -type ProxyFunc = func(*http.Request) (*url.URL, error) func (c *Client) startReconnecting() error { c.mu.Lock() From 4138b18b1bfcad5c238b5b67d0eebd0ba8101bb7 Mon Sep 17 00:00:00 2001 From: Zach Larum Date: Fri, 6 Sep 2024 09:36:39 -0700 Subject: [PATCH 3/7] Fixing formatting --- client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.go b/client.go index a2a6976..4d6c6b5 100644 --- a/client.go +++ b/client.go @@ -888,7 +888,7 @@ func (c *Client) startReconnecting() error { c.mu.Unlock() wsConfig := websocketConfig{ - Proxy: c.config.Proxy, + Proxy: c.config.Proxy, NetDialContext: c.config.NetDialContext, TLSConfig: c.config.TLSConfig, HandshakeTimeout: c.config.HandshakeTimeout, From ced6ba405cfb26482bd9ff86509f29d97b63e442 Mon Sep 17 00:00:00 2001 From: Zach Larum Date: Fri, 6 Sep 2024 12:11:23 -0700 Subject: [PATCH 4/7] Changing default initialization of proxy --- client.go | 3 +++ transport_websocket.go | 4 ---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index 4d6c6b5..de19d39 100644 --- a/client.go +++ b/client.go @@ -99,6 +99,9 @@ func newClient(endpoint string, isProtobuf bool, config Config) *Client { if config.Name == "" { config.Name = "go" } + if config.Proxy == nil { + config.Proxy = http.ProxyFromEnvironment + } // 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. diff --git a/transport_websocket.go b/transport_websocket.go index c0ca11f..82159ff 100644 --- a/transport_websocket.go +++ b/transport_websocket.go @@ -96,10 +96,6 @@ type websocketConfig struct { func newWebsocketTransport(url string, protocolType protocol.Type, config websocketConfig) (transport, error) { wsHeaders := config.Header - if config.Proxy == nil { - config.Proxy = http.ProxyFromEnvironment - } - dialer := &websocket.Dialer{} dialer.Proxy = config.Proxy dialer.NetDialContext = config.NetDialContext From 83aeaa1205bf2f001aa2db78cce00e0a6586e497 Mon Sep 17 00:00:00 2001 From: Zach Larum Date: Fri, 6 Sep 2024 12:16:51 -0700 Subject: [PATCH 5/7] Updating doc --- transport_websocket.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/transport_websocket.go b/transport_websocket.go index 82159ff..77f2b2c 100644 --- a/transport_websocket.go +++ b/transport_websocket.go @@ -63,8 +63,7 @@ type websocketTransport struct { // websocketConfig configures Websocket transport. type websocketConfig struct { - // Proxy specifies the function responsible for determining the proxy URL. If - // Proxy is nil, http.ProxyFromEnvironment is used. + // 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 From da8447c6e8118383366ade933af866e33aabce46 Mon Sep 17 00:00:00 2001 From: Zach Larum Date: Fri, 6 Sep 2024 12:23:53 -0700 Subject: [PATCH 6/7] Updating to remove default proxy lookup behavior now that users have the option to specify --- client.go | 4 +--- config.go | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/client.go b/client.go index de19d39..7d95491 100644 --- a/client.go +++ b/client.go @@ -99,9 +99,7 @@ func newClient(endpoint string, isProtobuf bool, config Config) *Client { if config.Name == "" { config.Name = "go" } - if config.Proxy == nil { - config.Proxy = http.ProxyFromEnvironment - } + // 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. diff --git a/config.go b/config.go index ccba593..4342ac4 100644 --- a/config.go +++ b/config.go @@ -31,8 +31,7 @@ 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. If - // Proxy is nil, http.ProxyFromEnvironment is used. + // 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. From 5a720a3fdc4a257c916616ce8809532151652d4a Mon Sep 17 00:00:00 2001 From: Eric Suedmeier Date: Tue, 15 Oct 2024 09:48:30 -0500 Subject: [PATCH 7/7] Default the proxy to maintain backwards compatibility Update the default value for `Proxy` configs so that they default to `http.ProxyFromEnvironment` instead of no proxy. --- config.go | 5 ++++- transport_websocket.go | 11 +++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index 4342ac4..004ccde 100644 --- a/config.go +++ b/config.go @@ -31,7 +31,10 @@ 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 specifies a function to return a proxy for a given Request. + // If the function returns a non-nil error, the request is aborted with the + // provided error. If function returns a nil *URL, no proxy is used. + // If Proxy is nil then http.ProxyFromEnvironment will be used. Proxy func(*http.Request) (*url.URL, error) // NetDialContext specifies the dial function for creating TCP connections. If // NetDialContext is nil, net.DialContext is used. diff --git a/transport_websocket.go b/transport_websocket.go index 77f2b2c..a0f39dd 100644 --- a/transport_websocket.go +++ b/transport_websocket.go @@ -63,7 +63,10 @@ type websocketTransport struct { // websocketConfig configures Websocket transport. type websocketConfig struct { - // Proxy specifies the function responsible for determining the proxy URL. + // Proxy specifies a function to return a proxy for a given Request. + // If the function returns a non-nil error, the request is aborted with the + // provided error. If function returns a nil *URL, no proxy is used. + // If Proxy is nil then http.ProxyFromEnvironment will be used. Proxy func(*http.Request) (*url.URL, error) // NetDialContext specifies the dial function for creating TCP connections. If @@ -96,7 +99,11 @@ func newWebsocketTransport(url string, protocolType protocol.Type, config websoc wsHeaders := config.Header dialer := &websocket.Dialer{} - dialer.Proxy = config.Proxy + if config.Proxy != nil { + dialer.Proxy = config.Proxy + } else { + dialer.Proxy = http.ProxyFromEnvironment + } dialer.NetDialContext = config.NetDialContext dialer.HandshakeTimeout = config.HandshakeTimeout