From 47a50c37bc0a79ce190fdb80615d6f6e002b2c7e Mon Sep 17 00:00:00 2001 From: Student Main Date: Tue, 5 May 2020 12:16:59 +0800 Subject: [PATCH] no longer record proxy setting as we can restore it on shutdown --- .../Controller/System/SystemProxy.cs | 2 +- shadowsocks-csharp/Util/SystemProxy/WinINet.cs | 73 +++++----------------- 2 files changed, 16 insertions(+), 59 deletions(-) diff --git a/shadowsocks-csharp/Controller/System/SystemProxy.cs b/shadowsocks-csharp/Controller/System/SystemProxy.cs index 56137e44..25370818 100644 --- a/shadowsocks-csharp/Controller/System/SystemProxy.cs +++ b/shadowsocks-csharp/Controller/System/SystemProxy.cs @@ -15,7 +15,7 @@ namespace Shadowsocks.Controller bool global = config.global; bool enabled = config.enabled; - if (forceDisable || WinINet.operational) + if (forceDisable || !WinINet.operational) { enabled = false; } diff --git a/shadowsocks-csharp/Util/SystemProxy/WinINet.cs b/shadowsocks-csharp/Util/SystemProxy/WinINet.cs index 9814fadd..b5b275f3 100644 --- a/shadowsocks-csharp/Util/SystemProxy/WinINet.cs +++ b/shadowsocks-csharp/Util/SystemProxy/WinINet.cs @@ -1,14 +1,13 @@ -using Newtonsoft.Json; -using NLog; -using System; +using System; using System.Collections.Generic; using System.ComponentModel; -using System.IO; using System.Linq; using System.Runtime.InteropServices; +using NLog; namespace Shadowsocks.Util.SystemProxy { + #region Windows API data structure definition public enum InternetOptions { Refresh = 37, @@ -71,8 +70,6 @@ namespace Shadowsocks.Util.SystemProxy } } - - [StructLayout(LayoutKind.Sequential)] public struct InternetPerConnectionOption { @@ -117,13 +114,14 @@ namespace Shadowsocks.Util.SystemProxy } } } + #endregion public class WinINetSetting { public InternetPerConnectionFlags Flags = InternetPerConnectionFlags.Direct; - public string ProxyServer; - public string ProxyBypass; - public string AutoConfigUrl; + public string ProxyServer = string.Empty; + public string ProxyBypass = string.Empty; + public string AutoConfigUrl = string.Empty; } public class WinINet @@ -138,7 +136,7 @@ namespace Shadowsocks.Util.SystemProxy { try { - Query(); + Record(); } catch (DllNotFoundException) { @@ -158,8 +156,6 @@ namespace Shadowsocks.Util.SystemProxy throw we; } } - - Load(); } public static void ProxyGlobal(string server, string bypass) @@ -172,6 +168,7 @@ namespace Shadowsocks.Util.SystemProxy }; Exec(options); } + public static void ProxyPAC(string url) { List options = new List @@ -181,6 +178,7 @@ namespace Shadowsocks.Util.SystemProxy }; Exec(options); } + public static void Direct() { List options = new List @@ -190,42 +188,11 @@ namespace Shadowsocks.Util.SystemProxy Exec(options); } - private static void Load() - { - try - { - string configContent = File.ReadAllText(Utils.GetTempPath(SettingFile)); - initialSetting = JsonConvert.DeserializeObject(configContent); - } - catch (Exception) - { - // Suppress all exceptions. finally block will initialize new user config settings. - } - finally - { - initialSetting ??= new WinINetSetting(); - } - } - private static void Save() - { - try - { - using (StreamWriter sw = new StreamWriter(File.Open(Utils.GetTempPath(SettingFile), FileMode.Create))) - { - string jsonString = JsonConvert.SerializeObject(initialSetting, Formatting.Indented); - sw.Write(jsonString); - sw.Flush(); - } - } - catch (IOException) - { - // logger.LogUsefulException(e); - } - } private static void Record() { initialSetting ??= Query(); } + public static void Restore() { Set(initialSetting); @@ -242,17 +209,13 @@ namespace Shadowsocks.Util.SystemProxy }; Exec(options); } + public static void Reset() { - Set(new WinINetSetting - { - Flags = InternetPerConnectionFlags.Direct, - ProxyServer = "", - ProxyBypass = "", - AutoConfigUrl = "", - }); + Set(new WinINetSetting()); } + #region Windows API wrapper public static WinINetSetting Query() { if (!operational) @@ -380,17 +343,11 @@ namespace Shadowsocks.Util.SystemProxy private static void Exec(List options) { - // TODO: optimize load and save - Load(); - Record(); - Exec(options, null); foreach (string conn in RAS.GetAllConnections()) { Exec(options, conn); } - - Save(); } private static void Exec(List options, string connName) @@ -442,6 +399,6 @@ namespace Shadowsocks.Util.SystemProxy [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool InternetQueryOption(IntPtr hInternet, uint dwOption, IntPtr lpBuffer, ref int lpdwBufferLength); - + #endregion } }