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

Release 2.1 #41

Merged
merged 21 commits into from
Feb 6, 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
2 changes: 1 addition & 1 deletion .build/default.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if(!$Configuration) { $Configuration = $env:Configuration }
if(!$Configuration) { $Configuration = "Release" }

if(!$Version) { $Version = $env:APPVEYOR_BUILD_VERSION }
if(!$Version) { $Version = "1.0.$BuildNumber" }
if(!$Version) { $Version = "2.0.$BuildNumber" }

if(!$Branch) { $Branch = $env:APPVEYOR_REPO_BRANCH }
if(!$Branch) { $Branch = "local" }
Expand Down
51 changes: 43 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Refer the HTTP Proxy Server library in your project, look up Test project to lea

Install by nuget:

Install-Package Titanium.Web.Proxy
Install-Package Titanium.Web.Proxy -Pre

After installing nuget package mark following files to be copied to app directory

Expand All @@ -34,13 +34,46 @@ Setup HTTP proxy:

```csharp
// listen to client request & server response events
ProxyServer.BeforeRequest += OnRequest;
ProxyServer.BeforeResponse += OnResponse;

ProxyServer.EnableSSL = true;
ProxyServer.SetAsSystemProxy = true;
ProxyServer.BeforeRequest += OnRequest;
ProxyServer.BeforeResponse += OnResponse;

//Exclude Https addresses you don't want to proxy
//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" }
};

//An explicit endpoint is where the client knows about the existance of a proxy
//So client sends request in a proxy friendly manner
ProxyServer.AddEndPoint(explicitEndPoint);
ProxyServer.Start();



//Transparent endpoint is usefull for reverse proxying (client is not aware of the existance of proxy)
//A transparent endpoint usually requires a network router port forwarding HTTP(S) packets to this endpoint
//Currently do not support Server Name Indication (It is not currently supported by SslStream class)
//That means that the transparent endpoint will always provide the same Generic Certificate to all HTTPS requests
//In this example only google.com will work for HTTPS requests
//Other sites will receive a certificate mismatch warning on browser
//Please read about it before asking questions!
var transparentEndPoint = new TransparentProxyEndPoint(IPAddress.Any, 8001, true) {
GenericCertificateName = "google.com"
};
ProxyServer.AddEndPoint(transparentEndPoint);


foreach (var endPoint in ProxyServer.ProxyEndPoints)
Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ",
endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);

//You can also add/remove end points after proxy has been started
ProxyServer.RemoveEndPoint(transparentEndPoint);

//Only explicit proxies can be set as system proxy!
ProxyServer.SetAsSystemHttpProxy(explicitEndPoint);
ProxyServer.SetAsSystemHttpsProxy(explicitEndPoint);

//wait here (You can use something else as a wait function, I am using this as a demo)
Console.Read();

Expand Down Expand Up @@ -113,6 +146,8 @@ Sample request and response event handlers
```
Future updates
============
* Add callbacks for client/server certificate validation/selection
* Support mutual authentication
* Add Server Name Indication (SNI) for transparent endpoints
* Support HTTP 2.0
* Support modification of web socket requests

16 changes: 0 additions & 16 deletions Titanium.Web.Proxy.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,6 @@ public static void Main(string[] args)
NativeMethods.SetConsoleCtrlHandler(NativeMethods.Handler, true);


Console.Write("Do you want to monitor HTTPS? (Y/N):");

var readLine = Console.ReadLine();
if (readLine != null && readLine.Trim().ToLower() == "y")
{
Controller.EnableSsl = true;
}

Console.Write("Do you want to set this as a System Proxy? (Y/N):");

var line = Console.ReadLine();
if (line != null && line.Trim().ToLower() == "y")
{
Controller.SetAsSystemProxy = true;
}

//Start proxy controller
Controller.StartProxy();

Expand Down
44 changes: 33 additions & 11 deletions Titanium.Web.Proxy.Test/ProxyTestController.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,57 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text.RegularExpressions;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Models;

