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

Commit

Permalink
Merge pull request #40 from titanium007/bug-fixes
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
justcoding121 committed Feb 6, 2016
2 parents b8c85c7 + 6800b59 commit bb6691d
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 102 deletions.
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
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,22 @@ Setup HTTP proxy:
// listen to client request & server response events
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.Loopback, 8000, true){
ExcludedHostNameRegex = new List<string>() { "dropbox.com" }
};

ProxyServer.EnableSSL = true;
ProxyServer.SetAsSystemProxy = true;
ProxyServer.Start();

var transparentEndPoint = new TransparentProxyEndPoint(IPAddress.Loopback, 8001, true);

ProxyServer.AddEndPoint(explicitEndPoint);
ProxyServer.AddEndPoint(transparentEndPoint);
ProxyServer.Start();

ProxyServer.SetAsSystemProxy(explicitEndPoint);

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

Expand Down
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
25 changes: 15 additions & 10 deletions Titanium.Web.Proxy.Test/ProxyTestController.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
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.Loopback, 8000, true){
ExcludedHostNameRegex = new List<string>() { "dropbox.com" }
};

var transparentEndPoint = new TransparentProxyEndPoint(IPAddress.Loopback, 8001, true);

ProxyServer.AddEndPoint(explicitEndPoint);
ProxyServer.AddEndPoint(transparentEndPoint);
ProxyServer.Start();

ProxyServer.ListeningPort = ProxyServer.ListeningPort;
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);

ProxyServer.SetAsSystemProxy(explicitEndPoint);

Console.WriteLine("Proxy listening on local machine port: {0} ", ProxyServer.ListeningPort);
}

public void Stop()
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
6 changes: 3 additions & 3 deletions Titanium.Web.Proxy/Helpers/Tcp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void SendRaw(Stream clientStream, string httpCmd, List<HttpHeader>
try
{
sslStream = new SslStream(tunnelStream);
sslStream.AuthenticateAsClient(hostName);
sslStream.AuthenticateAsClient(hostName, null, ProxyServer.SupportedProtocols, false);
tunnelStream = sslStream;
}
catch
Expand All @@ -69,10 +69,10 @@ public static void SendRaw(Stream clientStream, string httpCmd, List<HttpHeader>
if (sb != null)
clientStream.CopyToAsync(sb.ToString(), tunnelStream, BUFFER_SIZE);
else
clientStream.CopyToAsync(tunnelStream, BUFFER_SIZE);
clientStream.CopyToAsync(string.Empty, tunnelStream, BUFFER_SIZE);
});

var receiveRelay = Task.Factory.StartNew(() => tunnelStream.CopyToAsync(clientStream, BUFFER_SIZE));
var receiveRelay = Task.Factory.StartNew(() => tunnelStream.CopyToAsync(string.Empty, clientStream, BUFFER_SIZE));

Task.WaitAll(sendRelay, receiveRelay);
}
Expand Down
46 changes: 46 additions & 0 deletions Titanium.Web.Proxy/Models/EndPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Titanium.Web.Proxy.Models
{
public abstract class ProxyEndPoint
{
public ProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl)
{
this.IpAddress = IpAddress;
this.Port = Port;
this.EnableSsl = EnableSsl;
}

public IPAddress IpAddress { get; internal set; }
public int Port { get; internal set; }
public bool EnableSsl { get; internal set; }

internal TcpListener listener { get; set; }
}

public class ExplicitProxyEndPoint : ProxyEndPoint
{
internal bool IsSystemProxy { get; set; }
public List<string> ExcludedHostNameRegex { get; set; }

public ExplicitProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl)
: base(IpAddress, Port, EnableSsl)
{

}
}

public class TransparentProxyEndPoint : ProxyEndPoint
{
public TransparentProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl)
: base(IpAddress, Port, EnableSsl)
{

}
}
}
3 changes: 2 additions & 1 deletion Titanium.Web.Proxy/Network/TcpConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Net.Security;
using Titanium.Web.Proxy.Helpers;
using System.Threading;
using System.Security.Authentication;

namespace Titanium.Web.Proxy.Network
{
Expand Down Expand Up @@ -76,7 +77,7 @@ private static TcpConnection CreateClient(string Hostname, int port, bool IsSecu
try
{
sslStream = new SslStream(stream);
sslStream.AuthenticateAsClient(Hostname);
sslStream.AuthenticateAsClient(Hostname, null, ProxyServer.SupportedProtocols , false);
stream = (Stream)sslStream;
}
catch
Expand Down
Loading

0 comments on commit bb6691d

Please sign in to comment.