Improve speed test,add Hy2 and Tuic test

This commit is contained in:
2dust 2023-12-29 09:39:55 +08:00
parent 34f63f9006
commit 83b4ab83d1
7 changed files with 222 additions and 95 deletions

View file

@ -33,6 +33,7 @@ namespace v2rayN
public const string ConfigDB = "guiNDB.db"; public const string ConfigDB = "guiNDB.db";
public const string CoreConfigFileName = "config.json"; public const string CoreConfigFileName = "config.json";
public const string CorePreConfigFileName = "configPre.json"; public const string CorePreConfigFileName = "configPre.json";
public const string CoreSpeedtestConfigFileName = "configSpeedtest.json";
public const string V2raySampleClient = "v2rayN.Sample.SampleClientConfig"; public const string V2raySampleClient = "v2rayN.Sample.SampleClientConfig";
public const string SingboxSampleClient = "v2rayN.Sample.SingboxSampleClientConfig"; public const string SingboxSampleClient = "v2rayN.Sample.SingboxSampleClientConfig";
public const string V2raySampleHttprequestFileName = "v2rayN.Sample.SampleHttprequest"; public const string V2raySampleHttprequestFileName = "v2rayN.Sample.SampleHttprequest";

View file

@ -150,10 +150,26 @@ namespace v2rayN.Handler
return 0; return 0;
} }
public static string GenerateClientSpeedtestConfigString(Config config, List<ServerTestItem> selecteds, out string msg) public static int GenerateClientSpeedtestConfig(Config config, string fileName, List<ServerTestItem> selecteds, ECoreType coreType, out string msg)
{ {
var coreConfigV2ray = new CoreConfigV2ray(config); if (coreType == ECoreType.sing_box)
return coreConfigV2ray.GenerateClientSpeedtestConfigString(selecteds, out msg); {
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;
} }
} }
} }

View file

@ -1,4 +1,6 @@
using v2rayN.Base; using System.Net;
using System.Net.NetworkInformation;
using v2rayN.Base;
using v2rayN.Mode; using v2rayN.Mode;
using v2rayN.Resx; using v2rayN.Resx;
@ -66,6 +68,8 @@ namespace v2rayN.Handler
return 0; return 0;
} }
#region private gen function
private int GenLog(SingboxConfig singboxConfig) private int GenLog(SingboxConfig singboxConfig)
{ {
try try
@ -102,8 +106,6 @@ namespace v2rayN.Handler
return 0; return 0;
} }
#region inbound private
private int GenInbounds(SingboxConfig singboxConfig) private int GenInbounds(SingboxConfig singboxConfig)
{ {
try try
@ -204,10 +206,6 @@ namespace v2rayN.Handler
return inbound; return inbound;
} }
#endregion inbound private
#region outbound private
private int GenOutbound(ProfileItem node, Outbound4Sbox outbound) private int GenOutbound(ProfileItem node, Outbound4Sbox outbound)
{ {
try try
@ -491,10 +489,6 @@ namespace v2rayN.Handler
return 0; return 0;
} }
#endregion outbound private
#region routing rule private
private int GenRouting(SingboxConfig singboxConfig) private int GenRouting(SingboxConfig singboxConfig)
{ {
try try
@ -720,10 +714,6 @@ namespace v2rayN.Handler
} }
} }
#endregion routing rule private
#region dns private
private int GenDns(ProfileItem node, SingboxConfig singboxConfig) private int GenDns(ProfileItem node, SingboxConfig singboxConfig)
{ {
try try
@ -780,8 +770,6 @@ namespace v2rayN.Handler
return 0; return 0;
} }
#endregion dns private
private int GenStatistic(SingboxConfig singboxConfig) private int GenStatistic(SingboxConfig singboxConfig)
{ {
if (_config.guiItem.enableStatistics) if (_config.guiItem.enableStatistics)
@ -805,5 +793,155 @@ namespace v2rayN.Handler
} }
return 0; return 0;
} }
#endregion private gen function
#region Gen speedtest config
public int GenerateClientSpeedtestConfig(List<ServerTestItem> 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<SingboxConfig>(result);
if (singboxConfig == null)
{
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
}
List<IPEndPoint> lstIpEndPoints = new();
List<TcpConnectionInformation> 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<Outbound4Sbox>(txtOutbound);
GenOutbound(item, outbound);
outbound.tag = Global.ProxyTag + inbound.listen_port.ToString();
singboxConfig.outbounds.Add(outbound);
//rule
Rule4Sbox rule = new()
{
inbound = new List<string> { 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
} }
} }

View file

