diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs index a1d736d8..2afc4f49 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs @@ -519,7 +519,7 @@ public partial class CoreConfigSingboxService var outUrltest = new Outbound4Sbox { type = "urltest", - tag = $"{Global.ProxyTag}-auto", + tag = $"{baseTagName}-auto", outbounds = proxyTags, interrupt_exist_connections = false, }; @@ -533,7 +533,7 @@ public partial class CoreConfigSingboxService var outSelector = new Outbound4Sbox { type = "selector", - tag = Global.ProxyTag, + tag = baseTagName, outbounds = JsonUtils.DeepCopy(proxyTags), interrupt_exist_connections = false, }; @@ -613,22 +613,30 @@ public partial class CoreConfigSingboxService } else if (chainStartNodes.Count > 1) { - var existedChainNodes = JsonUtils.DeepCopy(resultOutbounds); + var existedChainNodes = CloneOutbounds(resultOutbounds); resultOutbounds.Clear(); + var j = 0; foreach (var chainStartNode in chainStartNodes) { - var existedChainNodesClone = JsonUtils.DeepCopy(existedChainNodes); - for (var j = 0; j < existedChainNodesClone.Count; j++) + var existedChainNodesClone = CloneOutbounds(existedChainNodes); + foreach (var existedChainNode in existedChainNodesClone) { - var existedChainNode = existedChainNodesClone[j]; var cloneTag = $"{existedChainNode.tag}-clone-{j + 1}"; existedChainNode.tag = cloneTag; + } + for (var k = 0; k < existedChainNodesClone.Count; k++) + { + var existedChainNode = existedChainNodesClone[k]; var previousDialerProxyTag = existedChainNode.detour; + var nextTag = k + 1 < existedChainNodesClone.Count + ? existedChainNodesClone[k + 1].tag + : chainStartNode.tag; existedChainNode.detour = (previousDialerProxyTag == currentTag) ? chainStartNode.tag - : existedChainNodesClone[j + 1].tag; + : nextTag; resultOutbounds.Add(existedChainNode); } + j++; } } } @@ -649,6 +657,33 @@ public partial class CoreConfigSingboxService return resultOutbounds; } + private static List CloneOutbounds(List source) + { + if (source is null || source.Count == 0) + { + return []; + } + + var result = new List(source.Count); + foreach (var item in source) + { + BaseServer4Sbox? clone = null; + if (item is Outbound4Sbox outbound) + { + clone = JsonUtils.DeepCopy(outbound); + } + else if (item is Endpoints4Sbox endpoint) + { + clone = JsonUtils.DeepCopy(endpoint); + } + if (clone is not null) + { + result.Add(clone); + } + } + return result; + } + private static void FillRangeProxy(List servers, SingboxConfig singboxConfig, bool prepend = true) { try diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs index 61efbfaf..3f0ddb7a 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs @@ -757,21 +757,31 @@ public partial class CoreConfigV2rayService { var existedChainNodes = JsonUtils.DeepCopy(resultOutbounds); resultOutbounds.Clear(); + var j = 0; foreach (var chainStartNode in chainStartNodes) { var existedChainNodesClone = JsonUtils.DeepCopy(existedChainNodes); - for (var j = 0; j < existedChainNodesClone.Count; j++) + foreach (var existedChainNode in existedChainNodesClone) { - var existedChainNode = existedChainNodesClone[j]; var cloneTag = $"{existedChainNode.tag}-clone-{j + 1}"; existedChainNode.tag = cloneTag; + } + for (var k = 0; k < existedChainNodesClone.Count; k++) + { + var existedChainNode = existedChainNodesClone[k]; var previousDialerProxyTag = existedChainNode.streamSettings?.sockopt?.dialerProxy; + var nextTag = k + 1 < existedChainNodesClone.Count + ? existedChainNodesClone[k + 1].tag + : chainStartNode.tag; existedChainNode.streamSettings.sockopt = new() { - dialerProxy = (previousDialerProxyTag == currentTag) ? chainStartNode.tag : existedChainNodesClone[j + 1].tag + dialerProxy = (previousDialerProxyTag == currentTag) + ? chainStartNode.tag + : nextTag }; resultOutbounds.Add(existedChainNode); } + j++; } } }