Skip to content

Commit bad5b0a

Browse files
committed
fix tls handshake on proxy
1 parent 75fbe70 commit bad5b0a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

client.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,16 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
256256
}
257257
if proxyURL != nil {
258258
forwardDial := newNetDialerFunc(proxyURL.Scheme, d.NetDial, d.NetDialContext, d.NetDialTLSContext)
259-
if proxyURL.Scheme == "http" || proxyURL.Scheme == "https" {
260-
netDial = newHTTPProxyDialerFunc(proxyURL, forwardDial)
259+
if proxyURL.Scheme == "https" && d.NetDialTLSContext == nil {
260+
tlsClientConfig := cloneTLSConfig(d.TLSClientConfig)
261+
if d.TLSClientConfig == nil {
262+
tlsClientConfig = &tls.Config{
263+
ServerName: proxyURL.Hostname(),
264+
}
265+
}
266+
netDial = newHTTPProxyDialerFunc(proxyURL, forwardDial, tlsClientConfig)
267+
} else if proxyURL.Scheme == "http" || proxyURL.Scheme == "https" {
268+
netDial = newHTTPProxyDialerFunc(proxyURL, forwardDial, nil)
261269
} else {
262270
dialer, err := proxy.FromURL(proxyURL, forwardDial)
263271
if err != nil {

proxy.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bufio"
99
"bytes"
1010
"context"
11+
"crypto/tls"
1112
"encoding/base64"
1213
"errors"
1314
"net"
@@ -46,14 +47,26 @@ func (fn netDialerFunc) DialContext(ctx context.Context, network, addr string) (
4647
return fn(ctx, network, addr)
4748
}
4849

49-
func newHTTPProxyDialerFunc(proxyURL *url.URL, forwardDial netDialerFunc) netDialerFunc {
50+
// newHTTPProxyDialerFunc returns a netDialerFunc that dials using the provided
51+
// proxyURL. The forwardDial function is used to establish the connection to the
52+
// proxy server. If tlsClientConfig is not nil, the connection to the proxy is
53+
// upgraded to a TLS connection with tls.Client.
54+
func newHTTPProxyDialerFunc(proxyURL *url.URL, forwardDial netDialerFunc, tlsClientConfig *tls.Config) netDialerFunc {
5055
return func(ctx context.Context, network, addr string) (net.Conn, error) {
5156
hostPort, _ := hostPortNoPort(proxyURL)
5257
conn, err := forwardDial(ctx, network, hostPort)
5358
if err != nil {
5459
return nil, err
5560
}
5661

62+
if tlsClientConfig != nil {
63+
tlsConn := tls.Client(conn, tlsClientConfig)
64+
if err = tlsConn.HandshakeContext(ctx); err != nil {
65+
return nil, err
66+
}
67+
conn = tlsConn
68+
}
69+
5770
connectHeader := make(http.Header)
5871
if user := proxyURL.User; user != nil {
5972
proxyUser := user.Username()

0 commit comments

Comments
 (0)