@@ -9,8 +9,6 @@ namespace Titanium.Web.Proxy.Test
99{
1010 public class ProxyTestController
1111 {
12-
13-
1412 public void StartProxy ( )
1513 {
1614 ProxyServer . BeforeRequest += OnRequest ;
@@ -19,36 +17,35 @@ public void StartProxy()
1917 //Exclude Https addresses you don't want to proxy
2018 //Usefull for clients that use certificate pinning
2119 //for example dropbox.com
22- var explicitEndPoint = new ExplicitProxyEndPoint ( IPAddress . Any , 8000 , true ) {
23- // ExcludedHttpsHostNameRegex = new List<string>() { "google.com", "dropbox.com" }
20+ var explicitEndPoint = new ExplicitProxyEndPoint ( IPAddress . Any , 8000 , true )
21+ {
22+ // ExcludedHttpsHostNameRegex = new List<string>() { "google.com", "dropbox.com" }
2423 } ;
2524
2625 //An explicit endpoint is where the client knows about the existance of a proxy
2726 //So client sends request in a proxy friendly manner
2827 ProxyServer . AddEndPoint ( explicitEndPoint ) ;
2928 ProxyServer . Start ( ) ;
3029
31-
30+
3231 //Transparent endpoint is usefull for reverse proxying (client is not aware of the existance of proxy)
3332 //A transparent endpoint usually requires a network router port forwarding HTTP(S) packets to this endpoint
3433 //Currently do not support Server Name Indication (It is not currently supported by SslStream class)
3534 //That means that the transparent endpoint will always provide the same Generic Certificate to all HTTPS requests
3635 //In this example only google.com will work for HTTPS requests
3736 //Other sites will receive a certificate mismatch warning on browser
3837 //Please read about it before asking questions!
39- var transparentEndPoint = new TransparentProxyEndPoint ( IPAddress . Any , 8001 , true ) {
38+ var transparentEndPoint = new TransparentProxyEndPoint ( IPAddress . Any , 8001 , true )
39+ {
4040 GenericCertificateName = "google.com"
41- } ;
41+ } ;
4242 ProxyServer . AddEndPoint ( transparentEndPoint ) ;
43-
43+
4444
4545 foreach ( var endPoint in ProxyServer . ProxyEndPoints )
46- Console . WriteLine ( "Listening on '{0}' endpoint at Ip {1} and port: {2} " ,
46+ Console . WriteLine ( "Listening on '{0}' endpoint at Ip {1} and port: {2} " ,
4747 endPoint . GetType ( ) . Name , endPoint . IpAddress , endPoint . Port ) ;
4848
49- //You can also add/remove end points after proxy has been started
50- ProxyServer . RemoveEndPoint ( transparentEndPoint ) ;
51-
5249 //Only explicit proxies can be set as system proxy!
5350 ProxyServer . SetAsSystemHttpProxy ( explicitEndPoint ) ;
5451 ProxyServer . SetAsSystemHttpsProxy ( explicitEndPoint ) ;
@@ -85,10 +82,20 @@ public void OnRequest(object sender, SessionEventArgs e)
8582
8683 //To cancel a request with a custom HTML content
8784 //Filter URL
88-
8985 if ( e . ProxySession . Request . RequestUri . AbsoluteUri . Contains ( "google.com" ) )
9086 {
91- e . Ok ( "<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>" ) ;
87+ e . Ok ( "<!DOCTYPE html>" +
88+ "<html><body><h1>" +
89+ "Website Blocked" +
90+ "</h1>" +
91+ "<p>Blocked by titanium web proxy.</p>" +
92+ "</body>" +
93+ "</html>" ) ;
94+ }
95+ //Redirect example
96+ if ( e . ProxySession . Request . RequestUri . AbsoluteUri . Contains ( "wikipedia.org" ) )
97+ {
98+ e . Redirect ( "https://www.paypal.com" ) ;
9299 }
93100 }
94101
@@ -107,7 +114,11 @@ public void OnResponse(object sender, SessionEventArgs e)
107114 {
108115 if ( e . ProxySession . Response . ContentType . Trim ( ) . ToLower ( ) . Contains ( "text/html" ) )
109116 {
117+ byte [ ] bodyBytes = e . GetResponseBody ( ) ;
118+ e . SetResponseBody ( bodyBytes ) ;
119+
110120 string body = e . GetResponseBodyAsString ( ) ;
121+ e . SetResponseBodyString ( body ) ;
111122 }
112123 }
113124 }
0 commit comments