@ -70,6 +70,8 @@ namespace v2rayN.Handler
return 0; return 0;
} }
#region private gen function
private int GenLog(V2rayConfig v2rayConfig) private int GenLog(V2rayConfig v2rayConfig)
{ {
try try
@ -885,16 +887,19 @@ namespace v2rayN.Handler
return 0; return 0;
} }
#endregion private gen function
#region Gen speedtest config #region Gen speedtest config
public string GenerateClientSpeedtestConfigString(List<ServerTestItem> selecteds, out string msg) public int GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds, out V2rayConfig? v2rayConfig, out string msg)
{ {
v2rayConfig = null;
try try
{ {
if (_config == null) if (_config == null)
{ {
msg = ResUI.CheckServerSettings; msg = ResUI.CheckServerSettings;
return ""; return -1;
} }
msg = ResUI.InitialConfiguration; msg = ResUI.InitialConfiguration;
@ -904,14 +909,14 @@ namespace v2rayN.Handler
if (Utils.IsNullOrEmpty(result) || txtOutbound.IsNullOrEmpty()) if (Utils.IsNullOrEmpty(result) || txtOutbound.IsNullOrEmpty())
{ {
msg = ResUI.FailedGetDefaultConfiguration; msg = ResUI.FailedGetDefaultConfiguration;
return ""; return -1;
} }
var v2rayConfig = Utils.FromJson<V2rayConfig>(result); v2rayConfig = Utils.FromJson<V2rayConfig>(result);
if (v2rayConfig == null) if (v2rayConfig == null)
{ {
msg = ResUI.FailedGenDefaultConfiguration; msg = ResUI.FailedGenDefaultConfiguration;
return ""; return -1;
} }
List<IPEndPoint> lstIpEndPoints = new(); List<IPEndPoint> lstIpEndPoints = new();
List<TcpConnectionInformation> lstTcpConns = new(); List<TcpConnectionInformation> lstTcpConns = new();
@ -928,6 +933,7 @@ namespace v2rayN.Handler
GenLog(v2rayConfig); GenLog(v2rayConfig);
v2rayConfig.inbounds.Clear(); // Remove "proxy" service for speedtest, avoiding port conflicts. v2rayConfig.inbounds.Clear(); // Remove "proxy" service for speedtest, avoiding port conflicts.
v2rayConfig.outbounds.RemoveAt(0);
int httpPort = LazyConfig.Instance.GetLocalPort("speedtest"); int httpPort = LazyConfig.Instance.GetLocalPort("speedtest");
@ -1019,13 +1025,13 @@ namespace v2rayN.Handler
} }
//msg = string.Format(ResUI.SuccessfulConfiguration"), node.getSummary()); //msg = string.Format(ResUI.SuccessfulConfiguration"), node.getSummary());
return Utils.ToJson(v2rayConfig); return 0;
} }
catch (Exception ex) catch (Exception ex)
{ {
Utils.SaveLog(ex.Message, ex); Utils.SaveLog(ex.Message, ex);
msg = ResUI.FailedGenDefaultConfiguration; msg = ResUI.FailedGenDefaultConfiguration;
return ""; return -1;
} }
} }

View file

