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

Commit bb6691d

Browse files
committed
Merge pull request #40 from titanium007/bug-fixes
Bug fixes
2 parents b8c85c7 + 6800b59 commit bb6691d

13 files changed

+238
-102
lines changed

.build/default.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if(!$Configuration) { $Configuration = $env:Configuration }
2121
if(!$Configuration) { $Configuration = "Release" }
2222

2323
if(!$Version) { $Version = $env:APPVEYOR_BUILD_VERSION }
24-
if(!$Version) { $Version = "1.0.$BuildNumber" }
24+
if(!$Version) { $Version = "2.0.$BuildNumber" }
2525

2626
if(!$Branch) { $Branch = $env:APPVEYOR_REPO_BRANCH }
2727
if(!$Branch) { $Branch = "local" }

README.md

+15-4
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,22 @@ Setup HTTP proxy:
3636
// listen to client request & server response events
3737
ProxyServer.BeforeRequest += OnRequest;
3838
ProxyServer.BeforeResponse += OnResponse;
39+
40+
//Exclude Https addresses you don't want to proxy
41+
//Usefull for clients that use certificate pinning
42+
//for example dropbox.com
43+
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Loopback, 8000, true){
44+
ExcludedHostNameRegex = new List<string>() { "dropbox.com" }
45+
};
3946

40-
ProxyServer.EnableSSL = true;
41-
ProxyServer.SetAsSystemProxy = true;
42-
ProxyServer.Start();
43-
47+
var transparentEndPoint = new TransparentProxyEndPoint(IPAddress.Loopback, 8001, true);
48+
49+
ProxyServer.AddEndPoint(explicitEndPoint);
50+
ProxyServer.AddEndPoint(transparentEndPoint);
51+
ProxyServer.Start();
52+
53+
ProxyServer.SetAsSystemProxy(explicitEndPoint);
54+
4455
//wait here (You can use something else as a wait function, I am using this as a demo)
4556
Console.Read();
4657

Titanium.Web.Proxy.Test/Program.cs

-16
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,6 @@ public static void Main(string[] args)
1414
NativeMethods.SetConsoleCtrlHandler(NativeMethods.Handler, true);
1515

1616

17-
Console.Write("Do you want to monitor HTTPS? (Y/N):");
18-
19-
var readLine = Console.ReadLine();
20-
if (readLine != null && readLine.Trim().ToLower() == "y")
21-
{
22-
Controller.EnableSsl = true;
23-
}
24-
25-
Console.Write("Do you want to set this as a System Proxy? (Y/N):");
26-
27-
var line = Console.ReadLine();
28-
if (line != null && line.Trim().ToLower() == "y")
29-
{
30-
Controller.SetAsSystemProxy = true;
31-
}
32-
3317
//Start proxy controller
3418
Controller.StartProxy();
3519

Titanium.Web.Proxy.Test/ProxyTestController.cs

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Net;
24
using System.Text.RegularExpressions;
35
using Titanium.Web.Proxy.EventArguments;
6+
using Titanium.Web.Proxy.Models;
47

58
namespace Titanium.Web.Proxy.Test
69
{
710
public class ProxyTestController
811
{
9-
public int ListeningPort { get; set; }
10-
public bool EnableSsl { get; set; }
11-
public bool SetAsSystemProxy { get; set; }
12+
1213

1314
public void StartProxy()
1415
{
1516
ProxyServer.BeforeRequest += OnRequest;
1617
ProxyServer.BeforeResponse += OnResponse;
1718

18-
ProxyServer.EnableSsl = EnableSsl;
19-
20-
ProxyServer.SetAsSystemProxy = SetAsSystemProxy;
21-
2219
//Exclude Https addresses you don't want to proxy
2320
//Usefull for clients that use certificate pinning
2421
//for example dropbox.com
25-
ProxyServer.ExcludedHttpsHostNameRegex.Add(".dropbox.com");
22+
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Loopback, 8000, true){
23+
ExcludedHostNameRegex = new List<string>() { "dropbox.com" }
24+
};
2625

26+
var transparentEndPoint = new TransparentProxyEndPoint(IPAddress.Loopback, 8001, true);
27+
28+
ProxyServer.AddEndPoint(explicitEndPoint);
29+
ProxyServer.AddEndPoint(transparentEndPoint);
2730
ProxyServer.Start();
2831

29-
ProxyServer.ListeningPort = ProxyServer.ListeningPort;
32+
foreach (var endPoint in ProxyServer.ProxyEndPoints)
33+
Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ", endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
34+
35+
ProxyServer.SetAsSystemProxy(explicitEndPoint);
3036

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

3439
public void Stop()

Titanium.Web.Proxy/Extensions/HttpWebResponseExtensions.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ public static class HttpWebResponseExtensions
88
{
99
public static Encoding GetResponseEncoding(this HttpWebSession response)
1010
{
11-
if (string.IsNullOrEmpty(response.Response.CharacterSet)) return Encoding.GetEncoding("ISO-8859-1");
12-
return Encoding.GetEncoding(response.Response.CharacterSet.Replace(@"""", string.Empty));
11+
if (string.IsNullOrEmpty(response.Response.CharacterSet))
12+
return Encoding.GetEncoding("ISO-8859-1");
13+
14+
try
15+
{
16+
return Encoding.GetEncoding(response.Response.CharacterSet.Replace(@"""", string.Empty));
17+
}
18+
catch { return Encoding.GetEncoding("ISO-8859-1"); }
1319
}
1420
}
1521
}

Titanium.Web.Proxy/Extensions/StreamExtensions.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ public static class StreamHelper
88
{
99
public static void CopyToAsync(this Stream input, string initialData, Stream output, int bufferSize)
1010
{
11-
var bytes = Encoding.ASCII.GetBytes(initialData);
12-
output.Write(bytes, 0, bytes.Length);
11+
if(!string.IsNullOrEmpty(initialData))
12+
{
13+
var bytes = Encoding.ASCII.GetBytes(initialData);
14+
output.Write(bytes, 0, bytes.Length);
15+
}
16+
1317
CopyToAsync(input, output, bufferSize);
1418
}
1519

1620
//http://stackoverflow.com/questions/1540658/net-asynchronous-stream-read-write
17-
public static void CopyToAsync(this Stream input, Stream output, int bufferSize)
21+
private static void CopyToAsync(this Stream input, Stream output, int bufferSize)
1822
{
1923
try
2024
{

Titanium.Web.Proxy/Helpers/Tcp.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static void SendRaw(Stream clientStream, string httpCmd, List<HttpHeader>
5151
try
5252
{
5353
sslStream = new SslStream(tunnelStream);
54-
sslStream.AuthenticateAsClient(hostName);
54+
sslStream.AuthenticateAsClient(hostName, null, ProxyServer.SupportedProtocols, false);
5555
tunnelStream = sslStream;
5656
}
5757
catch
@@ -69,10 +69,10 @@ public static void SendRaw(Stream clientStream, string httpCmd, List<HttpHeader>
6969
if (sb != null)
7070
clientStream.CopyToAsync(sb.ToString(), tunnelStream, BUFFER_SIZE);
7171
else
72-
clientStream.CopyToAsync(tunnelStream, BUFFER_SIZE);
72+
clientStream.CopyToAsync(string.Empty, tunnelStream, BUFFER_SIZE);
7373
});
7474

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

7777
Task.WaitAll(sendRelay, receiveRelay);
7878
}

Titanium.Web.Proxy/Models/EndPoint.cs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Net.Sockets;
6+
using System.Text;
7+
8+
namespace Titanium.Web.Proxy.Models
9+
{
10+
public abstract class ProxyEndPoint
11+
{
12+
public ProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl)
13+
{
14+
this.IpAddress = IpAddress;
15+
this.Port = Port;
16+
this.EnableSsl = EnableSsl;
17+
}
18+
19+
public IPAddress IpAddress { get; internal set; }
20+
public int Port { get; internal set; }
21+
public bool EnableSsl { get; internal set; }
22+
23+
internal TcpListener listener { get; set; }
24+
}
25+
26+
public class ExplicitProxyEndPoint : ProxyEndPoint
27+
{
28+
internal bool IsSystemProxy { get; set; }
29+
public List<string> ExcludedHostNameRegex { get; set; }
30+
31+
public ExplicitProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl)
32+
: base(IpAddress, Port, EnableSsl)
33+
{
34+
35+
}
36+
}
37+
38+
public class TransparentProxyEndPoint : ProxyEndPoint
39+
{
40+
public TransparentProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl)
41+
: base(IpAddress, Port, EnableSsl)
42+
{
43+
44+
}
45+
}
46+
}

Titanium.Web.Proxy/Network/TcpConnectionManager.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Net.Security;
1010
using Titanium.Web.Proxy.Helpers;
1111
using System.Threading;
12+
using System.Security.Authentication;
1213

1314
namespace Titanium.Web.Proxy.Network
1415
{
@@ -76,7 +77,7 @@ private static TcpConnection CreateClient(string Hostname, int port, bool IsSecu
7677
try
7778
{
7879
sslStream = new SslStream(stream);
79-
sslStream.AuthenticateAsClient(Hostname);
80+
sslStream.AuthenticateAsClient(Hostname, null, ProxyServer.SupportedProtocols , false);
8081
stream = (Stream)sslStream;
8182
}
8283
catch

0 commit comments

Comments
 (0)