@@ -135,7 +135,7 @@ private async Task HandleClient(ExplicitProxyEndPoint endPoint, TcpClient tcpCli
135135 clientStreamReader = new CustomBinaryReader ( clientStream , BufferSize ) ;
136136 clientStreamWriter = new HttpResponseWriter ( clientStream , BufferSize ) ;
137137 }
138- catch ( Exception e )
138+ catch ( Exception e )
139139 {
140140 ExceptionFunc ( new Exception ( $ "Could'nt authenticate client '{ connectHostname } ' with fake certificate.", e ) ) ;
141141 sslStream ? . Dispose ( ) ;
@@ -238,21 +238,32 @@ private async Task HandleClient(TransparentProxyEndPoint endPoint, TcpClient tcp
238238
239239 if ( clientHelloInfo != null )
240240 {
241- var sslStream = new SslStream ( clientStream ) ;
242- clientStream = new CustomBufferedStream ( sslStream , BufferSize ) ;
243-
244- string sniHostName = clientHelloInfo . GetServerName ( ) ?? endPoint . GenericCertificateName ;
241+ SslStream sslStream = null ;
242+ string sniHostName = null ;
245243
246- string certName = HttpHelper . GetWildCardDomainName ( sniHostName ) ;
247- var certificate = await CertificateManager . CreateCertificateAsync ( certName ) ;
248244 try
249245 {
246+ sslStream = new SslStream ( clientStream ) ;
247+
248+ sniHostName = clientHelloInfo . GetServerName ( ) ?? endPoint . GenericCertificateName ;
249+
250+ string certName = HttpHelper . GetWildCardDomainName ( sniHostName ) ;
251+ var certificate = await CertificateManager . CreateCertificateAsync ( certName ) ;
252+
250253 //Successfully managed to authenticate the client using the fake certificate
251254 await sslStream . AuthenticateAsServerAsync ( certificate , false , SslProtocols . Tls , false ) ;
255+
256+ //HTTPS server created - we can now decrypt the client's traffic
257+ clientStream = new CustomBufferedStream ( sslStream , BufferSize ) ;
258+
259+ clientStreamReader . Dispose ( ) ;
260+ clientStreamReader = new CustomBinaryReader ( clientStream , BufferSize ) ;
261+ clientStreamWriter = new HttpResponseWriter ( clientStream , BufferSize ) ;
252262 }
253263 catch ( Exception e )
254264 {
255265 ExceptionFunc ( new Exception ( $ "Could'nt authenticate client '{ sniHostName } ' with fake certificate.", e ) ) ;
266+ sslStream ? . Dispose ( ) ;
256267 return ;
257268 }
258269 }
0 commit comments