Commit 363f120e authored by titanium007's avatar titanium007

Simplify test project

parent ebe15c59
...@@ -37,8 +37,6 @@ namespace Titanium.Web.Proxy.Test ...@@ -37,8 +37,6 @@ namespace Titanium.Web.Proxy.Test
} }
controller.Visited += PageVisited;
//Start proxy controller //Start proxy controller
controller.StartProxy(); controller.StartProxy();
...@@ -49,10 +47,7 @@ namespace Titanium.Web.Proxy.Test ...@@ -49,10 +47,7 @@ namespace Titanium.Web.Proxy.Test
controller.Stop(); controller.Stop();
} }
private static void PageVisited(VisitedEventArgs e)
{
Console.WriteLine(string.Concat("Visited: ", e.URL));
}
static bool ConsoleEventCallback(int eventType) static bool ConsoleEventCallback(int eventType)
{ {
if (eventType == 2) if (eventType == 2)
......
...@@ -16,8 +16,6 @@ namespace Titanium.Web.Proxy.Test ...@@ -16,8 +16,6 @@ namespace Titanium.Web.Proxy.Test
{ {
public partial class ProxyTestController public partial class ProxyTestController
{ {
private List<string> _URLList = new List<string>();
private string _lastURL = string.Empty;
public int ListeningPort { get; set; } public int ListeningPort { get; set; }
public bool EnableSSL { get; set; } public bool EnableSSL { get; set; }
...@@ -26,11 +24,10 @@ namespace Titanium.Web.Proxy.Test ...@@ -26,11 +24,10 @@ namespace Titanium.Web.Proxy.Test
public void StartProxy() public void StartProxy()
{ {
if(Visited!=null)
{
ProxyServer.BeforeRequest += OnRequest; ProxyServer.BeforeRequest += OnRequest;
ProxyServer.BeforeResponse += OnResponse; ProxyServer.BeforeResponse += OnResponse;
}
ProxyServer.EnableSSL = EnableSSL; ProxyServer.EnableSSL = EnableSSL;
...@@ -46,140 +43,33 @@ namespace Titanium.Web.Proxy.Test ...@@ -46,140 +43,33 @@ namespace Titanium.Web.Proxy.Test
} }
public void Stop() public void Stop()
{
if (Visited!=null)
{ {
ProxyServer.BeforeRequest -= OnRequest; ProxyServer.BeforeRequest -= OnRequest;
ProxyServer.BeforeResponse -= OnResponse; ProxyServer.BeforeResponse -= OnResponse;
}
ProxyServer.Stop(); ProxyServer.Stop();
} }
public delegate void SiteVisitedEventHandler(VisitedEventArgs e);
public event SiteVisitedEventHandler Visited;
// Invoke the Changed event; called whenever list changes
protected virtual void OnChanged(VisitedEventArgs e)
{
if (Visited != null)
Visited(e);
}
//Test On Request, intecept requests //Test On Request, intecept requests
//Read browser URL send back to proxy by the injection script in OnResponse event //Read browser URL send back to proxy by the injection script in OnResponse event
public void OnRequest(object sender, SessionEventArgs e) public void OnRequest(object sender, SessionEventArgs e)
{ {
string Random = e.RequestURL.Substring(e.RequestURL.LastIndexOf(@"/") + 1);
int index = _URLList.IndexOf(Random);
if (index >= 0)
{
string URL = e.GetRequestHtmlBody();
if (_lastURL != URL)
{
OnChanged(new VisitedEventArgs() { hostname = e.RequestHostname, URL = URL, remoteIP = e.ClientIpAddress, remotePort = e.ClientPort });
} Console.WriteLine(e.RequestURL);
e.Ok(null);
_lastURL = URL;
}
} }
//Test script injection //Test script injection
//Insert script to read the Browser URL and send it back to proxy //Insert script to read the Browser URL and send it back to proxy
public void OnResponse(object sender, SessionEventArgs e) public void OnResponse(object sender, SessionEventArgs e)
{
try
{ {
if (e.ProxyRequest.Method == "GET" || e.ProxyRequest.Method == "POST")
{
if (e.ServerResponse.StatusCode == HttpStatusCode.OK)
{
if (e.ServerResponse.ContentType.Trim().ToLower().Contains("text/html"))
{
string c = e.ServerResponse.GetResponseHeader("X-Requested-With");
if (e.ServerResponse.GetResponseHeader("X-Requested-With") == "")
{
string responseHtmlBody = e.GetResponseHtmlBody();
string functioname = "fr" + RandomString(10);
string VisitedURL = RandomString(5);
string RequestVariable = "c" + RandomString(5);
string RandomURLEnding = RandomString(25);
string RandomLastRequest = RandomString(10);
string LocalRequest;
if (e.IsSSLRequest)
LocalRequest = "https://" + e.RequestHostname + "/" + RandomURLEnding;
else
LocalRequest = "http://" + e.RequestHostname + "/" + RandomURLEnding;
string script = "var " + RandomLastRequest + " = null;" +
"if(window.top==self) { " + "\n" +
" " + functioname + "();" +
"setInterval(" + functioname + ",500); " + "\n" + "}" +
"function " + functioname + "(){ " + "\n" +
"var " + RequestVariable + " = new XMLHttpRequest(); " + "\n" +
"var " + VisitedURL + " = null;" + "\n" +
"if(window.top.location.href!=null) " + "\n" +
"" + VisitedURL + " = window.top.location.href; else " + "\n" +
"" + VisitedURL + " = document.referrer; " +
"if(" + RandomLastRequest + "!= " + VisitedURL + ") {" +
RequestVariable + ".open(\"POST\",\"" + LocalRequest + "\", true); " + "\n" +
RequestVariable + ".send(" + VisitedURL + ");} " + RandomLastRequest + " = " + VisitedURL + "}";
Regex RE = new Regex("</body>", RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline);
string modifiedResponseHtmlBody = RE.Replace(responseHtmlBody, "<script type =\"text/javascript\">" + script + "</script></body>", 1);
if (modifiedResponseHtmlBody.Length != responseHtmlBody.Length)
{
e.SetResponseHtmlBody(modifiedResponseHtmlBody);
_URLList.Add(RandomURLEnding);
} }
} }
}
}
}
}
catch { }
}
private Random random = new Random((int)DateTime.Now.Ticks);
private string RandomString(int size)
{
StringBuilder builder = new StringBuilder();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return builder.ToString();
}
}
public class VisitedEventArgs : EventArgs
{
public string URL;
public string hostname;
public IPAddress remoteIP { get; set; }
public int remotePort { get; set; }
}
} }
...@@ -25,7 +25,7 @@ namespace Titanium.Web.Proxy ...@@ -25,7 +25,7 @@ namespace Titanium.Web.Proxy
private static void HandleClient(TcpClient Client) private static void HandleClient(TcpClient Client)
{ {
string connectionGroup = null;
Stream clientStream = null; Stream clientStream = null;
CustomBinaryReader clientStreamReader = null; CustomBinaryReader clientStreamReader = null;
StreamWriter connectStreamWriter = null; StreamWriter connectStreamWriter = null;
...@@ -34,8 +34,7 @@ namespace Titanium.Web.Proxy ...@@ -34,8 +34,7 @@ namespace Titanium.Web.Proxy
try try
{ {
//Separate HttpWebRequest connection group for each client
connectionGroup = ((IPEndPoint)Client.Client.RemoteEndPoint).Address.ToString();
clientStream = Client.GetStream(); clientStream = Client.GetStream();
...@@ -181,7 +180,7 @@ namespace Titanium.Web.Proxy ...@@ -181,7 +180,7 @@ namespace Titanium.Web.Proxy
} }
//Now create the request //Now create the request
HandleHttpSessionRequest(Client, httpCmd, connectionGroup, clientStream, tunnelHostName, requestLines, clientStreamReader, securehost); HandleHttpSessionRequest(Client, httpCmd, clientStream, tunnelHostName, requestLines, clientStreamReader, securehost);
...@@ -212,7 +211,7 @@ namespace Titanium.Web.Proxy ...@@ -212,7 +211,7 @@ namespace Titanium.Web.Proxy
} }
} }
private static void HandleHttpSessionRequest(TcpClient Client, string httpCmd, string connectionGroup, Stream clientStream, string tunnelHostName, List<string> requestLines, CustomBinaryReader clientStreamReader, string securehost) private static void HandleHttpSessionRequest(TcpClient Client, string httpCmd, Stream clientStream, string tunnelHostName, List<string> requestLines, CustomBinaryReader clientStreamReader, string securehost)
{ {
...@@ -324,7 +323,7 @@ namespace Titanium.Web.Proxy ...@@ -324,7 +323,7 @@ namespace Titanium.Web.Proxy
return; return;
} }
args.ProxyRequest.ConnectionGroupName = connectionGroup; args.ProxyRequest.ConnectionGroupName = args.RequestHostname;
args.ProxyRequest.AllowWriteStreamBuffering = true; args.ProxyRequest.AllowWriteStreamBuffering = true;
//If request was modified by user //If request was modified by user
...@@ -361,7 +360,7 @@ namespace Titanium.Web.Proxy ...@@ -361,7 +360,7 @@ namespace Titanium.Web.Proxy
requestLines.Add(tmpLine); requestLines.Add(tmpLine);
} }
httpCmd = requestLines.Count() > 0 ? requestLines[0] : null; httpCmd = requestLines.Count() > 0 ? requestLines[0] : null;
HandleHttpSessionRequest(Client, httpCmd, args.ProxyRequest.ConnectionGroupName, args.ClientStream, args.tunnelHostName, requestLines, args.ClientStreamReader, args.securehost); HandleHttpSessionRequest(Client, httpCmd, args.ClientStream, args.tunnelHostName, requestLines, args.ClientStreamReader, args.securehost);
} }
} }
...@@ -634,7 +633,7 @@ namespace Titanium.Web.Proxy ...@@ -634,7 +633,7 @@ namespace Titanium.Web.Proxy
TcpClient Client = args.Client; TcpClient Client = args.Client;
//Http request body sent, now wait for next request //Http request body sent, now wait for next request
HandleHttpSessionRequest(Client, httpCmd, args.ProxyRequest.ConnectionGroupName, args.ClientStream, args.tunnelHostName, requestLines, args.ClientStreamReader, args.securehost); HandleHttpSessionRequest(Client, httpCmd, args.ClientStream, args.tunnelHostName, requestLines, args.ClientStreamReader, args.securehost);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment