1212using  Titanium . Web . Proxy . Shared ; 
1313using  System . Security . Cryptography . X509Certificates ; 
1414using  Titanium . Web . Proxy . EventArguments ; 
15+ using  Titanium . Web . Proxy . Models ; 
1516
1617namespace  Titanium . Web . Proxy . Network 
1718{ 
@@ -38,7 +39,12 @@ internal class TcpConnectionManager
3839    { 
3940        static   Dictionary < string ,  List < TcpConnection > >  connectionCache  =  new  Dictionary < string ,  List < TcpConnection > > ( ) ; 
4041        static   SemaphoreSlim  connectionAccessLock  =  new  SemaphoreSlim ( 1 ) ; 
41-         internal  static   async  Task < TcpConnection >  GetClient ( SessionEventArgs  sessionArgs ,  string  hostname ,  int  port ,  bool  isHttps ,  Version  version ) 
42+ 
43+         internal  static   async  Task < TcpConnection >  GetClient ( string  hostname ,  int  port ,  bool  isHttps ,  Version  version ) 
44+         { 
45+             return  await  GetClient ( null ,  hostname ,  port ,  isHttps ,  version ) ; 
46+         } 
47+         internal  static   async  Task < TcpConnection >  GetClient ( ConnectRequest  connectRequest ,  string  hostname ,  int  port ,  bool  isHttps ,  Version  version ) 
4248        { 
4349            List < TcpConnection >  cachedConnections  =  null ; 
4450            TcpConnection  cached  =  null ; 
@@ -72,14 +78,14 @@ internal static async Task<TcpConnection> GetClient(SessionEventArgs sessionArgs
7278            } 
7379
7480            if  ( cached  ==  null ) 
75-                 cached  =  await  CreateClient ( sessionArgs ,  hostname ,  port ,  isHttps ,  version ) . ConfigureAwait ( false ) ; 
81+                 cached  =  await  CreateClient ( connectRequest ,  hostname ,  port ,  isHttps ,  version ) . ConfigureAwait ( false ) ; 
7682
7783
7884            //just create one more preemptively 
7985            if  ( cachedConnections  ==  null  ||  cachedConnections . Count ( )  <  2 ) 
8086            { 
81-                 var  task  =  CreateClient ( sessionArgs ,  hostname ,  port ,  isHttps ,  version ) 
82-                             . ContinueWith ( async  ( x )  =>  {  if  ( x . Status  ==  TaskStatus . RanToCompletion )  await  ReleaseClient ( x . Result ) ;  } ) ; 
87+                      var  task  =  CreateClient ( connectRequest ,  hostname ,  port ,  isHttps ,  version ) 
88+                                  . ContinueWith ( async  ( x )  =>  {  if  ( x . Status  ==  TaskStatus . RanToCompletion )  await  ReleaseClient ( x . Result ) ;  } ) ; 
8389            } 
8490
8591            return  cached ; 
@@ -90,7 +96,7 @@ internal static string GetConnectionKey(string hostname, int port, bool isHttps,
9096            return  string . Format ( "{0}:{1}:{2}:{3}:{4}" ,  hostname . ToLower ( ) ,  port ,  isHttps ,  version . Major ,  version . Minor ) ; 
9197        } 
9298
93-         private  static   async  Task < TcpConnection >  CreateClient ( SessionEventArgs   sessionArgs ,  string  hostname ,  int  port ,  bool  isHttps ,  Version  version ) 
99+         private  static   async  Task < TcpConnection >  CreateClient ( ConnectRequest   connectRequest ,  string  hostname ,  int  port ,  bool  isHttps ,  Version  version ) 
94100        { 
95101            TcpClient  client ; 
96102            Stream  stream ; 
@@ -106,8 +112,8 @@ private static async Task<TcpConnection> CreateClient(SessionEventArgs sessionAr
106112
107113                    using  ( var  writer  =  new  StreamWriter ( stream ,  Encoding . ASCII ,  Constants . BUFFER_SIZE ,  true ) ) 
108114                    { 
109-                         await  writer . WriteLineAsync ( string . Format ( "CONNECT {0}:{1} {2}" ,  sessionArgs . WebSession . Request . RequestUri . Host ,   sessionArgs . WebSession . Request . RequestUri . Port ,   sessionArgs . WebSession . Request . HttpVersion ) ) . ConfigureAwait ( false ) ; 
110-                         await  writer . WriteLineAsync ( string . Format ( "Host: {0}:{1}" ,  sessionArgs . WebSession . Request . RequestUri . Host ,   sessionArgs . WebSession . Request . RequestUri . Port ) ) . ConfigureAwait ( false ) ; 
115+                         await  writer . WriteLineAsync ( string . Format ( "CONNECT {0}:{1} {2}" ,  hostname ,   port ,   version ) ) . ConfigureAwait ( false ) ; 
116+                         await  writer . WriteLineAsync ( string . Format ( "Host: {0}:{1}" ,  hostname ,   port ) ) . ConfigureAwait ( false ) ; 
111117                        await  writer . WriteLineAsync ( "Connection: Keep-Alive" ) . ConfigureAwait ( false ) ; 
112118                        await  writer . WriteLineAsync ( ) . ConfigureAwait ( false ) ; 
113119                        await  writer . FlushAsync ( ) . ConfigureAwait ( false ) ; 
@@ -132,8 +138,9 @@ private static async Task<TcpConnection> CreateClient(SessionEventArgs sessionAr
132138
133139                try 
134140                { 
135-                     sslStream  =  new  CustomSslStream ( stream ,  true ,  new  RemoteCertificateValidationCallback ( ProxyServer . ValidateServerCertificate ) ) ; 
136-                     sslStream . Session  =  sessionArgs ; 
141+                     sslStream  =  new  CustomSslStream ( stream ,  true ,  new  RemoteCertificateValidationCallback ( ProxyServer . ValidateServerCertificate ) , 
142+                         new  LocalCertificateSelectionCallback ( ProxyServer . SelectClientCertificate ) ) ; 
143+                     sslStream . Param   =  connectRequest ; 
137144                    await  sslStream . AuthenticateAsClientAsync ( hostname ,  null ,  Constants . SupportedProtocols ,  false ) . ConfigureAwait ( false ) ; 
138145                    stream  =  ( Stream ) sslStream ; 
139146                } 
0 commit comments