16
16
using Titanium . Web . Proxy . Shared ;
17
17
using Titanium . Web . Proxy . Http ;
18
18
using Titanium . Web . Proxy . Extensions ;
19
+ using System . Threading . Tasks ;
19
20
20
21
namespace Titanium . Web . Proxy
21
22
{
22
23
partial class ProxyServer
23
24
{
24
25
//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 )
26
27
{
27
28
Stream clientStream = client . GetStream ( ) ;
28
- var clientStreamReader = new CustomBinaryReader ( clientStream , Encoding . ASCII ) ;
29
+ var clientStreamReader = new CustomBinaryReader ( clientStream ) ;
29
30
var clientStreamWriter = new StreamWriter ( clientStream ) ;
30
31
31
32
Uri httpRemoteUri ;
@@ -75,10 +76,10 @@ private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient clien
75
76
sslStream = new SslStream ( clientStream , true ) ;
76
77
77
78
//Successfully managed to authenticate the client using the fake certificate
78
- sslStream . AuthenticateAsServer ( certificate , false ,
79
+ await sslStream . AuthenticateAsServerAsync ( certificate , false ,
79
80
Constants . SupportedProtocols , false ) ;
80
81
81
- clientStreamReader = new CustomBinaryReader ( sslStream , Encoding . ASCII ) ;
82
+ clientStreamReader = new CustomBinaryReader ( sslStream ) ;
82
83
clientStreamWriter = new StreamWriter ( sslStream ) ;
83
84
//HTTPS server created - we can now decrypt the client's traffic
84
85
clientStream = sslStream ;
@@ -111,7 +112,7 @@ private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient clien
111
112
112
113
//Now create the request
113
114
114
- HandleHttpSessionRequest ( client , httpCmd , clientStream , clientStreamReader , clientStreamWriter ,
115
+ await HandleHttpSessionRequest ( client , httpCmd , clientStream , clientStreamReader , clientStreamWriter ,
115
116
httpRemoteUri . Scheme == Uri . UriSchemeHttps ? true : false ) ;
116
117
}
117
118
catch
@@ -122,7 +123,7 @@ private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient clien
122
123
123
124
//This is called when requests are routed through router to this endpoint
124
125
//For ssl requests
125
- private static void HandleClient ( TransparentProxyEndPoint endPoint , TcpClient tcpClient )
126
+ private static async Task HandleClient ( TransparentProxyEndPoint endPoint , TcpClient tcpClient )
126
127
{
127
128
Stream clientStream = tcpClient . GetStream ( ) ;
128
129
CustomBinaryReader clientStreamReader = null ;
@@ -146,7 +147,7 @@ private static void HandleClient(TransparentProxyEndPoint endPoint, TcpClient tc
146
147
sslStream . AuthenticateAsServer ( certificate , false ,
147
148
SslProtocols . Tls , false ) ;
148
149
149
- clientStreamReader = new CustomBinaryReader ( sslStream , Encoding . ASCII ) ;
150
+ clientStreamReader = new CustomBinaryReader ( sslStream ) ;
150
151
clientStreamWriter = new StreamWriter ( sslStream ) ;
151
152
//HTTPS server created - we can now decrypt the client's traffic
152
153
@@ -163,17 +164,17 @@ private static void HandleClient(TransparentProxyEndPoint endPoint, TcpClient tc
163
164
}
164
165
else
165
166
{
166
- clientStreamReader = new CustomBinaryReader ( clientStream , Encoding . ASCII ) ;
167
+ clientStreamReader = new CustomBinaryReader ( clientStream ) ;
167
168
}
168
169
169
170
var httpCmd = clientStreamReader . ReadLine ( ) ;
170
171
171
172
//Now create the request
172
- HandleHttpSessionRequest ( tcpClient , httpCmd , clientStream , clientStreamReader , clientStreamWriter ,
173
+ await HandleHttpSessionRequest ( tcpClient , httpCmd , clientStream , clientStreamReader , clientStreamWriter ,
173
174
true ) ;
174
175
}
175
176
176
- private static void HandleHttpSessionRequest ( TcpClient client , string httpCmd , Stream clientStream ,
177
+ private static async Task HandleHttpSessionRequest ( TcpClient client , string httpCmd , Stream clientStream ,
177
178
CustomBinaryReader clientStreamReader , StreamWriter clientStreamWriter , bool IsHttps )
178
179
{
179
180
TcpConnection connection = null ;
@@ -244,8 +245,8 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
244
245
245
246
//construct the web request that we are going to issue on behalf of the client.
246
247
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 )
249
250
: connection ;
250
251
251
252
lastRequestHostName = args . WebSession . Request . RequestUri . Host ;
@@ -288,7 +289,7 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
288
289
if ( args . WebSession . Request . RequestBodyRead )
289
290
{
290
291
args . WebSession . Request . ContentLength = args . WebSession . Request . RequestBody . Length ;
291
- var newStream = args . WebSession . ProxyClient . ServerStreamReader . BaseStream ;
292
+ var newStream = args . WebSession . ProxyClient . Stream ;
292
293
newStream . Write ( args . WebSession . Request . RequestBody , 0 , args . WebSession . Request . RequestBody . Length ) ;
293
294
}
294
295
else
0 commit comments