@@ -9,8 +9,6 @@ namespace Titanium.Web.Proxy.Test
9
9
{
10
10
public class ProxyTestController
11
11
{
12
-
13
-
14
12
public void StartProxy ( )
15
13
{
16
14
ProxyServer . BeforeRequest += OnRequest ;
@@ -19,36 +17,35 @@ public void StartProxy()
19
17
//Exclude Https addresses you don't want to proxy
20
18
//Usefull for clients that use certificate pinning
21
19
//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" }
24
23
} ;
25
24
26
25
//An explicit endpoint is where the client knows about the existance of a proxy
27
26
//So client sends request in a proxy friendly manner
28
27
ProxyServer . AddEndPoint ( explicitEndPoint ) ;
29
28
ProxyServer . Start ( ) ;
30
29
31
-
30
+
32
31
//Transparent endpoint is usefull for reverse proxying (client is not aware of the existance of proxy)
33
32
//A transparent endpoint usually requires a network router port forwarding HTTP(S) packets to this endpoint
34
33
//Currently do not support Server Name Indication (It is not currently supported by SslStream class)
35
34
//That means that the transparent endpoint will always provide the same Generic Certificate to all HTTPS requests
36
35
//In this example only google.com will work for HTTPS requests
37
36
//Other sites will receive a certificate mismatch warning on browser
38
37
//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
+ {
40
40
GenericCertificateName = "google.com"
41
- } ;
41
+ } ;
42
42
ProxyServer . AddEndPoint ( transparentEndPoint ) ;
43
-
43
+
44
44
45
45
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} " ,
47
47
endPoint . GetType ( ) . Name , endPoint . IpAddress , endPoint . Port ) ;
48
48
49
- //You can also add/remove end points after proxy has been started
50
- ProxyServer . RemoveEndPoint ( transparentEndPoint ) ;
51
-
52
49
//Only explicit proxies can be set as system proxy!
53
50
ProxyServer . SetAsSystemHttpProxy ( explicitEndPoint ) ;
54
51
ProxyServer . SetAsSystemHttpsProxy ( explicitEndPoint ) ;
@@ -85,10 +82,20 @@ public void OnRequest(object sender, SessionEventArgs e)
85
82
86
83
//To cancel a request with a custom HTML content
87
84
//Filter URL
88
-
89
85
if ( e . ProxySession . Request . RequestUri . AbsoluteUri . Contains ( "google.com" ) )
90
86
{
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" ) ;
92
99
}
93
100
}
94
101
@@ -107,7 +114,11 @@ public void OnResponse(object sender, SessionEventArgs e)
107
114
{
108
115
if ( e . ProxySession . Response . ContentType . Trim ( ) . ToLower ( ) . Contains ( "text/html" ) )
109
116
{
117
+ byte [ ] bodyBytes = e . GetResponseBody ( ) ;
118
+ e . SetResponseBody ( bodyBytes ) ;
119
+
110
120
string body = e . GetResponseBodyAsString ( ) ;
121
+ e . SetResponseBodyString ( body ) ;
111
122
}
112
123
}
113
124
}
0 commit comments