Browse Source

no longer record proxy setting as we can restore it on shutdown

pull/2895/head
Student Main 5 years ago
parent
commit
47a50c37bc
2 changed files with 16 additions and 59 deletions
  1. +1
    -1
      shadowsocks-csharp/Controller/System/SystemProxy.cs
  2. +15
    -58
      shadowsocks-csharp/Util/SystemProxy/WinINet.cs

+ 1
- 1
shadowsocks-csharp/Controller/System/SystemProxy.cs View File

@@ -15,7 +15,7 @@ namespace Shadowsocks.Controller
bool global = config.global; bool global = config.global;
bool enabled = config.enabled; bool enabled = config.enabled;
if (forceDisable || WinINet.operational)
if (forceDisable || !WinINet.operational)
{ {
enabled = false; enabled = false;
} }


+ 15
- 58
shadowsocks-csharp/Util/SystemProxy/WinINet.cs View File

@@ -1,14 +1,13 @@
using Newtonsoft.Json;
using NLog;
using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using NLog;


namespace Shadowsocks.Util.SystemProxy namespace Shadowsocks.Util.SystemProxy
{ {
#region Windows API data structure definition
public enum InternetOptions public enum InternetOptions
{ {
Refresh = 37, Refresh = 37,
@@ -71,8 +70,6 @@ namespace Shadowsocks.Util.SystemProxy
} }
} }




[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct InternetPerConnectionOption public struct InternetPerConnectionOption
{ {
@@ -117,13 +114,14 @@ namespace Shadowsocks.Util.SystemProxy
} }
} }
} }
#endregion


public class WinINetSetting public class WinINetSetting
{ {
public InternetPerConnectionFlags Flags = InternetPerConnectionFlags.Direct; 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 public class WinINet
@@ -138,7 +136,7 @@ namespace Shadowsocks.Util.SystemProxy
{ {
try try
{ {
Query();
Record();
} }
catch (DllNotFoundException) catch (DllNotFoundException)
{ {
@@ -158,8 +156,6 @@ namespace Shadowsocks.Util.SystemProxy
throw we; throw we;
} }
} }

Load();
} }


public static void ProxyGlobal(string server, string bypass) public static void ProxyGlobal(string server, string bypass)
@@ -172,6 +168,7 @@ namespace Shadowsocks.Util.SystemProxy
}; };
Exec(options); Exec(options);
} }

public static void ProxyPAC(string url) public static void ProxyPAC(string url)
{ {
List<InternetPerConnectionOption> options = new List<InternetPerConnectionOption> List<InternetPerConnectionOption> options = new List<InternetPerConnectionOption>
@@ -181,6 +178,7 @@ namespace Shadowsocks.Util.SystemProxy
}; };
Exec(options); Exec(options);
} }

public static void Direct() public static void Direct()
{ {
List<InternetPerConnectionOption> options = new List<InternetPerConnectionOption> List<InternetPerConnectionOption> options = new List<InternetPerConnectionOption>
@@ -190,42 +188,11 @@ namespace Shadowsocks.Util.SystemProxy
Exec(options); Exec(options);
} }


private static void Load()
{
try
{
string configContent = File.ReadAllText(Utils.GetTempPath(SettingFile));
initialSetting = JsonConvert.DeserializeObject<WinINetSetting>(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() private static void Record()
{ {
initialSetting ??= Query(); initialSetting ??= Query();
} }

public static void Restore() public static void Restore()
{ {
Set(initialSetting); Set(initialSetting);
@@ -242,17 +209,13 @@ namespace Shadowsocks.Util.SystemProxy
}; };
Exec(options); Exec(options);
} }

public static void Reset() public static void Reset()
{ {
Set(new WinINetSetting
{
Flags = InternetPerConnectionFlags.Direct,
ProxyServer = "",
ProxyBypass = "",
AutoConfigUrl = "",
});
Set(new WinINetSetting());
} }


#region Windows API wrapper
public static WinINetSetting Query() public static WinINetSetting Query()
{ {
if (!operational) if (!operational)
@@ -380,17 +343,11 @@ namespace Shadowsocks.Util.SystemProxy


private static void Exec(List<InternetPerConnectionOption> options) private static void Exec(List<InternetPerConnectionOption> options)
{ {
// TODO: optimize load and save
Load();
Record();

Exec(options, null); Exec(options, null);
foreach (string conn in RAS.GetAllConnections()) foreach (string conn in RAS.GetAllConnections())
{ {
Exec(options, conn); Exec(options, conn);
} }

Save();
} }


private static void Exec(List<InternetPerConnectionOption> options, string connName) private static void Exec(List<InternetPerConnectionOption> options, string connName)
@@ -442,6 +399,6 @@ namespace Shadowsocks.Util.SystemProxy


[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool InternetQueryOption(IntPtr hInternet, uint dwOption, IntPtr lpBuffer, ref int lpdwBufferLength); private static extern bool InternetQueryOption(IntPtr hInternet, uint dwOption, IntPtr lpBuffer, ref int lpdwBufferLength);
#endregion
} }
} }

Loading…
Cancel
Save