From 91af50f99a6398eb37cc6f2a0ba2ad4e6aad82aa Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Wed, 8 Oct 2025 17:13:54 +0800 Subject: [PATCH] Optimize code ,add IsGroupType extension. Adjust EConfigType --- v2rayN/ServiceLib/Common/Extension.cs | 10 ++++++++++ v2rayN/ServiceLib/Enums/EConfigType.cs | 6 ++---- v2rayN/ServiceLib/Global.cs | 2 ++ v2rayN/ServiceLib/Handler/ConfigHandler.cs | 4 ++-- v2rayN/ServiceLib/Manager/ActionPrecheckManager.cs | 2 +- v2rayN/ServiceLib/Manager/CoreManager.cs | 2 +- v2rayN/ServiceLib/Manager/ProfileGroupItemManager.cs | 2 +- v2rayN/ServiceLib/Models/ProfileItem.cs | 2 +- .../CoreConfig/Singbox/CoreConfigSingboxService.cs | 3 ++- .../CoreConfig/Singbox/SingboxOutboundService.cs | 6 +++--- .../CoreConfig/Singbox/SingboxRoutingService.cs | 4 ++-- .../CoreConfig/V2ray/CoreConfigV2rayService.cs | 2 +- .../Services/CoreConfig/V2ray/V2rayOutboundService.cs | 6 +++--- .../Services/CoreConfig/V2ray/V2rayRoutingService.cs | 4 ++-- v2rayN/ServiceLib/Services/SpeedtestService.cs | 4 ++-- v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs | 2 +- v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs | 2 +- 17 files changed, 37 insertions(+), 26 deletions(-) diff --git a/v2rayN/ServiceLib/Common/Extension.cs b/v2rayN/ServiceLib/Common/Extension.cs index 49d59534..d36b8169 100644 --- a/v2rayN/ServiceLib/Common/Extension.cs +++ b/v2rayN/ServiceLib/Common/Extension.cs @@ -84,4 +84,14 @@ public static class Extension { return source.Concat(new[] { string.Empty }).ToList(); } + + public static bool IsGroupType(this EConfigType configType) + { + return configType is EConfigType.PolicyGroup or EConfigType.ProxyChain; + } + + public static bool IsComplexType(this EConfigType configType) + { + return configType is EConfigType.Custom or EConfigType.PolicyGroup or EConfigType.ProxyChain; + } } diff --git a/v2rayN/ServiceLib/Enums/EConfigType.cs b/v2rayN/ServiceLib/Enums/EConfigType.cs index 5ddb8451..287d7b94 100644 --- a/v2rayN/ServiceLib/Enums/EConfigType.cs +++ b/v2rayN/ServiceLib/Enums/EConfigType.cs @@ -13,8 +13,6 @@ public enum EConfigType WireGuard = 9, HTTP = 10, Anytls = 11, - - Group = 1000, - PolicyGroup = 1001, - ProxyChain = 1002, + PolicyGroup = 101, + ProxyChain = 102, } diff --git a/v2rayN/ServiceLib/Global.cs b/v2rayN/ServiceLib/Global.cs index c7f18d2c..16741fc1 100644 --- a/v2rayN/ServiceLib/Global.cs +++ b/v2rayN/ServiceLib/Global.cs @@ -316,6 +316,8 @@ public class Global EConfigType.HTTP, ]; + public static readonly HashSet SingboxOnlyConfigType = SingboxSupportConfigType.Except(XraySupportConfigType).ToHashSet(); + public static readonly List DomainStrategies = [ AsIs, diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index 33a5c52a..787d64f3 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -357,7 +357,7 @@ public static class ConfigHandler { } } - else if (profileItem.ConfigType > EConfigType.Group) + else if (profileItem.ConfigType.IsGroupType()) { var profileGroupItem = await AppManager.Instance.GetProfileGroupItem(it.IndexId); await AddGroupServerCommon(config, profileItem, profileGroupItem, true); @@ -1255,7 +1255,7 @@ public static class ConfigHandler if (node.ConfigType != EConfigType.Custom && coreType != ECoreType.sing_box && config.TunModeItem.EnableTun) { var tun2SocksAddress = node.Address; - if (node.ConfigType > EConfigType.Group) + if (node.ConfigType.IsGroupType()) { var lstAddresses = (await ProfileGroupItemManager.GetAllChildDomainAddresses(node.IndexId)).ToList(); if (lstAddresses.Count > 0) diff --git a/v2rayN/ServiceLib/Manager/ActionPrecheckManager.cs b/v2rayN/ServiceLib/Manager/ActionPrecheckManager.cs index 2ae20c0b..15c14d07 100644 --- a/v2rayN/ServiceLib/Manager/ActionPrecheckManager.cs +++ b/v2rayN/ServiceLib/Manager/ActionPrecheckManager.cs @@ -112,7 +112,7 @@ public class ActionPrecheckManager(Config config) } } - if (item.ConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain) + if (item.ConfigType.IsGroupType()) { ProfileGroupItemManager.Instance.TryGet(item.IndexId, out var group); if (group is null || group.ChildItems.IsNullOrEmpty()) diff --git a/v2rayN/ServiceLib/Manager/CoreManager.cs b/v2rayN/ServiceLib/Manager/CoreManager.cs index 07876db8..bd01fe5b 100644 --- a/v2rayN/ServiceLib/Manager/CoreManager.cs +++ b/v2rayN/ServiceLib/Manager/CoreManager.cs @@ -95,7 +95,7 @@ public class CoreManager public async Task LoadCoreConfigSpeedtest(List selecteds) { - var coreType = selecteds.Exists(t => t.ConfigType is EConfigType.Hysteria2 or EConfigType.TUIC or EConfigType.Anytls) ? ECoreType.sing_box : ECoreType.Xray; + var coreType = selecteds.Any(t => Global.SingboxOnlyConfigType.Contains(t.ConfigType)) ? ECoreType.sing_box : ECoreType.Xray; var fileName = string.Format(Global.CoreSpeedtestConfigFileName, Utils.GetGuid(false)); var configPath = Utils.GetBinConfigPath(fileName); var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType); diff --git a/v2rayN/ServiceLib/Manager/ProfileGroupItemManager.cs b/v2rayN/ServiceLib/Manager/ProfileGroupItemManager.cs index 09e6461f..72c55870 100644 --- a/v2rayN/ServiceLib/Manager/ProfileGroupItemManager.cs +++ b/v2rayN/ServiceLib/Manager/ProfileGroupItemManager.cs @@ -269,7 +269,7 @@ public class ProfileGroupItemManager { childAddresses.Add(childNode.Address); } - else if (childNode.ConfigType > EConfigType.Group) + else if (childNode.ConfigType.IsGroupType()) { var subAddresses = await GetAllChildDomainAddresses(childNode.IndexId); foreach (var addr in subAddresses) diff --git a/v2rayN/ServiceLib/Models/ProfileItem.cs b/v2rayN/ServiceLib/Models/ProfileItem.cs index da34600b..55f658e9 100644 --- a/v2rayN/ServiceLib/Models/ProfileItem.cs +++ b/v2rayN/ServiceLib/Models/ProfileItem.cs @@ -66,7 +66,7 @@ public class ProfileItem : ReactiveObject public bool IsComplex() { - return ConfigType is EConfigType.Custom or > EConfigType.Group; + return ConfigType.IsComplexType(); } public bool IsValid() diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/CoreConfigSingboxService.cs index 8a46059d..e7644a3d 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/CoreConfigSingboxService.cs @@ -1,5 +1,6 @@ using System.Net; using System.Net.NetworkInformation; +using ServiceLib.Common; namespace ServiceLib.Services.CoreConfig; @@ -29,7 +30,7 @@ public partial class CoreConfigSingboxService(Config config) ret.Msg = ResUI.InitialConfiguration; - if (node?.ConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain) + if (node.ConfigType.IsGroupType()) { switch (node.ConfigType) { diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs index 997e1281..097df4c3 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs @@ -208,7 +208,7 @@ public partial class CoreConfigSingboxService { try { - if (node.ConfigType is not (EConfigType.PolicyGroup or EConfigType.ProxyChain)) + if (!node.ConfigType.IsGroupType()) { return -1; } @@ -494,7 +494,7 @@ public partial class CoreConfigSingboxService { index++; - if (node.ConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain) + if (node.ConfigType.IsGroupType()) { var (childProfiles, profileGroupItem) = await ProfileGroupItemManager.GetChildProfileItems(node.IndexId); if (childProfiles.Count <= 0) @@ -669,7 +669,7 @@ public partial class CoreConfigSingboxService var node = nodes[i]; if (node == null) continue; - if (node.ConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain) + if (node.ConfigType.IsGroupType()) { var (childProfiles, profileGroupItem) = await ProfileGroupItemManager.GetChildProfileItems(node.IndexId); if (childProfiles.Count <= 0) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs index ea0fb073..4855c7d6 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs @@ -379,7 +379,7 @@ public partial class CoreConfigSingboxService if (node == null || (!Global.SingboxSupportConfigType.Contains(node.ConfigType) - && node.ConfigType is not (EConfigType.PolicyGroup or EConfigType.ProxyChain))) + && !node.ConfigType.IsGroupType())) { return Global.ProxyTag; } @@ -391,7 +391,7 @@ public partial class CoreConfigSingboxService return tag; } - if (node.ConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain) + if (node.ConfigType.IsGroupType()) { var ret = await GenGroupOutbound(node, singboxConfig, tag); if (ret == 0) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs index d8768b3b..8c31ddae 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs @@ -30,7 +30,7 @@ public partial class CoreConfigV2rayService(Config config) ret.Msg = ResUI.InitialConfiguration; - if (node?.ConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain) + if (node.ConfigType.IsGroupType()) { switch (node.ConfigType) { diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs index bed2f28a..ef642ce5 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs @@ -484,7 +484,7 @@ public partial class CoreConfigV2rayService { try { - if (node.ConfigType is not (EConfigType.PolicyGroup or EConfigType.ProxyChain)) + if (!node.ConfigType.IsGroupType()) { return -1; } @@ -631,7 +631,7 @@ public partial class CoreConfigV2rayService { index++; - if (node.ConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain) + if (node.ConfigType.IsGroupType()) { var (childProfiles, _) = await ProfileGroupItemManager.GetChildProfileItems(node.IndexId); if (childProfiles.Count <= 0) @@ -782,7 +782,7 @@ public partial class CoreConfigV2rayService var node = nodes[i]; if (node == null) continue; - if (node.ConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain) + if (node.ConfigType.IsGroupType()) { var (childProfiles, _) = await ProfileGroupItemManager.GetChildProfileItems(node.IndexId); if (childProfiles.Count <= 0) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs index d2fb2b62..83df2ab2 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs @@ -135,7 +135,7 @@ public partial class CoreConfigV2rayService if (node == null || (!Global.XraySupportConfigType.Contains(node.ConfigType) - && node.ConfigType is not (EConfigType.PolicyGroup or EConfigType.ProxyChain))) + && !node.ConfigType.IsGroupType())) { return Global.ProxyTag; } @@ -146,7 +146,7 @@ public partial class CoreConfigV2rayService return tag; } - if (node.ConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain) + if (node.ConfigType.IsGroupType()) { var ret = await GenGroupOutbound(node, v2rayConfig, tag); if (ret == 0) diff --git a/v2rayN/ServiceLib/Services/SpeedtestService.cs b/v2rayN/ServiceLib/Services/SpeedtestService.cs index d528becc..b2cd6a2b 100644 --- a/v2rayN/ServiceLib/Services/SpeedtestService.cs +++ b/v2rayN/ServiceLib/Services/SpeedtestService.cs @@ -64,7 +64,7 @@ public class SpeedtestService(Config config, Func updateF var lstSelected = new List(); foreach (var it in selecteds) { - if (it.ConfigType is EConfigType.Custom or EConfigType.PolicyGroup or EConfigType.ProxyChain) + if (it.ConfigType.IsComplexType()) { continue; } @@ -339,7 +339,7 @@ public class SpeedtestService(Config config, Func updateF { List> lstTest = new(); var lst1 = lstSelected.Where(t => Global.XraySupportConfigType.Contains(t.ConfigType)).ToList(); - var lst2 = lstSelected.Where(t => Global.SingboxSupportConfigType.Contains(t.ConfigType) && !Global.XraySupportConfigType.Contains(t.ConfigType)).ToList(); + var lst2 = lstSelected.Where(t => Global.SingboxOnlyConfigType.Contains(t.ConfigType)).ToList(); for (var num = 0; num < (int)Math.Ceiling(lst1.Count * 1.0 / pageSize); num++) { diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index a0803309..8120b64d 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -350,7 +350,7 @@ public class MainWindowViewModel : MyReactiveObject { ret = await _updateView?.Invoke(EViewAction.AddServer2Window, item); } - else if (eConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain) + else if (eConfigType.IsGroupType()) { ret = await _updateView?.Invoke(EViewAction.AddGroupServerWindow, item); } diff --git a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs index 289dc36e..8223632d 100644 --- a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs @@ -510,7 +510,7 @@ public class ProfilesViewModel : MyReactiveObject { ret = await _updateView?.Invoke(EViewAction.AddServer2Window, item); } - else if (eConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain) + else if (eConfigType.IsGroupType()) { ret = await _updateView?.Invoke(EViewAction.AddGroupServerWindow, item); }