@@ -14,7 +14,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
1414 public class ProxyTestController
1515 {
1616 private readonly ProxyServer proxyServer ;
17-
17+ private ExplicitProxyEndPoint explicitEndPoint ;
1818
1919 //keep track of request headers
2020 private readonly IDictionary < Guid , HeaderCollection > requestHeaderHistory = new ConcurrentDictionary < Guid , HeaderCollection > ( ) ;
@@ -52,38 +52,31 @@ public void StartProxy()
5252 {
5353 proxyServer . BeforeRequest += OnRequest ;
5454 proxyServer . BeforeResponse += OnResponse ;
55- proxyServer . TunnelConnectRequest += OnTunnelConnectRequest ;
56- proxyServer . TunnelConnectResponse += OnTunnelConnectResponse ;
55+
5756 proxyServer . ServerCertificateValidationCallback += OnCertificateValidation ;
5857 proxyServer . ClientCertificateSelectionCallback += OnCertificateSelection ;
5958
6059 //proxyServer.EnableWinAuth = true;
6160
62- var explicitEndPoint = new ExplicitProxyEndPoint ( IPAddress . Any , 8000 , true )
61+ explicitEndPoint = new ExplicitProxyEndPoint ( IPAddress . Any , 8000 , true )
6362 {
64- //Exclude Https addresses you don't want to proxy
65- //Useful for clients that use certificate pinning
66- //for example google.com and dropbox.com
67- ExcludedHttpsHostNameRegex = new List < string >
68- {
69- "dropbox.com"
70- } ,
71-
72- //Include Https addresses you want to proxy (others will be excluded)
73- //for example github.com
74- //IncludedHttpsHostNameRegex = new List<string>
75- //{
76- // "github.com"
77- //},
78-
79- //You can set only one of the ExcludedHttpsHostNameRegex and IncludedHttpsHostNameRegex properties, otherwise ArgumentException will be thrown
80-
81- //Use self-issued generic certificate on all https requests
82- //Optimizes performance by not creating a certificate for each https-enabled domain
83- //Useful when certificate trust is not required by proxy clients
84- //GenericCertificate = new X509Certificate2(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "genericcert.pfx"), "password")
63+ //You can set only one of the ExcludedHttpsHostNameRegex and IncludedHttpsHostNameRegex properties, otherwise ArgumentException will be thrown
64+
65+ //Use self-issued generic certificate on all https requests
66+ //Optimizes performance by not creating a certificate for each https-enabled domain
67+ //Useful when certificate trust is not required by proxy clients
68+ //GenericCertificate = new X509Certificate2(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "genericcert.pfx"), "password")
8569 } ;
8670
71+ //Exclude Https addresses you don't want to proxy
72+ //Useful for clients that use certificate pinning
73+ //for example google.com and dropbox.com
74+ explicitEndPoint . BeforeTunnelConnect += OnBeforeTunnelConnect ;
75+
76+
77+ explicitEndPoint . TunnelConnectRequest += OnTunnelConnectRequest ;
78+ explicitEndPoint . TunnelConnectResponse += OnTunnelConnectResponse ;
79+
8780 //An explicit endpoint is where the client knows about the existence of a proxy
8881 //So client sends request in a proxy friendly manner
8982 proxyServer . AddEndPoint ( explicitEndPoint ) ;
@@ -120,8 +113,10 @@ public void StartProxy()
120113
121114 public void Stop ( )
122115 {
123- proxyServer . TunnelConnectRequest -= OnTunnelConnectRequest ;
124- proxyServer . TunnelConnectResponse -= OnTunnelConnectResponse ;
116+ explicitEndPoint . BeforeTunnelConnect -= OnBeforeTunnelConnect ;
117+ explicitEndPoint . TunnelConnectRequest -= OnTunnelConnectRequest ;
118+ explicitEndPoint . TunnelConnectResponse -= OnTunnelConnectResponse ;
119+
125120 proxyServer . BeforeRequest -= OnRequest ;
126121 proxyServer . BeforeResponse -= OnResponse ;
127122 proxyServer . ServerCertificateValidationCallback -= OnCertificateValidation ;
@@ -133,6 +128,20 @@ public void Stop()
133128 //proxyServer.CertificateManager.RemoveTrustedRootCertificates();
134129 }
135130
131+ private async Task < bool > OnBeforeTunnelConnect ( string hostname )
132+ {
133+ if ( hostname . Contains ( "amazon.com" ) || hostname . Contains ( "paypal.com" ) )
134+ {
135+ //exclude bing.com and google.com from being decrypted
136+ //instead it will be relayed via a secure TCP tunnel
137+ return await Task . FromResult ( true ) ;
138+ }
139+ else
140+ {
141+ return await Task . FromResult ( false ) ;
142+ }
143+ }
144+
136145 private async Task OnTunnelConnectRequest ( object sender , TunnelConnectSessionEventArgs e )
137146 {
138147 Console . WriteLine ( "Tunnel to: " + e . WebSession . Request . Host ) ;
@@ -172,7 +181,7 @@ private async Task OnRequest(object sender, SessionEventArgs e)
172181
173182 //To cancel a request with a custom HTML content
174183 //Filter URL
175- if ( e . WebSession . Request . RequestUri . AbsoluteUri . Contains ( "google .com" ) )
184+ if ( e . WebSession . Request . RequestUri . AbsoluteUri . Contains ( "yahoo .com" ) )
176185 {
177186 await e . Ok ( "<!DOCTYPE html>" +
178187 "<html><body><h1>" +
0 commit comments