@@ -72,29 +72,23 @@ private static async void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient
72
72
73
73
await WriteConnectResponse ( clientStreamWriter , version ) . ConfigureAwait ( false ) ;
74
74
75
- var certificate = await CertManager . CreateCertificate ( httpRemoteUri . Host , false ) ;
75
+
76
76
77
77
SslStream sslStream = null ;
78
78
79
79
try
80
80
{
81
- var connectRequest = new ConnectRequest ( ) { Stream = clientStream , Uri = httpRemoteUri } ;
82
81
83
- await TcpConnectionManager . GetClient ( connectRequest , httpRemoteUri . Host , httpRemoteUri . Port , true , version ) . ConfigureAwait ( false ) ;
82
+ await TcpConnectionManager . GetClient ( httpRemoteUri . Host , httpRemoteUri . Port , true , version ) . ConfigureAwait ( false ) ;
83
+
84
+ sslStream = new SslStream ( clientStream , true ) ;
85
+ var certificate = await CertManager . CreateCertificate ( httpRemoteUri . Host , false ) ;
86
+ //Successfully managed to authenticate the client using the fake certificate
87
+ await sslStream . AuthenticateAsServerAsync ( certificate , false ,
88
+ Constants . SupportedProtocols , false ) . ConfigureAwait ( false ) ;
89
+ //HTTPS server created - we can now decrypt the client's traffic
90
+ clientStream = sslStream ;
84
91
85
- if ( clientStream is SslStream )
86
- {
87
- sslStream = clientStream as SslStream ;
88
- }
89
- else
90
- {
91
- sslStream = new SslStream ( clientStream , true ) ;
92
- //Successfully managed to authenticate the client using the fake certificate
93
- await sslStream . AuthenticateAsServerAsync ( certificate , false ,
94
- Constants . SupportedProtocols , false ) . ConfigureAwait ( false ) ;
95
- //HTTPS server created - we can now decrypt the client's traffic
96
- clientStream = sslStream ;
97
- }
98
92
99
93
clientStreamReader = new CustomBinaryReader ( sslStream ) ;
100
94
clientStreamWriter = new StreamWriter ( sslStream ) ;
@@ -192,8 +186,6 @@ await HandleHttpSessionRequest(tcpClient, httpCmd, clientStream, clientStreamRea
192
186
private static async Task HandleHttpSessionRequest ( TcpClient client , string httpCmd , Stream clientStream ,
193
187
CustomBinaryReader clientStreamReader , StreamWriter clientStreamWriter , bool isHttps )
194
188
{
195
- TcpConnection connection = null ;
196
- string lastRequest = null ;
197
189
198
190
while ( true )
199
191
{
@@ -276,12 +268,8 @@ await TcpHelper.SendRaw(clientStream, httpCmd, args.WebSession.Request.RequestHe
276
268
}
277
269
278
270
//construct the web request that we are going to issue on behalf of the client.
279
- connection = connection == null ?
280
- await TcpConnectionManager . GetClient ( args . WebSession . Request . RequestUri . Host , args . WebSession . Request . RequestUri . Port , args . IsHttps , version ) . ConfigureAwait ( false )
281
- : lastRequest != args . WebSession . Request . RequestUri . Host ? await TcpConnectionManager . GetClient ( args . WebSession . Request . RequestUri . Host , args . WebSession . Request . RequestUri . Port , args . IsHttps , version ) . ConfigureAwait ( false )
282
- : connection ;
271
+ var connection = await TcpConnectionManager . GetClient ( args . WebSession . Request . RequestUri . Host , args . WebSession . Request . RequestUri . Port , args . IsHttps , version ) . ConfigureAwait ( false ) ;
283
272
284
- lastRequest = TcpConnectionManager . GetConnectionKey ( args . WebSession . Request . RequestUri . Host , args . WebSession . Request . RequestUri . Port , args . IsHttps , version ) ;
285
273
286
274
args . WebSession . Request . RequestLocked = true ;
287
275
@@ -350,11 +338,12 @@ await WriteResponseStatus(args.WebSession.Response.HttpVersion, "417",
350
338
//if connection is closing exit
351
339
if ( args . WebSession . Response . ResponseKeepAlive == false )
352
340
{
353
- connection . TcpClient . Close ( ) ;
354
341
Dispose ( client , clientStream , clientStreamReader , clientStreamWriter , args ) ;
355
342
return ;
356
343
}
357
344
345
+ await TcpConnectionManager . ReleaseClient ( connection ) ;
346
+
358
347
// read the next request
359
348
httpCmd = await clientStreamReader . ReadLineAsync ( ) . ConfigureAwait ( false ) ;
360
349
@@ -367,8 +356,6 @@ await WriteResponseStatus(args.WebSession.Response.HttpVersion, "417",
367
356
368
357
}
369
358
370
- if ( connection != null )
371
- await TcpConnectionManager . ReleaseClient ( connection ) ;
372
359
}
373
360
374
361
private static async Task WriteConnectResponse ( StreamWriter clientStreamWriter , Version httpVersion )
@@ -460,8 +447,6 @@ internal static bool ValidateServerCertificate(
460
447
X509Chain chain ,
461
448
SslPolicyErrors sslPolicyErrors )
462
449
{
463
- var customSslStream = sender as CustomSslStream ;
464
-
465
450
if ( ServerCertificateValidationCallback != null )
466
451
{
467
452
var args = new CertificateValidationEventArgs ( ) ;
@@ -510,43 +495,25 @@ internal static X509Certificate SelectClientCertificate(
510
495
X509Certificate clientCertificate = null ;
511
496
var customSslStream = sender as CustomSslStream ;
512
497
513
- if ( customSslStream . Param is ConnectRequest && remoteCertificate != null )
514
- {
515
- var connectRequest = customSslStream . Param as ConnectRequest ;
516
-
517
- var sslStream = new SslStream ( connectRequest . Stream , true ) ;
518
-
519
- var certificate = CertManager . CreateCertificate ( connectRequest . Uri . Host , false ) . Result ;
520
- //Successfully managed to authenticate the client using the fake certificate
521
- sslStream . AuthenticateAsServerAsync ( certificate , true ,
522
- Constants . SupportedProtocols , false ) . Wait ( ) ;
523
-
524
- connectRequest . Stream = sslStream ;
525
-
526
- clientCertificate = sslStream . RemoteCertificate ;
527
-
528
- }
529
- else if ( customSslStream . Param is ConnectRequest )
498
+ if ( acceptableIssuers != null &&
499
+ acceptableIssuers . Length > 0 &&
500
+ localCertificates != null &&
501
+ localCertificates . Count > 0 )
530
502
{
531
- if ( acceptableIssuers != null &&
532
- acceptableIssuers . Length > 0 &&
533
- localCertificates != null &&
534
- localCertificates . Count > 0 )
503
+ // Use the first certificate that is from an acceptable issuer.
504
+ foreach ( X509Certificate certificate in localCertificates )
535
505
{
536
- // Use the first certificate that is from an acceptable issuer.
537
- foreach ( X509Certificate certificate in localCertificates )
538
- {
539
- string issuer = certificate . Issuer ;
540
- if ( Array . IndexOf ( acceptableIssuers , issuer ) != - 1 )
541
- clientCertificate = certificate ;
542
- }
506
+ string issuer = certificate . Issuer ;
507
+ if ( Array . IndexOf ( acceptableIssuers , issuer ) != - 1 )
508
+ clientCertificate = certificate ;
543
509
}
544
-
545
- if ( localCertificates != null &&
546
- localCertificates . Count > 0 )
547
- clientCertificate = localCertificates [ 0 ] ;
548
510
}
549
511
512
+ if ( localCertificates != null &&
513
+ localCertificates . Count > 0 )
514
+ clientCertificate = localCertificates [ 0 ] ;
515
+
516
+
550
517
if ( ClientCertificateSelectionCallback != null )
551
518
{
552
519
var args = new CertificateSelectionEventArgs ( ) ;
0 commit comments