@ -67,18 +67,19 @@ namespace v2rayN.Handler
} }
} }
public int LoadCoreConfigString(List<ServerTestItem> _selecteds) public int LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds)
{ {
int pid = -1; int pid = -1;
string configStr = CoreConfigHandler.GenerateClientSpeedtestConfigString(_config, _selecteds, out string msg); var coreType = selecteds.Exists(t => t.configType == EConfigType.Hysteria2 || t.configType == EConfigType.Tuic) ? ECoreType.sing_box : ECoreType.Xray;
if (configStr == "") string configPath = Utils.GetConfigPath(Global.CoreSpeedtestConfigFileName);
if (CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType, out string msg) != 0)
{ {
ShowMsg(false, msg); ShowMsg(false, msg);
} }
else else
{ {
ShowMsg(false, msg); ShowMsg(false, msg);
pid = CoreStartViaString(configStr); pid = CoreStartSpeedtest(configPath, coreType);
} }
return pid; return pid;
} }
@ -115,7 +116,7 @@ namespace v2rayN.Handler
} }
foreach (string vName in it.coreExes) foreach (string vName in it.coreExes)
{ {
Process[] existing = Process.GetProcessesByName(vName); var existing = Process.GetProcessesByName(vName);
foreach (Process p in existing) foreach (Process p in existing)
{ {
string? path = p.MainModule?.FileName; string? path = p.MainModule?.FileName;
@ -138,7 +139,7 @@ namespace v2rayN.Handler
{ {
try try
{ {
Process _p = Process.GetProcessById(pid); var _p = Process.GetProcessById(pid);
KillProcess(_p); KillProcess(_p);
} }
catch (Exception ex) 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"))); ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
try try
{ {
var coreInfo = LazyConfig.Instance.GetCoreInfo(ECoreType.Xray); var coreInfo = LazyConfig.Instance.GetCoreInfo(coreType);
string fileName = CoreFindexe(coreInfo); var proc = RunProcess(new(), coreInfo, $" -c {configPath}", true, ShowMsg);
if (fileName == "") return -1; if (proc is null)
Process p = new()
{ {
StartInfo = new ProcessStartInfo return -1;
{
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());
} }
Global.ProcessJob.AddProcess(p.Handle); return proc.Id;
return p.Id;
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -302,7 +261,7 @@ namespace v2rayN.Handler
} }
Process proc = new() Process proc = new()
{ {
StartInfo = new ProcessStartInfo StartInfo = new()
{ {
FileName = fileName, FileName = fileName,
Arguments = string.Format(coreInfo.arguments, configPath), 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 try
{ {
p.CloseMainWindow(); proc.CloseMainWindow();
p.WaitForExit(100); proc.WaitForExit(100);
if (!p.HasExited) if (!proc.HasExited)
{ {
p.Kill(); proc.Kill();
p.WaitForExit(100); proc.WaitForExit(100);
} }
} }
catch (Exception ex) catch (Exception ex)

View file

@ -277,7 +277,7 @@ namespace v2rayN.Handler
{ {
coreType = ECoreType.Xray, coreType = ECoreType.Xray,
coreExes = new List<string> { "xray", "wxray" }, coreExes = new List<string> { "xray", "wxray" },
arguments = "", arguments = "run {0}",
coreUrl = Global.XrayCoreUrl, coreUrl = Global.XrayCoreUrl,
coreReleaseApiUrl = Global.XrayCoreUrl.Replace(Global.GithubUrl, Global.GithubApiUrl), coreReleaseApiUrl = Global.XrayCoreUrl.Replace(Global.GithubUrl, Global.GithubApiUrl),
coreDownloadUrl32 = Global.XrayCoreUrl + "/download/{0}/Xray-windows-{1}.zip", coreDownloadUrl32 = Global.XrayCoreUrl + "/download/{0}/Xray-windows-{1}.zip",
@ -364,7 +364,7 @@ namespace v2rayN.Handler
{ {
coreType = ECoreType.sing_box, coreType = ECoreType.sing_box,
coreExes = new List<string> { "sing-box-client", "sing-box" }, coreExes = new List<string> { "sing-box-client", "sing-box" },
arguments = "run{0}", arguments = "run {0} --disable-color",
coreUrl = Global.SingboxCoreUrl, coreUrl = Global.SingboxCoreUrl,
redirectInfo = true, redirectInfo = true,
coreReleaseApiUrl = Global.SingboxCoreUrl.Replace(Global.GithubUrl, Global.GithubApiUrl), coreReleaseApiUrl = Global.SingboxCoreUrl.Replace(Global.GithubUrl, Global.GithubApiUrl),

View file

@ -30,7 +30,7 @@ namespace v2rayN.Handler
_selecteds = new List<ServerTestItem>(); _selecteds = new List<ServerTestItem>();
foreach (var it in selecteds) foreach (var it in selecteds)
{ {
if (it.configType == EConfigType.Custom || it.configType == EConfigType.Hysteria2 || it.configType == EConfigType.Tuic) if (it.configType == EConfigType.Custom)
{ {
continue; continue;
} }
@ -150,7 +150,7 @@ namespace v2rayN.Handler
{ {
string msg = string.Empty; string msg = string.Empty;
pid = _coreHandler.LoadCoreConfigString(_selecteds); pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds);
if (pid < 0) if (pid < 0)
{ {
UpdateFunc("", ResUI.FailedToRunCore); UpdateFunc("", ResUI.FailedToRunCore);
@ -196,7 +196,10 @@ namespace v2rayN.Handler
} }
finally finally
{ {
if (pid > 0) _coreHandler.CoreStopPid(pid); if (pid > 0)
{
_coreHandler.CoreStopPid(pid);
}
ProfileExHandler.Instance.SaveTo(); ProfileExHandler.Instance.SaveTo();
} }
@ -211,7 +214,7 @@ namespace v2rayN.Handler
// _selecteds = _selecteds.OrderBy(t => t.delay).ToList(); // _selecteds = _selecteds.OrderBy(t => t.delay).ToList();
//} //}
pid = _coreHandler.LoadCoreConfigString(_selecteds); pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds);
if (pid < 0) if (pid < 0)
{ {
UpdateFunc("", ResUI.FailedToRunCore); UpdateFunc("", ResUI.FailedToRunCore);
@ -268,7 +271,7 @@ namespace v2rayN.Handler
private async Task RunSpeedTestMulti() private async Task RunSpeedTestMulti()
{ {
int pid = -1; int pid = -1;
pid = _coreHandler.LoadCoreConfigString(_selecteds); pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds);
if (pid < 0) if (pid < 0)
{ {
UpdateFunc("", ResUI.FailedToRunCore); UpdateFunc("", ResUI.FailedToRunCore);