@@ -16,8 +16,6 @@ namespace Titanium.Web.Proxy.Test
16
16
{
17
17
public partial class ProxyTestController
18
18
{
19
- private List < string > _URLList = new List < string > ( ) ;
20
- private string _lastURL = string . Empty ;
21
19
22
20
public int ListeningPort { get ; set ; }
23
21
public bool EnableSSL { get ; set ; }
@@ -26,160 +24,52 @@ public partial class ProxyTestController
26
24
public void StartProxy ( )
27
25
{
28
26
29
- if ( Visited != null )
30
- {
31
- ProxyServer . BeforeRequest += OnRequest ;
32
- ProxyServer . BeforeResponse += OnResponse ;
33
- }
34
27
35
- ProxyServer . EnableSSL = EnableSSL ;
28
+ ProxyServer . BeforeRequest += OnRequest ;
29
+ ProxyServer . BeforeResponse += OnResponse ;
36
30
37
- ProxyServer . SetAsSystemProxy = SetAsSystemProxy ;
31
+
32
+ ProxyServer . EnableSSL = EnableSSL ;
33
+
34
+ ProxyServer . SetAsSystemProxy = SetAsSystemProxy ;
38
35
39
36
40
37
ProxyServer . Start ( ) ;
41
38
42
39
43
40
ListeningPort = ProxyServer . ListeningPort ;
44
-
45
- Console . WriteLine ( String . Format ( "Proxy listening on local machine port: {0} " , ProxyServer . ListeningPort ) ) ;
46
41
42
+ Console . WriteLine ( String . Format ( "Proxy listening on local machine port: {0} " , ProxyServer . ListeningPort ) ) ;
43
+
47
44
}
48
45
public void Stop ( )
49
46
{
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 ;
58
49
50
+ ProxyServer . Stop ( ) ;
51
+ }
59
52
60
- public delegate void SiteVisitedEventHandler ( VisitedEventArgs e ) ;
61
- public event SiteVisitedEventHandler Visited ;
62
53
63
54
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
- }
70
55
//Test On Request, intecept requests
71
56
//Read browser URL send back to proxy by the injection script in OnResponse event
72
57
public void OnRequest ( object sender , SessionEventArgs e )
73
58
{
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 ( ) ;
80
59
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 ) ;
84
61
85
- }
86
-
87
- e . Ok ( null ) ;
88
- _lastURL = URL ;
89
- }
90
62
}
91
63
92
64
//Test script injection
93
65
//Insert script to read the Browser URL and send it back to proxy
94
66
public void OnResponse ( object sender , SessionEventArgs e )
95
67
{
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
-
156
68
157
- }
158
69
159
70
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 ( ) ;
172
71
}
173
72
174
-
175
-
176
73
}
177
- public class VisitedEventArgs : EventArgs
178
- {
179
- public string URL ;
180
- public string hostname ;
181
74
182
- public IPAddress remoteIP { get ; set ; }
183
- public int remotePort { get ; set ; }
184
- }
185
75
}
0 commit comments