@@ -65,6 +65,12 @@ type Dialer struct {
65
65
// TLSClientConfig is ignored.
66
66
NetDialTLSContext func (ctx context.Context , network , addr string ) (net.Conn , error )
67
67
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
+
68
74
// Proxy specifies a function to return a proxy for a given
69
75
// Request. If the function returns a non-nil error, the
70
76
// request is aborted with the provided error.
@@ -346,26 +352,31 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
346
352
}
347
353
}()
348
354
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
351
361
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
358
368
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
+ }
366
376
367
- if err != nil {
368
- return nil , nil , err
377
+ if err != nil {
378
+ return nil , nil , err
379
+ }
369
380
}
370
381
}
371
382
0 commit comments