@@ -10,6 +10,24 @@ import type { OutgoingHttpHeaders } from 'http';
1010
1111const debug = createDebug ( 'https-proxy-agent' ) ;
1212
13+ const setServernameFromNonIpHost = <
14+ T extends { host ?: string ; servername ?: string }
15+ > (
16+ options : T
17+ ) => {
18+ if (
19+ options . servername === undefined &&
20+ options . host &&
21+ ! net . isIP ( options . host )
22+ ) {
23+ return {
24+ ...options ,
25+ servername : options . host ,
26+ } ;
27+ }
28+ return options ;
29+ } ;
30+
1331// eslint-disable-next-line @typescript-eslint/no-unused-vars
1432type Protocol < T > = T extends `${infer Protocol } :${infer _ } ` ? Protocol : never ;
1533
@@ -92,12 +110,7 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
92110 let socket : net . Socket ;
93111 if ( proxy . protocol === 'https:' ) {
94112 debug ( 'Creating `tls.Socket`: %o' , this . connectOpts ) ;
95- const servername =
96- this . connectOpts . servername || this . connectOpts . host ;
97- socket = tls . connect ( {
98- ...this . connectOpts ,
99- servername,
100- } ) ;
113+ socket = tls . connect ( setServernameFromNonIpHost ( this . connectOpts ) ) ;
101114 } else {
102115 debug ( 'Creating `net.Socket`: %o' , this . connectOpts ) ;
103116 socket = net . connect ( this . connectOpts ) ;
@@ -146,11 +159,14 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
146159 // The proxy is connecting to a TLS server, so upgrade
147160 // this socket connection to a TLS connection.
148161 debug ( 'Upgrading socket connection to TLS' ) ;
149- const servername = opts . servername || opts . host ;
150162 return tls . connect ( {
151- ...omit ( opts , 'host' , 'path' , 'port' ) ,
163+ ...omit (
164+ setServernameFromNonIpHost ( opts ) ,
165+ 'host' ,
166+ 'path' ,
167+ 'port'
168+ ) ,
152169 socket,
153- servername,
154170 } ) ;
155171 }
156172
0 commit comments