diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index 99348260..8f5d0b15 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -27,6 +27,7 @@ public const string ConfigFileName = "guiNConfig.json"; public const string ConfigDB = "guiNDB.db"; public const string coreConfigFileName = "config.json"; + public const string corePreConfigFileName = "configPre.json"; public const string v2raySampleClient = "v2rayN.Sample.SampleClientConfig"; public const string SingboxSampleClient = "v2rayN.Sample.SingboxSampleClientConfig"; diff --git a/v2rayN/v2rayN/Handler/CoreHandler.cs b/v2rayN/v2rayN/Handler/CoreHandler.cs index 818cfaf2..9875ff85 100644 --- a/v2rayN/v2rayN/Handler/CoreHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreHandler.cs @@ -11,10 +11,9 @@ namespace v2rayN.Handler /// internal class CoreHandler { - private static string _coreCConfigRes = Global.coreConfigFileName; private CoreInfo? _coreInfo; - private int _processId = 0; private Process? _process; + private Process? _processPre; private Action _updateFunc; public CoreHandler(Action update) @@ -39,7 +38,7 @@ namespace v2rayN.Handler ShowMsg(false, ResUI.CheckServerSettings); return; } - string fileName = Utils.GetConfigPath(_coreCConfigRes); + string fileName = Utils.GetConfigPath(Global.coreConfigFileName); if (CoreConfigHandler.GenerateClientConfig(node, fileName, out string msg, out string content) != 0) { ShowMsg(false, msg); @@ -51,21 +50,6 @@ namespace v2rayN.Handler CoreStop(); CoreStart(node); } - - //start a socks service - if (_process != null && !_process.HasExited && node.configType == EConfigType.Custom && node.preSocksPort > 0) - { - var itemSocks = new ProfileItem() - { - configType = EConfigType.Socks, - address = Global.Loopback, - port = node.preSocksPort - }; - if (CoreConfigHandler.GenerateClientConfig(itemSocks, null, out string msg2, out string configStr) == 0) - { - _processId = CoreStartViaString(configStr); - } - } } public int LoadCoreConfigString(Config config, List _selecteds) @@ -114,10 +98,11 @@ namespace v2rayN.Handler } } - if (_processId > 0) + if (_processPre != null) { - CoreStopPid(_processId); - _processId = 0; + KillProcess(_processPre); + _processPre.Dispose(); + _processPre = null; } } catch (Exception ex) @@ -165,67 +150,33 @@ namespace v2rayN.Handler { ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString())); - try + var proc = RunProcess(node, _coreInfo, "", ShowMsg); + if (proc is null) { - string fileName = CoreFindexe(_coreInfo); - if (fileName == "") return; - - var displayLog = node.configType != EConfigType.Custom || node.displayLog; - Process p = new() - { - StartInfo = new ProcessStartInfo - { - FileName = fileName, - Arguments = _coreInfo.arguments, - WorkingDirectory = Utils.GetConfigPath(), - UseShellExecute = false, - RedirectStandardOutput = displayLog, - RedirectStandardError = displayLog, - CreateNoWindow = true, - StandardOutputEncoding = displayLog ? Encoding.UTF8 : null, - StandardErrorEncoding = displayLog ? Encoding.UTF8 : null, - } - }; - if (displayLog) - { - p.OutputDataReceived += (sender, e) => - { - if (!string.IsNullOrEmpty(e.Data)) - { - string msg = e.Data + Environment.NewLine; - ShowMsg(false, msg); - } - }; - p.ErrorDataReceived += (sender, e) => - { - if (!string.IsNullOrEmpty(e.Data)) - { - string msg = e.Data + Environment.NewLine; - ShowMsg(false, msg); - } - }; - } - p.Start(); - if (displayLog) - { - p.BeginOutputReadLine(); - p.BeginErrorReadLine(); - } - _process = p; - - if (p.WaitForExit(1000)) - { - throw new Exception(displayLog ? p.StandardError.ReadToEnd() : "启动进程失败并退出 (Failed to start the process and exited)"); - } - - Global.processJob.AddProcess(p.Handle); + return; } - catch (Exception ex) + _process = proc; + + //start a socks service + if (_process != null && !_process.HasExited && node.configType == EConfigType.Custom && node.preSocksPort > 0) { - //Utils.SaveLog(Utils.ToJson(node)); - Utils.SaveLog(ex.Message, ex); - string msg = ex.Message; - ShowMsg(true, msg); + var itemSocks = new ProfileItem() + { + coreType = ECoreType.sing_box, + configType = EConfigType.Socks, + address = Global.Loopback, + port = node.preSocksPort + }; + string fileName2 = Utils.GetConfigPath(Global.corePreConfigFileName); + if (CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2, out string msg2, out string configStr) == 0) + { + var coreInfo = LazyConfig.Instance.GetCoreInfo(ECoreType.sing_box); + var proc2 = RunProcess(node, coreInfo, $" -c {Global.corePreConfigFileName}", ShowMsg); + if (proc2 is not null) + { + _processPre = proc2; + } + } } } @@ -300,6 +251,93 @@ namespace v2rayN.Handler _updateFunc(updateToTrayTooltip, msg); } + private int SetCore(Config config, ProfileItem node) + { + if (node == null) + { + return -1; + } + var coreType = LazyConfig.Instance.GetCoreType(node, node.configType); + + _coreInfo = LazyConfig.Instance.GetCoreInfo(coreType); + + if (_coreInfo == null) + { + return -1; + } + return 0; + } + + #region Process + + private Process? RunProcess(ProfileItem node, CoreInfo coreInfo, string configPath, Action update) + { + try + { + string fileName = CoreFindexe(coreInfo); + if (Utils.IsNullOrEmpty(fileName)) + { + return null; + } + var displayLog = node.configType != EConfigType.Custom || node.displayLog; + Process proc = new() + { + StartInfo = new ProcessStartInfo + { + FileName = fileName, + Arguments = string.Format(coreInfo.arguments, configPath), + WorkingDirectory = Utils.GetConfigPath(), + UseShellExecute = false, + RedirectStandardOutput = displayLog, + RedirectStandardError = displayLog, + CreateNoWindow = true, + StandardOutputEncoding = displayLog ? Encoding.UTF8 : null, + StandardErrorEncoding = displayLog ? Encoding.UTF8 : null, + } + }; + if (displayLog) + { + proc.OutputDataReceived += (sender, e) => + { + if (!string.IsNullOrEmpty(e.Data)) + { + string msg = e.Data + Environment.NewLine; + update(false, msg); + } + }; + proc.ErrorDataReceived += (sender, e) => + { + if (!string.IsNullOrEmpty(e.Data)) + { + string msg = e.Data + Environment.NewLine; + update(false, msg); + } + }; + } + proc.Start(); + if (displayLog) + { + proc.BeginOutputReadLine(); + proc.BeginErrorReadLine(); + } + + if (proc.WaitForExit(1000)) + { + throw new Exception(displayLog ? proc.StandardError.ReadToEnd() : "启动进程失败并退出 (Failed to start the process and exited)"); + } + + Global.processJob.AddProcess(proc.Handle); + return proc; + } + catch (Exception ex) + { + Utils.SaveLog(ex.Message, ex); + string msg = ex.Message; + update(true, msg); + return null; + } + } + private void KillProcess(Process p) { try @@ -318,21 +356,6 @@ namespace v2rayN.Handler } } - private int SetCore(Config config, ProfileItem node) - { - if (node == null) - { - return -1; - } - var coreType = LazyConfig.Instance.GetCoreType(node, node.configType); - - _coreInfo = LazyConfig.Instance.GetCoreInfo(coreType); - - if (_coreInfo == null) - { - return -1; - } - return 0; - } + #endregion Process } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Handler/LazyConfig.cs b/v2rayN/v2rayN/Handler/LazyConfig.cs index 6c7e124a..1bf629f1 100644 --- a/v2rayN/v2rayN/Handler/LazyConfig.cs +++ b/v2rayN/v2rayN/Handler/LazyConfig.cs @@ -343,7 +343,7 @@ namespace v2rayN.Handler { coreType = ECoreType.sing_box, coreExes = new List { "sing-box-client", "sing-box" }, - arguments = "run", + arguments = "run{0}", coreUrl = Global.singboxCoreUrl, redirectInfo = true, coreReleaseApiUrl = Global.singboxCoreUrl.Replace(Global.githubUrl, Global.githubApiUrl), diff --git a/v2rayN/v2rayN/Handler/TunHandler.cs b/v2rayN/v2rayN/Handler/TunHandler.cs index 95184f72..d446bb82 100644 --- a/v2rayN/v2rayN/Handler/TunHandler.cs +++ b/v2rayN/v2rayN/Handler/TunHandler.cs @@ -186,7 +186,6 @@ namespace v2rayN.Base return true; } - private void routingDirectExe(out List lstDnsExe, out List lstDirectExe) { lstDnsExe = new(); diff --git a/v2rayN/v2rayN/Resx/ResUI.Designer.cs b/v2rayN/v2rayN/Resx/ResUI.Designer.cs index 950d18bf..c8fa1902 100644 --- a/v2rayN/v2rayN/Resx/ResUI.Designer.cs +++ b/v2rayN/v2rayN/Resx/ResUI.Designer.cs @@ -2951,7 +2951,7 @@ namespace v2rayN.Resx { } /// - /// 查找类似 * After setting this value, an socks service will be started using V2ray to provide functions such as speed display 的本地化字符串。 + /// 查找类似 * After setting this value, an socks service will be started using Sing-box to provide functions such as speed display 的本地化字符串。 /// public static string TipPreSocksPort { get { diff --git a/v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx b/v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx index 6e5d989e..6bd3a9ec 100644 --- a/v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx +++ b/v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx @@ -701,7 +701,7 @@ txtPreSocksPort - * After setting this value, an socks service will be started using V2ray to provide functions such as speed display + * After setting this value, an socks service will be started using Sing-box to provide functions such as speed display Browse diff --git a/v2rayN/v2rayN/Resx/ResUI.resx b/v2rayN/v2rayN/Resx/ResUI.resx index cdeb2dc4..e67f580d 100644 --- a/v2rayN/v2rayN/Resx/ResUI.resx +++ b/v2rayN/v2rayN/Resx/ResUI.resx @@ -701,7 +701,7 @@ txtPreSocksPort - * After setting this value, an socks service will be started using V2ray to provide functions such as speed display + * After setting this value, an socks service will be started using Sing-box to provide functions such as speed display Browse diff --git a/v2rayN/v2rayN/Resx/ResUI.ru.resx b/v2rayN/v2rayN/Resx/ResUI.ru.resx index c6ef7309..9307e2ae 100644 --- a/v2rayN/v2rayN/Resx/ResUI.ru.resx +++ b/v2rayN/v2rayN/Resx/ResUI.ru.resx @@ -701,7 +701,7 @@ txtPreSocksPort - * После установки этого значения служба socks будет запущена с использованием V2ray для обеспечения таких функций, как отображение скорости + * После установки этого значения служба socks будет запущена с использованием Sing-box для обеспечения таких функций, как отображение скорости Просмотр diff --git a/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx b/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx index 5493598e..ed7ee730 100644 --- a/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx +++ b/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx @@ -701,7 +701,7 @@ Socks端口 - * 自定义配置的Socks端口值,可不设置;当设置此值后,将使用V2ray-core额外启动一个前置Socks服务,提供分流和速度显示等功能 + * 自定义配置的Socks端口值,可不设置;当设置此值后,将使用Sing-box额外启动一个前置Socks服务,提供分流和速度显示等功能 浏览