Commit 363f120e authored by titanium007's avatar titanium007

Simplify test project

parent ebe15c59
......@@ -36,9 +36,7 @@ namespace Titanium.Web.Proxy.Test
controller.SetAsSystemProxy = true;
}
controller.Visited += PageVisited;
//Start proxy controller
controller.StartProxy();
......@@ -49,10 +47,7 @@ namespace Titanium.Web.Proxy.Test
controller.Stop();
}
private static void PageVisited(VisitedEventArgs e)
{
Console.WriteLine(string.Concat("Visited: ", e.URL));
}
static bool ConsoleEventCallback(int eventType)
{
if (eventType == 2)
......
......@@ -16,8 +16,6 @@ namespace Titanium.Web.Proxy.Test
{
public partial class ProxyTestController
{
private List<string> _URLList = new List<string>();
private string _lastURL = string.Empty;
public int ListeningPort { get; set; }
public bool EnableSSL { get; set; }
......@@ -26,160 +24,52 @@ namespace Titanium.Web.Proxy.Test
public void StartProxy()
{
if(Visited!=null)
{
ProxyServer.BeforeRequest += OnRequest;
ProxyServer.BeforeResponse += OnResponse;
}
ProxyServer.EnableSSL = EnableSSL;
ProxyServer.BeforeRequest += OnRequest;
ProxyServer.BeforeResponse += OnResponse;
ProxyServer.SetAsSystemProxy = SetAsSystemProxy;
ProxyServer.EnableSSL = EnableSSL;
ProxyServer.SetAsSystemProxy = SetAsSystemProxy;
ProxyServer.Start();
ListeningPort = ProxyServer.ListeningPort;
Console.WriteLine(String.Format("Proxy listening on local machine port: {0} ", ProxyServer.ListeningPort));
Console.WriteLine(String.Format("Proxy listening on local machine port: {0} ", ProxyServer.ListeningPort));
}
public void Stop()
{
if (Visited!=null)
{
ProxyServer.BeforeRequest -= OnRequest;
ProxyServer.BeforeResponse -= OnResponse;
}
ProxyServer.Stop();
}
ProxyServer.BeforeRequest -= OnRequest;
ProxyServer.BeforeResponse -= OnResponse;
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
//Read browser URL send back to proxy by the injection script in OnResponse event
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
//Insert script to read the Browser URL and send it back to proxy
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
private static void HandleClient(TcpClient Client)
{
string connectionGroup = null;
Stream clientStream = null;
CustomBinaryReader clientStreamReader = null;
StreamWriter connectStreamWriter = null;
......@@ -34,8 +34,7 @@ namespace Titanium.Web.Proxy
try
{
//Separate HttpWebRequest connection group for each client
connectionGroup = ((IPEndPoint)Client.Client.RemoteEndPoint).Address.ToString();
clientStream = Client.GetStream();
......@@ -181,7 +180,7 @@ namespace Titanium.Web.Proxy
}
//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
}
}
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
return;
}
args.ProxyRequest.ConnectionGroupName = connectionGroup;
args.ProxyRequest.ConnectionGroupName = args.RequestHostname;
args.ProxyRequest.AllowWriteStreamBuffering = true;
//If request was modified by user
......@@ -361,7 +360,7 @@ namespace Titanium.Web.Proxy
requestLines.Add(tmpLine);
}
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
TcpClient Client = args.Client;
//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