From cfc6e674daf2c247d18c45eb3f30fe38613a5f79 Mon Sep 17 00:00:00 2001 From: noisyfox Date: Sat, 12 Nov 2016 19:38:47 +1100 Subject: [PATCH] Catch and log known exceptions in SystemProxy --- .../Controller/System/SystemProxy.cs | 29 ++++++++++++------- .../Util/SystemProxy/ProxyException.cs | 28 ++++++++++++++++++ .../Util/SystemProxy/WinINet.cs | 5 ++-- shadowsocks-csharp/shadowsocks-csharp.csproj | 1 + 4 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 shadowsocks-csharp/Util/SystemProxy/ProxyException.cs diff --git a/shadowsocks-csharp/Controller/System/SystemProxy.cs b/shadowsocks-csharp/Controller/System/SystemProxy.cs index b932b240..b06b00c6 100644 --- a/shadowsocks-csharp/Controller/System/SystemProxy.cs +++ b/shadowsocks-csharp/Controller/System/SystemProxy.cs @@ -21,25 +21,32 @@ namespace Shadowsocks.Controller enabled = false; } - if (enabled) + try { - if (global) + if (enabled) { - WinINet.SetIEProxy(true, true, "127.0.0.1:" + config.localPort.ToString(), ""); + if (global) + { + WinINet.SetIEProxy(true, true, "127.0.0.1:" + config.localPort.ToString(), ""); + } + else + { + string pacUrl; + if (config.useOnlinePac && !config.pacUrl.IsNullOrEmpty()) + pacUrl = config.pacUrl; + else + pacUrl = $"http://127.0.0.1:{config.localPort}/pac?t={GetTimestamp(DateTime.Now)}"; + WinINet.SetIEProxy(true, false, "", pacUrl); + } } else { - string pacUrl; - if (config.useOnlinePac && !config.pacUrl.IsNullOrEmpty()) - pacUrl = config.pacUrl; - else - pacUrl = $"http://127.0.0.1:{config.localPort}/pac?t={GetTimestamp(DateTime.Now)}"; - WinINet.SetIEProxy(true, false, "", pacUrl); + WinINet.SetIEProxy(false, false, "", ""); } } - else + catch (ProxyException ex) { - WinINet.SetIEProxy(false, false, "", ""); + Logging.LogUsefulException(ex); } } } diff --git a/shadowsocks-csharp/Util/SystemProxy/ProxyException.cs b/shadowsocks-csharp/Util/SystemProxy/ProxyException.cs new file mode 100644 index 00000000..67a00e2c --- /dev/null +++ b/shadowsocks-csharp/Util/SystemProxy/ProxyException.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace Shadowsocks.Util.SystemProxy +{ + class ProxyException : Exception + { + public ProxyException() + { + } + + public ProxyException(string message) : base(message) + { + } + + public ProxyException(string message, Exception innerException) : base(message, innerException) + { + } + + protected ProxyException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } +} diff --git a/shadowsocks-csharp/Util/SystemProxy/WinINet.cs b/shadowsocks-csharp/Util/SystemProxy/WinINet.cs index c5dddee9..184f47e7 100644 --- a/shadowsocks-csharp/Util/SystemProxy/WinINet.cs +++ b/shadowsocks-csharp/Util/SystemProxy/WinINet.cs @@ -17,6 +17,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Runtime.InteropServices; using Shadowsocks.Controller; @@ -131,7 +132,7 @@ namespace Shadowsocks.Util.SystemProxy // Throw an exception if this operation failed. if (!bReturn) { - throw new Exception("InternetSetOption: " + Marshal.GetLastWin32Error()); + throw new ProxyException("InternetSetOption failed.", new Win32Exception()); } // Notify the system that the registry settings have been changed and cause @@ -161,7 +162,7 @@ namespace Shadowsocks.Util.SystemProxy var ret = RemoteAccessService.GetAllConns(ref allConnections); if (ret == 2) - throw new Exception("Cannot get all connections"); + throw new ProxyException("Cannot get all connections"); if (ret == 1) { diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index 26ea4fd2..d1881c87 100644 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -191,6 +191,7 @@ +