Set TLS SNI hostname to be the same as Host field in header

This commit is contained in:
wenlong 2018-09-17 16:09:07 +08:00
parent 66b9c49e59
commit 9957931bbf
1 changed files with 5 additions and 2 deletions

View File

@ -193,6 +193,9 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
} }
} }
hostPort, hostNoPort := hostPortNoPort(u)
sniHostName := hostNoPort
// Set the request headers using the capitalization for names and values in // Set the request headers using the capitalization for names and values in
// RFC examples. Although the capitalization shouldn't matter, there are // RFC examples. Although the capitalization shouldn't matter, there are
// servers that depend on it. The Header.Set method is not used because the // servers that depend on it. The Header.Set method is not used because the
@ -209,6 +212,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
case k == "Host": case k == "Host":
if len(vs) > 0 { if len(vs) > 0 {
req.Host = vs[0] req.Host = vs[0]
sniHostName = vs[0]
} }
case k == "Upgrade" || case k == "Upgrade" ||
k == "Connection" || k == "Connection" ||
@ -282,7 +286,6 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
} }
} }
hostPort, hostNoPort := hostPortNoPort(u)
trace := httptrace.ContextClientTrace(ctx) trace := httptrace.ContextClientTrace(ctx)
if trace != nil && trace.GetConn != nil { if trace != nil && trace.GetConn != nil {
trace.GetConn(hostPort) trace.GetConn(hostPort)
@ -307,7 +310,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
if u.Scheme == "https" { if u.Scheme == "https" {
cfg := cloneTLSConfig(d.TLSClientConfig) cfg := cloneTLSConfig(d.TLSClientConfig)
if cfg.ServerName == "" { if cfg.ServerName == "" {
cfg.ServerName = hostNoPort cfg.ServerName = sniHostName
} }
tlsConn := tls.Client(netConn, cfg) tlsConn := tls.Client(netConn, cfg)
netConn = tlsConn netConn = tlsConn