@@ -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