1515using  Titanium . Web . Proxy . Helpers ; 
1616using  Titanium . Web . Proxy . Network ; 
1717using  Titanium . Web . Proxy . Models ; 
18+ using  System . Security . Cryptography . X509Certificates ; 
1819
1920namespace  Titanium . Web . Proxy 
2021{ 
2122    partial  class  ProxyServer 
2223    { 
24+         //This is called when client is aware of proxy 
2325        private  static void  HandleClient ( ExplicitProxyEndPoint  endPoint ,  TcpClient  client ) 
2426        { 
2527            Stream  clientStream  =  client . GetStream ( ) ; 
@@ -54,7 +56,7 @@ private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient clien
5456                var  excluded  =  endPoint . ExcludedHostNameRegex  !=  null  ?  endPoint . ExcludedHostNameRegex . Any ( x =>  Regex . IsMatch ( httpRemoteUri . Host ,  x ) )  :  false ; 
5557
5658                //Client wants to create a secure tcp tunnel (its a HTTPS request) 
57-                 if  ( httpVerb . ToUpper ( )  ==  "CONNECT"  &&  ! excluded  &&  httpRemoteUri . Port != 80 ) 
59+                 if  ( httpVerb . ToUpper ( )  ==  "CONNECT"  &&  ! excluded  &&  httpRemoteUri . Port   !=   80 ) 
5860                { 
5961                    httpRemoteUri  =  new  Uri ( "https://"  +  httpCmdSplit [ 1 ] ) ; 
6062                    clientStreamReader . ReadAllLines ( ) ; 
@@ -105,7 +107,7 @@ private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient clien
105107                } 
106108
107109                //Now create the request 
108-                  
110+ 
109111                HandleHttpSessionRequest ( client ,  httpCmd ,  clientStream ,  clientStreamReader ,  clientStreamWriter , 
110112                    httpRemoteUri . Scheme  ==  Uri . UriSchemeHttps  ?  true  :  false ) ; 
111113            } 
@@ -115,12 +117,21 @@ private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient clien
115117            } 
116118        } 
117119
118-         private  static void  HandleClient ( TransparentProxyEndPoint  endPoint ,  TcpClient  client ) 
120+         //This is called when requests are routed through router to this endpoint 
121+         private  static void  HandleClient ( TransparentProxyEndPoint  endPoint ,  TcpClient  tcpClient ) 
119122        { 
120-             var  sslStream  =  new  SslStream ( client . GetStream ( ) ,  true ) ; 
123+             var  sslStream  =  new  SslStream ( tcpClient . GetStream ( ) ,  true ) ; 
121124            CustomBinaryReader  clientStreamReader  =  null ; 
122125            StreamWriter  clientStreamWriter  =  null ; 
123-             var  certificate  =  CertManager . CreateCertificate ( "127.0.0.1" ) ; 
126+             X509Certificate2  certificate  =  null ; 
127+ 
128+             //if(endPoint.UseServerNameIndication) 
129+             //{ 
130+             //   //implement in future once SNI supported by SSL stream 
131+             //    certificate = CertManager.CreateCertificate(endPoint.GenericCertificateName); 
132+             //} 
133+             //else 
134+             certificate  =  CertManager . CreateCertificate ( endPoint . GenericCertificateName ) ; 
124135
125136            try 
126137            { 
@@ -133,20 +144,19 @@ private static void HandleClient(TransparentProxyEndPoint endPoint, TcpClient cl
133144                //HTTPS server created - we can now decrypt the client's traffic 
134145
135146            } 
136- 
137-             catch  ( Exception  e ) 
147+             catch  ( Exception ) 
138148            { 
139149                if  ( sslStream  !=  null ) 
140150                    sslStream . Dispose ( ) ; 
141151
142-                 Dispose ( client ,  sslStream ,  clientStreamReader ,  clientStreamWriter ,  null ) ; 
152+                 Dispose ( tcpClient ,  sslStream ,  clientStreamReader ,  clientStreamWriter ,  null ) ; 
143153                return ; 
144154            } 
145155
146156            var  httpCmd  =  clientStreamReader . ReadLine ( ) ; 
147157
148158            //Now create the request 
149-             HandleHttpSessionRequest ( client ,  httpCmd ,  sslStream ,  clientStreamReader ,  clientStreamWriter , 
159+             HandleHttpSessionRequest ( tcpClient ,  httpCmd ,  sslStream ,  clientStreamReader ,  clientStreamWriter , 
150160                true ) ; 
151161        } 
152162
0 commit comments