From 83b4ab83d1417eca08d3f9d268e2ce03535bd584 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Fri, 29 Dec 2023 09:39:55 +0800 Subject: [PATCH] =?UTF-8?q?Improve=20speed=20test=EF=BC=8Cadd=20Hy2=20and?= =?UTF-8?q?=20Tuic=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v2rayN/v2rayN/Global.cs | 1 + v2rayN/v2rayN/Handler/CoreConfigHandler.cs | 22 ++- v2rayN/v2rayN/Handler/CoreConfigSingbox.cs | 172 +++++++++++++++++++-- v2rayN/v2rayN/Handler/CoreConfigV2ray.cs | 20 ++- v2rayN/v2rayN/Handler/CoreHandler.cs | 85 +++------- v2rayN/v2rayN/Handler/LazyConfig.cs | 4 +- v2rayN/v2rayN/Handler/SpeedtestHandler.cs | 13 +- 7 files changed, 222 insertions(+), 95 deletions(-) diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index 81858e25..ea7fe878 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -33,6 +33,7 @@ namespace v2rayN public const string ConfigDB = "guiNDB.db"; public const string CoreConfigFileName = "config.json"; public const string CorePreConfigFileName = "configPre.json"; + public const string CoreSpeedtestConfigFileName = "configSpeedtest.json"; public const string V2raySampleClient = "v2rayN.Sample.SampleClientConfig"; public const string SingboxSampleClient = "v2rayN.Sample.SingboxSampleClientConfig"; public const string V2raySampleHttprequestFileName = "v2rayN.Sample.SampleHttprequest"; diff --git a/v2rayN/v2rayN/Handler/CoreConfigHandler.cs b/v2rayN/v2rayN/Handler/CoreConfigHandler.cs index 00f4e9ec..2fca828a 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigHandler.cs @@ -150,10 +150,26 @@ namespace v2rayN.Handler return 0; } - public static string GenerateClientSpeedtestConfigString(Config config, List selecteds, out string msg) + public static int GenerateClientSpeedtestConfig(Config config, string fileName, List selecteds, ECoreType coreType, out string msg) { - var coreConfigV2ray = new CoreConfigV2ray(config); - return coreConfigV2ray.GenerateClientSpeedtestConfigString(selecteds, out msg); + if (coreType == ECoreType.sing_box) + { + if ((new CoreConfigSingbox(config)).GenerateClientSpeedtestConfig(selecteds, out SingboxConfig? singboxConfig, out msg) != 0) + { + return -1; + } + Utils.ToJsonFile(singboxConfig, fileName, false); + } + else + { + if ((new CoreConfigV2ray(config)).GenerateClientSpeedtestConfig(selecteds, out V2rayConfig? v2rayConfig, out msg) != 0) + { + return -1; + } + + Utils.ToJsonFile(v2rayConfig, fileName, false); + } + return 0; } } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs index 7c012342..bf8c3f75 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs @@ -1,4 +1,6 @@ -using v2rayN.Base; +using System.Net; +using System.Net.NetworkInformation; +using v2rayN.Base; using v2rayN.Mode; using v2rayN.Resx; @@ -66,6 +68,8 @@ namespace v2rayN.Handler return 0; } + #region private gen function + private int GenLog(SingboxConfig singboxConfig) { try @@ -102,8 +106,6 @@ namespace v2rayN.Handler return 0; } - #region inbound private - private int GenInbounds(SingboxConfig singboxConfig) { try @@ -204,10 +206,6 @@ namespace v2rayN.Handler return inbound; } - #endregion inbound private - - #region outbound private - private int GenOutbound(ProfileItem node, Outbound4Sbox outbound) { try @@ -491,10 +489,6 @@ namespace v2rayN.Handler return 0; } - #endregion outbound private - - #region routing rule private - private int GenRouting(SingboxConfig singboxConfig) { try @@ -720,10 +714,6 @@ namespace v2rayN.Handler } } - #endregion routing rule private - - #region dns private - private int GenDns(ProfileItem node, SingboxConfig singboxConfig) { try @@ -780,8 +770,6 @@ namespace v2rayN.Handler return 0; } - #endregion dns private - private int GenStatistic(SingboxConfig singboxConfig) { if (_config.guiItem.enableStatistics) @@ -805,5 +793,155 @@ namespace v2rayN.Handler } return 0; } + + #endregion private gen function + + #region Gen speedtest config + + public int GenerateClientSpeedtestConfig(List selecteds, out SingboxConfig? singboxConfig, out string msg) + { + singboxConfig = null; + try + { + if (_config == null) + { + msg = ResUI.CheckServerSettings; + return -1; + } + + msg = ResUI.InitialConfiguration; + + string result = Utils.GetEmbedText(Global.SingboxSampleClient); + string txtOutbound = Utils.GetEmbedText(Global.SingboxSampleOutbound); + if (Utils.IsNullOrEmpty(result) || txtOutbound.IsNullOrEmpty()) + { + msg = ResUI.FailedGetDefaultConfiguration; + return -1; + } + + singboxConfig = Utils.FromJson(result); + if (singboxConfig == null) + { + msg = ResUI.FailedGenDefaultConfiguration; + return -1; + } + List lstIpEndPoints = new(); + List lstTcpConns = new(); + try + { + lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()); + lstIpEndPoints.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners()); + lstTcpConns.AddRange(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections()); + } + catch (Exception ex) + { + Utils.SaveLog(ex.Message, ex); + } + + GenLog(singboxConfig); + GenDns(new(), singboxConfig); + singboxConfig.inbounds.Clear(); // Remove "proxy" service for speedtest, avoiding port conflicts. + singboxConfig.outbounds.RemoveAt(0); + + int httpPort = LazyConfig.Instance.GetLocalPort("speedtest"); + + foreach (var it in selecteds) + { + if (it.configType == EConfigType.Custom) + { + continue; + } + if (it.port <= 0) + { + continue; + } + if (it.configType is EConfigType.VMess or EConfigType.VLESS) + { + var item2 = LazyConfig.Instance.GetProfileItem(it.indexId); + if (item2 is null || Utils.IsNullOrEmpty(item2.id) || !Utils.IsGuidByParse(item2.id)) + { + continue; + } + } + + //find unuse port + var port = httpPort; + for (int k = httpPort; k < Global.MaxPort; k++) + { + if (lstIpEndPoints?.FindIndex(_it => _it.Port == k) >= 0) + { + continue; + } + if (lstTcpConns?.FindIndex(_it => _it.LocalEndPoint.Port == k) >= 0) + { + continue; + } + //found + port = k; + httpPort = port + 1; + break; + } + + //Port In Used + if (lstIpEndPoints?.FindIndex(_it => _it.Port == port) >= 0) + { + continue; + } + it.port = port; + it.allowTest = true; + + //inbound + Inbound4Sbox inbound = new() + { + listen = Global.Loopback, + listen_port = port, + type = Global.InboundHttp + }; + inbound.tag = Global.InboundHttp + inbound.listen_port.ToString(); + singboxConfig.inbounds.Add(inbound); + + //outbound + var item = LazyConfig.Instance.GetProfileItem(it.indexId); + if (item is null) + { + continue; + } + if (item.configType == EConfigType.Shadowsocks + && !Global.SsSecuritysInXray.Contains(item.security)) + { + continue; + } + if (item.configType == EConfigType.VLESS + && !Global.Flows.Contains(item.flow)) + { + continue; + } + + var outbound = Utils.FromJson(txtOutbound); + GenOutbound(item, outbound); + outbound.tag = Global.ProxyTag + inbound.listen_port.ToString(); + singboxConfig.outbounds.Add(outbound); + + //rule + Rule4Sbox rule = new() + { + inbound = new List { inbound.tag }, + outbound = outbound.tag + }; + singboxConfig.route.rules.Add(rule); + } + + //msg = string.Format(ResUI.SuccessfulConfiguration"), node.getSummary()); + return 0; + } + catch (Exception ex) + { + Utils.SaveLog(ex.Message, ex); + msg = ResUI.FailedGenDefaultConfiguration; + return -1; + } + } + + #endregion Gen speedtest config } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs index 8121f7a8..1f1eb390 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs @@ -70,6 +70,8 @@ namespace v2rayN.Handler return 0; } + #region private gen function + private int GenLog(V2rayConfig v2rayConfig) { try @@ -885,16 +887,19 @@ namespace v2rayN.Handler return 0; } + #endregion private gen function + #region Gen speedtest config - public string GenerateClientSpeedtestConfigString(List selecteds, out string msg) + public int GenerateClientSpeedtestConfig(List selecteds, out V2rayConfig? v2rayConfig, out string msg) { + v2rayConfig = null; try { if (_config == null) { msg = ResUI.CheckServerSettings; - return ""; + return -1; } msg = ResUI.InitialConfiguration; @@ -904,14 +909,14 @@ namespace v2rayN.Handler if (Utils.IsNullOrEmpty(result) || txtOutbound.IsNullOrEmpty()) { msg = ResUI.FailedGetDefaultConfiguration; - return ""; + return -1; } - var v2rayConfig = Utils.FromJson(result); + v2rayConfig = Utils.FromJson(result); if (v2rayConfig == null) { msg = ResUI.FailedGenDefaultConfiguration; - return ""; + return -1; } List lstIpEndPoints = new(); List lstTcpConns = new(); @@ -928,6 +933,7 @@ namespace v2rayN.Handler GenLog(v2rayConfig); v2rayConfig.inbounds.Clear(); // Remove "proxy" service for speedtest, avoiding port conflicts. + v2rayConfig.outbounds.RemoveAt(0); int httpPort = LazyConfig.Instance.GetLocalPort("speedtest"); @@ -1019,13 +1025,13 @@ namespace v2rayN.Handler } //msg = string.Format(ResUI.SuccessfulConfiguration"), node.getSummary()); - return Utils.ToJson(v2rayConfig); + return 0; } catch (Exception ex) { Utils.SaveLog(ex.Message, ex); msg = ResUI.FailedGenDefaultConfiguration; - return ""; + return -1; } } diff --git a/v2rayN/v2rayN/Handler/CoreHandler.cs b/v2rayN/v2rayN/Handler/CoreHandler.cs index 377b0758..5a85e014 100644 --- a/v2rayN/v2rayN/Handler/CoreHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreHandler.cs @@ -67,18 +67,19 @@ namespace v2rayN.Handler } } - public int LoadCoreConfigString(List _selecteds) + public int LoadCoreConfigSpeedtest(List selecteds) { int pid = -1; - string configStr = CoreConfigHandler.GenerateClientSpeedtestConfigString(_config, _selecteds, out string msg); - if (configStr == "") + var coreType = selecteds.Exists(t => t.configType == EConfigType.Hysteria2 || t.configType == EConfigType.Tuic) ? ECoreType.sing_box : ECoreType.Xray; + string configPath = Utils.GetConfigPath(Global.CoreSpeedtestConfigFileName); + if (CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType, out string msg) != 0) { ShowMsg(false, msg); } else { ShowMsg(false, msg); - pid = CoreStartViaString(configStr); + pid = CoreStartSpeedtest(configPath, coreType); } return pid; } @@ -115,7 +116,7 @@ namespace v2rayN.Handler } foreach (string vName in it.coreExes) { - Process[] existing = Process.GetProcessesByName(vName); + var existing = Process.GetProcessesByName(vName); foreach (Process p in existing) { string? path = p.MainModule?.FileName; @@ -138,7 +139,7 @@ namespace v2rayN.Handler { try { - Process _p = Process.GetProcessById(pid); + var _p = Process.GetProcessById(pid); KillProcess(_p); } catch (Exception ex) @@ -218,62 +219,20 @@ namespace v2rayN.Handler } } - private int CoreStartViaString(string configStr) + private int CoreStartSpeedtest(string configPath, ECoreType coreType) { ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))); try { - var coreInfo = LazyConfig.Instance.GetCoreInfo(ECoreType.Xray); - string fileName = CoreFindexe(coreInfo); - if (fileName == "") return -1; - - Process p = new() + var coreInfo = LazyConfig.Instance.GetCoreInfo(coreType); + var proc = RunProcess(new(), coreInfo, $" -c {configPath}", true, ShowMsg); + if (proc is null) { - StartInfo = new ProcessStartInfo - { - FileName = fileName, - Arguments = "-config stdin:", - WorkingDirectory = Utils.GetConfigPath(), - UseShellExecute = false, - RedirectStandardInput = true, - RedirectStandardOutput = true, - RedirectStandardError = true, - CreateNoWindow = true, - StandardOutputEncoding = Encoding.UTF8, - StandardErrorEncoding = Encoding.UTF8 - } - }; - 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(); - p.BeginOutputReadLine(); - p.BeginErrorReadLine(); - - p.StandardInput.Write(configStr); - p.StandardInput.Close(); - - if (p.WaitForExit(1000)) - { - throw new Exception(p.StandardError.ReadToEnd()); + return -1; } - Global.ProcessJob.AddProcess(p.Handle); - return p.Id; + return proc.Id; } catch (Exception ex) { @@ -302,7 +261,7 @@ namespace v2rayN.Handler } Process proc = new() { - StartInfo = new ProcessStartInfo + StartInfo = new() { FileName = fileName, Arguments = string.Format(coreInfo.arguments, configPath), @@ -358,16 +317,20 @@ namespace v2rayN.Handler } } - private void KillProcess(Process p) + private void KillProcess(Process? proc) { + if (proc is null) + { + return; + } try { - p.CloseMainWindow(); - p.WaitForExit(100); - if (!p.HasExited) + proc.CloseMainWindow(); + proc.WaitForExit(100); + if (!proc.HasExited) { - p.Kill(); - p.WaitForExit(100); + proc.Kill(); + proc.WaitForExit(100); } } catch (Exception ex) diff --git a/v2rayN/v2rayN/Handler/LazyConfig.cs b/v2rayN/v2rayN/Handler/LazyConfig.cs index 6a98f916..73040e24 100644 --- a/v2rayN/v2rayN/Handler/LazyConfig.cs +++ b/v2rayN/v2rayN/Handler/LazyConfig.cs @@ -277,7 +277,7 @@ namespace v2rayN.Handler { coreType = ECoreType.Xray, coreExes = new List { "xray", "wxray" }, - arguments = "", + arguments = "run {0}", coreUrl = Global.XrayCoreUrl, coreReleaseApiUrl = Global.XrayCoreUrl.Replace(Global.GithubUrl, Global.GithubApiUrl), coreDownloadUrl32 = Global.XrayCoreUrl + "/download/{0}/Xray-windows-{1}.zip", @@ -364,7 +364,7 @@ namespace v2rayN.Handler { coreType = ECoreType.sing_box, coreExes = new List { "sing-box-client", "sing-box" }, - arguments = "run{0}", + arguments = "run {0} --disable-color", coreUrl = Global.SingboxCoreUrl, redirectInfo = true, coreReleaseApiUrl = Global.SingboxCoreUrl.Replace(Global.GithubUrl, Global.GithubApiUrl), diff --git a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs index 0b828973..dad40271 100644 --- a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs +++ b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs @@ -30,7 +30,7 @@ namespace v2rayN.Handler _selecteds = new List(); foreach (var it in selecteds) { - if (it.configType == EConfigType.Custom || it.configType == EConfigType.Hysteria2 || it.configType == EConfigType.Tuic) + if (it.configType == EConfigType.Custom) { continue; } @@ -150,7 +150,7 @@ namespace v2rayN.Handler { string msg = string.Empty; - pid = _coreHandler.LoadCoreConfigString(_selecteds); + pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds); if (pid < 0) { UpdateFunc("", ResUI.FailedToRunCore); @@ -196,7 +196,10 @@ namespace v2rayN.Handler } finally { - if (pid > 0) _coreHandler.CoreStopPid(pid); + if (pid > 0) + { + _coreHandler.CoreStopPid(pid); + } ProfileExHandler.Instance.SaveTo(); } @@ -211,7 +214,7 @@ namespace v2rayN.Handler // _selecteds = _selecteds.OrderBy(t => t.delay).ToList(); //} - pid = _coreHandler.LoadCoreConfigString(_selecteds); + pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds); if (pid < 0) { UpdateFunc("", ResUI.FailedToRunCore); @@ -268,7 +271,7 @@ namespace v2rayN.Handler private async Task RunSpeedTestMulti() { int pid = -1; - pid = _coreHandler.LoadCoreConfigString(_selecteds); + pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds); if (pid < 0) { UpdateFunc("", ResUI.FailedToRunCore);