Skip to content

Commit 36a33de

Browse files
authored
refactor(api): add buildTransport that uses ProxyTransport (#1225)
* refactor(api): rename applyProxy to NewProxyTransport - NewProxyTransport creates a cloned transport which does the required proxy handling in dial etc. - Remove use of 'customTransport' * add test * review comments - add doc string - unexport - delete low value tests
1 parent 39cf965 commit 36a33de

File tree

3 files changed

+37
-43
lines changed

3 files changed

+37
-43
lines changed

internal/api/api.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,39 +87,39 @@ type ClientOpts struct {
8787
ProxyPath string
8888
}
8989

90-
// NewClient creates a new API client.
91-
func NewClient(opts ClientOpts) Client {
92-
if opts.Out == nil {
93-
panic("unexpected nil out option")
94-
}
95-
96-
flags := opts.Flags
97-
if flags == nil {
98-
flags = defaultFlags()
99-
}
100-
101-
httpClient := http.DefaultClient
102-
90+
func buildTransport(opts ClientOpts, flags *Flags) *http.Transport {
10391
transport := http.DefaultTransport.(*http.Transport).Clone()
104-
customTransport := false
10592

10693
if flags.insecureSkipVerify != nil && *flags.insecureSkipVerify {
107-
customTransport = true
10894
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
10995
}
11096

11197
if transport.TLSClientConfig == nil {
11298
transport.TLSClientConfig = &tls.Config{}
11399
}
114100

115-
if applyProxy(transport, opts.ProxyURL, opts.ProxyPath) {
116-
customTransport = true
101+
if opts.ProxyURL != nil || opts.ProxyPath != "" {
102+
transport = withProxyTransport(transport, opts.ProxyURL, opts.ProxyPath)
117103
}
118104

119-
if customTransport {
120-
httpClient = &http.Client{
121-
Transport: transport,
122-
}
105+
return transport
106+
}
107+
108+
// NewClient creates a new API client.
109+
func NewClient(opts ClientOpts) Client {
110+
if opts.Out == nil {
111+
panic("unexpected nil out option")
112+
}
113+
114+
flags := opts.Flags
115+
if flags == nil {
116+
flags = defaultFlags()
117+
}
118+
119+
transport := buildTransport(opts, flags)
120+
121+
httpClient := &http.Client{
122+
Transport: transport,
123123
}
124124

125125
return &client{

internal/api/api_test.go

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

internal/api/proxy.go

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

14-
func applyProxy(transport *http.Transport, proxyURL *url.URL, proxyPath string) (applied bool) {
15-
if proxyURL == nil && proxyPath == "" {
16-
return false
17-
}
18-
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 {
1921
handshakeTLS := func(ctx context.Context, conn net.Conn, addr string) (net.Conn, error) {
2022
// Extract the hostname (without the port) for TLS SNI
2123
host, _, err := net.SplitHostPort(addr)
@@ -26,16 +28,14 @@ func applyProxy(transport *http.Transport, proxyURL *url.URL, proxyPath string)
2628
ServerName: host,
2729
// Pull InsecureSkipVerify from the target host transport
2830
// so that insecure-skip-verify flag settings are honored for the proxy server
29-
InsecureSkipVerify: transport.TLSClientConfig.InsecureSkipVerify,
31+
InsecureSkipVerify: baseTransport.TLSClientConfig.InsecureSkipVerify,
3032
})
3133
if err := tlsConn.HandshakeContext(ctx); err != nil {
3234
return nil, err
3335
}
3436
return tlsConn, nil
3537
}
3638

37-
proxyApplied := false
38-
3939
if proxyPath != "" {
4040
dial := func(ctx context.Context, _, _ string) (net.Conn, error) {
4141
d := net.Dialer{}
@@ -48,17 +48,15 @@ func applyProxy(transport *http.Transport, proxyURL *url.URL, proxyPath string)
4848
}
4949
return handshakeTLS(ctx, conn, addr)
5050
}
51-
transport.DialContext = dial
52-
transport.DialTLSContext = dialTLS
51+
baseTransport.DialContext = dial
52+
baseTransport.DialTLSContext = dialTLS
5353
// clear out any system proxy settings
54-
transport.Proxy = nil
55-
proxyApplied = true
54+
baseTransport.Proxy = nil
5655
} else if proxyURL != nil {
5756
switch proxyURL.Scheme {
5857
case "socks5", "socks5h":
5958
// SOCKS proxies work out of the box - no need to manually dial
60-
transport.Proxy = http.ProxyURL(proxyURL)
61-
proxyApplied = true
59+
baseTransport.Proxy = http.ProxyURL(proxyURL)
6260
case "http", "https":
6361
dial := func(ctx context.Context, network, addr string) (net.Conn, error) {
6462
// Dial the proxy
@@ -126,13 +124,12 @@ func applyProxy(transport *http.Transport, proxyURL *url.URL, proxyPath string)
126124
}
127125
return handshakeTLS(ctx, conn, addr)
128126
}
129-
transport.DialContext = dial
130-
transport.DialTLSContext = dialTLS
127+
baseTransport.DialContext = dial
128+
baseTransport.DialTLSContext = dialTLS
131129
// clear out any system proxy settings
132-
transport.Proxy = nil
133-
proxyApplied = true
130+
baseTransport.Proxy = nil
134131
}
135132
}
136133

137-
return proxyApplied
134+
return baseTransport
138135
}

0 commit comments

Comments
 (0)