@@ -142,34 +142,14 @@ class SocksProxyConnection {
142142 dummyServer . emit ( 'connection' , this . internal ) ;
143143 dummyServer . on ( 'secureConnection' , internalTLS => {
144144 debugLogger . log ( 'client-certificates' , `Browser->Proxy ${ this . host } :${ this . port } chooses ALPN ${ internalTLS . alpnProtocol } ` ) ;
145- const tlsOptions : tls . ConnectionOptions = {
146- socket : this . target ,
147- host : this . host ,
148- port : this . port ,
149- rejectUnauthorized : ! this . socksProxy . ignoreHTTPSErrors ,
150- ALPNProtocols : [ internalTLS . alpnProtocol || 'http/1.1' ] ,
151- ...clientCertificatesToTLSOptions ( this . socksProxy . clientCertificates , new URL ( `https://${ this . host } :${ this . port } ` ) . origin ) ,
152- } ;
153- if ( ! net . isIP ( this . host ) )
154- tlsOptions . servername = this . host ;
155- const targetTLS = tls . connect ( tlsOptions ) ;
156-
157- targetTLS . on ( 'secureConnect' , ( ) => {
158- internalTLS . pipe ( targetTLS ) ;
159- targetTLS . pipe ( internalTLS ) ;
160- } ) ;
161145
162- // Handle close and errors
146+ let targetTLS : tls . TLSSocket | undefined = undefined ;
163147 const closeBothSockets = ( ) => {
164148 internalTLS . end ( ) ;
165- targetTLS . end ( ) ;
149+ targetTLS ? .end ( ) ;
166150 } ;
167151
168- internalTLS . on ( 'end' , ( ) => closeBothSockets ( ) ) ;
169- targetTLS . on ( 'end' , ( ) => closeBothSockets ( ) ) ;
170-
171- internalTLS . on ( 'error' , ( ) => closeBothSockets ( ) ) ;
172- targetTLS . on ( 'error' , error => {
152+ const handleError = ( error : Error ) => {
173153 debugLogger . log ( 'client-certificates' , `error when connecting to target: ${ error . message } ` ) ;
174154 const responseBody = 'Playwright client-certificate error: ' + error . message ;
175155 if ( internalTLS ?. alpnProtocol === 'h2' ) {
@@ -204,7 +184,38 @@ class SocksProxyConnection {
204184 ] . join ( '\r\n' ) ) ;
205185 closeBothSockets ( ) ;
206186 }
187+ } ;
188+
189+ let secureContext : tls . SecureContext ;
190+ try {
191+ secureContext = tls . createSecureContext ( clientCertificatesToTLSOptions ( this . socksProxy . clientCertificates , new URL ( `https://${ this . host } :${ this . port } ` ) . origin ) ) ;
192+ } catch ( error ) {
193+ handleError ( error ) ;
194+ return ;
195+ }
196+
197+ const tlsOptions : tls . ConnectionOptions = {
198+ socket : this . target ,
199+ host : this . host ,
200+ port : this . port ,
201+ rejectUnauthorized : ! this . socksProxy . ignoreHTTPSErrors ,
202+ ALPNProtocols : [ internalTLS . alpnProtocol || 'http/1.1' ] ,
203+ servername : ! net . isIP ( this . host ) ? this . host : undefined ,
204+ secureContext,
205+ } ;
206+
207+ targetTLS = tls . connect ( tlsOptions ) ;
208+
209+ targetTLS . on ( 'secureConnect' , ( ) => {
210+ internalTLS . pipe ( targetTLS ) ;
211+ targetTLS . pipe ( internalTLS ) ;
207212 } ) ;
213+
214+ internalTLS . on ( 'end' , ( ) => closeBothSockets ( ) ) ;
215+ targetTLS . on ( 'end' , ( ) => closeBothSockets ( ) ) ;
216+
217+ internalTLS . on ( 'error' , ( ) => closeBothSockets ( ) ) ;
218+ targetTLS . on ( 'error' , handleError ) ;
208219 } ) ;
209220 } ) ;
210221 }
0 commit comments