From e29d292732ecdae51512a7a7b57747b1c23b9b38 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Thu, 11 Sep 2025 13:23:38 +0800 Subject: [PATCH] Add generate policy group --- v2rayN/ServiceLib/Handler/ConfigHandler.cs | 52 ++++++++++--------- .../ServiceLib/Handler/CoreConfigHandler.cs | 20 ------- .../ViewModels/ProfilesViewModel.cs | 2 +- 3 files changed, 29 insertions(+), 45 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index 080b7aac..35b47269 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -1179,7 +1179,7 @@ public static class ConfigHandler } /// - /// Create a custom server that combines multiple servers for load balancing + /// Create a group server that combines multiple servers for load balancing /// Generates a configuration file that references multiple servers /// /// Current configuration @@ -1187,27 +1187,17 @@ public static class ConfigHandler /// Core type to use (Xray or sing_box) /// Load balancing algorithm /// Result object with success state and data - public static async Task AddCustomServer4Multiple(Config config, List selecteds, ECoreType coreType, EMultipleLoad multipleLoad) + public static async Task AddGroupServer4Multiple(Config config, List selecteds, ECoreType coreType, EMultipleLoad multipleLoad, string? subId) { - var indexId = Utils.GetMd5(Global.CoreMultipleLoadConfigFileName); - var configPath = Utils.GetConfigPath(Global.CoreMultipleLoadConfigFileName); + var result = new RetResult(); - var result = await CoreConfigHandler.GenerateClientMultipleLoadConfig(config, configPath, selecteds, coreType, multipleLoad); - if (result.Success != true) - { - return result; - } + var indexId = Utils.GetGuid(false); + var childProfileIndexId = Utils.List2String(selecteds.Select(p => p.IndexId).ToList()); - if (!File.Exists(configPath)) - { - return result; - } - - var profileItem = await AppManager.Instance.GetProfileItem(indexId) ?? new(); - profileItem.IndexId = indexId; + var remark = string.Empty; if (coreType == ECoreType.Xray) { - profileItem.Remarks = multipleLoad switch + remark = multipleLoad switch { EMultipleLoad.Random => ResUI.menuGenGroupMultipleServerXrayRandom, EMultipleLoad.RoundRobin => ResUI.menuGenGroupMultipleServerXrayRoundRobin, @@ -1218,14 +1208,28 @@ public static class ConfigHandler } else if (coreType == ECoreType.sing_box) { - profileItem.Remarks = ResUI.menuGenGroupMultipleServerSingBoxLeastPing; + remark = ResUI.menuGenGroupMultipleServerSingBoxLeastPing; } - profileItem.Address = Global.CoreMultipleLoadConfigFileName; - profileItem.ConfigType = EConfigType.Custom; - profileItem.CoreType = coreType; - - await AddServerCommon(config, profileItem, true); - + var profile = new ProfileItem + { + IndexId = indexId, + CoreType = coreType, + ConfigType = EConfigType.PolicyGroup, + Remarks = remark, + Address = childProfileIndexId, + }; + if (!subId.IsNullOrEmpty()) + { + profile.Subid = subId; + } + var profileGroup = new ProfileGroupItem + { + ChildItems = childProfileIndexId, + MultipleLoad = multipleLoad, + ParentIndexId = indexId, + }; + var ret = await AddGroupServerCommon(config, profile, profileGroup, true); + result.Success = ret == 0; result.Data = indexId; return result; } diff --git a/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs b/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs index 66d261ac..96c72bb1 100644 --- a/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs @@ -132,24 +132,4 @@ public static class CoreConfigHandler await File.WriteAllTextAsync(fileName, result.Data.ToString()); return result; } - - public static async Task GenerateClientMultipleLoadConfig(Config config, string fileName, List selecteds, ECoreType coreType, EMultipleLoad multipleLoad) - { - var result = new RetResult(); - if (coreType == ECoreType.sing_box) - { - result = await new CoreConfigSingboxService(config).GenerateClientMultipleLoadConfig(selecteds); - } - else - { - result = await new CoreConfigV2rayService(config).GenerateClientMultipleLoadConfig(selecteds, multipleLoad); - } - - if (result.Success != true) - { - return result; - } - await File.WriteAllTextAsync(fileName, result.Data.ToString()); - return result; - } } diff --git a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs index e23ee441..69be3410 100644 --- a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs @@ -618,7 +618,7 @@ public class ProfilesViewModel : MyReactiveObject return; } - var ret = await ConfigHandler.AddCustomServer4Multiple(_config, lstSelected, coreType, multipleLoad); + var ret = await ConfigHandler.AddGroupServer4Multiple(_config, lstSelected, coreType, multipleLoad, SelectedSub?.Id); if (ret.Success != true) { NoticeManager.Instance.Enqueue(ResUI.OperationFailed);