1616using Titanium . Web . Proxy . Shared ;
1717using Titanium . Web . Proxy . Http ;
1818using Titanium . Web . Proxy . Extensions ;
19+ using System . Threading . Tasks ;
1920
2021namespace Titanium . Web . Proxy
2122{
2223 partial class ProxyServer
2324 {
2425 //This is called when client is aware of proxy
25- private static void HandleClient ( ExplicitProxyEndPoint endPoint , TcpClient client )
26+ private static async void HandleClient ( ExplicitProxyEndPoint endPoint , TcpClient client )
2627 {
2728 Stream clientStream = client . GetStream ( ) ;
28- var clientStreamReader = new CustomBinaryReader ( clientStream , Encoding . ASCII ) ;
29+ var clientStreamReader = new CustomBinaryReader ( clientStream ) ;
2930 var clientStreamWriter = new StreamWriter ( clientStream ) ;
3031
3132 Uri httpRemoteUri ;
@@ -75,10 +76,10 @@ private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient clien
7576 sslStream = new SslStream ( clientStream , true ) ;
7677
7778 //Successfully managed to authenticate the client using the fake certificate
78- sslStream . AuthenticateAsServer ( certificate , false ,
79+ await sslStream . AuthenticateAsServerAsync ( certificate , false ,
7980 Constants . SupportedProtocols , false ) ;
8081
81- clientStreamReader = new CustomBinaryReader ( sslStream , Encoding . ASCII ) ;
82+ clientStreamReader = new CustomBinaryReader ( sslStream ) ;
8283 clientStreamWriter = new StreamWriter ( sslStream ) ;
8384 //HTTPS server created - we can now decrypt the client's traffic
8485 clientStream = sslStream ;
@@ -111,7 +112,7 @@ private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient clien
111112
112113 //Now create the request
113114
114- HandleHttpSessionRequest ( client , httpCmd , clientStream , clientStreamReader , clientStreamWriter ,
115+ await HandleHttpSessionRequest ( client , httpCmd , clientStream , clientStreamReader , clientStreamWriter ,
115116 httpRemoteUri . Scheme == Uri . UriSchemeHttps ? true : false ) ;
116117 }
117118 catch
@@ -122,7 +123,7 @@ private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient clien
122123
123124 //This is called when requests are routed through router to this endpoint
124125 //For ssl requests
125- private static void HandleClient ( TransparentProxyEndPoint endPoint , TcpClient tcpClient )
126+ private static async Task HandleClient ( TransparentProxyEndPoint endPoint , TcpClient tcpClient )
126127 {
127128 Stream clientStream = tcpClient . GetStream ( ) ;
128129 CustomBinaryReader clientStreamReader = null ;
@@ -146,7 +147,7 @@ private static void HandleClient(TransparentProxyEndPoint endPoint, TcpClient tc
146147 sslStream . AuthenticateAsServer ( certificate , false ,
147148 SslProtocols . Tls , false ) ;
148149
149- clientStreamReader = new CustomBinaryReader ( sslStream , Encoding . ASCII ) ;
150+ clientStreamReader = new CustomBinaryReader ( sslStream ) ;
150151 clientStreamWriter = new StreamWriter ( sslStream ) ;
151152 //HTTPS server created - we can now decrypt the client's traffic
152153
@@ -163,17 +164,17 @@ private static void HandleClient(TransparentProxyEndPoint endPoint, TcpClient tc
163164 }
164165 else
165166 {
166- clientStreamReader = new CustomBinaryReader ( clientStream , Encoding . ASCII ) ;
167+ clientStreamReader = new CustomBinaryReader ( clientStream ) ;
167168 }
168169
169170 var httpCmd = clientStreamReader . ReadLine ( ) ;
170171
171172 //Now create the request
172- HandleHttpSessionRequest ( tcpClient , httpCmd , clientStream , clientStreamReader , clientStreamWriter ,
173+ await HandleHttpSessionRequest ( tcpClient , httpCmd , clientStream , clientStreamReader , clientStreamWriter ,
173174 true ) ;
174175 }
175176
176- private static void HandleHttpSessionRequest ( TcpClient client , string httpCmd , Stream clientStream ,
177+ private static async Task HandleHttpSessionRequest ( TcpClient client , string httpCmd , Stream clientStream ,
177178 CustomBinaryReader clientStreamReader , StreamWriter clientStreamWriter , bool IsHttps )
178179 {
179180 TcpConnection connection = null ;
@@ -244,8 +245,8 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
244245
245246 //construct the web request that we are going to issue on behalf of the client.
246247 connection = connection == null ?
247- TcpConnectionManager . GetClient ( args , args . WebSession . Request . RequestUri . Host , args . WebSession . Request . RequestUri . Port , args . IsHttps , version )
248- : lastRequestHostName != args . WebSession . Request . RequestUri . Host ? TcpConnectionManager . GetClient ( args , args . WebSession . Request . RequestUri . Host , args . WebSession . Request . RequestUri . Port , args . IsHttps , version )
248+ await TcpConnectionManager . GetClient ( args , args . WebSession . Request . RequestUri . Host , args . WebSession . Request . RequestUri . Port , args . IsHttps , version )
249+ : lastRequestHostName != args . WebSession . Request . RequestUri . Host ? await TcpConnectionManager . GetClient ( args , args . WebSession . Request . RequestUri . Host , args . WebSession . Request . RequestUri . Port , args . IsHttps , version )
249250 : connection ;
250251
251252 lastRequestHostName = args . WebSession . Request . RequestUri . Host ;
@@ -288,7 +289,7 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
288289 if ( args . WebSession . Request . RequestBodyRead )
289290 {
290291 args . WebSession . Request . ContentLength = args . WebSession . Request . RequestBody . Length ;
291- var newStream = args . WebSession . ProxyClient . ServerStreamReader . BaseStream ;
292+ var newStream = args . WebSession . ProxyClient . Stream ;
292293 newStream . Write ( args . WebSession . Request . RequestBody , 0 , args . WebSession . Request . RequestBody . Length ) ;
293294 }
294295 else
0 commit comments