@@ -54,44 +54,22 @@ class ALPNCache {
54
54
const result = new ManualPromise < string > ( ) ;
55
55
this . _cache . set ( cacheKey , result ) ;
56
56
result . then ( success ) ;
57
- if ( ! proxySocket ) {
58
- createTLSSocket ( {
59
- host,
60
- port,
61
- servername : net . isIP ( host ) ? undefined : host ,
62
- ALPNProtocols : [ 'h2' , 'http/1.1' ] ,
63
- rejectUnauthorized : false ,
64
- secureContext
65
- } ) . then ( socket => {
66
- // The server may not respond with ALPN, in which case we default to http/1.1.
67
- result . resolve ( socket . alpnProtocol || 'http/1.1' ) ;
68
- socket . end ( ) ;
69
- } ) . catch ( error => {
70
- debugLogger . log ( 'client-certificates' , `ALPN error: ${ error . message } ` ) ;
71
- result . resolve ( 'http/1.1' ) ;
72
- } ) ;
73
- } else {
74
- const socket = tls . connect ( {
75
- socket : proxySocket ,
76
- port : port ,
77
- host : host ,
78
- ALPNProtocols : [ 'h2' , 'http/1.1' ] ,
79
- rejectUnauthorized : false ,
80
- secureContext : secureContext ,
81
- servername : net . isIP ( host ) ? undefined : host
82
- } ) ;
83
- socket . on ( 'secureConnect' , ( ) => {
84
- result . resolve ( socket . alpnProtocol || 'http/1.1' ) ;
85
- socket . end ( ) ;
86
- } ) ;
87
- socket . on ( 'error' , error => {
88
- debugLogger . log ( 'client-certificates' , `ALPN error: ${ error . message } ` ) ;
89
- result . resolve ( 'http/1.1' ) ;
90
- } ) ;
91
- socket . on ( 'timeout' , ( ) => {
92
- result . resolve ( 'http/1.1' ) ;
93
- } ) ;
94
- }
57
+ createTLSSocket ( {
58
+ socket : proxySocket ,
59
+ host,
60
+ port,
61
+ servername : net . isIP ( host ) ? undefined : host ,
62
+ ALPNProtocols : [ 'h2' , 'http/1.1' ] ,
63
+ rejectUnauthorized : false ,
64
+ secureContext,
65
+ } ) . then ( socket => {
66
+ // The server may not respond with ALPN, in which case we default to http/1.1.
67
+ result . resolve ( socket . alpnProtocol || 'http/1.1' ) ;
68
+ socket . end ( ) ;
69
+ } ) . catch ( error => {
70
+ debugLogger . log ( 'client-certificates' , `ALPN error: ${ error . message } ` ) ;
71
+ result . resolve ( 'http/1.1' ) ;
72
+ } ) ;
95
73
}
96
74
}
97
75
@@ -123,11 +101,7 @@ class SocksProxyConnection {
123
101
}
124
102
125
103
async connect ( ) {
126
- if ( this . socksProxy . proxyAgentFromOptions )
127
- this . target = await this . socksProxy . proxyAgentFromOptions . callback ( new EventEmitter ( ) as any , { host : rewriteToLocalhostIfNeeded ( this . host ) , port : this . port , secureEndpoint : false } ) ;
128
- else
129
- this . target = await createSocket ( rewriteToLocalhostIfNeeded ( this . host ) , this . port ) ;
130
-
104
+ this . target = await this . _createProxySocket ( ) ?? await createSocket ( rewriteToLocalhostIfNeeded ( this . host ) , this . port ) ;
131
105
this . target . once ( 'close' , this . _targetCloseEventListener ) ;
132
106
this . target . once ( 'error' , error => this . socksProxy . _socksProxy . sendSocketError ( { uid : this . uid , error : error . message } ) ) ;
133
107
if ( this . _closed ) {
@@ -166,19 +140,21 @@ class SocksProxyConnection {
166
140
this . target . write ( data ) ;
167
141
}
168
142
143
+ private async _createProxySocket ( ) {
144
+ if ( this . socksProxy . proxyAgentFromOptions )
145
+ return await this . socksProxy . proxyAgentFromOptions . callback ( new EventEmitter ( ) as any , { host : rewriteToLocalhostIfNeeded ( this . host ) , port : this . port , secureEndpoint : false } ) ;
146
+ }
147
+
169
148
private async _attachTLSListeners ( ) {
170
149
this . internal = new stream . Duplex ( {
171
- read : ( ) => { } ,
150
+ read : ( ) => { } ,
172
151
write : ( data , encoding , callback ) => {
173
152
this . socksProxy . _socksProxy . sendSocketData ( { uid : this . uid , data } ) ;
174
153
callback ( ) ;
175
154
}
176
155
} ) ;
177
156
const secureContext = this . socksProxy . secureContextMap . get ( new URL ( `https://${ this . host } :${ this . port } ` ) . origin ) ;
178
- let proxySocket : stream . Duplex | undefined = undefined ;
179
- if ( this . socksProxy . proxyAgentFromOptions )
180
- proxySocket = await this . socksProxy . proxyAgentFromOptions . callback ( new EventEmitter ( ) as any , { host : rewriteToLocalhostIfNeeded ( this . host ) , port : this . port , secureEndpoint : false } ) ;
181
-
157
+ const proxySocket = await this . _createProxySocket ( ) ;
182
158
this . socksProxy . alpnCache . get ( rewriteToLocalhostIfNeeded ( this . host ) , this . port , secureContext , proxySocket , alpnProtocolChosenByServer => {
183
159
proxySocket ?. destroy ( ) ;
184
160
debugLogger . log ( 'client-certificates' , `Proxy->Target ${ this . host } :${ this . port } chooses ALPN ${ alpnProtocolChosenByServer } ` ) ;
@@ -251,7 +227,7 @@ class SocksProxyConnection {
251
227
rejectUnauthorized : ! this . socksProxy . ignoreHTTPSErrors ,
252
228
ALPNProtocols : [ internalTLS . alpnProtocol || 'http/1.1' ] ,
253
229
servername : ! net . isIP ( this . host ) ? this . host : undefined ,
254
- secureContext : secureContext ,
230
+ secureContext,
255
231
} ) ;
256
232
257
233
targetTLS . once ( 'secureConnect' , ( ) => {
0 commit comments