Skip to content

Commit f639570

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 f639570

2 files changed

Lines changed: 25 additions & 31 deletions

File tree

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 = NewProxyTransport(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/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)