diff --git a/v2rayN/ServiceLib/Models/ProfileGroupItem.cs b/v2rayN/ServiceLib/Models/ProfileGroupItem.cs index eb6ac49f..bb1615ae 100644 --- a/v2rayN/ServiceLib/Models/ProfileGroupItem.cs +++ b/v2rayN/ServiceLib/Models/ProfileGroupItem.cs @@ -1,4 +1,5 @@ using SQLite; + namespace ServiceLib.Models; [Serializable] @@ -10,4 +11,48 @@ public class ProfileGroupItem public string ChildItems { get; set; } public EMultipleLoad MultipleLoad { get; set; } = EMultipleLoad.LeastPing; + + public bool HasCycle() + { + return HasCycle(new HashSet(), new HashSet()); + } + + public bool HasCycle(HashSet visited, HashSet stack) + { + if (string.IsNullOrEmpty(ParentIndexId)) + return false; + + if (stack.Contains(ParentIndexId)) + return true; + + if (visited.Contains(ParentIndexId)) + return false; + + visited.Add(ParentIndexId); + stack.Add(ParentIndexId); + + if (string.IsNullOrEmpty(ChildItems)) + { + return false; + } + + var childIds = Utils.String2List(ChildItems) + .Where(p => !string.IsNullOrEmpty(p)) + .ToList(); + + var childProfiles = childIds.Select(ProfileGroupItemManager.Instance.GetOrDefault)//这里是内存访问 + .Where(p => p != null) + .ToList(); + + foreach (var child in childProfiles) + { + if (child.HasCycle(visited, stack)) + { + return true; + } + } + + stack.Remove(ParentIndexId); + return false; + } } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs index b16ccac2..2fbf42b5 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayOutboundService.cs @@ -496,7 +496,8 @@ public partial class CoreConfigV2rayService return -1; } - var hasCycle = await node.HasCycle(new HashSet(), new HashSet()); + var hasCycle = profileGroupItem.HasCycle(); + //var hasCycle = await node.HasCycle(new HashSet(), new HashSet()); if (hasCycle) { return -1;