Refactor ProxySettingWindows

This commit is contained in:
2dust 2024-10-06 20:44:43 +08:00
parent 53a2fbd0ff
commit 4d84eede56
4 changed files with 72 additions and 32 deletions

View file

@ -1,9 +1,12 @@
using System.Runtime.InteropServices; using Microsoft.Win32;
using static v2rayN.Common.ProxySetting.InternetConnectionOption; 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"; private const string _regPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";
@ -11,24 +14,24 @@ namespace v2rayN.Common
{ {
if (type == 1) if (type == 1)
{ {
WindowsUtils.RegWriteValue(_regPath, "ProxyEnable", 0); RegWriteValue(_regPath, "ProxyEnable", 0);
WindowsUtils.RegWriteValue(_regPath, "ProxyServer", string.Empty); RegWriteValue(_regPath, "ProxyServer", string.Empty);
WindowsUtils.RegWriteValue(_regPath, "ProxyOverride", string.Empty); RegWriteValue(_regPath, "ProxyOverride", string.Empty);
WindowsUtils.RegWriteValue(_regPath, "AutoConfigURL", string.Empty); RegWriteValue(_regPath, "AutoConfigURL", string.Empty);
} }
if (type == 2) if (type == 2)
{ {
WindowsUtils.RegWriteValue(_regPath, "ProxyEnable", 1); RegWriteValue(_regPath, "ProxyEnable", 1);
WindowsUtils.RegWriteValue(_regPath, "ProxyServer", strProxy ?? string.Empty); RegWriteValue(_regPath, "ProxyServer", strProxy ?? string.Empty);
WindowsUtils.RegWriteValue(_regPath, "ProxyOverride", exceptions ?? string.Empty); RegWriteValue(_regPath, "ProxyOverride", exceptions ?? string.Empty);
WindowsUtils.RegWriteValue(_regPath, "AutoConfigURL", string.Empty); RegWriteValue(_regPath, "AutoConfigURL", string.Empty);
} }
else if (type == 4) else if (type == 4)
{ {
WindowsUtils.RegWriteValue(_regPath, "ProxyEnable", 0); RegWriteValue(_regPath, "ProxyEnable", 0);
WindowsUtils.RegWriteValue(_regPath, "ProxyServer", string.Empty); RegWriteValue(_regPath, "ProxyServer", string.Empty);
WindowsUtils.RegWriteValue(_regPath, "ProxyOverride", string.Empty); RegWriteValue(_regPath, "ProxyOverride", string.Empty);
WindowsUtils.RegWriteValue(_regPath, "AutoConfigURL", strProxy ?? string.Empty); RegWriteValue(_regPath, "AutoConfigURL", strProxy ?? string.Empty);
} }
return true; return true;
} }
@ -72,7 +75,7 @@ namespace v2rayN.Common
catch (Exception ex) catch (Exception ex)
{ {
SetProxyFallback(strProxy, exceptions, type); SetProxyFallback(strProxy, exceptions, type);
Logging.SaveLog(ex.Message, ex); //Logging.SaveLog(ex.Message, ex);
return false; return false;
} }
} }
@ -88,7 +91,7 @@ namespace v2rayN.Common
} }
else if (type is 2 or 4) // named proxy or autoproxy script URL 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; 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 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();
}
}
} }
} }

View file

@ -1,4 +1,5 @@
using v2rayN.Desktop.Common; using PacLib;
using v2rayN.Desktop.Common;
namespace v2rayN.Desktop.Handler namespace v2rayN.Desktop.Handler
{ {
@ -26,7 +27,25 @@ namespace v2rayN.Desktop.Handler
{ {
if (Utils.IsWindows()) if (Utils.IsWindows())
{ {
//TODO var strExceptions = "";
if (config.systemProxyItem.notProxyLocalAddress)
{
strExceptions = $"<local>;{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()) else if (Utils.IsLinux())
{ {
@ -41,7 +60,7 @@ namespace v2rayN.Desktop.Handler
{ {
if (Utils.IsWindows()) if (Utils.IsWindows())
{ {
//TODO ProxySettingWindows.UnsetProxy();
} }
else if (Utils.IsLinux()) else if (Utils.IsLinux())
{ {

View file

@ -33,6 +33,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\PacLib\PacLib.csproj" />
<ProjectReference Include="..\ServiceLib\ServiceLib.csproj" /> <ProjectReference Include="..\ServiceLib\ServiceLib.csproj" />
</ItemGroup> </ItemGroup>

View file

@ -1,12 +1,9 @@
using PacLib; using PacLib;
using v2rayN.Common;
namespace v2rayN.Handler namespace v2rayN.Handler
{ {
public static class SysProxyHandler public static class SysProxyHandler
{ {
private const string _regPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";
public static bool UpdateSysProxy(Config config, bool forceDisable) public static bool UpdateSysProxy(Config config, bool forceDisable)
{ {
var type = config.systemProxyItem.sysProxyType; var type = config.systemProxyItem.sysProxyType;
@ -45,11 +42,11 @@ namespace v2rayN.Handler
.Replace("{http_port}", port.ToString()) .Replace("{http_port}", port.ToString())
.Replace("{socks_port}", portSocks.ToString()); .Replace("{socks_port}", portSocks.ToString());
} }
ProxySetting.SetProxy(strProxy, strExceptions, 2); ProxySettingWindows.SetProxy(strProxy, strExceptions, 2);
} }
else if (type == ESysProxyType.ForcedClear) else if (type == ESysProxyType.ForcedClear)
{ {
ProxySetting.UnsetProxy(); ProxySettingWindows.UnsetProxy();
} }
else if (type == ESysProxyType.Unchanged) else if (type == ESysProxyType.Unchanged)
{ {
@ -58,7 +55,7 @@ namespace v2rayN.Handler
{ {
PacHandler.Start(Utils.GetConfigPath(), port, portPac); PacHandler.Start(Utils.GetConfigPath(), port, portPac);
var strProxy = $"{Global.HttpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}"; var strProxy = $"{Global.HttpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}";
ProxySetting.SetProxy(strProxy, "", 4); ProxySettingWindows.SetProxy(strProxy, "", 4);
} }
if (type != ESysProxyType.Pac) if (type != ESysProxyType.Pac)
@ -72,10 +69,5 @@ namespace v2rayN.Handler
} }
return true; return true;
} }
public static void ResetIEProxy4WindowsShutDown()
{
ProxySetting.UnsetProxy();
}
} }
} }