Commit 3da0c593 authored by titanium007's avatar titanium007

Rewrite system proxy set

parent de0ec9c4
...@@ -20,20 +20,22 @@ namespace Titanium.Web.Proxy.Test ...@@ -20,20 +20,22 @@ namespace Titanium.Web.Proxy.Test
//Usefull for clients that use certificate pinning //Usefull for clients that use certificate pinning
//for example dropbox.com //for example dropbox.com
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Loopback, 8000, true){ var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Loopback, 8000, true){
ExcludedHostNameRegex = new List<string>() { "dropbox.com" } ExcludedHttpsHostNameRegex = new List<string>() { "dropbox.com" }
}; };
var transparentEndPoint = new TransparentProxyEndPoint(IPAddress.Loopback, 8001, true); var transparentEndPoint = new TransparentProxyEndPoint(IPAddress.Loopback, 8001, true);
ProxyServer.AddEndPoint(explicitEndPoint); ProxyServer.AddEndPoint(explicitEndPoint);
ProxyServer.AddEndPoint(transparentEndPoint);
ProxyServer.Start(); ProxyServer.Start();
//You can also add/remove end points after proxy has been started
ProxyServer.AddEndPoint(transparentEndPoint);
foreach (var endPoint in ProxyServer.ProxyEndPoints) 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("Listening on '{0}' endpoint at Ip {1} and port: {2} ", endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
ProxyServer.SetAsSystemProxy(explicitEndPoint); ProxyServer.SetAsSystemHttpProxy(explicitEndPoint);
ProxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
} }
public void Stop() public void Stop()
......
...@@ -25,8 +25,10 @@ namespace Titanium.Web.Proxy.Models ...@@ -25,8 +25,10 @@ namespace Titanium.Web.Proxy.Models
public class ExplicitProxyEndPoint : ProxyEndPoint public class ExplicitProxyEndPoint : ProxyEndPoint
{ {
internal bool IsSystemProxy { get; set; } internal bool IsSystemHttpProxy { get; set; }
public List<string> ExcludedHostNameRegex { get; set; } internal bool IsSystemHttpsProxy { get; set; }
public List<string> ExcludedHttpsHostNameRegex { get; set; }
public ExplicitProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl) public ExplicitProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl)
: base(IpAddress, Port, EnableSsl) : base(IpAddress, Port, EnableSsl)
......
...@@ -35,7 +35,7 @@ namespace Titanium.Web.Proxy ...@@ -35,7 +35,7 @@ namespace Titanium.Web.Proxy
Encoding.ASCII.GetBytes(0.ToString("x2") + Environment.NewLine + Environment.NewLine); Encoding.ASCII.GetBytes(0.ToString("x2") + Environment.NewLine + Environment.NewLine);
#if NET45 #if NET45
internal static SslProtocols SupportedProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Ssl3 ; internal static SslProtocols SupportedProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Ssl3;
#else #else
public static SslProtocols SupportedProtocols = SslProtocols.Tls | SslProtocols.Ssl3; public static SslProtocols SupportedProtocols = SslProtocols.Tls | SslProtocols.Ssl3;
#endif #endif
...@@ -53,7 +53,7 @@ namespace Titanium.Web.Proxy ...@@ -53,7 +53,7 @@ namespace Titanium.Web.Proxy
private static CertificateManager CertManager { get; set; } private static CertificateManager CertManager { get; set; }
private static bool EnableSsl { get; set; } private static bool EnableSsl { get; set; }
private static bool certTrusted { get; set; } private static bool certTrusted { get; set; }
private static bool proxyStarted { get; set; } private static bool proxyRunning { get; set; }
public static string RootCertificateName { get; set; } public static string RootCertificateName { get; set; }
...@@ -69,52 +69,78 @@ namespace Titanium.Web.Proxy ...@@ -69,52 +69,78 @@ namespace Titanium.Web.Proxy
public static void AddEndPoint(ProxyEndPoint endPoint) public static void AddEndPoint(ProxyEndPoint endPoint)
{ {
if (proxyStarted)
throw new Exception("Cannot add end points after proxy started.");
ProxyEndPoints.Add(endPoint); ProxyEndPoints.Add(endPoint);
if (proxyRunning)
Listen(endPoint);
} }
public static void SetAsSystemProxy(ExplicitProxyEndPoint endPoint) public static void RemoveEndPoint(ProxyEndPoint endPoint)
{ {
if (ProxyEndPoints.Contains(endPoint) == false) if (ProxyEndPoints.Contains(endPoint) == false)
throw new Exception("Cannot set endPoints not added to proxy as system proxy"); throw new Exception("Cannot remove endPoints not added to proxy");
if (!proxyStarted) ProxyEndPoints.Remove(endPoint);
throw new Exception("Cannot set system proxy settings before proxy has been started.");
if (proxyRunning)
QuitListen(endPoint);
}
//clear any settings previously added
ProxyEndPoints.OfType<ExplicitProxyEndPoint>().ToList().ForEach(x => x.IsSystemProxy = false);
endPoint.IsSystemProxy = true; public static void SetAsSystemHttpProxy(ExplicitProxyEndPoint endPoint)
{
VerifyProxy(endPoint);
//clear any settings previously added
ProxyEndPoints.OfType<ExplicitProxyEndPoint>().ToList().ForEach(x => x.IsSystemHttpProxy = false);
SystemProxyHelper.EnableProxyHttp( SystemProxyHelper.EnableProxyHttp(
Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Loopback) ? "127.0.0.1" : endPoint.IpAddress.ToString(), endPoint.Port); Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Loopback) ? "127.0.0.1" : endPoint.IpAddress.ToString(), endPoint.Port);
endPoint.IsSystemHttpProxy = true;
#if !DEBUG #if !DEBUG
FireFoxHelper.AddFirefox(); FireFoxHelper.AddFirefox();
#endif #endif
Console.WriteLine("Set endpoint at Ip {1} and port: {2} as System HTTPS Proxy", endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
}
public static void SetAsSystemHttpsProxy(ExplicitProxyEndPoint endPoint)
{
VerifyProxy(endPoint);
if (!endPoint.EnableSsl)
{
throw new Exception("Endpoint do not support Https connections");
}
//clear any settings previously added
ProxyEndPoints.OfType<ExplicitProxyEndPoint>().ToList().ForEach(x => x.IsSystemHttpsProxy = false);
RootCertificateName = RootCertificateName ?? "Titanium_Proxy_Test_Root";
if (endPoint.EnableSsl) //If certificate was trusted by the machine
if (certTrusted)
{ {
RootCertificateName = RootCertificateName ?? "Titanium_Proxy_Test_Root"; SystemProxyHelper.EnableProxyHttps(
Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Loopback) ? "127.0.0.1" : endPoint.IpAddress.ToString(),
//If certificate was trusted by the machine endPoint.Port);
if (certTrusted)
{
SystemProxyHelper.EnableProxyHttps(
Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Loopback) ? "127.0.0.1" : endPoint.IpAddress.ToString(),
endPoint.Port);
}
} }
Console.WriteLine("Set endpoint at Ip {1} and port: {2} as System Proxy", endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port); endPoint.IsSystemHttpsProxy = true;
#if !DEBUG
FireFoxHelper.AddFirefox();
#endif
Console.WriteLine("Set endpoint at Ip {1} and port: {2} as System HTTPS Proxy", endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
} }
public static void Start() public static void Start()
{ {
if (proxyRunning)
throw new Exception("Proxy is already running.");
EnableSsl = ProxyEndPoints.Any(x => x.EnableSsl); EnableSsl = ProxyEndPoints.Any(x => x.EnableSsl);
if (EnableSsl) if (EnableSsl)
...@@ -122,22 +148,65 @@ namespace Titanium.Web.Proxy ...@@ -122,22 +148,65 @@ namespace Titanium.Web.Proxy
foreach (var endPoint in ProxyEndPoints) foreach (var endPoint in ProxyEndPoints)
{ {
Listen(endPoint);
}
proxyRunning = true;
}
public static void Stop()
{
if (!proxyRunning)
throw new Exception("Proxy is not running.");
endPoint.listener = new TcpListener(endPoint.IpAddress, endPoint.Port); var SetAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.IsSystemHttpProxy || x.IsSystemHttpsProxy);
endPoint.listener.Start();
endPoint.Port = ((IPEndPoint)endPoint.listener.LocalEndpoint).Port; if (SetAsSystemProxy)
// accept clients asynchronously {
endPoint.listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint); SystemProxyHelper.DisableAllProxy();
#if !DEBUG
FireFoxHelper.RemoveFirefox();
#endif
}
foreach (var endPoint in ProxyEndPoints)
{
endPoint.listener.Stop();
} }
proxyStarted = true; CertManager.Dispose();
proxyRunning = false;
}
private static void Listen(ProxyEndPoint endPoint)
{
endPoint.listener = new TcpListener(endPoint.IpAddress, endPoint.Port);
endPoint.listener.Start();
endPoint.Port = ((IPEndPoint)endPoint.listener.LocalEndpoint).Port;
// accept clients asynchronously
endPoint.listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint);
}
private static void QuitListen(ProxyEndPoint endPoint)
{
endPoint.listener.Stop();
}
private static void VerifyProxy(ExplicitProxyEndPoint endPoint)
{
if (ProxyEndPoints.Contains(endPoint) == false)
throw new Exception("Cannot set endPoints not added to proxy as system proxy");
if (!proxyRunning)
throw new Exception("Cannot set system proxy settings before proxy has been started.");
} }
private static void OnAcceptConnection(IAsyncResult asyn) private static void OnAcceptConnection(IAsyncResult asyn)
{ {
var endPoint = (ProxyEndPoint)asyn.AsyncState; var endPoint = (ProxyEndPoint)asyn.AsyncState;
var client = endPoint.listener.EndAcceptTcpClient(asyn); var client = endPoint.listener.EndAcceptTcpClient(asyn);
try try
...@@ -151,30 +220,10 @@ namespace Titanium.Web.Proxy ...@@ -151,30 +220,10 @@ namespace Titanium.Web.Proxy
{ {
// ignored // ignored
} }
// Get the listener that handles the client request. // Get the listener that handles the client request.
endPoint.listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint); endPoint.listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint);
}
public static void Stop()
{
var SetAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.IsSystemProxy);
if (SetAsSystemProxy)
{
SystemProxyHelper.DisableAllProxy();
#if !DEBUG
FireFoxHelper.RemoveFirefox();
#endif
}
foreach (var endPoint in ProxyEndPoints)
{
endPoint.listener.Stop();
}
CertManager.Dispose();
} }
} }
} }
\ No newline at end of file
...@@ -53,7 +53,7 @@ namespace Titanium.Web.Proxy ...@@ -53,7 +53,7 @@ namespace Titanium.Web.Proxy
var httpVersion = httpCmdSplit[2]; var httpVersion = httpCmdSplit[2];
var excluded = endPoint.ExcludedHostNameRegex != null ? endPoint.ExcludedHostNameRegex.Any(x => Regex.IsMatch(httpRemoteUri.Host, x)) : false; var excluded = endPoint.ExcludedHttpsHostNameRegex != null ? endPoint.ExcludedHttpsHostNameRegex.Any(x => Regex.IsMatch(httpRemoteUri.Host, x)) : false;
//Client wants to create a secure tcp tunnel (its a HTTPS request) //Client wants to create a secure tcp tunnel (its a HTTPS request)
if (httpVerb.ToUpper() == "CONNECT" && !excluded && httpRemoteUri.Port != 80) if (httpVerb.ToUpper() == "CONNECT" && !excluded && httpRemoteUri.Port != 80)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment