11using System ;
22using System . Collections . Concurrent ;
3+ using System . IO ;
34using System . Linq ;
45using System . Net ;
56using System . Net . Security ;
67using System . Threading ;
78using System . Threading . Tasks ;
89using Titanium . Web . Proxy . EventArguments ;
9- using Titanium . Web . Proxy . Examples . Basic . Helpers ;
1010using Titanium . Web . Proxy . Exceptions ;
1111using Titanium . Web . Proxy . Helpers ;
1212using Titanium . Web . Proxy . Http ;
@@ -18,13 +18,14 @@ namespace Titanium.Web.Proxy.Examples.Basic
1818 public class ProxyTestController : IDisposable
1919 {
2020 private readonly ProxyServer proxyServer ;
21- private ExplicitProxyEndPoint explicitEndPoint ;
2221
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
2625 = new ConcurrentQueue < Tuple < ConsoleColor ? , string > > ( ) ;
2726
27+ private ExplicitProxyEndPoint explicitEndPoint ;
28+
2829 public ProxyTestController ( )
2930 {
3031 Task . Run ( ( ) => ListenToConsole ( ) ) ;
@@ -42,13 +43,9 @@ public ProxyTestController()
4243 proxyServer . ExceptionFunc = async exception =>
4344 {
4445 if ( exception is ProxyHttpException phex )
45- {
4646 WriteToConsole ( exception . Message + ": " + phex . InnerException ? . Message , ConsoleColor . Red ) ;
47- }
4847 else
49- {
5048 WriteToConsole ( exception . Message , ConsoleColor . Red ) ;
51- }
5249 } ;
5350
5451 proxyServer . TcpTimeWaitSeconds = 10 ;
@@ -74,6 +71,14 @@ public ProxyTestController()
7471 //proxyServer.CertificateManager.RootCertificate = new X509Certificate2("myCert.pfx", string.Empty, X509KeyStorageFlags.Exportable);
7572 }
7673
74+ private CancellationToken CancellationToken => cancellationTokenSource . Token ;
75+
76+ public void Dispose ( )
77+ {
78+ cancellationTokenSource . Dispose ( ) ;
79+ proxyServer . Dispose ( ) ;
80+ }
81+
7782 public void StartProxy ( )
7883 {
7984 proxyServer . BeforeRequest += OnRequest ;
@@ -127,18 +132,13 @@ public void StartProxy()
127132 //proxyServer.AddEndPoint(socksEndPoint);
128133
129134 foreach ( var endPoint in proxyServer . ProxyEndPoints )
130- {
131135 Console . WriteLine ( "Listening on '{0}' endpoint at Ip {1} and port: {2} " , endPoint . GetType ( ) . Name ,
132136 endPoint . IpAddress , endPoint . Port ) ;
133- }
134137
135138 // Only explicit proxies can be set as system proxy!
136139 //proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
137140 //proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
138- if ( RunTime . IsWindows )
139- {
140- proxyServer . SetAsSystemProxy ( explicitEndPoint , ProxyProtocolType . AllHttp ) ;
141- }
141+ if ( RunTime . IsWindows ) proxyServer . SetAsSystemProxy ( explicitEndPoint , ProxyProtocolType . AllHttp ) ;
142142 }
143143
144144 public void Stop ( )
@@ -191,23 +191,19 @@ private async Task<IExternalProxy> OnCustomUpStreamProxyFailureFunc(SessionEvent
191191
192192 private async Task OnBeforeTunnelConnectRequest ( object sender , TunnelConnectSessionEventArgs e )
193193 {
194- string hostname = e . HttpClient . Request . RequestUri . Host ;
194+ var hostname = e . HttpClient . Request . RequestUri . Host ;
195195 e . GetState ( ) . PipelineInfo . AppendLine ( nameof ( OnBeforeTunnelConnectRequest ) + ":" + hostname ) ;
196196 WriteToConsole ( "Tunnel to: " + hostname ) ;
197197
198198 var clientLocalIp = e . ClientLocalEndPoint . Address ;
199199 if ( ! clientLocalIp . Equals ( IPAddress . Loopback ) && ! clientLocalIp . Equals ( IPAddress . IPv6Loopback ) )
200- {
201200 e . HttpClient . UpStreamEndPoint = new IPEndPoint ( clientLocalIp , 0 ) ;
202- }
203201
204202 if ( hostname . Contains ( "dropbox.com" ) )
205- {
206203 // Exclude Https addresses you don't want to proxy
207204 // Useful for clients that use certificate pinning
208205 // for example dropbox.com
209206 e . DecryptSsl = false ;
210- }
211207 }
212208
213209 private void WebSocket_DataSent ( object sender , DataEventArgs e )
@@ -231,20 +227,18 @@ private void WebSocketDataSentReceived(SessionEventArgs args, DataEventArgs e, b
231227 if ( frame . OpCode == WebsocketOpCode . Binary )
232228 {
233229 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" ) ) ) ;
235231 WriteToConsole ( str , color ) ;
236232 }
237233
238- if ( frame . OpCode == WebsocketOpCode . Text )
239- {
240- WriteToConsole ( frame . GetText ( ) , color ) ;
241- }
234+ if ( frame . OpCode == WebsocketOpCode . Text ) WriteToConsole ( frame . GetText ( ) , color ) ;
242235 }
243236 }
244237
245238 private Task OnBeforeTunnelConnectResponse ( object sender , TunnelConnectSessionEventArgs e )
246239 {
247- e . GetState ( ) . PipelineInfo . AppendLine ( nameof ( OnBeforeTunnelConnectResponse ) + ":" + e . HttpClient . Request . RequestUri ) ;
240+ e . GetState ( ) . PipelineInfo
241+ . AppendLine ( nameof ( OnBeforeTunnelConnectResponse ) + ":" + e . HttpClient . Request . RequestUri ) ;
248242
249243 return Task . CompletedTask ;
250244 }
@@ -256,14 +250,10 @@ private async Task OnRequest(object sender, SessionEventArgs e)
256250
257251 var clientLocalIp = e . ClientLocalEndPoint . Address ;
258252 if ( ! clientLocalIp . Equals ( IPAddress . Loopback ) && ! clientLocalIp . Equals ( IPAddress . IPv6Loopback ) )
259- {
260253 e . HttpClient . UpStreamEndPoint = new IPEndPoint ( clientLocalIp , 0 ) ;
261- }
262254
263255 if ( e . HttpClient . Request . Url . Contains ( "yahoo.com" ) )
264- {
265256 e . CustomUpStreamProxy = new ExternalProxy ( "localhost" , 8888 ) ;
266- }
267257
268258 WriteToConsole ( "Active Client Connections:" + ( ( ProxyServer ) sender ) . ClientConnectionCount ) ;
269259 WriteToConsole ( e . HttpClient . Request . Url ) ;
@@ -310,10 +300,7 @@ private async Task MultipartRequestPartSent(object sender, MultipartRequestPartS
310300
311301 var session = ( SessionEventArgs ) sender ;
312302 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 ( ) ) ;
317304 }
318305
319306 private async Task OnResponse ( object sender , SessionEventArgs e )
@@ -328,7 +315,7 @@ private async Task OnResponse(object sender, SessionEventArgs e)
328315
329316 WriteToConsole ( "Active Server Connections:" + ( ( ProxyServer ) sender ) . ServerConnectionCount ) ;
330317
331- string ext = System . IO . Path . GetExtension ( e . HttpClient . Request . RequestUri . AbsolutePath ) ;
318+ var ext = Path . GetExtension ( e . HttpClient . Request . RequestUri . AbsolutePath ) ;
332319
333320 // access user data set in request to do something with it
334321 //var userData = e.HttpClient.UserData as CustomUserData;
@@ -385,10 +372,7 @@ public Task OnCertificateValidation(object sender, CertificateValidationEventArg
385372 e . GetState ( ) . PipelineInfo . AppendLine ( nameof ( OnCertificateValidation ) ) ;
386373
387374 // 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 ;
392376
393377 return Task . CompletedTask ;
394378 }
@@ -423,7 +407,7 @@ private async Task ListenToConsole()
423407
424408 if ( consoleColor . HasValue )
425409 {
426- ConsoleColor existing = Console . ForegroundColor ;
410+ var existing = Console . ForegroundColor ;
427411 Console . ForegroundColor = consoleColor . Value ;
428412 Console . WriteLine ( message ) ;
429413 Console . ForegroundColor = existing ;
@@ -439,12 +423,6 @@ private async Task ListenToConsole()
439423 }
440424 }
441425
442- public void Dispose ( )
443- {
444- cancellationTokenSource . Dispose ( ) ;
445- proxyServer . Dispose ( ) ;
446- }
447-
448426 ///// <summary>
449427 ///// User data object as defined by user.
450428 ///// User data can be set to each SessionEventArgs.HttpClient.UserData property
@@ -456,4 +434,4 @@ public void Dispose()
456434 // public string RequestBodyString { get; set; }
457435 //}
458436 }
459- }
437+ }
0 commit comments