Skip to content

Commit d19fc21

Browse files
committed
refactor(api): rename applyProxy yo NewProxyTransport
- NewProxyTransport creates a cloned transport which does the required proxy handling in dial etc. - Remove use of 'customTransport'
1 parent b627480 commit d19fc21

File tree

2 files changed

+8
-20
lines changed

2 files changed

+8
-20
lines changed

internal/api/api.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,28 +98,22 @@ func NewClient(opts ClientOpts) Client {
9898
flags = defaultFlags()
9999
}
100100

101-
httpClient := http.DefaultClient
102-
103101
transport := http.DefaultTransport.(*http.Transport).Clone()
104-
customTransport := false
105102

106103
if flags.insecureSkipVerify != nil && *flags.insecureSkipVerify {
107-
customTransport = true
108104
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
109105
}
110106

111107
if transport.TLSClientConfig == nil {
112108
transport.TLSClientConfig = &tls.Config{}
113109
}
114110

115-
if applyProxy(transport, opts.ProxyURL, opts.ProxyPath) {
116-
customTransport = true
111+
if opts.ProxyURL != nil || opts.ProxyPath != "" {
112+
transport = NewProxyTransport(transport, opts.ProxyURL, opts.ProxyPath)
117113
}
118114

119-
if customTransport {
120-
httpClient = &http.Client{
121-
Transport: transport,
122-
}
115+
httpClient := &http.Client{
116+
Transport: transport,
123117
}
124118

125119
return &client{

internal/api/proxy.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ 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-
}
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()
1817

1918
handshakeTLS := func(ctx context.Context, conn net.Conn, addr string) (net.Conn, error) {
2019
// Extract the hostname (without the port) for TLS SNI
@@ -34,8 +33,6 @@ func applyProxy(transport *http.Transport, proxyURL *url.URL, proxyPath string)
3433
return tlsConn, nil
3534
}
3635

37-
proxyApplied := false
38-
3936
if proxyPath != "" {
4037
dial := func(ctx context.Context, _, _ string) (net.Conn, error) {
4138
d := net.Dialer{}
@@ -52,13 +49,11 @@ func applyProxy(transport *http.Transport, proxyURL *url.URL, proxyPath string)
5249
transport.DialTLSContext = dialTLS
5350
// clear out any system proxy settings
5451
transport.Proxy = nil
55-
proxyApplied = true
5652
} else if proxyURL != nil {
5753
switch proxyURL.Scheme {
5854
case "socks5", "socks5h":
5955
// SOCKS proxies work out of the box - no need to manually dial
6056
transport.Proxy = http.ProxyURL(proxyURL)
61-
proxyApplied = true
6257
case "http", "https":
6358
dial := func(ctx context.Context, network, addr string) (net.Conn, error) {
6459
// Dial the proxy
@@ -130,9 +125,8 @@ func applyProxy(transport *http.Transport, proxyURL *url.URL, proxyPath string)
130125
transport.DialTLSContext = dialTLS
131126
// clear out any system proxy settings
132127
transport.Proxy = nil
133-
proxyApplied = true
134128
}
135129
}
136130

137-
return proxyApplied
131+
return transport
138132
}

0 commit comments

Comments
 (0)