Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 363f120

Browse files
committed
Simplify test project
1 parent ebe15c5 commit 363f120

File tree

3 files changed

+22
-138
lines changed

3 files changed

+22
-138
lines changed

Titanium.Web.Proxy.Test/Program.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ public static void Main(string[] args)
3636
controller.SetAsSystemProxy = true;
3737

3838
}
39-
40-
controller.Visited += PageVisited;
41-
39+
4240
//Start proxy controller
4341
controller.StartProxy();
4442

@@ -49,10 +47,7 @@ public static void Main(string[] args)
4947
controller.Stop();
5048
}
5149

52-
private static void PageVisited(VisitedEventArgs e)
53-
{
54-
Console.WriteLine(string.Concat("Visited: ", e.URL));
55-
}
50+
5651
static bool ConsoleEventCallback(int eventType)
5752
{
5853
if (eventType == 2)

Titanium.Web.Proxy.Test/ProxyTestController.cs

+13-123
Original file line numberDiff line numberDiff line change
@@ -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
}

Titanium.Web.Proxy/RequestHandler.cs

+7-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ partial class ProxyServer
2525
private static void HandleClient(TcpClient Client)
2626
{
2727

28-
string connectionGroup = null;
28+
2929
Stream clientStream = null;
3030
CustomBinaryReader clientStreamReader = null;
3131
StreamWriter connectStreamWriter = null;
@@ -34,8 +34,7 @@ private static void HandleClient(TcpClient Client)
3434

3535
try
3636
{
37-
//Separate HttpWebRequest connection group for each client
38-
connectionGroup = ((IPEndPoint)Client.Client.RemoteEndPoint).Address.ToString();
37+
3938

4039
clientStream = Client.GetStream();
4140

@@ -181,7 +180,7 @@ private static void HandleClient(TcpClient Client)
181180
}
182181

183182
//Now create the request
184-
HandleHttpSessionRequest(Client, httpCmd, connectionGroup, clientStream, tunnelHostName, requestLines, clientStreamReader, securehost);
183+
HandleHttpSessionRequest(Client, httpCmd, clientStream, tunnelHostName, requestLines, clientStreamReader, securehost);
185184

186185

187186

@@ -212,7 +211,7 @@ private static void HandleClient(TcpClient Client)
212211
}
213212

214213
}
215-
private static void HandleHttpSessionRequest(TcpClient Client, string httpCmd, string connectionGroup, Stream clientStream, string tunnelHostName, List<string> requestLines, CustomBinaryReader clientStreamReader, string securehost)
214+
private static void HandleHttpSessionRequest(TcpClient Client, string httpCmd, Stream clientStream, string tunnelHostName, List<string> requestLines, CustomBinaryReader clientStreamReader, string securehost)
216215
{
217216

218217

@@ -324,7 +323,7 @@ private static void HandleHttpSessionRequest(TcpClient Client, string httpCmd, s
324323
return;
325324
}
326325

327-
args.ProxyRequest.ConnectionGroupName = connectionGroup;
326+
args.ProxyRequest.ConnectionGroupName = args.RequestHostname;
328327
args.ProxyRequest.AllowWriteStreamBuffering = true;
329328

330329
//If request was modified by user
@@ -361,7 +360,7 @@ private static void HandleHttpSessionRequest(TcpClient Client, string httpCmd, s
361360
requestLines.Add(tmpLine);
362361
}
363362
httpCmd = requestLines.Count() > 0 ? requestLines[0] : null;
364-
HandleHttpSessionRequest(Client, httpCmd, args.ProxyRequest.ConnectionGroupName, args.ClientStream, args.tunnelHostName, requestLines, args.ClientStreamReader, args.securehost);
363+
HandleHttpSessionRequest(Client, httpCmd, args.ClientStream, args.tunnelHostName, requestLines, args.ClientStreamReader, args.securehost);
365364
}
366365
}
367366

@@ -634,7 +633,7 @@ private static void GetRequestStreamCallback(IAsyncResult AsynchronousResult)
634633
TcpClient Client = args.Client;
635634

636635
//Http request body sent, now wait for next request
637-
HandleHttpSessionRequest(Client, httpCmd, args.ProxyRequest.ConnectionGroupName, args.ClientStream, args.tunnelHostName, requestLines, args.ClientStreamReader, args.securehost);
636+
HandleHttpSessionRequest(Client, httpCmd, args.ClientStream, args.tunnelHostName, requestLines, args.ClientStreamReader, args.securehost);
638637
}
639638
}
640639

0 commit comments

Comments
 (0)