Skip to content

Commit 5ebfac6

Browse files
committed
review comments
- add doc string - unexport - delete low value tests
1 parent 4d20fc9 commit 5ebfac6

File tree

3 files changed

+17
-54
lines changed

3 files changed

+17
-54
lines changed

internal/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func buildTransport(opts ClientOpts, flags *Flags) *http.Transport {
9999
}
100100

101101
if opts.ProxyURL != nil || opts.ProxyPath != "" {
102-
transport = NewProxyTransport(transport, opts.ProxyURL, opts.ProxyPath)
102+
transport = withProxyTransport(transport, opts.ProxyURL, opts.ProxyPath)
103103
}
104104

105105
return transport

internal/api/api_test.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

internal/api/proxy.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import (
1111
"net/url"
1212
)
1313

14-
func NewProxyTransport(base *http.Transport, proxyURL *url.URL, proxyPath string) *http.Transport {
15-
// Clone so that we don't change the original transport
16-
transport := base.Clone()
17-
14+
// withProxyTransport modifies the given transport to handle proxying of unix, socks5 and http connections.
15+
//
16+
// Note: baseTransport is considered to be a clone created with transport.Clone()
17+
//
18+
// - If a the proxyPath is not empty, a unix socket proxy is created.
19+
// - Otherwise, the proxyURL is used to determine if we should proxy socks5 / http connections
20+
func withProxyTransport(baseTransport *http.Transport, proxyURL *url.URL, proxyPath string) *http.Transport {
1821
handshakeTLS := func(ctx context.Context, conn net.Conn, addr string) (net.Conn, error) {
1922
// Extract the hostname (without the port) for TLS SNI
2023
host, _, err := net.SplitHostPort(addr)
@@ -25,7 +28,7 @@ func NewProxyTransport(base *http.Transport, proxyURL *url.URL, proxyPath string
2528
ServerName: host,
2629
// Pull InsecureSkipVerify from the target host transport
2730
// so that insecure-skip-verify flag settings are honored for the proxy server
28-
InsecureSkipVerify: transport.TLSClientConfig.InsecureSkipVerify,
31+
InsecureSkipVerify: baseTransport.TLSClientConfig.InsecureSkipVerify,
2932
})
3033
if err := tlsConn.HandshakeContext(ctx); err != nil {
3134
return nil, err
@@ -45,15 +48,15 @@ func NewProxyTransport(base *http.Transport, proxyURL *url.URL, proxyPath string
4548
}
4649
return handshakeTLS(ctx, conn, addr)
4750
}
48-
transport.DialContext = dial
49-
transport.DialTLSContext = dialTLS
51+
baseTransport.DialContext = dial
52+
baseTransport.DialTLSContext = dialTLS
5053
// clear out any system proxy settings
51-
transport.Proxy = nil
54+
baseTransport.Proxy = nil
5255
} else if proxyURL != nil {
5356
switch proxyURL.Scheme {
5457
case "socks5", "socks5h":
5558
// SOCKS proxies work out of the box - no need to manually dial
56-
transport.Proxy = http.ProxyURL(proxyURL)
59+
baseTransport.Proxy = http.ProxyURL(proxyURL)
5760
case "http", "https":
5861
dial := func(ctx context.Context, network, addr string) (net.Conn, error) {
5962
// Dial the proxy
@@ -121,12 +124,12 @@ func NewProxyTransport(base *http.Transport, proxyURL *url.URL, proxyPath string
121124
}
122125
return handshakeTLS(ctx, conn, addr)
123126
}
124-
transport.DialContext = dial
125-
transport.DialTLSContext = dialTLS
127+
baseTransport.DialContext = dial
128+
baseTransport.DialTLSContext = dialTLS
126129
// clear out any system proxy settings
127-
transport.Proxy = nil
130+
baseTransport.Proxy = nil
128131
}
129132
}
130133

131-
return transport
134+
return baseTransport
132135
}

0 commit comments

Comments
 (0)