Commit cdc148c0 authored by Honfika's avatar Honfika

Restore proxy settings on proxy stop/exit

parent 89710bf8
......@@ -14,10 +14,6 @@ namespace Titanium.Web.Proxy.Examples.Basic
//fix console hang due to QuickEdit mode
ConsoleHelper.DisableQuickEditMode();
//On Console exit make sure we also exit the proxy
NativeMethods.Handler = ConsoleEventCallback;
NativeMethods.SetConsoleCtrlHandler(NativeMethods.Handler, true);
//Start proxy controller
controller.StartProxy();
......@@ -27,34 +23,5 @@ namespace Titanium.Web.Proxy.Examples.Basic
controller.Stop();
}
private static bool ConsoleEventCallback(int eventType)
{
if (eventType != 2) return false;
try
{
controller.Stop();
}
catch
{
// ignored
}
return false;
}
}
internal static class NativeMethods
{
// Keeps it from getting garbage collected
internal static ConsoleEventDelegate Handler;
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
// Pinvoke
internal delegate bool ConsoleEventDelegate(int eventType);
}
}
......@@ -37,6 +37,18 @@ namespace Titanium.Web.Proxy.Helpers
[DllImport("wininet.dll")]
internal static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer,
int dwBufferLength);
[DllImport("kernel32.dll")]
internal static extern IntPtr GetConsoleWindow();
// Keeps it from getting garbage collected
internal static ConsoleEventDelegate Handler;
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
// Pinvoke
internal delegate bool ConsoleEventDelegate(int eventType);
}
internal class HttpSystemProxyValue
......@@ -47,7 +59,7 @@ namespace Titanium.Web.Proxy.Helpers
public override string ToString()
{
string protocol = null;
string protocol;
switch (ProtocolType)
{
case ProxyProtocolType.Http:
......@@ -59,6 +71,7 @@ namespace Titanium.Web.Proxy.Helpers
default:
throw new Exception("Unsupported protocol type");
}
return $"{protocol}={HostName}:{Port}";
}
}
......@@ -71,6 +84,31 @@ namespace Titanium.Web.Proxy.Helpers
internal const int InternetOptionSettingsChanged = 39;
internal const int InternetOptionRefresh = 37;
private bool originalValuesLoaded;
private int? originalProxyEnable;
private string originalProxyServer;
public SystemProxyManager()
{
AppDomain.CurrentDomain.ProcessExit += (o, args) => RestoreOriginalSettings();
if (Environment.UserInteractive && NativeMethods.GetConsoleWindow() != IntPtr.Zero)
{
NativeMethods.Handler = eventType =>
{
if (eventType != 2)
{
return false;
}
RestoreOriginalSettings();
return false;
};
//On Console exit make sure we also exit the proxy
NativeMethods.SetConsoleCtrlHandler(NativeMethods.Handler, true);
}
}
/// <summary>
/// Set the HTTP and/or HTTPS proxy server for current machine
/// </summary>
......@@ -84,6 +122,7 @@ namespace Titanium.Web.Proxy.Helpers
if (reg != null)
{
SaveOriginalProxyConfiguration(reg);
PrepareRegistry(reg);
var exisitingContent = reg.GetValue("ProxyServer") as string;
......@@ -111,10 +150,10 @@ namespace Titanium.Web.Proxy.Helpers
reg.SetValue("ProxyEnable", 1);
reg.SetValue("ProxyServer", string.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray()));
}
Refresh();
}
}
/// <summary>
/// Remove the HTTP and/or HTTPS proxy setting from current machine
......@@ -123,7 +162,11 @@ namespace Titanium.Web.Proxy.Helpers
{
var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
if (reg?.GetValue("ProxyServer") != null)
if (reg != null)
{
SaveOriginalProxyConfiguration(reg);
if (reg.GetValue("ProxyServer") != null)
{
var exisitingContent = reg.GetValue("ProxyServer") as string;
......@@ -144,6 +187,7 @@ namespace Titanium.Web.Proxy.Helpers
Refresh();
}
}
/// <summary>
/// Removes all types of proxy settings (both http and https)
......@@ -155,12 +199,56 @@ namespace Titanium.Web.Proxy.Helpers
if (reg != null)
{
SaveOriginalProxyConfiguration(reg);
reg.SetValue("ProxyEnable", 0);
reg.SetValue("ProxyServer", string.Empty);
Refresh();
}
}
internal void RestoreOriginalSettings()
{
if (!originalValuesLoaded)
{
return;
}
var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
if (reg != null)
{
if (originalProxyEnable.HasValue)
{
reg.SetValue("ProxyEnable", originalProxyEnable.Value);
}
else if (reg.GetValue("ProxyEnable") != null)
{
reg.DeleteValue("ProxyEnable");
}
if (originalProxyServer != null)
{
reg.SetValue("ProxyServer", originalProxyServer);
}
else if (reg.GetValue("ProxyServer") != null)
{
reg.DeleteValue("ProxyServer");
}
originalValuesLoaded = false;
Refresh();
}
}
private void SaveOriginalProxyConfiguration(RegistryKey reg)
{
originalProxyServer = reg.GetValue("ProxyServer") as string;
originalProxyEnable = reg.GetValue("ProxyEnable") as int?;
originalValuesLoaded = true;
}
/// <summary>
/// Get the current system proxy setting values
......@@ -239,7 +327,7 @@ namespace Titanium.Web.Proxy.Helpers
reg.SetValue("ProxyEnable", 0);
}
if (reg.GetValue("ProxyServer") == null || reg.GetValue("ProxyEnable") as string == "0")
if (reg.GetValue("ProxyServer") == null || reg.GetValue("ProxyEnable") as int? == 0)
{
reg.SetValue("ProxyServer", string.Empty);
}
......
......@@ -287,8 +287,10 @@ namespace Titanium.Web.Proxy
ProxyEndPoints = new List<ProxyEndPoint>();
tcpConnectionFactory = new TcpConnectionFactory();
if (!RunTime.IsRunningOnMono)
{
systemProxySettingsManager = new SystemProxyManager();
}
CertificateManager = new CertificateManager(ExceptionFunc);
if (rootCertificateName != null)
......@@ -530,11 +532,14 @@ namespace Titanium.Web.Proxy
throw new Exception("Proxy is not running.");
}
if (!RunTime.IsRunningOnMono)
{
var setAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.IsSystemHttpProxy || x.IsSystemHttpsProxy);
if (setAsSystemProxy)
{
systemProxySettingsManager.DisableAllProxy();
systemProxySettingsManager.RestoreOriginalSettings();
}
}
foreach (var endPoint in ProxyEndPoints)
......
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