From 631968459257af331982c1061498d954ac457694 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Wed, 30 Jul 2025 09:08:59 +0800 Subject: [PATCH] Enhances core configuration handling. --- v2rayN/ServiceLib/Common/Utils.cs | 21 +++++++++++++++++-- v2rayN/ServiceLib/Global.cs | 4 ++-- v2rayN/ServiceLib/Handler/CoreAdminHandler.cs | 2 +- v2rayN/ServiceLib/Handler/CoreHandler.cs | 20 +++++++++--------- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/v2rayN/ServiceLib/Common/Utils.cs b/v2rayN/ServiceLib/Common/Utils.cs index 38869ebb..f7fd3445 100644 --- a/v2rayN/ServiceLib/Common/Utils.cs +++ b/v2rayN/ServiceLib/Common/Utils.cs @@ -813,7 +813,7 @@ public class Utils } } - public static string GetBinConfigPath(string filename = "") + public static string GetBinConfigPath(string filename = "", ECoreType coreType = ECoreType.v2rayN) { var tempPath = Path.Combine(StartupPath(), "binConfigs"); if (!Directory.Exists(tempPath)) @@ -827,10 +827,27 @@ public class Utils } else { - return Path.Combine(tempPath, filename); + return Path.Combine(tempPath, GetBinConfigFileName(filename, coreType)); } } + public static string GetBinConfigFileName(string filename, ECoreType coreType = ECoreType.v2rayN) + { + var fileSuffix = coreType switch + { + ECoreType.sing_box => ".json", + ECoreType.Xray => ".json", + ECoreType.hysteria2 => ".json", + ECoreType.naiveproxy => ".json", + ECoreType.tuic => ".json", + ECoreType.juicity => ".json", + ECoreType.brook => ".cac", + ECoreType.shadowquic => ".yaml", + _ => string.Empty + }; + return filename.EndsWith(fileSuffix) ? filename : $"{filename}{fileSuffix}"; + } + #endregion TempPath #region Platform diff --git a/v2rayN/ServiceLib/Global.cs b/v2rayN/ServiceLib/Global.cs index f7371cf2..773f0bc9 100644 --- a/v2rayN/ServiceLib/Global.cs +++ b/v2rayN/ServiceLib/Global.cs @@ -12,8 +12,8 @@ public class Global public const string PromotionUrl = @"aHR0cHM6Ly85LjIzNDQ1Ni54eXovYWJjLmh0bWw="; public const string ConfigFileName = "guiNConfig.json"; - public const string CoreConfigFileName = "config.json"; - public const string CorePreConfigFileName = "configPre.json"; + public const string CoreConfigFileName = "config"; + public const string CorePreConfigFileName = "configPre"; public const string CoreSpeedtestConfigFileName = "configTest{0}.json"; public const string CoreMultipleLoadConfigFileName = "configMultipleLoad.json"; public const string ClashMixinConfigFileName = "Mixin.yaml"; diff --git a/v2rayN/ServiceLib/Handler/CoreAdminHandler.cs b/v2rayN/ServiceLib/Handler/CoreAdminHandler.cs index ec99b26b..9ce5a411 100644 --- a/v2rayN/ServiceLib/Handler/CoreAdminHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreAdminHandler.cs @@ -35,7 +35,7 @@ public class CoreAdminHandler { StringBuilder sb = new(); sb.AppendLine("#!/bin/bash"); - var cmdLine = $"{fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetBinConfigPath(configPath).AppendQuotes())}"; + var cmdLine = $"{fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetBinConfigPath(configPath, coreInfo.CoreType).AppendQuotes())}"; sb.AppendLine($"sudo -S {cmdLine}"); var shFilePath = await FileManager.CreateLinuxShellFile("run_as_sudo.sh", sb.ToString(), true); diff --git a/v2rayN/ServiceLib/Handler/CoreHandler.cs b/v2rayN/ServiceLib/Handler/CoreHandler.cs index 201b9ba6..3ab29804 100644 --- a/v2rayN/ServiceLib/Handler/CoreHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreHandler.cs @@ -101,7 +101,7 @@ public class CoreHandler { var coreType = selecteds.Exists(t => t.ConfigType is EConfigType.Hysteria2 or EConfigType.TUIC or EConfigType.Anytls) ? ECoreType.sing_box : ECoreType.Xray; var fileName = string.Format(Global.CoreSpeedtestConfigFileName, Utils.GetGuid(false)); - var configPath = Utils.GetBinConfigPath(fileName); + var configPath = Utils.GetBinConfigPath(fileName, coreType); var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType); UpdateFunc(false, result.Msg); if (result.Success != true) @@ -130,15 +130,15 @@ public class CoreHandler return -1; } + var coreType = AppHandler.Instance.GetCoreType(node, node.ConfigType); var fileName = string.Format(Global.CoreSpeedtestConfigFileName, Utils.GetGuid(false)); - var configPath = Utils.GetBinConfigPath(fileName); + var configPath = Utils.GetBinConfigPath(fileName, coreType); var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, node, testItem, configPath); if (result.Success != true) { return -1; } - var coreType = AppHandler.Instance.GetCoreType(node, node.ConfigType); var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(coreType); var proc = await RunProcess(coreInfo, fileName, true, false); if (proc is null) @@ -231,7 +231,8 @@ public class CoreHandler private async Task CoreStart(CoreLaunchContext context) { - var fileName = Utils.GetBinConfigPath(Global.CoreConfigFileName); + var coreType = context.SplitCore ? context.PureEndpointCore : context.CoreType; + var fileName = Utils.GetBinConfigPath(Global.CoreConfigFileName, coreType); var result = context.SplitCore ? await CoreConfigHandler.GeneratePureEndpointConfig(context.Node, fileName) : await CoreConfigHandler.GenerateClientConfig(context.Node, fileName); @@ -257,7 +258,7 @@ public class CoreHandler var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(context.CoreType); var displayLog = context.Node.ConfigType != EConfigType.Custom || context.Node.DisplayLog; - var proc = await RunProcess(coreInfo, Global.CoreConfigFileName, displayLog, true); + var proc = await RunProcess(coreInfo, Utils.GetBinConfigFileName(Global.CoreConfigFileName, coreType), displayLog, true); if (proc is null) { @@ -266,8 +267,7 @@ public class CoreHandler } _process = proc; - var (_, coreType, preCoreType) = AppHandler.Instance.GetCoreAndPreType(context.Node); - _config.RunningCoreType = (ECoreType)(preCoreType != null ? preCoreType : coreType); + _config.RunningCoreType = (ECoreType)(context.PreCoreType != null ? context.PreCoreType : coreType); return true; } @@ -278,7 +278,7 @@ public class CoreHandler return true; // No pre-core needed, consider successful } - var fileName = Utils.GetBinConfigPath(Global.CorePreConfigFileName); + var fileName = Utils.GetBinConfigPath(Global.CorePreConfigFileName, (ECoreType)context.PreCoreType); var itemSocks = new ProfileItem() { CoreType = context.PreCoreType, @@ -296,7 +296,7 @@ public class CoreHandler } var coreInfo = CoreInfoHandler.Instance.GetCoreInfo((ECoreType)context.PreCoreType); - var proc = await RunProcess(coreInfo, Global.CorePreConfigFileName, true, true); + var proc = await RunProcess(coreInfo, Utils.GetBinConfigFileName(Global.CorePreConfigFileName, (ECoreType)context.PreCoreType), true, true); if (proc is null || (_process?.HasExited == true)) { @@ -355,7 +355,7 @@ public class CoreHandler StartInfo = new() { FileName = fileName, - Arguments = string.Format(coreInfo.Arguments, coreInfo.AbsolutePath ? Utils.GetBinConfigPath(configPath).AppendQuotes() : configPath), + Arguments = string.Format(coreInfo.Arguments, coreInfo.AbsolutePath ? Utils.GetBinConfigPath(configPath, coreInfo.CoreType).AppendQuotes() : configPath), WorkingDirectory = Utils.GetBinConfigPath(), UseShellExecute = false, RedirectStandardOutput = displayLog,