Improve speed test,add Hy2 and Tuic test

pull/4567/head
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 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";

View File

@ -150,10 +150,26 @@ namespace v2rayN.Handler
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);
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;
}
}
}

View File

@ -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<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;
}
#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<ServerTestItem> selecteds, out string msg)
public int GenerateClientSpeedtestConfig(List<ServerTestItem> 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<V2rayConfig>(result);
v2rayConfig = Utils.FromJson<V2rayConfig>(result);
if (v2rayConfig == null)
{
msg = ResUI.FailedGenDefaultConfiguration;
return "";
return -1;
}
List<IPEndPoint> lstIpEndPoints = new();
List<TcpConnectionInformation> 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;
}
}

View File

@ -67,18 +67,19 @@ namespace v2rayN.Handler
}
}
public int LoadCoreConfigString(List<ServerTestItem> _selecteds)
public int LoadCoreConfigSpeedtest(List<ServerTestItem> 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)

View File

@ -277,7 +277,7 @@ namespace v2rayN.Handler
{
coreType = ECoreType.Xray,
coreExes = new List<string> { "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<string> { "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),

View File

@ -30,7 +30,7 @@ namespace v2rayN.Handler
_selecteds = new List<ServerTestItem>();
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);