@@ -16,8 +16,6 @@ namespace Titanium.Web.Proxy.Test
1616{
1717 public partial class ProxyTestController
1818 {
19- private List < string > _URLList = new List < string > ( ) ;
20- private string _lastURL = string . Empty ;
2119
2220 public int ListeningPort { get ; set ; }
2321 public bool EnableSSL { get ; set ; }
@@ -26,160 +24,52 @@ public partial class ProxyTestController
2624 public void StartProxy ( )
2725 {
2826
29- if ( Visited != null )
30- {
31- ProxyServer . BeforeRequest += OnRequest ;
32- ProxyServer . BeforeResponse += OnResponse ;
33- }
3427
35- ProxyServer . EnableSSL = EnableSSL ;
28+ ProxyServer . BeforeRequest += OnRequest ;
29+ ProxyServer . BeforeResponse += OnResponse ;
3630
37- ProxyServer . SetAsSystemProxy = SetAsSystemProxy ;
31+
32+ ProxyServer . EnableSSL = EnableSSL ;
33+
34+ ProxyServer . SetAsSystemProxy = SetAsSystemProxy ;
3835
3936
4037 ProxyServer . Start ( ) ;
4138
4239
4340 ListeningPort = ProxyServer . ListeningPort ;
44-
45- Console . WriteLine ( String . Format ( "Proxy listening on local machine port: {0} " , ProxyServer . ListeningPort ) ) ;
4641
42+ Console . WriteLine ( String . Format ( "Proxy listening on local machine port: {0} " , ProxyServer . ListeningPort ) ) ;
43+
4744 }
4845 public void Stop ( )
4946 {
50- if ( Visited != null )
51- {
52- ProxyServer . BeforeRequest -= OnRequest ;
53- ProxyServer . BeforeResponse -= OnResponse ;
54- }
55- ProxyServer . Stop ( ) ;
56- }
57-
47+ ProxyServer . BeforeRequest -= OnRequest ;
48+ ProxyServer . BeforeResponse -= OnResponse ;
5849
50+ ProxyServer . Stop ( ) ;
51+ }
5952
60- public delegate void SiteVisitedEventHandler ( VisitedEventArgs e ) ;
61- public event SiteVisitedEventHandler Visited ;
6253
6354
64- // Invoke the Changed event; called whenever list changes
65- protected virtual void OnChanged ( VisitedEventArgs e )
66- {
67- if ( Visited != null )
68- Visited ( e ) ;
69- }
7055 //Test On Request, intecept requests
7156 //Read browser URL send back to proxy by the injection script in OnResponse event
7257 public void OnRequest ( object sender , SessionEventArgs e )
7358 {
74- string Random = e . RequestURL . Substring ( e . RequestURL . LastIndexOf ( @"/" ) + 1 ) ;
75- int index = _URLList . IndexOf ( Random ) ;
76- if ( index >= 0 )
77- {
78-
79- string URL = e . GetRequestHtmlBody ( ) ;
8059
81- if ( _lastURL != URL )
82- {
83- OnChanged ( new VisitedEventArgs ( ) { hostname = e . RequestHostname , URL = URL , remoteIP = e . ClientIpAddress , remotePort = e . ClientPort } ) ;
60+ Console . WriteLine ( e . RequestURL ) ;
8461
85- }
86-
87- e . Ok ( null ) ;
88- _lastURL = URL ;
89- }
9062 }
9163
9264 //Test script injection
9365 //Insert script to read the Browser URL and send it back to proxy
9466 public void OnResponse ( object sender , SessionEventArgs e )
9567 {
96- try
97- {
98-
99-
100- if ( e . ProxyRequest . Method == "GET" || e . ProxyRequest . Method == "POST" )
101- {
102- if ( e . ServerResponse . StatusCode == HttpStatusCode . OK )
103- {
104- if ( e . ServerResponse . ContentType . Trim ( ) . ToLower ( ) . Contains ( "text/html" ) )
105- {
106- string c = e . ServerResponse . GetResponseHeader ( "X-Requested-With" ) ;
107- if ( e . ServerResponse . GetResponseHeader ( "X-Requested-With" ) == "" )
108- {
109- string responseHtmlBody = e . GetResponseHtmlBody ( ) ;
110-
111- string functioname = "fr" + RandomString ( 10 ) ;
112- string VisitedURL = RandomString ( 5 ) ;
113-
114- string RequestVariable = "c" + RandomString ( 5 ) ;
115- string RandomURLEnding = RandomString ( 25 ) ;
116- string RandomLastRequest = RandomString ( 10 ) ;
117- string LocalRequest ;
118-
119- if ( e . IsSSLRequest )
120- LocalRequest = "https://" + e . RequestHostname + "/" + RandomURLEnding ;
121- else
122- LocalRequest = "http://" + e . RequestHostname + "/" + RandomURLEnding ;
123-
124- string script = "var " + RandomLastRequest + " = null;" +
125- "if(window.top==self) { " + "\n " +
126- " " + functioname + "();" +
127- "setInterval(" + functioname + ",500); " + "\n " + "}" +
128- "function " + functioname + "(){ " + "\n " +
129- "var " + RequestVariable + " = new XMLHttpRequest(); " + "\n " +
130- "var " + VisitedURL + " = null;" + "\n " +
131- "if(window.top.location.href!=null) " + "\n " +
132- "" + VisitedURL + " = window.top.location.href; else " + "\n " +
133- "" + VisitedURL + " = document.referrer; " +
134- "if(" + RandomLastRequest + "!= " + VisitedURL + ") {" +
135- RequestVariable + ".open(\" POST\" ,\" " + LocalRequest + "\" , true); " + "\n " +
136- RequestVariable + ".send(" + VisitedURL + ");} " + RandomLastRequest + " = " + VisitedURL + "}" ;
137-
138-
139- Regex RE = new Regex ( "</body>" , RegexOptions . RightToLeft | RegexOptions . IgnoreCase | RegexOptions . Multiline ) ;
140-
141- string modifiedResponseHtmlBody = RE . Replace ( responseHtmlBody , "<script type =\" text/javascript\" >" + script + "</script></body>" , 1 ) ;
142- if ( modifiedResponseHtmlBody . Length != responseHtmlBody . Length )
143- {
144- e . SetResponseHtmlBody ( modifiedResponseHtmlBody ) ;
145- _URLList . Add ( RandomURLEnding ) ;
146-
147- }
148-
149- }
150- }
151- }
152- }
153- }
154- catch { }
155-
15668
157- }
15869
15970
160- private Random random = new Random ( ( int ) DateTime . Now . Ticks ) ;
161- private string RandomString ( int size )
162- {
163- StringBuilder builder = new StringBuilder ( ) ;
164- char ch ;
165- for ( int i = 0 ; i < size ; i ++ )
166- {
167- ch = Convert . ToChar ( Convert . ToInt32 ( Math . Floor ( 26 * random . NextDouble ( ) + 65 ) ) ) ;
168- builder . Append ( ch ) ;
169- }
170-
171- return builder . ToString ( ) ;
17271 }
17372
174-
175-
17673 }
177- public class VisitedEventArgs : EventArgs
178- {
179- public string URL ;
180- public string hostname ;
18174
182- public IPAddress remoteIP { get ; set ; }
183- public int remotePort { get ; set ; }
184- }
18575}
0 commit comments