1
1
using System ;
2
2
using System . Collections . Concurrent ;
3
+ using System . IO ;
3
4
using System . Linq ;
4
5
using System . Net ;
5
6
using System . Net . Security ;
6
7
using System . Threading ;
7
8
using System . Threading . Tasks ;
8
9
using Titanium . Web . Proxy . EventArguments ;
9
- using Titanium . Web . Proxy . Examples . Basic . Helpers ;
10
10
using Titanium . Web . Proxy . Exceptions ;
11
11
using Titanium . Web . Proxy . Helpers ;
12
12
using Titanium . Web . Proxy . Http ;
@@ -18,13 +18,14 @@ namespace Titanium.Web.Proxy.Examples.Basic
18
18
public class ProxyTestController : IDisposable
19
19
{
20
20
private readonly ProxyServer proxyServer ;
21
- private ExplicitProxyEndPoint explicitEndPoint ;
22
21
23
- private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource ( ) ;
24
- private CancellationToken CancellationToken => cancellationTokenSource . Token ;
25
- private ConcurrentQueue < Tuple < ConsoleColor ? , string > > consoleMessageQueue
22
+ private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource ( ) ;
23
+
24
+ private readonly ConcurrentQueue < Tuple < ConsoleColor ? , string > > consoleMessageQueue
26
25
= new ConcurrentQueue < Tuple < ConsoleColor ? , string > > ( ) ;
27
26
27
+ private ExplicitProxyEndPoint explicitEndPoint ;
28
+
28
29
public ProxyTestController ( )
29
30
{
30
31
Task . Run ( ( ) => ListenToConsole ( ) ) ;
@@ -42,13 +43,9 @@ public ProxyTestController()
42
43
proxyServer . ExceptionFunc = async exception =>
43
44
{
44
45
if ( exception is ProxyHttpException phex )
45
- {
46
46
WriteToConsole ( exception . Message + ": " + phex . InnerException ? . Message , ConsoleColor . Red ) ;
47
- }
48
47
else
49
- {
50
48
WriteToConsole ( exception . Message , ConsoleColor . Red ) ;
51
- }
52
49
} ;
53
50
54
51
proxyServer . TcpTimeWaitSeconds = 10 ;
@@ -74,6 +71,14 @@ public ProxyTestController()
74
71
//proxyServer.CertificateManager.RootCertificate = new X509Certificate2("myCert.pfx", string.Empty, X509KeyStorageFlags.Exportable);
75
72
}
76
73
74
+ private CancellationToken CancellationToken => cancellationTokenSource . Token ;
75
+
76
+ public void Dispose ( )
77
+ {
78
+ cancellationTokenSource . Dispose ( ) ;
79
+ proxyServer . Dispose ( ) ;
80
+ }
81
+
77
82
public void StartProxy ( )
78
83
{
79
84
proxyServer . BeforeRequest += OnRequest ;
@@ -127,18 +132,13 @@ public void StartProxy()
127
132
//proxyServer.AddEndPoint(socksEndPoint);
128
133
129
134
foreach ( var endPoint in proxyServer . ProxyEndPoints )
130
- {
131
135
Console . WriteLine ( "Listening on '{0}' endpoint at Ip {1} and port: {2} " , endPoint . GetType ( ) . Name ,
132
136
endPoint . IpAddress , endPoint . Port ) ;
133
- }
134
137
135
138
// Only explicit proxies can be set as system proxy!
136
139
//proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
137
140
//proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
138
- if ( RunTime . IsWindows )
139
- {
140
- proxyServer . SetAsSystemProxy ( explicitEndPoint , ProxyProtocolType . AllHttp ) ;
141
- }
141
+ if ( RunTime . IsWindows ) proxyServer . SetAsSystemProxy ( explicitEndPoint , ProxyProtocolType . AllHttp ) ;
142
142
}
143
143
144
144
public void Stop ( )
@@ -191,23 +191,19 @@ private async Task<IExternalProxy> OnCustomUpStreamProxyFailureFunc(SessionEvent
191
191
192
192
private async Task OnBeforeTunnelConnectRequest ( object sender , TunnelConnectSessionEventArgs e )
193
193
{
194
- string hostname = e . HttpClient . Request . RequestUri . Host ;
194
+ var hostname = e . HttpClient . Request . RequestUri . Host ;
195
195
e . GetState ( ) . PipelineInfo . AppendLine ( nameof ( OnBeforeTunnelConnectRequest ) + ":" + hostname ) ;
196
196
WriteToConsole ( "Tunnel to: " + hostname ) ;
197
197
198
198
var clientLocalIp = e . ClientLocalEndPoint . Address ;
199
199
if ( ! clientLocalIp . Equals ( IPAddress . Loopback ) && ! clientLocalIp . Equals ( IPAddress . IPv6Loopback ) )
200
- {
201
200
e . HttpClient . UpStreamEndPoint = new IPEndPoint ( clientLocalIp , 0 ) ;
202
- }
203
201
204
202
if ( hostname . Contains ( "dropbox.com" ) )
205
- {
206
203
// Exclude Https addresses you don't want to proxy
207
204
// Useful for clients that use certificate pinning
208
205
// for example dropbox.com
209
206
e . DecryptSsl = false ;
210
- }
211
207
}
212
208
213
209
private void WebSocket_DataSent ( object sender , DataEventArgs e )
@@ -231,20 +227,18 @@ private void WebSocketDataSentReceived(SessionEventArgs args, DataEventArgs e, b
231
227
if ( frame . OpCode == WebsocketOpCode . Binary )
232
228
{
233
229
var data = frame . Data . ToArray ( ) ;
234
- string str = string . Join ( "," , data . ToArray ( ) . Select ( x => x . ToString ( "X2" ) ) ) ;
230
+ var str = string . Join ( "," , data . ToArray ( ) . Select ( x => x . ToString ( "X2" ) ) ) ;
235
231
WriteToConsole ( str , color ) ;
236
232
}
237
233
238
- if ( frame . OpCode == WebsocketOpCode . Text )
239
- {
240
- WriteToConsole ( frame . GetText ( ) , color ) ;
241
- }
234
+ if ( frame . OpCode == WebsocketOpCode . Text ) WriteToConsole ( frame . GetText ( ) , color ) ;
242
235
}
243
236
}
244
237
245
238
private Task OnBeforeTunnelConnectResponse ( object sender , TunnelConnectSessionEventArgs e )
246
239
{
247
- e . GetState ( ) . PipelineInfo . AppendLine ( nameof ( OnBeforeTunnelConnectResponse ) + ":" + e . HttpClient . Request . RequestUri ) ;
240
+ e . GetState ( ) . PipelineInfo
241
+ . AppendLine ( nameof ( OnBeforeTunnelConnectResponse ) + ":" + e . HttpClient . Request . RequestUri ) ;
248
242
249
243
return Task . CompletedTask ;
250
244
}
@@ -256,14 +250,10 @@ private async Task OnRequest(object sender, SessionEventArgs e)
256
250
257
251
var clientLocalIp = e . ClientLocalEndPoint . Address ;
258
252
if ( ! clientLocalIp . Equals ( IPAddress . Loopback ) && ! clientLocalIp . Equals ( IPAddress . IPv6Loopback ) )
259
- {
260
253
e . HttpClient . UpStreamEndPoint = new IPEndPoint ( clientLocalIp , 0 ) ;
261
- }
262
254
263
255
if ( e . HttpClient . Request . Url . Contains ( "yahoo.com" ) )
264
- {
265
256
e . CustomUpStreamProxy = new ExternalProxy ( "localhost" , 8888 ) ;
266
- }
267
257
268
258
WriteToConsole ( "Active Client Connections:" + ( ( ProxyServer ) sender ) . ClientConnectionCount ) ;
269
259
WriteToConsole ( e . HttpClient . Request . Url ) ;
@@ -310,10 +300,7 @@ private async Task MultipartRequestPartSent(object sender, MultipartRequestPartS
310
300
311
301
var session = ( SessionEventArgs ) sender ;
312
302
WriteToConsole ( "Multipart form data headers:" ) ;
313
- foreach ( var header in e . Headers )
314
- {
315
- WriteToConsole ( header . ToString ( ) ) ;
316
- }
303
+ foreach ( var header in e . Headers ) WriteToConsole ( header . ToString ( ) ) ;
317
304
}
318
305
319
306
private async Task OnResponse ( object sender , SessionEventArgs e )
@@ -328,7 +315,7 @@ private async Task OnResponse(object sender, SessionEventArgs e)
328
315
329
316
WriteToConsole ( "Active Server Connections:" + ( ( ProxyServer ) sender ) . ServerConnectionCount ) ;
330
317
331
- string ext = System . IO . Path . GetExtension ( e . HttpClient . Request . RequestUri . AbsolutePath ) ;
318
+ var ext = Path . GetExtension ( e . HttpClient . Request . RequestUri . AbsolutePath ) ;
332
319
333
320
// access user data set in request to do something with it
334
321
//var userData = e.HttpClient.UserData as CustomUserData;
@@ -385,10 +372,7 @@ public Task OnCertificateValidation(object sender, CertificateValidationEventArg
385
372
e . GetState ( ) . PipelineInfo . AppendLine ( nameof ( OnCertificateValidation ) ) ;
386
373
387
374
// set IsValid to true/false based on Certificate Errors
388
- if ( e . SslPolicyErrors == SslPolicyErrors . None )
389
- {
390
- e . IsValid = true ;
391
- }
375
+ if ( e . SslPolicyErrors == SslPolicyErrors . None ) e . IsValid = true ;
392
376
393
377
return Task . CompletedTask ;
394
378
}
@@ -423,7 +407,7 @@ private async Task ListenToConsole()
423
407
424
408
if ( consoleColor . HasValue )
425
409
{
426
- ConsoleColor existing = Console . ForegroundColor ;
410
+ var existing = Console . ForegroundColor ;
427
411
Console . ForegroundColor = consoleColor . Value ;
428
412
Console . WriteLine ( message ) ;
429
413
Console . ForegroundColor = existing ;
@@ -439,12 +423,6 @@ private async Task ListenToConsole()
439
423
}
440
424
}
441
425
442
- public void Dispose ( )
443
- {
444
- cancellationTokenSource . Dispose ( ) ;
445
- proxyServer . Dispose ( ) ;
446
- }
447
-
448
426
///// <summary>
449
427
///// User data object as defined by user.
450
428
///// User data can be set to each SessionEventArgs.HttpClient.UserData property
@@ -456,4 +434,4 @@ public void Dispose()
456
434
// public string RequestBodyString { get; set; }
457
435
//}
458
436
}
459
- }
437
+ }
0 commit comments