Commit 3da0c593 authored by titanium007's avatar titanium007

Rewrite system proxy set

parent de0ec9c4
......@@ -20,20 +20,22 @@ namespace Titanium.Web.Proxy.Test
//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" }
ExcludedHttpsHostNameRegex = new List<string>() { "dropbox.com" }
};
var transparentEndPoint = new TransparentProxyEndPoint(IPAddress.Loopback, 8001, true);
ProxyServer.AddEndPoint(explicitEndPoint);
ProxyServer.AddEndPoint(transparentEndPoint);
ProxyServer.Start();
//You can also add/remove end points after proxy has been started
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);
ProxyServer.SetAsSystemProxy(explicitEndPoint);
ProxyServer.SetAsSystemHttpProxy(explicitEndPoint);
ProxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
}
public void Stop()
......
......@@ -25,8 +25,10 @@ namespace Titanium.Web.Proxy.Models
public class ExplicitProxyEndPoint : ProxyEndPoint
{
internal bool IsSystemProxy { get; set; }
public List<string> ExcludedHostNameRegex { get; set; }
internal bool IsSystemHttpProxy { get; set; }
internal bool IsSystemHttpsProxy { get; set; }
public List<string> ExcludedHttpsHostNameRegex { get; set; }
public ExplicitProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl)
: base(IpAddress, Port, EnableSsl)
......
......@@ -35,7 +35,7 @@ namespace Titanium.Web.Proxy
Encoding.ASCII.GetBytes(0.ToString("x2") + Environment.NewLine + Environment.NewLine);
#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
public static SslProtocols SupportedProtocols = SslProtocols.Tls | SslProtocols.Ssl3;
#endif
......@@ -53,7 +53,7 @@ namespace Titanium.Web.Proxy
private static CertificateManager CertManager { get; set; }
private static bool EnableSsl { 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; }
......@@ -69,35 +69,55 @@ namespace Titanium.Web.Proxy
public static void AddEndPoint(ProxyEndPoint endPoint)
{
if (proxyStarted)
throw new Exception("Cannot add end points after proxy started.");
ProxyEndPoints.Add(endPoint);
}
if (proxyRunning)
Listen(endPoint);
}
public static void SetAsSystemProxy(ExplicitProxyEndPoint endPoint)
public static void RemoveEndPoint(ProxyEndPoint endPoint)
{
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)
throw new Exception("Cannot set system proxy settings before proxy has been started.");
ProxyEndPoints.Remove(endPoint);
//clear any settings previously added
ProxyEndPoints.OfType<ExplicitProxyEndPoint>().ToList().ForEach(x => x.IsSystemProxy = false);
if (proxyRunning)
QuitListen(endPoint);
}
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(
Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Loopback) ? "127.0.0.1" : endPoint.IpAddress.ToString(), endPoint.Port);
endPoint.IsSystemHttpProxy = 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 SetAsSystemHttpsProxy(ExplicitProxyEndPoint endPoint)
{
VerifyProxy(endPoint);
if (endPoint.EnableSsl)
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 certificate was trusted by the machine
......@@ -107,14 +127,20 @@ namespace Titanium.Web.Proxy
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()
{
if (proxyRunning)
throw new Exception("Proxy is already running.");
EnableSsl = ProxyEndPoints.Any(x => x.EnableSsl);
if (EnableSsl)
......@@ -122,7 +148,39 @@ namespace Titanium.Web.Proxy
foreach (var endPoint in ProxyEndPoints)
{
Listen(endPoint);
}
proxyRunning = true;
}
public static void Stop()
{
if (!proxyRunning)
throw new Exception("Proxy is not running.");
var SetAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.IsSystemHttpProxy || x.IsSystemHttpsProxy);
if (SetAsSystemProxy)
{
SystemProxyHelper.DisableAllProxy();
#if !DEBUG
FireFoxHelper.RemoveFirefox();
#endif
}
foreach (var endPoint in ProxyEndPoints)
{
endPoint.listener.Stop();
}
CertManager.Dispose();
proxyRunning = false;
}
private static void Listen(ProxyEndPoint endPoint)
{
endPoint.listener = new TcpListener(endPoint.IpAddress, endPoint.Port);
endPoint.listener.Start();
......@@ -131,13 +189,24 @@ namespace Titanium.Web.Proxy
endPoint.listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint);
}
proxyStarted = true;
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)
{
var endPoint = (ProxyEndPoint)asyn.AsyncState;
var client = endPoint.listener.EndAcceptTcpClient(asyn);
try
......@@ -151,30 +220,10 @@ namespace Titanium.Web.Proxy
{
// ignored
}
// Get the listener that handles the client request.
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
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)
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