diff --git a/v2rayN/v2rayN/Common/ProxySetting.cs b/v2rayN/PacLib/ProxySettingWindows.cs similarity index 88% rename from v2rayN/v2rayN/Common/ProxySetting.cs rename to v2rayN/PacLib/ProxySettingWindows.cs index 1b102f5c..bbcb3775 100644 --- a/v2rayN/v2rayN/Common/ProxySetting.cs +++ b/v2rayN/PacLib/ProxySettingWindows.cs @@ -1,9 +1,12 @@ -using System.Runtime.InteropServices; -using static v2rayN.Common.ProxySetting.InternetConnectionOption; +using Microsoft.Win32; +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using static PacLib.ProxySettingWindows.InternetConnectionOption; -namespace v2rayN.Common +namespace PacLib { - internal class ProxySetting + public class ProxySettingWindows { private const string _regPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings"; @@ -11,24 +14,24 @@ namespace v2rayN.Common { if (type == 1) { - WindowsUtils.RegWriteValue(_regPath, "ProxyEnable", 0); - WindowsUtils.RegWriteValue(_regPath, "ProxyServer", string.Empty); - WindowsUtils.RegWriteValue(_regPath, "ProxyOverride", string.Empty); - WindowsUtils.RegWriteValue(_regPath, "AutoConfigURL", string.Empty); + RegWriteValue(_regPath, "ProxyEnable", 0); + RegWriteValue(_regPath, "ProxyServer", string.Empty); + RegWriteValue(_regPath, "ProxyOverride", string.Empty); + RegWriteValue(_regPath, "AutoConfigURL", string.Empty); } if (type == 2) { - WindowsUtils.RegWriteValue(_regPath, "ProxyEnable", 1); - WindowsUtils.RegWriteValue(_regPath, "ProxyServer", strProxy ?? string.Empty); - WindowsUtils.RegWriteValue(_regPath, "ProxyOverride", exceptions ?? string.Empty); - WindowsUtils.RegWriteValue(_regPath, "AutoConfigURL", string.Empty); + RegWriteValue(_regPath, "ProxyEnable", 1); + RegWriteValue(_regPath, "ProxyServer", strProxy ?? string.Empty); + RegWriteValue(_regPath, "ProxyOverride", exceptions ?? string.Empty); + RegWriteValue(_regPath, "AutoConfigURL", string.Empty); } else if (type == 4) { - WindowsUtils.RegWriteValue(_regPath, "ProxyEnable", 0); - WindowsUtils.RegWriteValue(_regPath, "ProxyServer", string.Empty); - WindowsUtils.RegWriteValue(_regPath, "ProxyOverride", string.Empty); - WindowsUtils.RegWriteValue(_regPath, "AutoConfigURL", strProxy ?? string.Empty); + RegWriteValue(_regPath, "ProxyEnable", 0); + RegWriteValue(_regPath, "ProxyServer", string.Empty); + RegWriteValue(_regPath, "ProxyOverride", string.Empty); + RegWriteValue(_regPath, "AutoConfigURL", strProxy ?? string.Empty); } return true; } @@ -72,7 +75,7 @@ namespace v2rayN.Common catch (Exception ex) { SetProxyFallback(strProxy, exceptions, type); - Logging.SaveLog(ex.Message, ex); + //Logging.SaveLog(ex.Message, ex); return false; } } @@ -88,7 +91,7 @@ namespace v2rayN.Common } else if (type is 2 or 4) // named proxy or autoproxy script URL { - optionCount = Utils.IsNullOrEmpty(exceptions) ? 2 : 3; + optionCount = string.IsNullOrEmpty(exceptions) ? 2 : 3; } int m_Int = (int)PerConnFlags.PROXY_TYPE_DIRECT; @@ -356,5 +359,30 @@ namespace v2rayN.Common ref int lpcEntries // Number of entries written to the buffer ); } + + private static void RegWriteValue(string path, string name, object value) + { + RegistryKey? regKey = null; + try + { + regKey = Registry.CurrentUser.CreateSubKey(path); + if (string.IsNullOrEmpty(value.ToString())) + { + regKey?.DeleteValue(name, false); + } + else + { + regKey?.SetValue(name, value); + } + } + catch (Exception ex) + { + //Logging.SaveLog(ex.Message, ex); + } + finally + { + regKey?.Close(); + } + } } } \ No newline at end of file diff --git a/v2rayN/v2rayN.Desktop/Handler/SysProxyHandler.cs b/v2rayN/v2rayN.Desktop/Handler/SysProxyHandler.cs index 8dc30d19..b635e97e 100644 --- a/v2rayN/v2rayN.Desktop/Handler/SysProxyHandler.cs +++ b/v2rayN/v2rayN.Desktop/Handler/SysProxyHandler.cs @@ -1,4 +1,5 @@ -using v2rayN.Desktop.Common; +using PacLib; +using v2rayN.Desktop.Common; namespace v2rayN.Desktop.Handler { @@ -26,7 +27,25 @@ namespace v2rayN.Desktop.Handler { if (Utils.IsWindows()) { - //TODO + var strExceptions = ""; + if (config.systemProxyItem.notProxyLocalAddress) + { + strExceptions = $";{config.constItem.defIEProxyExceptions};{config.systemProxyItem.systemProxyExceptions}"; + } + + var strProxy = string.Empty; + if (Utils.IsNullOrEmpty(config.systemProxyItem.systemProxyAdvancedProtocol)) + { + strProxy = $"{Global.Loopback}:{port}"; + } + else + { + strProxy = config.systemProxyItem.systemProxyAdvancedProtocol + .Replace("{ip}", Global.Loopback) + .Replace("{http_port}", port.ToString()) + .Replace("{socks_port}", portSocks.ToString()); + } + ProxySettingWindows.SetProxy(strProxy, strExceptions, 2); } else if (Utils.IsLinux()) { @@ -41,7 +60,7 @@ namespace v2rayN.Desktop.Handler { if (Utils.IsWindows()) { - //TODO + ProxySettingWindows.UnsetProxy(); } else if (Utils.IsLinux()) { diff --git a/v2rayN/v2rayN.Desktop/v2rayN.Desktop.csproj b/v2rayN/v2rayN.Desktop/v2rayN.Desktop.csproj index 3b5d8198..e0a8cb1e 100644 --- a/v2rayN/v2rayN.Desktop/v2rayN.Desktop.csproj +++ b/v2rayN/v2rayN.Desktop/v2rayN.Desktop.csproj @@ -33,6 +33,7 @@ + diff --git a/v2rayN/v2rayN/Handler/SysProxyHandler.cs b/v2rayN/v2rayN/Handler/SysProxyHandler.cs index 1efe3d76..1374bac6 100644 --- a/v2rayN/v2rayN/Handler/SysProxyHandler.cs +++ b/v2rayN/v2rayN/Handler/SysProxyHandler.cs @@ -1,12 +1,9 @@ using PacLib; -using v2rayN.Common; namespace v2rayN.Handler { public static class SysProxyHandler { - private const string _regPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings"; - public static bool UpdateSysProxy(Config config, bool forceDisable) { var type = config.systemProxyItem.sysProxyType; @@ -45,11 +42,11 @@ namespace v2rayN.Handler .Replace("{http_port}", port.ToString()) .Replace("{socks_port}", portSocks.ToString()); } - ProxySetting.SetProxy(strProxy, strExceptions, 2); + ProxySettingWindows.SetProxy(strProxy, strExceptions, 2); } else if (type == ESysProxyType.ForcedClear) { - ProxySetting.UnsetProxy(); + ProxySettingWindows.UnsetProxy(); } else if (type == ESysProxyType.Unchanged) { @@ -58,7 +55,7 @@ namespace v2rayN.Handler { PacHandler.Start(Utils.GetConfigPath(), port, portPac); var strProxy = $"{Global.HttpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}"; - ProxySetting.SetProxy(strProxy, "", 4); + ProxySettingWindows.SetProxy(strProxy, "", 4); } if (type != ESysProxyType.Pac) @@ -72,10 +69,5 @@ namespace v2rayN.Handler } return true; } - - public static void ResetIEProxy4WindowsShutDown() - { - ProxySetting.UnsetProxy(); - } } } \ No newline at end of file