From e156aaae340678192ff06560eaf1e69b3ff0815d Mon Sep 17 00:00:00 2001 From: DHR60 Date: Thu, 24 Jul 2025 21:26:52 +0800 Subject: [PATCH] Fixes (Prioritize Node.CoreType over SplitCore configuration) --- v2rayN/ServiceLib/Handler/AppHandler.cs | 63 ++++++++++++++++++++++++ v2rayN/ServiceLib/Handler/CoreHandler.cs | 60 ++-------------------- 2 files changed, 66 insertions(+), 57 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/AppHandler.cs b/v2rayN/ServiceLib/Handler/AppHandler.cs index f3b7e449..1422ad73 100644 --- a/v2rayN/ServiceLib/Handler/AppHandler.cs +++ b/v2rayN/ServiceLib/Handler/AppHandler.cs @@ -1,3 +1,7 @@ +using DynamicData; +using ServiceLib.Enums; +using ServiceLib.Models; + namespace ServiceLib.Handler; public sealed class AppHandler @@ -246,5 +250,64 @@ public sealed class AppHandler return item?.CoreType ?? ECoreType.Xray; } + public (bool, ECoreType, ECoreType?) GetCoreAndPreType(ProfileItem profileItem) + { + var splitCore = _config.SplitCoreItem.EnableSplitCore; + var coreType = GetCoreType(profileItem, profileItem.ConfigType); + ECoreType? preCoreType = null; + + var pureEndpointCore = GetSplitCoreType(profileItem, profileItem.ConfigType); + var splitRouteCore = _config.SplitCoreItem.RouteCoreType; + var enableTun = _config.TunModeItem.EnableTun; + + if (profileItem.ConfigType == EConfigType.Custom) + { + splitCore = false; + coreType = profileItem.CoreType ?? ECoreType.Xray; + if (profileItem.PreSocksPort > 0) + { + preCoreType = enableTun ? ECoreType.sing_box : GetCoreType(profileItem, profileItem.ConfigType); + } + else + { + preCoreType = null; + } + } + else if (!splitCore && profileItem.CoreType is not (ECoreType.Xray or ECoreType.sing_box)) + { + // Force SplitCore for cores that don't support direct routing (like Hysteria2, TUIC, etc.) + splitCore = true; + preCoreType = enableTun ? ECoreType.sing_box : splitRouteCore; + } + else if (splitCore) + { + // User explicitly enabled SplitCore + preCoreType = enableTun ? ECoreType.sing_box : splitRouteCore; + coreType = pureEndpointCore; + + if (preCoreType == coreType) + { + preCoreType = null; + splitCore = false; + } + } + else if (enableTun) // EnableTun is true but SplitCore is false + { + // TUN mode handling for Xray/sing_box cores + preCoreType = ECoreType.sing_box; + + if (preCoreType == coreType) // CoreType is sing_box + { + preCoreType = null; + } + else // CoreType is xray, etc. + { + // Force SplitCore for non-split cores + splitCore = true; + } + } + return (splitCore, coreType, preCoreType); + } + #endregion Core Type } diff --git a/v2rayN/ServiceLib/Handler/CoreHandler.cs b/v2rayN/ServiceLib/Handler/CoreHandler.cs index 14530e05..5b045238 100644 --- a/v2rayN/ServiceLib/Handler/CoreHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreHandler.cs @@ -76,7 +76,6 @@ public class CoreHandler // Create launch context and configure parameters var context = new CoreLaunchContext(node, _config); context.AdjustForConfigType(); - context.AdjustForSplitCore(); // Start main core if (!await CoreStart(context)) @@ -210,74 +209,21 @@ public class CoreHandler /// public void AdjustForConfigType() { + (SplitCore, CoreType, PreCoreType) = AppHandler.Instance.GetCoreAndPreType(Node); if (Node.ConfigType == EConfigType.Custom) { - SplitCore = false; - CoreType = Node.CoreType ?? ECoreType.Xray; if (Node.PreSocksPort > 0) { - PreCoreType = EnableTun ? ECoreType.sing_box : AppHandler.Instance.GetCoreType(Node, Node.ConfigType); PreSocksPort = Node.PreSocksPort.Value; } else { EnableTun = false; - PreCoreType = null; } } - else if (!SplitCore && !EnableTun) + else if (PreCoreType != null) { - switch (Node.CoreType) - { - case ECoreType.hysteria2: - case ECoreType.tuic: - Node.CoreType = ECoreType.sing_box; - CoreType = ECoreType.sing_box; - break; - case ECoreType.v2fly: - case ECoreType.v2fly_v5: - Node.CoreType = ECoreType.Xray; - CoreType = ECoreType.Xray; - break; - default: - break; - } - } - } - - /// - /// Adjust split core configuration - /// - public void AdjustForSplitCore() - { - if (SplitCore) - { - PreCoreType = EnableTun ? ECoreType.sing_box : SplitRouteCore; - CoreType = PureEndpointCore; - - if (PreCoreType == CoreType) - { - PreCoreType = null; - SplitCore = false; - } - else - { - PreSocksPort = AppHandler.Instance.GetLocalPort(EInboundProtocol.split); - } - } - else if (EnableTun) - { - PreCoreType = ECoreType.sing_box; - - if (PreCoreType == CoreType) // CoreType is sing_box - { - PreCoreType = null; - } - else // CoreType is xray, hysteria2, tuic, etc. - { - SplitCore = true; - PreSocksPort = AppHandler.Instance.GetLocalPort(EInboundProtocol.split); - } + PreSocksPort = AppHandler.Instance.GetLocalPort(EInboundProtocol.split); } } }