Commit 929f8627 authored by titanium007's avatar titanium007

cleanup

parent 44b4d4b7
using System; using System;
using System.Text.RegularExpressions;
using Titanium.Web.Proxy.EventArguments; using Titanium.Web.Proxy.EventArguments;
namespace Titanium.Web.Proxy.Test namespace Titanium.Web.Proxy.Test
...@@ -46,27 +47,27 @@ namespace Titanium.Web.Proxy.Test ...@@ -46,27 +47,27 @@ namespace Titanium.Web.Proxy.Test
Console.WriteLine(e.ProxySession.Request.RequestUrl); Console.WriteLine(e.ProxySession.Request.RequestUrl);
////read request headers ////read request headers
//var requestHeaders = e.RequestHeaders; var requestHeaders = e.ProxySession.Request.RequestHeaders;
//if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT")) if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
//{ {
// //Get/Set request body bytes //Get/Set request body bytes
// byte[] bodyBytes = e.GetRequestBody(); byte[] bodyBytes = e.GetRequestBody();
// e.SetRequestBody(bodyBytes); e.SetRequestBody(bodyBytes);
// //Get/Set request body as string //Get/Set request body as string
// string bodyString = e.GetRequestBodyAsString(); string bodyString = e.GetRequestBodyAsString();
// e.SetRequestBodyString(bodyString); e.SetRequestBodyString(bodyString);
//} }
////To cancel a request with a custom HTML content //To cancel a request with a custom HTML content
////Filter URL //Filter URL
//if (e.RequestURL.Contains("google.com")) if (e.ProxySession.Request.RequestUrl.Contains("google.com"))
//{ {
// e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>"); e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
//} }
} }
//Test script injection //Test script injection
...@@ -74,10 +75,10 @@ namespace Titanium.Web.Proxy.Test ...@@ -74,10 +75,10 @@ namespace Titanium.Web.Proxy.Test
public void OnResponse(object sender, SessionEventArgs e) public void OnResponse(object sender, SessionEventArgs e)
{ {
////read response headers ////read response headers
//var responseHeaders = e.ResponseHeaders; var responseHeaders = e.ProxySession.Response.ResponseHeaders;
//if (e.ResponseStatusCode == HttpStatusCode.OK) //if (e.ResponseStatusCode == "200")
//{ //{
// if (e.ResponseContentType.Trim().ToLower().Contains("text/html")) // if (e.ResponseContentType.Trim().ToLower().Contains("text/html"))
// { // {
......
...@@ -79,14 +79,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -79,14 +79,7 @@ namespace Titanium.Web.Proxy.EventArguments
public void Dispose() public void Dispose()
{ {
//if (ProxyRequest != null)
// ProxyRequest.Abort();
//if (ResponseStream != null)
// ResponseStream.Dispose();
//if (ServerResponse != null)
// ServerResponse.Close();
} }
private void ReadRequestBody() private void ReadRequestBody()
......
...@@ -35,6 +35,7 @@ namespace Titanium.Web.Proxy.Http ...@@ -35,6 +35,7 @@ namespace Titanium.Web.Proxy.Http
internal byte[] RequestBody { get; set; } internal byte[] RequestBody { get; set; }
internal string RequestBodyString { get; set; } internal string RequestBodyString { get; set; }
internal bool RequestBodyRead { get; set; } internal bool RequestBodyRead { get; set; }
public bool UpgradeToWebSocket { get; set; }
public List<HttpHeader> RequestHeaders { get; internal set; } public List<HttpHeader> RequestHeaders { get; internal set; }
internal bool RequestLocked { get; set; } internal bool RequestLocked { get; set; }
...@@ -42,6 +43,7 @@ namespace Titanium.Web.Proxy.Http ...@@ -42,6 +43,7 @@ namespace Titanium.Web.Proxy.Http
{ {
this.RequestHeaders = new List<HttpHeader>(); this.RequestHeaders = new List<HttpHeader>();
} }
} }
public class Response public class Response
...@@ -62,15 +64,17 @@ namespace Titanium.Web.Proxy.Http ...@@ -62,15 +64,17 @@ namespace Titanium.Web.Proxy.Http
public bool ResponseKeepAlive { get; set; } public bool ResponseKeepAlive { get; set; }
public string ResponseContentType { get; set; } public string ResponseContentType { get; set; }
public int ContentLength { get; set; } public int ContentLength { get; set; }
public bool IsChunked { get; set; }
public Response() public Response()
{ {
this.ResponseHeaders = new List<HttpHeader>(); this.ResponseHeaders = new List<HttpHeader>();
this.ResponseKeepAlive = true;
} }
public bool IsChunked { get; set; }
} }
public class HttpWebSession public class HttpWebSession
......
...@@ -20,28 +20,10 @@ namespace Titanium.Web.Proxy.Http ...@@ -20,28 +20,10 @@ namespace Titanium.Web.Proxy.Http
internal class TcpConnectionManager internal class TcpConnectionManager
{ {
static Dictionary<string, Stack<TcpConnection>> ConnectionCache = new Dictionary<string, Stack<TcpConnection>>();
public static TcpConnection GetClient(string Hostname, int port, bool IsSecure) public static TcpConnection GetClient(string Hostname, int port, bool IsSecure)
{ {
//var key = string.Concat(Hostname, ":", port, ":", IsSecure);
//TcpConnection client;
//lock (ConnectionCache)
//{
// Stack<TcpConnection> connections;
// if (!ConnectionCache.TryGetValue(key, out connections))
// {
// return CreateClient(Hostname, port, IsSecure);
// }
// if (connections.Count > 0)
// {
// client = connections.Pop();
// }
// else
return CreateClient(Hostname, port, IsSecure); return CreateClient(Hostname, port, IsSecure);
//}
//return client;
} }
private static TcpConnection CreateClient(string Hostname, int port, bool IsSecure) private static TcpConnection CreateClient(string Hostname, int port, bool IsSecure)
...@@ -69,25 +51,5 @@ namespace Titanium.Web.Proxy.Http ...@@ -69,25 +51,5 @@ namespace Titanium.Web.Proxy.Http
return new TcpConnection() { Client = client, ServerStreamReader = new CustomBinaryReader(stream, Encoding.ASCII), Stream = stream }; return new TcpConnection() { Client = client, ServerStreamReader = new CustomBinaryReader(stream, Encoding.ASCII), Stream = stream };
} }
public static void AddClient(string Hostname, int port, bool IsSecure, TcpConnection Client)
{
var key = string.Concat(Hostname, ":", port, ":", IsSecure);
lock (ConnectionCache)
{
Stack<TcpConnection> connections;
if (!ConnectionCache.TryGetValue(key, out connections))
{
connections = new Stack<TcpConnection>();
connections.Push(Client);
ConnectionCache.Add(key, connections);
}
connections.Push(Client);
}
}
} }
} }
...@@ -163,44 +163,27 @@ namespace Titanium.Web.Proxy ...@@ -163,44 +163,27 @@ namespace Titanium.Web.Proxy
args.ProxySession.Request.RequestHeaders.Add(new HttpHeader(header[0], header[1])); args.ProxySession.Request.RequestHeaders.Add(new HttpHeader(header[0], header[1]));
} }
for (var i = 0; i < args.ProxySession.Request.RequestHeaders.Count; i++) SetRequestHeaders(args.ProxySession.Request.RequestHeaders, args.ProxySession);
{ if (args.ProxySession.Request.UpgradeToWebSocket)
var rawHeader = args.ProxySession.Request.RequestHeaders[i];
//if request was upgrade to web-socket protocol then relay the request without proxying
if ((rawHeader.Name.ToLower() == "upgrade") && (rawHeader.Value.ToLower() == "websocket"))
{ {
TcpHelper.SendRaw(clientStreamReader.BaseStream, httpCmd, args.ProxySession.Request.RequestHeaders, TcpHelper.SendRaw(clientStreamReader.BaseStream, httpCmd, args.ProxySession.Request.RequestHeaders,
httpRemoteUri.Host, httpRemoteUri.Port, httpRemoteUri.Scheme == Uri.UriSchemeHttps); httpRemoteUri.Host, httpRemoteUri.Port, httpRemoteUri.Scheme == Uri.UriSchemeHttps);
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args); Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
return; return;
} }
}
args.ProxySession.Request.RequestUri = httpRemoteUri; args.ProxySession.Request.RequestUri = httpRemoteUri;
//args.ProxyRequest.Proxy = null;
//args.ProxyRequest.UseDefaultCredentials = true;
args.ProxySession.Request.Method = httpMethod; args.ProxySession.Request.Method = httpMethod;
args.ProxySession.Request.Version = httpVersion; args.ProxySession.Request.Version = httpVersion;
// args.ProxyRequest.ProtocolVersion = version;
args.Client.ClientStream = clientStream; args.Client.ClientStream = clientStream;
args.Client.ClientStreamReader = clientStreamReader; args.Client.ClientStreamReader = clientStreamReader;
args.Client.ClientStreamWriter = clientStreamWriter; args.Client.ClientStreamWriter = clientStreamWriter;
// args.ProxyRequest.AllowAutoRedirect = false;
// args.ProxyRequest.AutomaticDecompression = DecompressionMethods.None;
args.ProxySession.Request.RequestHostname = args.ProxySession.Request.RequestUri.Host; args.ProxySession.Request.RequestHostname = args.ProxySession.Request.RequestUri.Host;
args.ProxySession.Request.RequestUrl = args.ProxySession.Request.RequestUri.OriginalString; args.ProxySession.Request.RequestUrl = args.ProxySession.Request.RequestUri.OriginalString;
args.Client.ClientPort = ((IPEndPoint)client.Client.RemoteEndPoint).Port; args.Client.ClientPort = ((IPEndPoint)client.Client.RemoteEndPoint).Port;
args.Client.ClientIpAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address; args.Client.ClientIpAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
args.ProxySession.Request.RequestHttpVersion = version; args.ProxySession.Request.RequestHttpVersion = version;
//args.RequestIsAlive = args.ProxyRequest.KeepAlive;
//args.ProxyRequest.AllowWriteStreamBuffering = true;
//If requested interception //If requested interception
...@@ -218,9 +201,9 @@ namespace Titanium.Web.Proxy ...@@ -218,9 +201,9 @@ namespace Titanium.Web.Proxy
return; return;
} }
SetRequestHeaders(args.ProxySession.Request.RequestHeaders, args.ProxySession);
//construct the web request that we are going to issue on behalf of the client. //construct the web request that we are going to issue on behalf of the client.
connection = connection == null ? TcpConnectionManager.GetClient(args.ProxySession.Request.RequestUri.Host, args.ProxySession.Request.RequestUri.Port, args.IsHttps): connection; connection = connection == null ? TcpConnectionManager.GetClient(args.ProxySession.Request.RequestUri.Host, args.ProxySession.Request.RequestUri.Port, args.IsHttps) : connection;
args.ProxySession.SetConnection(connection); args.ProxySession.SetConnection(connection);
args.ProxySession.SendRequest(); args.ProxySession.SendRequest();
...@@ -243,16 +226,13 @@ namespace Titanium.Web.Proxy ...@@ -243,16 +226,13 @@ namespace Titanium.Web.Proxy
HandleHttpSessionResponse(args); HandleHttpSessionResponse(args);
//if connection is closing exit //if connection is closing exit
if (args.ProxySession.Response.ResponseHeaders.Any(x => x.Name.ToLower() == "connection" && x.Value.ToLower() == "close")) if (args.ProxySession.Response.ResponseKeepAlive == false)
{ {
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args); Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
return; return;
} }
// args.ProxySession.ProxyClient.Client.Close();
// if (args.ProxySession.ProxyClient.Client.Connected)
// TcpConnectionManager.AddClient(args.ProxySession.Request.RequestUri.Host, args.ProxySession.Request.RequestUri.Port, args.IsHttps, args.ProxySession.ProxyClient);
// // read the next request // read the next request
httpCmd = clientStreamReader.ReadLine(); httpCmd = clientStreamReader.ReadLine();
} }
...@@ -263,7 +243,7 @@ namespace Titanium.Web.Proxy ...@@ -263,7 +243,7 @@ namespace Titanium.Web.Proxy
} }
} }
if(connection != null) if (connection != null)
connection.Client.Close(); connection.Client.Close();
} }
...@@ -271,7 +251,6 @@ namespace Titanium.Web.Proxy ...@@ -271,7 +251,6 @@ namespace Titanium.Web.Proxy
{ {
clientStreamWriter.WriteLine(httpVersion + " 200 Connection established"); clientStreamWriter.WriteLine(httpVersion + " 200 Connection established");
clientStreamWriter.WriteLine("Timestamp: {0}", DateTime.Now); clientStreamWriter.WriteLine("Timestamp: {0}", DateTime.Now);
//clientStreamWriter.WriteLine("connection:close");
clientStreamWriter.WriteLine(); clientStreamWriter.WriteLine();
clientStreamWriter.Flush(); clientStreamWriter.Flush();
} }
...@@ -282,15 +261,9 @@ namespace Titanium.Web.Proxy ...@@ -282,15 +261,9 @@ namespace Titanium.Web.Proxy
{ {
switch (requestHeaders[i].Name.ToLower()) switch (requestHeaders[i].Name.ToLower())
{ {
case "accept":
// webRequest.Accept = requestHeaders[i].Value;
break;
case "accept-encoding": case "accept-encoding":
requestHeaders[i].Value = "gzip,deflate,zlib"; requestHeaders[i].Value = "gzip,deflate,zlib";
break; break;
case "cookie":
//webRequest.Headers["Cookie"] = requestHeaders[i].Value;
break;
case "connection": case "connection":
if (requestHeaders[i].Value.ToLower() == "keep-alive") if (requestHeaders[i].Value.ToLower() == "keep-alive")
webRequest.Request.RequestKeepAlive = true; webRequest.Request.RequestKeepAlive = true;
...@@ -304,43 +277,19 @@ namespace Titanium.Web.Proxy ...@@ -304,43 +277,19 @@ namespace Titanium.Web.Proxy
case "content-type": case "content-type":
webRequest.Request.RequestContentType = requestHeaders[i].Value; webRequest.Request.RequestContentType = requestHeaders[i].Value;
break; break;
case "expect":
//if (requestHeaders[i].Value.ToLower() == "100-continue")
// webRequest.ServicePoint.Expect100Continue = true;
// else
// webRequest.Expect = requestHeaders[i].Value;
break;
case "host": case "host":
webRequest.Request.RequestHost = requestHeaders[i].Value; webRequest.Request.RequestHost = requestHeaders[i].Value;
break; break;
case "if-modified-since":
// var sb = requestHeaders[i].Value.Trim().Split(SemiSplit);
// DateTime d;
// if (DateTime.TryParse(sb[0], out d))
// webRequest.IfModifiedSince = d;
break;
case "proxy-connection": case "proxy-connection":
if (requestHeaders[i].Value.ToLower() == "keep-alive") if (requestHeaders[i].Value.ToLower() == "keep-alive")
webRequest.Request.RequestKeepAlive = true; webRequest.Request.RequestKeepAlive = true;
else if (requestHeaders[i].Value.ToLower() == "close") else if (requestHeaders[i].Value.ToLower() == "close")
webRequest.Request.RequestKeepAlive = false; webRequest.Request.RequestKeepAlive = false;
break; break;
case "range":
// var startEnd = requestHeaders[i].Value.Replace(Environment.NewLine, "").Remove(0, 6).Split('-'); case "upgrade":
// if (startEnd.Length > 1) if (requestHeaders[i].Value.ToLower() == "websocket")
// { webRequest.Request.UpgradeToWebSocket = true;
// if (!string.IsNullOrEmpty(startEnd[1]))
// webRequest.AddRange(int.Parse(startEnd[0]), int.Parse(startEnd[1]));
// else webRequest.AddRange(int.Parse(startEnd[0]));
// }
// else
// webRequest.AddRange(int.Parse(startEnd[0]));
break;
case "referer":
// webRequest.Referer = requestHeaders[i].Value;
break;
case "user-agent":
// webRequest.UserAgent = requestHeaders[i].Value;
break; break;
//revisit this, transfer-encoding is not a request header according to spec //revisit this, transfer-encoding is not a request header according to spec
...@@ -351,14 +300,8 @@ namespace Titanium.Web.Proxy ...@@ -351,14 +300,8 @@ namespace Titanium.Web.Proxy
else else
webRequest.Request.RequestSendChunked = false; webRequest.Request.RequestSendChunked = false;
break; break;
case "upgrade":
// if (requestHeaders[i].Value.ToLower() == "http/1.1")
// webRequest.Headers.Add("Upgrade", requestHeaders[i].Value);
break;
default: default:
// webRequest.Headers.Add(requestHeaders[i].Name, requestHeaders[i].Value);
break; break;
} }
} }
...@@ -418,21 +361,15 @@ namespace Titanium.Web.Proxy ...@@ -418,21 +361,15 @@ namespace Titanium.Web.Proxy
} }
postStream.Write(buffer, 0, buffer.Length); postStream.Write(buffer, 0, buffer.Length);
} }
//postStream.Close();
} }
catch catch
{ {
// postStream.Close();
// postStream.Dispose();
throw; throw;
} }
} }
//Need to revist, find any potential bugs //Need to revist, find any potential bugs
else if (args.ProxySession.Request.RequestSendChunked) else if (args.ProxySession.Request.RequestSendChunked)
{ {
//args.ProxyRequest.AllowWriteStreamBuffering = true;
try try
{ {
while (true) while (true)
...@@ -454,14 +391,9 @@ namespace Titanium.Web.Proxy ...@@ -454,14 +391,9 @@ namespace Titanium.Web.Proxy
break; break;
} }
} }
//postStream.Close();
} }
catch catch
{ {
// postStream.Close();
// postStream.Dispose();
throw; throw;
} }
} }
......
...@@ -39,7 +39,7 @@ namespace Titanium.Web.Proxy ...@@ -39,7 +39,7 @@ namespace Titanium.Web.Proxy
if (args.ProxySession.Response.ResponseBodyRead) if (args.ProxySession.Response.ResponseBodyRead)
{ {
var isChunked = args.ProxySession.Response.ResponseHeaders.Any(x => x.Name.ToLower() == "transfer-encoding" && x.Value.ToLower().Contains("chunked")); var isChunked =args.ProxySession.Response.IsChunked;
var contentEncoding = args.ProxySession.Response.ResponseContentEncoding; var contentEncoding = args.ProxySession.Response.ResponseContentEncoding;
switch (contentEncoding.ToLower()) switch (contentEncoding.ToLower())
...@@ -105,14 +105,17 @@ namespace Titanium.Web.Proxy ...@@ -105,14 +105,17 @@ namespace Titanium.Web.Proxy
response.Response.ResponseCharacterSet = response.Response.ResponseHeaders[i].Value.Split(';')[1].ToLower().Replace("charset=", string.Empty).Trim(); response.Response.ResponseCharacterSet = response.Response.ResponseHeaders[i].Value.Split(';')[1].ToLower().Replace("charset=", string.Empty).Trim();
} }
else else
response.Response.ResponseContentType = response.Response.ResponseHeaders[i].Value.Trim(); response.Response.ResponseContentType = response.Response.ResponseHeaders[i].Value.ToLower().Trim();
break; break;
case "transfer-encoding": case "transfer-encoding":
if (response.Response.ResponseHeaders[i].Value.Contains("chunked")) if (response.Response.ResponseHeaders[i].Value.ToLower().Contains("chunked"))
response.Response.IsChunked = true; response.Response.IsChunked = true;
break; break;
case "connection":
if (response.Response.ResponseHeaders[i].Value.ToLower().Contains("close"))
response.Response.ResponseKeepAlive = false;
break;
default: default:
break; break;
} }
......
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