Add generate policy group

This commit is contained in:
DHR60 2025-09-11 13:23:38 +08:00
parent 9ef228db1e
commit e29d292732
3 changed files with 29 additions and 45 deletions

View file

@ -1179,7 +1179,7 @@ public static class ConfigHandler
} }
/// <summary> /// <summary>
/// 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 /// Generates a configuration file that references multiple servers
/// </summary> /// </summary>
/// <param name="config">Current configuration</param> /// <param name="config">Current configuration</param>
@ -1187,27 +1187,17 @@ public static class ConfigHandler
/// <param name="coreType">Core type to use (Xray or sing_box)</param> /// <param name="coreType">Core type to use (Xray or sing_box)</param>
/// <param name="multipleLoad">Load balancing algorithm</param> /// <param name="multipleLoad">Load balancing algorithm</param>
/// <returns>Result object with success state and data</returns> /// <returns>Result object with success state and data</returns>
public static async Task<RetResult> AddCustomServer4Multiple(Config config, List<ProfileItem> selecteds, ECoreType coreType, EMultipleLoad multipleLoad) public static async Task<RetResult> AddGroupServer4Multiple(Config config, List<ProfileItem> selecteds, ECoreType coreType, EMultipleLoad multipleLoad, string? subId)
{ {
var indexId = Utils.GetMd5(Global.CoreMultipleLoadConfigFileName); var result = new RetResult();
var configPath = Utils.GetConfigPath(Global.CoreMultipleLoadConfigFileName);
var result = await CoreConfigHandler.GenerateClientMultipleLoadConfig(config, configPath, selecteds, coreType, multipleLoad); var indexId = Utils.GetGuid(false);
if (result.Success != true) var childProfileIndexId = Utils.List2String(selecteds.Select(p => p.IndexId).ToList());
{
return result;
}
if (!File.Exists(configPath)) var remark = string.Empty;
{
return result;
}
var profileItem = await AppManager.Instance.GetProfileItem(indexId) ?? new();
profileItem.IndexId = indexId;
if (coreType == ECoreType.Xray) if (coreType == ECoreType.Xray)
{ {
profileItem.Remarks = multipleLoad switch remark = multipleLoad switch
{ {
EMultipleLoad.Random => ResUI.menuGenGroupMultipleServerXrayRandom, EMultipleLoad.Random => ResUI.menuGenGroupMultipleServerXrayRandom,
EMultipleLoad.RoundRobin => ResUI.menuGenGroupMultipleServerXrayRoundRobin, EMultipleLoad.RoundRobin => ResUI.menuGenGroupMultipleServerXrayRoundRobin,
@ -1218,14 +1208,28 @@ public static class ConfigHandler
} }
else if (coreType == ECoreType.sing_box) else if (coreType == ECoreType.sing_box)
{ {
profileItem.Remarks = ResUI.menuGenGroupMultipleServerSingBoxLeastPing; remark = ResUI.menuGenGroupMultipleServerSingBoxLeastPing;
} }
profileItem.Address = Global.CoreMultipleLoadConfigFileName; var profile = new ProfileItem
profileItem.ConfigType = EConfigType.Custom; {
profileItem.CoreType = coreType; IndexId = indexId,
CoreType = coreType,
await AddServerCommon(config, profileItem, true); 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; result.Data = indexId;
return result; return result;
} }

View file

@ -132,24 +132,4 @@ public static class CoreConfigHandler
await File.WriteAllTextAsync(fileName, result.Data.ToString()); await File.WriteAllTextAsync(fileName, result.Data.ToString());
return result; return result;
} }
public static async Task<RetResult> GenerateClientMultipleLoadConfig(Config config, string fileName, List<ProfileItem> 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;
}
} }

View file

@ -618,7 +618,7 @@ public class ProfilesViewModel : MyReactiveObject
return; 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) if (ret.Success != true)
{ {
NoticeManager.Instance.Enqueue(ResUI.OperationFailed); NoticeManager.Instance.Enqueue(ResUI.OperationFailed);