Skip to content

Commit 0aa5087

Browse files
sleeyaxJeelsBoobz
authored andcommitted
Add optional method ProxyTLSConnection (closes gorilla#779)
Removed the call to NetDialTLSContext from the HTTP proxy CONNECT step and replaced it with a regular net.Dial in order to prevent connection issues. Custom TLS connections can now be made via the new optional ProxyTLSConnection method, after the proxy connection has been successfully established.
1 parent 92f470e commit 0aa5087

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

client.go

+28-17
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ type Dialer struct {
6565
// TLSClientConfig is ignored.
6666
NetDialTLSContext func(ctx context.Context, network, addr string) (net.Conn, error)
6767

68+
// ProxyTLSConnection specifies the dial function for creating TLS connections through a Proxy. If
69+
// ProxyTLSConnection is nil, NetDialTLSContext is used.
70+
// If ProxyTLSConnection is set, Dial assumes the TLS handshake is done there and
71+
// TLSClientConfig is ignored.
72+
ProxyTLSConnection func(ctx context.Context, proxyConn net.Conn) (net.Conn, error)
73+
6874
// Proxy specifies a function to return a proxy for a given
6975
// Request. If the function returns a non-nil error, the
7076
// request is aborted with the provided error.
@@ -346,26 +352,31 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
346352
}
347353
}()
348354

349-
if u.Scheme == "https" && d.NetDialTLSContext == nil {
350-
// If NetDialTLSContext is set, assume that the TLS handshake has already been done
355+
if u.Scheme == "https" {
356+
if d.ProxyTLSConnection != nil && d.Proxy != nil {
357+
// If we are connected to a proxy, perform the TLS handshake through the existing tunnel
358+
netConn, err = d.ProxyTLSConnection(ctx, netConn)
359+
} else if d.NetDialTLSContext == nil {
360+
// If NetDialTLSContext is set, assume that the TLS handshake has already been done
351361

352-
cfg := cloneTLSConfig(d.TLSClientConfig)
353-
if cfg.ServerName == "" {
354-
cfg.ServerName = hostNoPort
355-
}
356-
tlsConn := tls.Client(netConn, cfg)
357-
netConn = tlsConn
362+
cfg := cloneTLSConfig(d.TLSClientConfig)
363+
if cfg.ServerName == "" {
364+
cfg.ServerName = hostNoPort
365+
}
366+
tlsConn := tls.Client(netConn, cfg)
367+
netConn = tlsConn
358368

359-
if trace != nil && trace.TLSHandshakeStart != nil {
360-
trace.TLSHandshakeStart()
361-
}
362-
err := doHandshake(ctx, tlsConn, cfg)
363-
if trace != nil && trace.TLSHandshakeDone != nil {
364-
trace.TLSHandshakeDone(tlsConn.ConnectionState(), err)
365-
}
369+
if trace != nil && trace.TLSHandshakeStart != nil {
370+
trace.TLSHandshakeStart()
371+
}
372+
err := doHandshake(ctx, tlsConn, cfg)
373+
if trace != nil && trace.TLSHandshakeDone != nil {
374+
trace.TLSHandshakeDone(tlsConn.ConnectionState(), err)
375+
}
366376

367-
if err != nil {
368-
return nil, nil, err
377+
if err != nil {
378+
return nil, nil, err
379+
}
369380
}
370381
}
371382

proxy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type httpProxyDialer struct {
3333

3434
func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error) {
3535
hostPort, _ := hostPortNoPort(hpd.proxyURL)
36-
conn, err := hpd.forwardDial(network, hostPort)
36+
conn, err := net.Dial(network, hostPort)
3737
if err != nil {
3838
return nil, err
3939
}

0 commit comments

Comments
 (0)