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

Release #53

Merged
merged 4 commits into from
Mar 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 33 additions & 33 deletions Titanium.Web.Proxy.Test/ProxyTestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void StartProxy()
//Usefull for clients that use certificate pinning
//for example dropbox.com
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true){
ExcludedHttpsHostNameRegex = new List<string>() { "dropbox.com" }
// ExcludedHttpsHostNameRegex = new List<string>() { "google.com", "dropbox.com" }
};

//An explicit endpoint is where the client knows about the existance of a proxy
Expand Down Expand Up @@ -68,49 +68,49 @@ public void OnRequest(object sender, SessionEventArgs e)
{
Console.WriteLine(e.ProxySession.Request.Url);

////read request headers
//var requestHeaders = e.ProxySession.Request.RequestHeaders;
//read request headers
var requestHeaders = e.ProxySession.Request.RequestHeaders;

//if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
//{
// //Get/Set request body bytes
// byte[] bodyBytes = e.GetRequestBody();
// e.SetRequestBody(bodyBytes);
if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
{
//Get/Set request body bytes
byte[] bodyBytes = e.GetRequestBody();
e.SetRequestBody(bodyBytes);

// //Get/Set request body as string
// string bodyString = e.GetRequestBodyAsString();
// e.SetRequestBodyString(bodyString);
//Get/Set request body as string
string bodyString = e.GetRequestBodyAsString();
e.SetRequestBodyString(bodyString);

//}
}

////To cancel a request with a custom HTML content
////Filter URL
//To cancel a request with a custom HTML content
//Filter URL

//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>");
//}
if (e.ProxySession.Request.RequestUri.AbsoluteUri.Contains("google.com"))
{
e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
}
}

//Test script injection
//Insert script to read the Browser URL and send it back to proxy
public void OnResponse(object sender, SessionEventArgs e)
{
////read response headers
// var responseHeaders = e.ProxySession.Response.ResponseHeaders;
//if (!e.ProxySession.Request.Hostname.Equals("medeczane.sgk.gov.tr")) return;
//if (e.RequestMethod == "GET" || e.RequestMethod == "POST")
//{
// if (e.ProxySession.Response.ResponseStatusCode == "200")
// {
// if (e.ProxySession.Response.ContentType.Trim().ToLower().Contains("text/html"))
// {
// string body = e.GetResponseBodyAsString();
// }
// }
//}

//read response headers
var responseHeaders = e.ProxySession.Response.ResponseHeaders;

//if (!e.ProxySession.Request.Host.Equals("medeczane.sgk.gov.tr")) return;
if (e.RequestMethod == "GET" || e.RequestMethod == "POST")
{
if (e.ProxySession.Response.ResponseStatusCode == "200")
{
if (e.ProxySession.Response.ContentType.Trim().ToLower().Contains("text/html"))
{
string body = e.GetResponseBodyAsString();
}
}
}
}
}
}
54 changes: 38 additions & 16 deletions Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary>
public class SessionEventArgs : EventArgs, IDisposable
{
readonly int _bufferSize;

/// <summary>
/// Constructor to initialize the proxy
/// </summary>
internal SessionEventArgs(int bufferSize)
internal SessionEventArgs()
{
_bufferSize = bufferSize;

Client = new ProxyClient();
ProxySession = new HttpWebSession();
}
Expand Down Expand Up @@ -385,10 +384,8 @@ public void SetResponseBodyString(string body)
/// Before request is made to server
/// Respond with the specified HTML string to client
/// and ignore the request
/// Marking as obsolete, need to comeup with a generic responder method in future
/// </summary>
/// <param name="html"></param>
// [Obsolete]
public void Ok(string html)
{
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
Expand All @@ -398,22 +395,47 @@ public void Ok(string html)

var result = Encoding.Default.GetBytes(html);

var connectStreamWriter = new StreamWriter(this.Client.ClientStream);
connectStreamWriter.WriteLine(string.Format("{0} {1} {2}", ProxySession.Request.HttpVersion, 200, "Ok"));
connectStreamWriter.WriteLine("Timestamp: {0}", DateTime.Now);
connectStreamWriter.WriteLine("content-length: " + result.Length);
connectStreamWriter.WriteLine("Cache-Control: no-cache, no-store, must-revalidate");
connectStreamWriter.WriteLine("Pragma: no-cache");
connectStreamWriter.WriteLine("Expires: 0");
Ok(result);
}

/// <summary>
/// Before request is made to server
/// Respond with the specified byte[] to client
/// and ignore the request
/// </summary>
/// <param name="body"></param>
public void Ok(byte[] result)
{
var response = new Response();

response.HttpVersion = ProxySession.Request.HttpVersion;
response.ResponseStatusCode = "200";
response.ResponseStatusDescription = "Ok";

connectStreamWriter.WriteLine(ProxySession.Request.IsAlive ? "Connection: Keep-Alive" : "Connection: close");
response.ResponseHeaders.Add(new HttpHeader("Timestamp", DateTime.Now.ToString()));

connectStreamWriter.WriteLine();
connectStreamWriter.Flush();
response.ResponseHeaders.Add(new HttpHeader("content-length", DateTime.Now.ToString()));
response.ResponseHeaders.Add(new HttpHeader("Cache-Control", "no-cache, no-store, must-revalidate"));
response.ResponseHeaders.Add(new HttpHeader("Pragma", "no-cache"));
response.ResponseHeaders.Add(new HttpHeader("Expires", "0"));

this.Client.ClientStream.Write(result, 0, result.Length);
response.ResponseBody = result;

Respond(response);

ProxySession.Request.CancelRequest = true;
}

/// a generic responder method
public void Respond(Response response)
{
ProxySession.Request.RequestLocked = true;

response.ResponseLocked = true;
response.ResponseBodyRead = true;

ProxySession.Response = response;
ProxyServer.HandleHttpSessionResponse(this);
}
}
}
6 changes: 3 additions & 3 deletions Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace Titanium.Web.Proxy.Extensions
public static class HttpWebRequestExtensions
{
//Get encoding of the HTTP request
public static Encoding GetEncoding(this HttpWebSession request)
public static Encoding GetEncoding(this Request request)
{
try
{
//return default if not specified
if (request.Request.ContentType == null) return Encoding.GetEncoding("ISO-8859-1");
if (request.ContentType == null) return Encoding.GetEncoding("ISO-8859-1");

//extract the encoding by finding the charset
var contentTypes = request.Request.ContentType.Split(';');
var contentTypes = request.ContentType.Split(';');
foreach (var contentType in contentTypes)
{
var encodingSplit = contentType.Split('=');
Expand Down
6 changes: 3 additions & 3 deletions Titanium.Web.Proxy/Extensions/HttpWebResponseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace Titanium.Web.Proxy.Extensions
{
public static class HttpWebResponseExtensions
{
public static Encoding GetResponseEncoding(this HttpWebSession response)
public static Encoding GetResponseEncoding(this Response response)
{
if (string.IsNullOrEmpty(response.Response.CharacterSet))
if (string.IsNullOrEmpty(response.CharacterSet))
return Encoding.GetEncoding("ISO-8859-1");

try
{
return Encoding.GetEncoding(response.Response.CharacterSet.Replace(@"""", string.Empty));
return Encoding.GetEncoding(response.CharacterSet.Replace(@"""", string.Empty));
}
catch { return Encoding.GetEncoding("ISO-8859-1"); }
}
Expand Down
Loading