From c76c302e443478e619f022e26284014af3b8fe10 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Wed, 25 Feb 2026 22:44:31 +0800 Subject: [PATCH] Fix --- v2rayN/ServiceLib/Handler/ConfigHandler.cs | 41 +++++++++++++++++++ v2rayN/ServiceLib/Manager/CoreManager.cs | 34 +++------------ .../V2ray/CoreConfigV2rayService.cs | 2 +- 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index b9a369ec..5f1073e4 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -1249,6 +1249,47 @@ public static class ConfigHandler return itemSocks; } + public static CoreConfigContext? GetPreSocksCoreConfigContext(CoreConfigContext nodeContext) + { + var config = nodeContext.AppConfig; + var node = nodeContext.Node; + var coreType = AppManager.Instance.GetCoreType(node, node.ConfigType); + + var preSocksItem = GetPreSocksItem(config, node, coreType); + if (preSocksItem != null) + { + return nodeContext with { Node = preSocksItem, }; + } + + if ((!nodeContext.IsTunEnabled) + || coreType != ECoreType.Xray + || node.ConfigType == EConfigType.Custom) + { + return null; + } + var tunProtectSsPort = Utils.GetFreePort(); + var proxyRelaySsPort = Utils.GetFreePort(); + var preItem = new ProfileItem() + { + CoreType = ECoreType.sing_box, + ConfigType = EConfigType.Shadowsocks, + Address = Global.Loopback, + Port = proxyRelaySsPort, + Password = Global.None, + }; + preItem.SetProtocolExtra(preItem.GetProtocolExtra() with + { + SsMethod = Global.None, + }); + var preContext = nodeContext with + { + Node = preItem, + TunProtectSsPort = tunProtectSsPort, + ProxyRelaySsPort = proxyRelaySsPort, + }; + return preContext; + } + /// /// Remove servers with invalid test results (timeout) /// Useful for cleaning up subscription lists diff --git a/v2rayN/ServiceLib/Manager/CoreManager.cs b/v2rayN/ServiceLib/Manager/CoreManager.cs index 0e4c33d6..3efa4592 100644 --- a/v2rayN/ServiceLib/Manager/CoreManager.cs +++ b/v2rayN/ServiceLib/Manager/CoreManager.cs @@ -67,37 +67,13 @@ public class CoreManager var fileName = Utils.GetBinConfigPath(Global.CoreConfigFileName); var context = await CoreConfigHandler.BuildCoreConfigContext(_config, node); - CoreConfigContext? preContext = null; - if (context.IsTunEnabled) + var preContext = ConfigHandler.GetPreSocksCoreConfigContext(context); + if (preContext is not null) { - var coreType = AppManager.Instance.GetCoreType(node, node.ConfigType); - if (coreType == ECoreType.Xray && node.ConfigType != EConfigType.Custom) + context = context with { - var tunProtectSsPort = Utils.GetFreePort(); - var proxyRelaySsPort = Utils.GetFreePort(); - context = context with { TunProtectSsPort = tunProtectSsPort, ProxyRelaySsPort = proxyRelaySsPort, }; - var preItem = new ProfileItem() - { - CoreType = ECoreType.sing_box, - ConfigType = EConfigType.Shadowsocks, - Address = Global.Loopback, - Port = proxyRelaySsPort, - Password = Global.None, - }; - preItem.SetProtocolExtra(preItem.GetProtocolExtra() with - { - SsMethod = Global.None, - }); - preContext = context with { Node = preItem, }; - } - else - { - var preItem = ConfigHandler.GetPreSocksItem(_config, node, coreType); - if (preItem is not null) - { - preContext = context with { Node = preItem, }; - } - } + TunProtectSsPort = preContext.TunProtectSsPort, ProxyRelaySsPort = preContext.ProxyRelaySsPort, + }; } var result = await CoreConfigHandler.GenerateClientConfig(context, fileName); if (result.Success != true) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs index 6bb64313..eec40809 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/CoreConfigV2rayService.cs @@ -304,7 +304,7 @@ public partial class CoreConfigV2rayService(CoreConfigContext context) var protectNode = new ProfileItem() { - CoreType = ECoreType.sing_box, + CoreType = ECoreType.Xray, ConfigType = EConfigType.Shadowsocks, Address = Global.Loopback, Port = context.TunProtectSsPort,