namespace Titanium.Web.Proxy.Test
{
public class ProxyTestController
{
public int ListeningPort { get; set; }
public bool EnableSsl { get; set; }
public bool SetAsSystemProxy { get; set; }


public void StartProxy()
{
ProxyServer.BeforeRequest += OnRequest;
ProxyServer.BeforeResponse += OnResponse;

ProxyServer.EnableSsl = EnableSsl;

ProxyServer.SetAsSystemProxy = SetAsSystemProxy;

//Exclude Https addresses you don't want to proxy
//Usefull for clients that use certificate pinning
//for example dropbox.com
ProxyServer.ExcludedHttpsHostNameRegex.Add(".dropbox.com");
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true){
ExcludedHttpsHostNameRegex = new List<string>() { "dropbox.com" }
};

//An explicit endpoint is where the client knows about the existance of a proxy
//So client sends request in a proxy friendly manner
ProxyServer.AddEndPoint(explicitEndPoint);
ProxyServer.Start();

ProxyServer.ListeningPort = ProxyServer.ListeningPort;

//Transparent endpoint is usefull for reverse proxying (client is not aware of the existance of proxy)
//A transparent endpoint usually requires a network router port forwarding HTTP(S) packets to this endpoint
//Currently do not support Server Name Indication (It is not currently supported by SslStream class)
//That means that the transparent endpoint will always provide the same Generic Certificate to all HTTPS requests
//In this example only google.com will work for HTTPS requests
//Other sites will receive a certificate mismatch warning on browser
//Please read about it before asking questions!
var transparentEndPoint = new TransparentProxyEndPoint(IPAddress.Any, 8001, true) {
GenericCertificateName = "google.com"
};
ProxyServer.AddEndPoint(transparentEndPoint);


foreach (var endPoint in ProxyServer.ProxyEndPoints)
Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ",
endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);

Console.WriteLine("Proxy listening on local machine port: {0} ", ProxyServer.ListeningPort);
//You can also add/remove end points after proxy has been started
ProxyServer.RemoveEndPoint(transparentEndPoint);

//Only explicit proxies can be set as system proxy!
ProxyServer.SetAsSystemHttpProxy(explicitEndPoint);
ProxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
}

public void Stop()
Expand All @@ -39,7 +62,6 @@ public void Stop()
ProxyServer.Stop();
}


//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)
Expand Down
10 changes: 8 additions & 2 deletions Titanium.Web.Proxy/Extensions/HttpWebResponseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ public static class HttpWebResponseExtensions
{
public static Encoding GetResponseEncoding(this HttpWebSession response)
{
if (string.IsNullOrEmpty(response.Response.CharacterSet)) return Encoding.GetEncoding("ISO-8859-1");
return Encoding.GetEncoding(response.Response.CharacterSet.Replace(@"""", string.Empty));
if (string.IsNullOrEmpty(response.Response.CharacterSet))
return Encoding.GetEncoding("ISO-8859-1");

try
{
return Encoding.GetEncoding(response.Response.CharacterSet.Replace(@"""", string.Empty));
}
catch { return Encoding.GetEncoding("ISO-8859-1"); }
}
}
}
10 changes: 7 additions & 3 deletions Titanium.Web.Proxy/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ public static class StreamHelper
{
public static void CopyToAsync(this Stream input, string initialData, Stream output, int bufferSize)
{
var bytes = Encoding.ASCII.GetBytes(initialData);
output.Write(bytes, 0, bytes.Length);
if(!string.IsNullOrEmpty(initialData))
{
var bytes = Encoding.ASCII.GetBytes(initialData);
output.Write(bytes, 0, bytes.Length);
}

CopyToAsync(input, output, bufferSize);
}

//http://stackoverflow.com/questions/1540658/net-asynchronous-stream-read-write
public static void CopyToAsync(this Stream input, Stream output, int bufferSize)
private static void CopyToAsync(this Stream input, Stream output, int bufferSize)
{
try
{
Expand Down
Loading