From e2df1bc6cb5e7c4fc4bcf9dda6a5addcb2513649 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Wed, 1 Oct 2025 13:11:16 +0800 Subject: [PATCH] Fix --- .../Singbox/SingboxOutboundService.cs | 63 +++++++++++++------ .../Singbox/SingboxRoutingService.cs | 7 +-- .../CoreConfig/V2ray/V2rayOutboundService.cs | 42 ++++++++++--- .../CoreConfig/V2ray/V2rayRoutingService.cs | 7 +-- 4 files changed, 82 insertions(+), 37 deletions(-) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs index 77191b86..e114b433 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxOutboundService.cs @@ -640,12 +640,12 @@ public partial class CoreConfigSingboxService } // Merge results: first the selector/urltest/proxies, then other outbounds, and finally prev outbounds - resultOutbounds.AddRange(prevOutbounds); - resultOutbounds.AddRange(singboxConfig.outbounds); - singboxConfig.outbounds = resultOutbounds; - singboxConfig.endpoints ??= new List(); - resultEndpoints.AddRange(singboxConfig.endpoints); - singboxConfig.endpoints = resultEndpoints; + var serverList = new List(); + serverList = serverList.Concat(prevOutbounds) + .Concat(resultOutbounds) + .Concat(resultEndpoints) + .ToList(); + await AddRangeOutbounds(serverList, singboxConfig, baseTagName == Global.ProxyTag); } catch (Exception ex) { @@ -770,12 +770,11 @@ public partial class CoreConfigSingboxService resultOutbounds.Insert(0, outUrltest); resultOutbounds.Insert(0, outSelector); } - singboxConfig.outbounds ??= new(); - resultOutbounds.AddRange(singboxConfig.outbounds); - singboxConfig.outbounds = resultOutbounds; - singboxConfig.endpoints ??= new(); - resultEndpoints.AddRange(singboxConfig.endpoints); - singboxConfig.endpoints = resultEndpoints; + var serverList = new List(); + serverList = serverList.Concat(resultOutbounds) + .Concat(resultEndpoints) + .ToList(); + await AddRangeOutbounds(serverList, singboxConfig, baseTagName == Global.ProxyTag); return await Task.FromResult(0); } @@ -818,14 +817,40 @@ public partial class CoreConfigSingboxService resultOutbounds.Add(outbound); } } - singboxConfig.outbounds ??= new(); - resultOutbounds.AddRange(singboxConfig.outbounds); - singboxConfig.outbounds = resultOutbounds; - - singboxConfig.endpoints ??= new(); - resultEndpoints.AddRange(singboxConfig.endpoints); - singboxConfig.endpoints = resultEndpoints; + var serverList = new List(); + serverList = serverList.Concat(resultOutbounds) + .Concat(resultEndpoints) + .ToList(); + await AddRangeOutbounds(serverList, singboxConfig, baseTagName == Global.ProxyTag); + return await Task.FromResult(0); + } + private async Task AddRangeOutbounds(List servers, SingboxConfig singboxConfig, bool prepend = true) + { + try + { + if (servers is null || servers.Count <= 0) + { + return 0; + } + var outbounds = servers.Where(s => s is Outbound4Sbox).Cast().ToList(); + var endpoints = servers.Where(s => s is Endpoints4Sbox).Cast().ToList(); + singboxConfig.endpoints ??= new(); + if (prepend) + { + singboxConfig.outbounds.InsertRange(0, outbounds); + singboxConfig.endpoints.InsertRange(0, endpoints); + } + else + { + singboxConfig.outbounds.AddRange(outbounds); + singboxConfig.endpoints.AddRange(endpoints); + } + } + catch (Exception ex) + { + Logging.SaveLog(_tag, ex); + } return await Task.FromResult(0); } } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs index d9b271ff..3994e21b 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs @@ -376,7 +376,7 @@ public partial class CoreConfigSingboxService return Global.ProxyTag; } - var tag = Global.ProxyTag + node.IndexId.ToString(); + var tag = $"{node.IndexId}-{Global.ProxyTag}"; if (singboxConfig.outbounds.Any(o => o.tag == tag) || (singboxConfig.endpoints != null && singboxConfig.endpoints.Any(e => e.tag == tag))) { @@ -385,11 +385,10 @@ public partial class CoreConfigSingboxService if (node.ConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain) { - var childBaseTagName = $"{Global.ProxyTag}-{node.IndexId}"; - var ret = await GenGroupOutbound(node, singboxConfig, childBaseTagName); + var ret = await GenGroupOutbound(node, singboxConfig, tag); if (ret == 0) { - return childBaseTagName; + return tag; } return Global.ProxyTag; } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs index 195c0669..b16ccac2 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs @@ -1,3 +1,5 @@ +using ServiceLib.Models; + namespace ServiceLib.Services.CoreConfig; public partial class CoreConfigV2rayService @@ -728,9 +730,17 @@ public partial class CoreConfigV2rayService } // Merge results: first the main chain outbounds, then other outbounds, and finally utility outbounds - resultOutbounds.AddRange(prevOutbounds); - resultOutbounds.AddRange(v2rayConfig.outbounds); - v2rayConfig.outbounds = resultOutbounds; + if (baseTagName == Global.ProxyTag) + { + resultOutbounds.AddRange(prevOutbounds); + resultOutbounds.AddRange(v2rayConfig.outbounds); + v2rayConfig.outbounds = resultOutbounds; + } + else + { + v2rayConfig.outbounds.AddRange(prevOutbounds); + v2rayConfig.outbounds.AddRange(resultOutbounds); + } } catch (Exception ex) { @@ -842,13 +852,19 @@ public partial class CoreConfigV2rayService outbound.tag = baseTagName + (i + 1).ToString(); resultOutbounds.Add(outbound); } - v2rayConfig.outbounds ??= new(); - resultOutbounds.AddRange(v2rayConfig.outbounds); - v2rayConfig.outbounds = resultOutbounds; + if (baseTagName == Global.ProxyTag) + { + resultOutbounds.AddRange(v2rayConfig.outbounds); + v2rayConfig.outbounds = resultOutbounds; + } + else + { + v2rayConfig.outbounds.AddRange(resultOutbounds); + } return await Task.FromResult(0); } - private async Task GenChainOutboundsList(List nodes, V2rayConfig v2RayConfig, string baseTagName = Global.ProxyTag) + private async Task GenChainOutboundsList(List nodes, V2rayConfig v2rayConfig, string baseTagName = Global.ProxyTag) { // Based on actual network flow instead of data packets var nodesReverse = nodes.AsEnumerable().Reverse().ToList(); @@ -889,9 +905,15 @@ public partial class CoreConfigV2rayService resultOutbounds.Add(outbound); } - v2RayConfig.outbounds ??= new(); - resultOutbounds.AddRange(v2RayConfig.outbounds); - v2RayConfig.outbounds = resultOutbounds; + if (baseTagName == Global.ProxyTag) + { + resultOutbounds.AddRange(v2rayConfig.outbounds); + v2rayConfig.outbounds = resultOutbounds; + } + else + { + v2rayConfig.outbounds.AddRange(resultOutbounds); + } return await Task.FromResult(0); } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs index a82c5974..e4a67410 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs @@ -133,7 +133,7 @@ public partial class CoreConfigV2rayService return Global.ProxyTag; } - var tag = Global.ProxyTag + node.IndexId.ToString(); + var tag = $"{node.IndexId}-{Global.ProxyTag}"; if (v2rayConfig.outbounds.Any(p => p.tag == tag)) { return tag; @@ -141,11 +141,10 @@ public partial class CoreConfigV2rayService if (node.ConfigType is EConfigType.PolicyGroup or EConfigType.ProxyChain) { - var childBaseTagName = $"{Global.ProxyTag}-{node.IndexId}"; - var ret = await GenGroupOutbound(node, v2rayConfig, childBaseTagName); + var ret = await GenGroupOutbound(node, v2rayConfig, tag); if (ret == 0) { - return childBaseTagName; + return tag; } return Global.ProxyTag; }