Skip to content

Commit

Permalink
Add possibility to use a custom HTTP Proxy function (#94)
Browse files Browse the repository at this point in the history
Co-authored-by: Zach Larum <zach.larum@automox.com>
Co-authored-by: Zach Larum <170744520+ax-zlarum@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 16, 2024
1 parent 13ccb77 commit 345753d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
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"
}

// 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
6 changes: 6 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,11 @@ type Config struct {
// Version allows setting client version. This is an application
// specific information. By default, no version set.
Version string
// 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.
NetDialContext func(ctx context.Context, network, addr string) (net.Conn, error)
Expand Down
13 changes: 12 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,12 @@ type websocketTransport struct {

// websocketConfig configures Websocket transport.
type websocketConfig struct {
// 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.
NetDialContext func(ctx context.Context, network, addr string) (net.Conn, error)
Expand Down Expand Up @@ -92,7 +99,11 @@ func newWebsocketTransport(url string, protocolType protocol.Type, config websoc
wsHeaders := config.Header

dialer := &websocket.Dialer{}
dialer.Proxy = http.ProxyFromEnvironment
if config.Proxy != nil {
dialer.Proxy = config.Proxy
} else {
dialer.Proxy = http.ProxyFromEnvironment
}
dialer.NetDialContext = config.NetDialContext

dialer.HandshakeTimeout = config.HandshakeTimeout
Expand Down

0 comments on commit 345753d

Please sign in to comment.