From 0f4884d9d8e3752ecf08aaef5ce67466df2f4034 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 25 Jul 2024 10:26:39 +0800 Subject: [PATCH] Improve and refactor the code --- v2rayN/v2rayN/Handler/ConfigHandler.cs | 9 +++++++++ v2rayN/v2rayN/Handler/MainFormHandler.cs | 6 +++--- v2rayN/v2rayN/Handler/SysProxyHandle.cs | 10 +++++----- v2rayN/v2rayN/Models/Config.cs | 3 +-- v2rayN/v2rayN/Models/ConfigItems.cs | 9 +++++++++ .../v2rayN/ViewModels/MainWindowViewModel.cs | 20 +++++++++---------- .../ViewModels/OptionSettingViewModel.cs | 12 +++++------ v2rayN/v2rayN/v2rayN.csproj | 2 +- 8 files changed, 44 insertions(+), 27 deletions(-) diff --git a/v2rayN/v2rayN/Handler/ConfigHandler.cs b/v2rayN/v2rayN/Handler/ConfigHandler.cs index eb18382f..3fda6adf 100644 --- a/v2rayN/v2rayN/Handler/ConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/ConfigHandler.cs @@ -197,6 +197,15 @@ namespace v2rayN.Handler } config.clashUIItem ??= new(); + if (config.systemProxyItem == null) + { + config.systemProxyItem = new() + { + systemProxyExceptions = config.systemProxyExceptions, + systemProxyAdvancedProtocol = config.systemProxyAdvancedProtocol, + }; + } + LazyConfig.Instance.SetConfig(config); return 0; } diff --git a/v2rayN/v2rayN/Handler/MainFormHandler.cs b/v2rayN/v2rayN/Handler/MainFormHandler.cs index 7942ffd1..e2f37a2a 100644 --- a/v2rayN/v2rayN/Handler/MainFormHandler.cs +++ b/v2rayN/v2rayN/Handler/MainFormHandler.cs @@ -22,7 +22,7 @@ namespace v2rayN.Handler { try { - int index = (int)config.sysProxyType; + int index = (int)config.systemProxyItem.sysProxyType; //Load from routing setting var createdIcon = GetNotifyIcon4Routing(config); @@ -56,7 +56,7 @@ namespace v2rayN.Handler public System.Windows.Media.ImageSource GetAppIcon(Config config) { int index = 1; - switch (config.sysProxyType) + switch (config.systemProxyItem.sysProxyType) { case ESysProxyType.ForcedClear: index = 1; @@ -90,7 +90,7 @@ namespace v2rayN.Handler } Color color = ColorTranslator.FromHtml("#3399CC"); - int index = (int)config.sysProxyType; + int index = (int)config.systemProxyItem.sysProxyType; if (index > 0) { color = (new[] { Color.Red, Color.Purple, Color.DarkGreen, Color.Orange, Color.DarkSlateBlue, Color.RoyalBlue })[index - 1]; diff --git a/v2rayN/v2rayN/Handler/SysProxyHandle.cs b/v2rayN/v2rayN/Handler/SysProxyHandle.cs index be58fc97..bc0c601d 100644 --- a/v2rayN/v2rayN/Handler/SysProxyHandle.cs +++ b/v2rayN/v2rayN/Handler/SysProxyHandle.cs @@ -11,7 +11,7 @@ namespace v2rayN.Handler public static bool UpdateSysProxy(Config config, bool forceDisable) { - var type = config.sysProxyType; + var type = config.systemProxyItem.sysProxyType; if (forceDisable && type != ESysProxyType.Unchanged) { @@ -30,19 +30,19 @@ namespace v2rayN.Handler if (type == ESysProxyType.ForcedChange) { var strExceptions = ""; - if (config.notProxyLocalAddress) + if (config.systemProxyItem.notProxyLocalAddress) { - strExceptions = $";{config.constItem.defIEProxyExceptions};{config.systemProxyExceptions}"; + strExceptions = $";{config.constItem.defIEProxyExceptions};{config.systemProxyItem.systemProxyExceptions}"; } var strProxy = string.Empty; - if (Utils.IsNullOrEmpty(config.systemProxyAdvancedProtocol)) + if (Utils.IsNullOrEmpty(config.systemProxyItem.systemProxyAdvancedProtocol)) { strProxy = $"{Global.Loopback}:{port}"; } else { - strProxy = config.systemProxyAdvancedProtocol + strProxy = config.systemProxyItem.systemProxyAdvancedProtocol .Replace("{ip}", Global.Loopback) .Replace("{http_port}", port.ToString()) .Replace("{socks_port}", portSocks.ToString()); diff --git a/v2rayN/v2rayN/Models/Config.cs b/v2rayN/v2rayN/Models/Config.cs index e859c863..f7e78e61 100644 --- a/v2rayN/v2rayN/Models/Config.cs +++ b/v2rayN/v2rayN/Models/Config.cs @@ -12,9 +12,7 @@ namespace v2rayN.Models public string indexId { get; set; } public string subIndexId { get; set; } - public ESysProxyType sysProxyType { get; set; } public string systemProxyExceptions { get; set; } - public bool notProxyLocalAddress { get; set; } = true; public string systemProxyAdvancedProtocol { get; set; } public ECoreType runningCoreType { get; set; } @@ -48,6 +46,7 @@ namespace v2rayN.Models public Mux4SboxItem mux4SboxItem { get; set; } public HysteriaItem hysteriaItem { get; set; } public ClashUIItem clashUIItem { get; set; } + public SystemProxyItem systemProxyItem { get; set; } public List inbound { get; set; } public List globalHotkeys { get; set; } public List coreTypeItem { get; set; } diff --git a/v2rayN/v2rayN/Models/ConfigItems.cs b/v2rayN/v2rayN/Models/ConfigItems.cs index a877048f..031b0134 100644 --- a/v2rayN/v2rayN/Models/ConfigItems.cs +++ b/v2rayN/v2rayN/Models/ConfigItems.cs @@ -227,4 +227,13 @@ namespace v2rayN.Models public bool connectionsAutoRefresh { get; set; } public int connectionsRefreshInterval { get; set; } = 2; } + + [Serializable] + public class SystemProxyItem + { + public ESysProxyType sysProxyType { get; set; } + public string systemProxyExceptions { get; set; } + public bool notProxyLocalAddress { get; set; } = true; + public string systemProxyAdvancedProtocol { get; set; } + } } \ No newline at end of file diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index c9a5426d..23da4a0c 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -234,7 +234,7 @@ namespace v2rayN.ViewModels y => y != null && !y.Text.IsNullOrEmpty()) .Subscribe(c => ServerSelectedChanged(c)); - SystemProxySelected = (int)_config.sysProxyType; + SystemProxySelected = (int)_config.systemProxyItem.sysProxyType; this.WhenAnyValue( x => x.SystemProxySelected, y => y >= 0) @@ -429,7 +429,7 @@ namespace v2rayN.ViewModels //RefreshServers(); Reload(); - ChangeSystemProxyStatus(_config.sysProxyType, true); + ChangeSystemProxyStatus(_config.systemProxyItem.sysProxyType, true); } private void OnProgramStarted(object state, bool timeout) @@ -936,7 +936,7 @@ namespace v2rayN.ViewModels //ConfigHandler.SaveConfig(_config, false); - ChangeSystemProxyStatus(_config.sysProxyType, false); + ChangeSystemProxyStatus(_config.systemProxyItem.sysProxyType, false); }); } @@ -955,21 +955,21 @@ namespace v2rayN.ViewModels public void SetListenerType(ESysProxyType type) { - if (_config.sysProxyType == type) + if (_config.systemProxyItem.sysProxyType == type) { return; } - _config.sysProxyType = type; + _config.systemProxyItem.sysProxyType = type; ChangeSystemProxyStatus(type, true); - SystemProxySelected = (int)_config.sysProxyType; + SystemProxySelected = (int)_config.systemProxyItem.sysProxyType; ConfigHandler.SaveConfig(_config, false); } private void ChangeSystemProxyStatus(ESysProxyType type, bool blChange) { SysProxyHandle.UpdateSysProxy(_config, _config.tunModeItem.enableTun ? true : false); - _noticeHandler?.SendMessage($"{ResUI.TipChangeSystemProxy} - {_config.sysProxyType.ToString()}", true); + _noticeHandler?.SendMessage($"{ResUI.TipChangeSystemProxy} - {_config.systemProxyItem.sysProxyType.ToString()}", true); Application.Current?.Dispatcher.Invoke((Action)(() => { @@ -1046,7 +1046,7 @@ namespace v2rayN.ViewModels { return; } - if (_config.sysProxyType == (ESysProxyType)SystemProxySelected) + if (_config.systemProxyItem.sysProxyType == (ESysProxyType)SystemProxySelected) { return; } @@ -1223,7 +1223,7 @@ namespace v2rayN.ViewModels StringBuilder sb = new(); sb.Append($"[{EInboundProtocol.socks}:{LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks)}]"); sb.Append(" | "); - //if (_config.sysProxyType == ESysProxyType.ForcedChange) + //if (_config.systemProxyItem.sysProxyType == ESysProxyType.ForcedChange) //{ // sb.Append($"[{Global.InboundHttp}({ResUI.SystemProxy}):{LazyConfig.Instance.GetLocalPort(Global.InboundHttp)}]"); //} @@ -1293,4 +1293,4 @@ namespace v2rayN.ViewModels #endregion UI } -} +} \ No newline at end of file diff --git a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs index 6aca578e..b8f7e629 100644 --- a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs @@ -179,9 +179,9 @@ namespace v2rayN.ViewModels #region System proxy - notProxyLocalAddress = _config.notProxyLocalAddress; - systemProxyAdvancedProtocol = _config.systemProxyAdvancedProtocol; - systemProxyExceptions = _config.systemProxyExceptions; + notProxyLocalAddress = _config.systemProxyItem.notProxyLocalAddress; + systemProxyAdvancedProtocol = _config.systemProxyItem.systemProxyAdvancedProtocol; + systemProxyExceptions = _config.systemProxyItem.systemProxyExceptions; #endregion System proxy @@ -340,9 +340,9 @@ namespace v2rayN.ViewModels _config.uiItem.mainGirdOrientation = (EGirdOrientation)MainGirdOrientation; //systemProxy - _config.systemProxyExceptions = systemProxyExceptions; - _config.notProxyLocalAddress = notProxyLocalAddress; - _config.systemProxyAdvancedProtocol = systemProxyAdvancedProtocol; + _config.systemProxyItem.systemProxyExceptions = systemProxyExceptions; + _config.systemProxyItem.notProxyLocalAddress = notProxyLocalAddress; + _config.systemProxyItem.systemProxyAdvancedProtocol = systemProxyAdvancedProtocol; //tun mode _config.tunModeItem.strictRoute = TunStrictRoute; diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index 9a0b66b0..ed5af5c6 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -26,7 +26,7 @@ - +