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

Commit 1a558f0

Browse files
justcoding121justcoding121
authored andcommitted
Merge pull request #53 from justcoding121/release
Release
2 parents f10f779 + 12625c2 commit 1a558f0

File tree

7 files changed

+305
-176
lines changed

7 files changed

+305
-176
lines changed

Titanium.Web.Proxy.Test/ProxyTestController.cs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void StartProxy()
2020
//Usefull for clients that use certificate pinning
2121
//for example dropbox.com
2222
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true){
23-
ExcludedHttpsHostNameRegex = new List<string>() { "dropbox.com" }
23+
// ExcludedHttpsHostNameRegex = new List<string>() { "google.com", "dropbox.com" }
2424
};
2525

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

71-
////read request headers
72-
//var requestHeaders = e.ProxySession.Request.RequestHeaders;
71+
//read request headers
72+
var requestHeaders = e.ProxySession.Request.RequestHeaders;
7373

74-
//if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
75-
//{
76-
// //Get/Set request body bytes
77-
// byte[] bodyBytes = e.GetRequestBody();
78-
// e.SetRequestBody(bodyBytes);
74+
if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
75+
{
76+
//Get/Set request body bytes
77+
byte[] bodyBytes = e.GetRequestBody();
78+
e.SetRequestBody(bodyBytes);
7979

80-
// //Get/Set request body as string
81-
// string bodyString = e.GetRequestBodyAsString();
82-
// e.SetRequestBodyString(bodyString);
80+
//Get/Set request body as string
81+
string bodyString = e.GetRequestBodyAsString();
82+
e.SetRequestBodyString(bodyString);
8383

84-
//}
84+
}
8585

86-
////To cancel a request with a custom HTML content
87-
////Filter URL
86+
//To cancel a request with a custom HTML content
87+
//Filter URL
8888

89-
//if (e.ProxySession.Request.RequestUrl.Contains("google.com"))
90-
//{
91-
// e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
92-
//}
89+
if (e.ProxySession.Request.RequestUri.AbsoluteUri.Contains("google.com"))
90+
{
91+
e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
92+
}
9393
}
9494

