Commit e312b9bb authored by titanium007's avatar titanium007

cleanup proxy settings

parent 699956e3
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Microsoft.Win32; using Microsoft.Win32;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
namespace Titanium.Web.Proxy.Helpers namespace Titanium.Web.Proxy.Helpers
{ {
...@@ -11,36 +14,114 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -11,36 +14,114 @@ namespace Titanium.Web.Proxy.Helpers
int dwBufferLength); int dwBufferLength);
} }
internal class HttpSystemProxyValue
{
public string HostName { get; set; }
public int Port { get; set; }
public bool IsSecure { get; set; }
public override string ToString()
{
if (!IsSecure)
return "http=" + HostName + ":" + Port;
else
return "https=" + HostName + ":" + Port;
}
}
public static class SystemProxyHelper public static class SystemProxyHelper
{ {
public const int InternetOptionSettingsChanged = 39; public const int InternetOptionSettingsChanged = 39;
public const int InternetOptionRefresh = 37; public const int InternetOptionRefresh = 37;
private static object _prevProxyServer;
private static object _prevProxyEnable;
public static void EnableProxyHttp(string hostname, int port) public static void SetHttpProxy(string hostname, int port)
{ {
var reg = Registry.CurrentUser.OpenSubKey( var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
if (reg != null) if (reg != null)
{ {
_prevProxyEnable = reg.GetValue("ProxyEnable"); prepareRegistry(reg);
_prevProxyServer = reg.GetValue("ProxyServer");
var exisitingContent = reg.GetValue("ProxyServer") as string;
var existingSystemProxyValues = GetSystemProxyValues(exisitingContent);
existingSystemProxyValues.RemoveAll(x => !x.IsSecure);
existingSystemProxyValues.Add(new HttpSystemProxyValue()
{
HostName = hostname,
IsSecure = false,
Port = port
});
reg.SetValue("ProxyEnable", 1);
reg.SetValue("ProxyServer", String.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray()));
}
Refresh();
}
public static void RemoveHttpProxy()
{
var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
if (reg != null)
{
var exisitingContent = reg.GetValue("ProxyServer") as string;
var existingSystemProxyValues = GetSystemProxyValues(exisitingContent);
existingSystemProxyValues.RemoveAll(x => !x.IsSecure);
reg.SetValue("ProxyEnable", 1); reg.SetValue("ProxyEnable", 1);
reg.SetValue("ProxyServer", "http=" + hostname + ":" + port + ";"); reg.SetValue("ProxyServer", String.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray()));
} }
Refresh(); Refresh();
} }
public static void EnableProxyHttps(string hostname, int port) public static void SetHttpsProxy(string hostname, int port)
{ {
var reg = Registry.CurrentUser.OpenSubKey( var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
if (reg != null)
{
prepareRegistry(reg);
var exisitingContent = reg.GetValue("ProxyServer") as string;
var existingSystemProxyValues = GetSystemProxyValues(exisitingContent);
existingSystemProxyValues.RemoveAll(x => x.IsSecure);
existingSystemProxyValues.Add(new HttpSystemProxyValue()
{
HostName = hostname,
IsSecure = true,
Port = port
});
reg.SetValue("ProxyEnable", 1);
reg.SetValue("ProxyServer", String.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray()));
}
Refresh();
}
public static void RemoveHttpsProxy()
{
var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
if (reg != null) if (reg != null)
{ {
var exisitingContent = reg.GetValue("ProxyServer") as string;
var existingSystemProxyValues = GetSystemProxyValues(exisitingContent);
existingSystemProxyValues.RemoveAll(x => x.IsSecure);
reg.SetValue("ProxyEnable", 1); reg.SetValue("ProxyEnable", 1);
reg.SetValue("ProxyServer", "http=" + hostname + ":" + port + ";https=" + hostname + ":" + port); reg.SetValue("ProxyServer", String.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray()));
} }
Refresh(); Refresh();
} }
...@@ -48,18 +129,88 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -48,18 +129,88 @@ namespace Titanium.Web.Proxy.Helpers
{ {
var reg = Registry.CurrentUser.OpenSubKey( var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
if (reg != null) if (reg != null)
{ {
reg.SetValue("ProxyEnable", _prevProxyEnable); reg.SetValue("ProxyEnable", 0);
if (_prevProxyServer != null) reg.SetValue("ProxyServer", string.Empty);
reg.SetValue("ProxyServer", _prevProxyServer);
} }
Refresh(); Refresh();
} }
private static List<HttpSystemProxyValue> GetSystemProxyValues(string prevServerValue)
{
var result = new List<HttpSystemProxyValue>();
if (string.IsNullOrWhiteSpace(prevServerValue))
return result;
var proxyValues = prevServerValue.Split(';');
if (proxyValues.Length > 0)
{
foreach (var value in proxyValues)
{
var parsedValue = parseProxyValue(value);
if (parsedValue != null)
result.Add(parsedValue);
}
}
else
{
var parsedValue = parseProxyValue(prevServerValue);
if (parsedValue != null)
result.Add(parsedValue);
}
return result;
}
private static HttpSystemProxyValue parseProxyValue(string value)
{
var tmp = Regex.Replace(value, @"\s+", " ").Trim().ToLower();
if (tmp.StartsWith("http="))
{
var endPoint = tmp.Substring(5);
return new HttpSystemProxyValue()
{
HostName = endPoint.Split(':')[0],
Port = int.Parse(endPoint.Split(':')[1]),
IsSecure = false
};
}
else if (tmp.StartsWith("https="))
{
var endPoint = tmp.Substring(5);
return new HttpSystemProxyValue()
{
HostName = endPoint.Split(':')[0],
Port = int.Parse(endPoint.Split(':')[1]),
IsSecure = false
};
}
return null;
}
private static void prepareRegistry(RegistryKey reg)
{
if (reg.GetValue("ProxyEnable") == null)
{
reg.SetValue("ProxyEnable", 0);
}
if (reg.GetValue("ProxyServer") == null || reg.GetValue("ProxyEnable") as string == "0")
{
reg.SetValue("ProxyServer", string.Empty);
}
}
private static void Refresh() private static void Refresh()
{ {
NativeMethods.InternetSetOption(IntPtr.Zero, InternetOptionSettingsChanged, IntPtr.Zero,0); NativeMethods.InternetSetOption(IntPtr.Zero, InternetOptionSettingsChanged, IntPtr.Zero, 0);
NativeMethods.InternetSetOption(IntPtr.Zero, InternetOptionRefresh, IntPtr.Zero, 0); NativeMethods.InternetSetOption(IntPtr.Zero, InternetOptionRefresh, IntPtr.Zero, 0);
} }
} }
......
...@@ -95,7 +95,7 @@ namespace Titanium.Web.Proxy ...@@ -95,7 +95,7 @@ namespace Titanium.Web.Proxy
//clear any settings previously added //clear any settings previously added
ProxyEndPoints.OfType<ExplicitProxyEndPoint>().ToList().ForEach(x => x.IsSystemHttpProxy = false); ProxyEndPoints.OfType<ExplicitProxyEndPoint>().ToList().ForEach(x => x.IsSystemHttpProxy = false);
SystemProxyHelper.EnableProxyHttp( SystemProxyHelper.SetHttpProxy(
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; endPoint.IsSystemHttpProxy = true;
...@@ -106,6 +106,11 @@ namespace Titanium.Web.Proxy ...@@ -106,6 +106,11 @@ namespace Titanium.Web.Proxy
} }
public static void DisableSystemHttpProxy()
{
SystemProxyHelper.RemoveHttpProxy();
}
public static void SetAsSystemHttpsProxy(ExplicitProxyEndPoint endPoint) public static void SetAsSystemHttpsProxy(ExplicitProxyEndPoint endPoint)
{ {
VerifyProxy(endPoint); VerifyProxy(endPoint);
...@@ -123,7 +128,7 @@ namespace Titanium.Web.Proxy ...@@ -123,7 +128,7 @@ namespace Titanium.Web.Proxy
//If certificate was trusted by the machine //If certificate was trusted by the machine
if (certTrusted) if (certTrusted)
{ {
SystemProxyHelper.EnableProxyHttps( SystemProxyHelper.SetHttpsProxy(
Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Loopback) ? "127.0.0.1" : endPoint.IpAddress.ToString(), Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Loopback) ? "127.0.0.1" : endPoint.IpAddress.ToString(),
endPoint.Port); endPoint.Port);
} }
...@@ -136,6 +141,16 @@ namespace Titanium.Web.Proxy ...@@ -136,6 +141,16 @@ namespace Titanium.Web.Proxy
Console.WriteLine("Set endpoint at Ip {1} and port: {2} as System HTTPS Proxy", endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port); Console.WriteLine("Set endpoint at Ip {1} and port: {2} as System HTTPS Proxy", endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
} }
public static void DisableSystemHttpsProxy()
{
SystemProxyHelper.RemoveHttpsProxy();
}
public static void DisableAllSystemProxies()
{
SystemProxyHelper.DisableAllProxy();
}
public static void Start() public static void Start()
{ {
if (proxyRunning) if (proxyRunning)
...@@ -159,9 +174,9 @@ namespace Titanium.Web.Proxy ...@@ -159,9 +174,9 @@ namespace Titanium.Web.Proxy
if (!proxyRunning) if (!proxyRunning)
throw new Exception("Proxy is not running."); throw new Exception("Proxy is not running.");
var SetAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.IsSystemHttpProxy || x.IsSystemHttpsProxy); var setAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.IsSystemHttpProxy || x.IsSystemHttpsProxy);
if (SetAsSystemProxy) if (setAsSystemProxy)
{ {
SystemProxyHelper.DisableAllProxy(); SystemProxyHelper.DisableAllProxy();
#if !DEBUG #if !DEBUG
...@@ -207,22 +222,24 @@ namespace Titanium.Web.Proxy ...@@ -207,22 +222,24 @@ namespace Titanium.Web.Proxy
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);
try try
{ {
var client = endPoint.listener.EndAcceptTcpClient(asyn);
if (endPoint.GetType() == typeof(TransparentProxyEndPoint)) if (endPoint.GetType() == typeof(TransparentProxyEndPoint))
Task.Factory.StartNew(() => HandleClient(endPoint as TransparentProxyEndPoint, client)); Task.Factory.StartNew(() => HandleClient(endPoint as TransparentProxyEndPoint, client));
else else
Task.Factory.StartNew(() => HandleClient(endPoint as ExplicitProxyEndPoint, client)); Task.Factory.StartNew(() => HandleClient(endPoint as ExplicitProxyEndPoint, client));
// Get the listener that handles the client request.
endPoint.listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint);
} }
catch catch
{ {
// ignored // ignored
} }
// Get the listener that handles the client request.
endPoint.listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint);
} }
} }
......
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