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 ...@@ -14,10 +14,6 @@ namespace Titanium.Web.Proxy.Examples.Basic
//fix console hang due to QuickEdit mode //fix console hang due to QuickEdit mode
ConsoleHelper.DisableQuickEditMode(); ConsoleHelper.DisableQuickEditMode();
//On Console exit make sure we also exit the proxy
NativeMethods.Handler = ConsoleEventCallback;
NativeMethods.SetConsoleCtrlHandler(NativeMethods.Handler, true);
//Start proxy controller //Start proxy controller
controller.StartProxy(); controller.StartProxy();
...@@ -27,34 +23,5 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -27,34 +23,5 @@ namespace Titanium.Web.Proxy.Examples.Basic
controller.Stop(); 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 ...@@ -37,6 +37,18 @@ namespace Titanium.Web.Proxy.Helpers
[DllImport("wininet.dll")] [DllImport("wininet.dll")]
internal static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, internal static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer,
int dwBufferLength); 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 internal class HttpSystemProxyValue
...@@ -47,7 +59,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -47,7 +59,7 @@ namespace Titanium.Web.Proxy.Helpers
public override string ToString() public override string ToString()
{ {
string protocol = null; string protocol;
switch (ProtocolType) switch (ProtocolType)
{ {
case ProxyProtocolType.Http: case ProxyProtocolType.Http:
...@@ -59,6 +71,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -59,6 +71,7 @@ namespace Titanium.Web.Proxy.Helpers
default: default:
throw new Exception("Unsupported protocol type"); throw new Exception("Unsupported protocol type");
} }
return $"{protocol}={HostName}:{Port}"; return $"{protocol}={HostName}:{Port}";
} }
} }
...@@ -71,6 +84,31 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -71,6 +84,31 @@ namespace Titanium.Web.Proxy.Helpers
internal const int InternetOptionSettingsChanged = 39; internal const int InternetOptionSettingsChanged = 39;
internal const int InternetOptionRefresh = 37; 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> /// <summary>
/// Set the HTTP and/or HTTPS proxy server for current machine /// Set the HTTP and/or HTTPS proxy server for current machine
/// </summary> /// </summary>
...@@ -84,6 +122,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -84,6 +122,7 @@ namespace Titanium.Web.Proxy.Helpers
if (reg != null) if (reg != null)
{ {
SaveOriginalProxyConfiguration(reg);
PrepareRegistry(reg); PrepareRegistry(reg);
var exisitingContent = reg.GetValue("ProxyServer") as string; var exisitingContent = reg.GetValue("ProxyServer") as string;
...@@ -111,9 +150,9 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -111,9 +150,9 @@ namespace Titanium.Web.Proxy.Helpers
reg.SetValue("ProxyEnable", 1); reg.SetValue("ProxyEnable", 1);
reg.SetValue("ProxyServer", string.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray())); reg.SetValue("ProxyServer", string.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray()));
}
Refresh(); Refresh();
}
} }
/// <summary> /// <summary>
...@@ -123,26 +162,31 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -123,26 +162,31 @@ 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?.GetValue("ProxyServer") != null) if (reg != null)
{ {
var exisitingContent = reg.GetValue("ProxyServer") as string; SaveOriginalProxyConfiguration(reg);
var existingSystemProxyValues = GetSystemProxyValues(exisitingContent);
existingSystemProxyValues.RemoveAll(x => (protocolType & x.ProtocolType) != 0);
if (existingSystemProxyValues.Count != 0) if (reg.GetValue("ProxyServer") != null)
{
reg.SetValue("ProxyEnable", 1);
reg.SetValue("ProxyServer", string.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray()));
}
else
{ {
reg.SetValue("ProxyEnable", 0); var exisitingContent = reg.GetValue("ProxyServer") as string;
reg.SetValue("ProxyServer", string.Empty);
var existingSystemProxyValues = GetSystemProxyValues(exisitingContent);
existingSystemProxyValues.RemoveAll(x => (protocolType & x.ProtocolType) != 0);
if (existingSystemProxyValues.Count != 0)
{
reg.SetValue("ProxyEnable", 1);
reg.SetValue("ProxyServer", string.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray()));
}
else
{
reg.SetValue("ProxyEnable", 0);
reg.SetValue("ProxyServer", string.Empty);
}
} }
}
Refresh(); Refresh();
}
} }
/// <summary> /// <summary>
...@@ -155,11 +199,55 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -155,11 +199,55 @@ namespace Titanium.Web.Proxy.Helpers
if (reg != null) if (reg != null)
{ {
SaveOriginalProxyConfiguration(reg);
reg.SetValue("ProxyEnable", 0); reg.SetValue("ProxyEnable", 0);
reg.SetValue("ProxyServer", string.Empty); 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();
} }
}
Refresh(); private void SaveOriginalProxyConfiguration(RegistryKey reg)
{
originalProxyServer = reg.GetValue("ProxyServer") as string;
originalProxyEnable = reg.GetValue("ProxyEnable") as int?;
originalValuesLoaded = true;
} }
/// <summary> /// <summary>
...@@ -239,7 +327,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -239,7 +327,7 @@ namespace Titanium.Web.Proxy.Helpers
reg.SetValue("ProxyEnable", 0); 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); reg.SetValue("ProxyServer", string.Empty);
} }
......
...@@ -287,9 +287,11 @@ namespace Titanium.Web.Proxy ...@@ -287,9 +287,11 @@ namespace Titanium.Web.Proxy
ProxyEndPoints = new List<ProxyEndPoint>(); ProxyEndPoints = new List<ProxyEndPoint>();
tcpConnectionFactory = new TcpConnectionFactory(); tcpConnectionFactory = new TcpConnectionFactory();
systemProxySettingsManager = new SystemProxyManager(); if (!RunTime.IsRunningOnMono)
{
systemProxySettingsManager = new SystemProxyManager();
}
CertificateManager = new CertificateManager(ExceptionFunc); CertificateManager = new CertificateManager(ExceptionFunc);
if (rootCertificateName != null) if (rootCertificateName != null)
{ {
...@@ -530,11 +532,14 @@ namespace Titanium.Web.Proxy ...@@ -530,11 +532,14 @@ namespace Titanium.Web.Proxy
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); if (!RunTime.IsRunningOnMono)
if (setAsSystemProxy)
{ {
systemProxySettingsManager.DisableAllProxy(); var setAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.IsSystemHttpProxy || x.IsSystemHttpsProxy);
if (setAsSystemProxy)
{
systemProxySettingsManager.RestoreOriginalSettings();
}
} }
foreach (var endPoint in ProxyEndPoints) 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