9595
//Test script injection
9696
//Insert script to read the Browser URL and send it back to proxy
9797
public void OnResponse(object sender, SessionEventArgs e)
9898
{
99-
100-
////read response headers
101-
// var responseHeaders = e.ProxySession.Response.ResponseHeaders;
102-
103-
//if (!e.ProxySession.Request.Hostname.Equals("medeczane.sgk.gov.tr")) return;
104-
//if (e.RequestMethod == "GET" || e.RequestMethod == "POST")
105-
//{
106-
// if (e.ProxySession.Response.ResponseStatusCode == "200")
107-
// {
108-
// if (e.ProxySession.Response.ContentType.Trim().ToLower().Contains("text/html"))
109-
// {
110-
// string body = e.GetResponseBodyAsString();
111-
// }
112-
// }
113-
//}
99+
100+
//read response headers
101+
var responseHeaders = e.ProxySession.Response.ResponseHeaders;
102+
103+
//if (!e.ProxySession.Request.Host.Equals("medeczane.sgk.gov.tr")) return;
104+
if (e.RequestMethod == "GET" || e.RequestMethod == "POST")
105+
{
106+
if (e.ProxySession.Response.ResponseStatusCode == "200")
107+
{
108+
if (e.ProxySession.Response.ContentType.Trim().ToLower().Contains("text/html"))
109+
{
110+
string body = e.GetResponseBodyAsString();
111+
}
112+
}
113+
}
114114
}
115115
}
116116
}

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ namespace Titanium.Web.Proxy.EventArguments
2222
/// </summary>
2323
public class SessionEventArgs : EventArgs, IDisposable
2424
{
25-
readonly int _bufferSize;
2625

2726
/// <summary>
2827
/// Constructor to initialize the proxy
2928
/// </summary>
30-
internal SessionEventArgs(int bufferSize)
29+
internal SessionEventArgs()
3130
{
32-
_bufferSize = bufferSize;
31+
3332
Client = new ProxyClient();
3433
ProxySession = new HttpWebSession();
3534
}
@@ -385,10 +384,8 @@ public void SetResponseBodyString(string body)
385384
/// Before request is made to server
386385
/// Respond with the specified HTML string to client
387386
/// and ignore the request
388-
/// Marking as obsolete, need to comeup with a generic responder method in future
389387
/// </summary>
390388
/// <param name="html"></param>
391-
// [Obsolete]
392389
public void Ok(string html)
393390
{
394391
if (ProxySession.Request.RequestLocked) throw new Exception("You cannot call this function after request is made to server.");
@@ -398,22 +395,47 @@ public void Ok(string html)
398395

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

401-
var connectStreamWriter = new StreamWriter(this.Client.ClientStream);
402-
connectStreamWriter.WriteLine(string.Format("{0} {1} {2}", ProxySession.Request.HttpVersion, 200, "Ok"));
403-
connectStreamWriter.WriteLine("Timestamp: {0}", DateTime.Now);
404-
connectStreamWriter.WriteLine("content-length: " + result.Length);
405-
connectStreamWriter.WriteLine("Cache-Control: no-cache, no-store, must-revalidate");
406-
connectStreamWriter.WriteLine("Pragma: no-cache");
407-
connectStreamWriter.WriteLine("Expires: 0");
398+
Ok(result);
399+
}
400+
401+
/// <summary>
402+
/// Before request is made to server
403+
/// Respond with the specified byte[] to client
404+
/// and ignore the request
405+
/// </summary>
406+
/// <param name="body"></param>
407+
public void Ok(byte[] result)
408+
{
409+
var response = new Response();
410+
411+
response.HttpVersion = ProxySession.Request.HttpVersion;
412+
response.ResponseStatusCode = "200";
413+
response.ResponseStatusDescription = "Ok";
408414

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

411-
connectStreamWriter.WriteLine();
412-
connectStreamWriter.Flush();
417+
response.ResponseHeaders.Add(new HttpHeader("content-length", DateTime.Now.ToString()));
418+
response.ResponseHeaders.Add(new HttpHeader("Cache-Control", "no-cache, no-store, must-revalidate"));
419+
response.ResponseHeaders.Add(new HttpHeader("Pragma", "no-cache"));
420+
response.ResponseHeaders.Add(new HttpHeader("Expires", "0"));
413421

414-
this.Client.ClientStream.Write(result, 0, result.Length);
422+
response.ResponseBody = result;
423+
424+
Respond(response);
415425

416426
ProxySession.Request.CancelRequest = true;
417427
}
428+
429+
/// a generic responder method
430+
public void Respond(Response response)
431+
{
432+
ProxySession.Request.RequestLocked = true;
433+
434+
response.ResponseLocked = true;
435+
response.ResponseBodyRead = true;
436+
437+
ProxySession.Response = response;
438+
ProxyServer.HandleHttpSessionResponse(this);
439+
}
418440
}
419441
}

Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ namespace Titanium.Web.Proxy.Extensions
1010
public static class HttpWebRequestExtensions
1111
{
1212
//Get encoding of the HTTP request
13-
public static Encoding GetEncoding(this HttpWebSession request)
13+
public static Encoding GetEncoding(this Request request)
1414
{
1515
try
1616
{
1717
//return default if not specified
18-
if (request.Request.ContentType == null) return Encoding.GetEncoding("ISO-8859-1");
18+
if (request.ContentType == null) return Encoding.GetEncoding("ISO-8859-1");
1919

2020
//extract the encoding by finding the charset
21-
var contentTypes = request.Request.ContentType.Split(';');
21+
var contentTypes = request.ContentType.Split(';');
2222
foreach (var contentType in contentTypes)
2323
{
2424
var encodingSplit = contentType.Split('=');

Titanium.Web.Proxy/Extensions/HttpWebResponseExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ namespace Titanium.Web.Proxy.Extensions
66
{
77
public static class HttpWebResponseExtensions
88
{
9-
public static Encoding GetResponseEncoding(this HttpWebSession response)
9+
public static Encoding GetResponseEncoding(this Response response)
1010
{
11-
if (string.IsNullOrEmpty(response.Response.CharacterSet))
11+
if (string.IsNullOrEmpty(response.CharacterSet))
1212
return Encoding.GetEncoding("ISO-8859-1");
1313

1414
try
1515
{
16-
return Encoding.GetEncoding(response.Response.CharacterSet.Replace(@"""", string.Empty));
16+
return Encoding.GetEncoding(response.CharacterSet.Replace(@"""", string.Empty));
1717
}
1818
catch { return Encoding.GetEncoding("ISO-8859-1"); }
1919
}

0 commit comments

Comments
 (0)