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