From d4880dbe02f6f859e43fbfc41f522a8bddf1c5b0 Mon Sep 17 00:00:00 2001 From: DHR60 <192860629+DHR60@users.noreply.github.com> Date: Mon, 2 Nov 2026 01:29:45 +0000 Subject: [PATCH] Fix policy group (#10495) --- .../V2ray/CoreConfigV2rayService.cs | 6 ++-- .../CoreConfig/V2ray/V2rayOutboundService.cs | 30 ++++++++++++++++++- .../CoreConfig/V2ray/V2rayRoutingService.cs | 5 ++-- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs index bee41134..6687bb99 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs @@ -176,13 +176,13 @@ public partial class CoreConfigV2rayService(CoreConfigContext context) var tag = Global.ProxyTag + inbound.port.ToString(); var isBalancer = false; //outbound - var proxyOutbounds = - new CoreConfigV2rayService(context with { Node = item }).BuildAllProxyOutbounds(tag); + var service = new CoreConfigV2rayService(context with { Node = item }); + var proxyOutbounds = service.BuildAllProxyOutbounds(tag); _coreConfig.outbounds.AddRange(proxyOutbounds); if (proxyOutbounds.Count(n => n.tag.StartsWith(tag)) > 1) { isBalancer = true; - var multipleLoad = _node.GetProtocolExtra().MultipleLoad ?? EMultipleLoad.LeastPing; + var multipleLoad = service.FindFirstPolicyGroupMultipleLoad() ?? EMultipleLoad.LeastPing; GenObservatory(multipleLoad, tag); GenBalancer(multipleLoad, tag); } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs index 3426d6c8..fe69aeef 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs @@ -8,7 +8,7 @@ public partial class CoreConfigV2rayService _coreConfig.outbounds.InsertRange(0, proxyOutboundList); if (proxyOutboundList.Count(n => n.tag.StartsWith(Global.ProxyTag)) > 1) { - var multipleLoad = _node.GetProtocolExtra().MultipleLoad ?? EMultipleLoad.LeastPing; + var multipleLoad = FindFirstPolicyGroupMultipleLoad() ?? EMultipleLoad.LeastPing; GenObservatory(multipleLoad); GenBalancer(multipleLoad); } @@ -914,4 +914,32 @@ public partial class CoreConfigV2rayService } return clonedOutbounds; } + + private EMultipleLoad? FindFirstPolicyGroupMultipleLoad() + { + if (!_node.ConfigType.IsGroupType()) + { + return null; + } + + // BFS: the nearest policy group in the group tree decides the multiple load strategy. + var queue = new Queue([_node]); + while (queue.Count > 0) + { + var node = queue.Dequeue(); + if (node.ConfigType == EConfigType.PolicyGroup) + { + return node.GetProtocolExtra().MultipleLoad; + } + foreach (var childId in Utils.String2List(node.GetProtocolExtra().ChildItems) ?? []) + { + if (context.AllProxiesMap.TryGetValue(childId, out var childNode) + && childNode.ConfigType.IsGroupType()) + { + queue.Enqueue(childNode); + } + } + } + return null; + } } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs index 69dc963b..a80329c8 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs @@ -200,11 +200,12 @@ public partial class CoreConfigV2rayService return tag; } - var proxyOutbounds = new CoreConfigV2rayService(context with { Node = node, }).BuildAllProxyOutbounds(tag); + var service = new CoreConfigV2rayService(context with { Node = node, }); + var proxyOutbounds = service.BuildAllProxyOutbounds(tag); _coreConfig.outbounds.AddRange(proxyOutbounds); if (proxyOutbounds.Count(n => n.tag.StartsWith(tag)) > 1) { - var multipleLoad = node.GetProtocolExtra().MultipleLoad ?? EMultipleLoad.LeastPing; + var multipleLoad = service.FindFirstPolicyGroupMultipleLoad() ?? EMultipleLoad.LeastPing; GenObservatory(multipleLoad, tag); GenBalancer(multipleLoad, tag); }