From 74f980aab1ae4170ae407841cae7b5702783d5e8 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Wed, 16 Oct 2024 09:20:05 +0800 Subject: [PATCH] Refactor system proxy --- v2rayN/ServiceLib/Enums/EViewAction.cs | 1 - v2rayN/ServiceLib/GlobalUsings.cs | 3 +- .../Handler/SysProxy}/ProxySettingLinux.cs | 2 +- .../Handler/SysProxy}/ProxySettingOSX.cs | 2 +- .../Handler/SysProxy}/ProxySettingWindows.cs | 10 +-- .../Handler/SysProxy}/SysProxyHandler.cs | 20 ++--- v2rayN/ServiceLib/ServiceLib.csproj | 1 + .../ViewModels/MainWindowViewModel.cs | 5 +- .../ViewModels/StatusBarViewModel.cs | 6 +- .../Views/StatusBarView.axaml.cs | 6 -- v2rayN/v2rayN.Desktop/v2rayN.Desktop.csproj | 1 - v2rayN/v2rayN/Handler/SysProxyHandler.cs | 73 ------------------- v2rayN/v2rayN/Views/StatusBarView.xaml.cs | 5 -- v2rayN/v2rayN/v2rayN.csproj | 1 - 14 files changed, 26 insertions(+), 110 deletions(-) rename v2rayN/{v2rayN.Desktop/Common => ServiceLib/Handler/SysProxy}/ProxySettingLinux.cs (99%) rename v2rayN/{v2rayN.Desktop/Common => ServiceLib/Handler/SysProxy}/ProxySettingOSX.cs (82%) rename v2rayN/{PacLib => ServiceLib/Handler/SysProxy}/ProxySettingWindows.cs (98%) rename v2rayN/{v2rayN.Desktop/Handler => ServiceLib/Handler/SysProxy}/SysProxyHandler.cs (82%) delete mode 100644 v2rayN/v2rayN/Handler/SysProxyHandler.cs diff --git a/v2rayN/ServiceLib/Enums/EViewAction.cs b/v2rayN/ServiceLib/Enums/EViewAction.cs index d62943c616..18065310be 100644 --- a/v2rayN/ServiceLib/Enums/EViewAction.cs +++ b/v2rayN/ServiceLib/Enums/EViewAction.cs @@ -7,7 +7,6 @@ public enum EViewAction SaveFileDialog, AddBatchRoutingRulesYesNo, AdjustMainLvColWidth, - UpdateSysProxy, SetClipboardData, AddServerViaClipboard, ImportRulesFromClipboard, diff --git a/v2rayN/ServiceLib/GlobalUsings.cs b/v2rayN/ServiceLib/GlobalUsings.cs index 23d2a0642a..a4bf3ccde5 100644 --- a/v2rayN/ServiceLib/GlobalUsings.cs +++ b/v2rayN/ServiceLib/GlobalUsings.cs @@ -7,4 +7,5 @@ global using ServiceLib.Services.Statistics; global using ServiceLib.Services.CoreConfig; global using ServiceLib.Models; -global using ServiceLib.Resx; \ No newline at end of file +global using ServiceLib.Resx; +global using ServiceLib.Handler.SysProxy; \ No newline at end of file diff --git a/v2rayN/v2rayN.Desktop/Common/ProxySettingLinux.cs b/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingLinux.cs similarity index 99% rename from v2rayN/v2rayN.Desktop/Common/ProxySettingLinux.cs rename to v2rayN/ServiceLib/Handler/SysProxy/ProxySettingLinux.cs index abde3751c5..a06d329bcc 100644 --- a/v2rayN/v2rayN.Desktop/Common/ProxySettingLinux.cs +++ b/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingLinux.cs @@ -1,4 +1,4 @@ -namespace v2rayN.Desktop.Common +namespace ServiceLib.Handler.SysProxy { public class ProxySettingLinux { diff --git a/v2rayN/v2rayN.Desktop/Common/ProxySettingOSX.cs b/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs similarity index 82% rename from v2rayN/v2rayN.Desktop/Common/ProxySettingOSX.cs rename to v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs index fbe058948b..e2eb4e262f 100644 --- a/v2rayN/v2rayN.Desktop/Common/ProxySettingOSX.cs +++ b/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs @@ -1,4 +1,4 @@ -namespace v2rayN.Desktop.Common +namespace ServiceLib.Handler.SysProxy { public class ProxySettingOSX { diff --git a/v2rayN/PacLib/ProxySettingWindows.cs b/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingWindows.cs similarity index 98% rename from v2rayN/PacLib/ProxySettingWindows.cs rename to v2rayN/ServiceLib/Handler/SysProxy/ProxySettingWindows.cs index bbcb3775f1..112e759c57 100644 --- a/v2rayN/PacLib/ProxySettingWindows.cs +++ b/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingWindows.cs @@ -1,10 +1,10 @@ -using Microsoft.Win32; + using System; using System.Collections.Generic; using System.Runtime.InteropServices; -using static PacLib.ProxySettingWindows.InternetConnectionOption; +using static ServiceLib.Handler.SysProxy.ProxySettingWindows.InternetConnectionOption; -namespace PacLib +namespace ServiceLib.Handler.SysProxy { public class ProxySettingWindows { @@ -362,10 +362,10 @@ ref int lpcEntries // Number of entries written to the buffer private static void RegWriteValue(string path, string name, object value) { - RegistryKey? regKey = null; + Microsoft.Win32.RegistryKey? regKey = null; try { - regKey = Registry.CurrentUser.CreateSubKey(path); + regKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(path); if (string.IsNullOrEmpty(value.ToString())) { regKey?.DeleteValue(name, false); diff --git a/v2rayN/v2rayN.Desktop/Handler/SysProxyHandler.cs b/v2rayN/ServiceLib/Handler/SysProxy/SysProxyHandler.cs similarity index 82% rename from v2rayN/v2rayN.Desktop/Handler/SysProxyHandler.cs rename to v2rayN/ServiceLib/Handler/SysProxy/SysProxyHandler.cs index 26163db2e1..98220cc03c 100644 --- a/v2rayN/v2rayN.Desktop/Handler/SysProxyHandler.cs +++ b/v2rayN/ServiceLib/Handler/SysProxy/SysProxyHandler.cs @@ -1,7 +1,6 @@ using PacLib; -using v2rayN.Desktop.Common; -namespace v2rayN.Desktop.Handler +namespace ServiceLib.Handler.SysProxy { public static class SysProxyHandler { @@ -16,9 +15,9 @@ public static async Task UpdateSysProxy(Config config, bool forceDisable) try { - int port = AppHandler.Instance.GetLocalPort(EInboundProtocol.http); - int portSocks = AppHandler.Instance.GetLocalPort(EInboundProtocol.socks); - int portPac = AppHandler.Instance.GetLocalPort(EInboundProtocol.pac); + var port = AppHandler.Instance.GetLocalPort(EInboundProtocol.http); + var portSocks = AppHandler.Instance.GetLocalPort(EInboundProtocol.socks); + var portPac = AppHandler.Instance.GetLocalPort(EInboundProtocol.pac); if (port <= 0) { return false; @@ -73,12 +72,15 @@ public static async Task UpdateSysProxy(Config config, bool forceDisable) } else if (type == ESysProxyType.Pac) { + PacHandler.Start(Utils.GetConfigPath(), port, portPac); + var strProxy = $"{Global.HttpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}"; + ProxySettingWindows.SetProxy(strProxy, "", 4); } - //if (type != ESysProxyType.Pac) - //{ - // PacHandler.Stop(); - //} + if (type != ESysProxyType.Pac) + { + PacHandler.Stop(); + } } catch (Exception ex) { diff --git a/v2rayN/ServiceLib/ServiceLib.csproj b/v2rayN/ServiceLib/ServiceLib.csproj index d9b47d737c..26187ec7ef 100644 --- a/v2rayN/ServiceLib/ServiceLib.csproj +++ b/v2rayN/ServiceLib/ServiceLib.csproj @@ -43,6 +43,7 @@ + diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index 084e0f868f..bf00f0d414 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -263,7 +263,7 @@ public async Task MyAppExitAsync(bool blWindowsShutDown) { Logging.SaveLog("MyAppExit Begin"); //if (blWindowsShutDown) - await _updateView?.Invoke(EViewAction.UpdateSysProxy, true); + await SysProxyHandler.UpdateSysProxy(_config, true); ConfigHandler.SaveConfig(_config); ProfileExHandler.Instance.SaveTo(); @@ -492,12 +492,13 @@ public async Task Reload() await LoadCore(); Locator.Current.GetService()?.TestServerAvailability(); + await SysProxyHandler.UpdateSysProxy(_config, false); _updateView?.Invoke(EViewAction.DispatcherReload, null); } public void ReloadResult() { - //ChangeSystemProxyStatusAsync(_config.systemProxyItem.sysProxyType, false); + //Locator.Current.GetService()?.ChangeSystemProxyAsync(_config.systemProxyItem.sysProxyType, false); BlReloadEnabled = true; ShowClashUI = _config.IsRunningCore(ECoreType.sing_box); if (ShowClashUI) diff --git a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs index a93500bc09..315e7ebfd7 100644 --- a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs @@ -3,7 +3,6 @@ using ReactiveUI.Fody.Helpers; using Splat; using System.Reactive; -using System.Reactive.Linq; using System.Text; namespace ServiceLib.ViewModels @@ -318,10 +317,9 @@ public async Task SetListenerType(ESysProxyType type) ConfigHandler.SaveConfig(_config, false); } - private async Task ChangeSystemProxyAsync(ESysProxyType type, bool blChange) + public async Task ChangeSystemProxyAsync(ESysProxyType type, bool blChange) { - //await _updateView?.Invoke(EViewAction.UpdateSysProxy, _config.tunModeItem.enableTun ? true : false); - _updateView?.Invoke(EViewAction.UpdateSysProxy, false); + await SysProxyHandler.UpdateSysProxy(_config, false); BlSystemProxyClear = (type == ESysProxyType.ForcedClear); BlSystemProxySet = (type == ESysProxyType.ForcedChange); diff --git a/v2rayN/v2rayN.Desktop/Views/StatusBarView.axaml.cs b/v2rayN/v2rayN.Desktop/Views/StatusBarView.axaml.cs index 2fc63d8e3f..7e33745294 100644 --- a/v2rayN/v2rayN.Desktop/Views/StatusBarView.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/StatusBarView.axaml.cs @@ -6,7 +6,6 @@ using Splat; using System.Reactive.Disposables; using v2rayN.Desktop.Common; -using v2rayN.Desktop.Handler; namespace v2rayN.Desktop.Views { @@ -91,11 +90,6 @@ private async Task UpdateViewHandler(EViewAction action, object? obj) { switch (action) { - case EViewAction.UpdateSysProxy: - if (obj is null) return false; - await SysProxyHandler.UpdateSysProxy(_config, (bool)obj); - break; - case EViewAction.DispatcherServerAvailability: if (obj is null) return false; Dispatcher.UIThread.Post(() => diff --git a/v2rayN/v2rayN.Desktop/v2rayN.Desktop.csproj b/v2rayN/v2rayN.Desktop/v2rayN.Desktop.csproj index 2ed2ee1b9d..5319c9039f 100644 --- a/v2rayN/v2rayN.Desktop/v2rayN.Desktop.csproj +++ b/v2rayN/v2rayN.Desktop/v2rayN.Desktop.csproj @@ -33,7 +33,6 @@ - diff --git a/v2rayN/v2rayN/Handler/SysProxyHandler.cs b/v2rayN/v2rayN/Handler/SysProxyHandler.cs deleted file mode 100644 index b61f0317da..0000000000 --- a/v2rayN/v2rayN/Handler/SysProxyHandler.cs +++ /dev/null @@ -1,73 +0,0 @@ -using PacLib; - -namespace v2rayN.Handler -{ - public static class SysProxyHandler - { - public static bool UpdateSysProxy(Config config, bool forceDisable) - { - var type = config.systemProxyItem.sysProxyType; - - if (forceDisable && type != ESysProxyType.Unchanged) - { - type = ESysProxyType.ForcedClear; - } - - try - { - int port = AppHandler.Instance.GetLocalPort(EInboundProtocol.http); - int portSocks = AppHandler.Instance.GetLocalPort(EInboundProtocol.socks); - int portPac = AppHandler.Instance.GetLocalPort(EInboundProtocol.pac); - if (port <= 0) - { - return false; - } - if (type == ESysProxyType.ForcedChange) - { - 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 (type == ESysProxyType.ForcedClear) - { - ProxySettingWindows.UnsetProxy(); - } - else if (type == ESysProxyType.Unchanged) - { - } - else if (type == ESysProxyType.Pac) - { - PacHandler.Start(Utils.GetConfigPath(), port, portPac); - var strProxy = $"{Global.HttpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}"; - ProxySettingWindows.SetProxy(strProxy, "", 4); - } - - if (type != ESysProxyType.Pac) - { - PacHandler.Stop(); - } - } - catch (Exception ex) - { - Logging.SaveLog(ex.Message, ex); - } - return true; - } - } -} \ No newline at end of file diff --git a/v2rayN/v2rayN/Views/StatusBarView.xaml.cs b/v2rayN/v2rayN/Views/StatusBarView.xaml.cs index f1e8c09a32..ea53a5563c 100644 --- a/v2rayN/v2rayN/Views/StatusBarView.xaml.cs +++ b/v2rayN/v2rayN/Views/StatusBarView.xaml.cs @@ -96,11 +96,6 @@ private async Task UpdateViewHandler(EViewAction action, object? obj) Application.Current.MainWindow.Icon = WindowsHandler.Instance.GetAppIcon(_config); }), DispatcherPriority.Normal); break; - - case EViewAction.UpdateSysProxy: - if (obj is null) return false; - SysProxyHandler.UpdateSysProxy(_config, (bool)obj); - break; } return await Task.FromResult(true); } diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index 66d3e499ac..274ea8b8d9 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -36,7 +36,6 @@ -