Improve server test

pull/2077/head
2dust 2022-01-28 21:05:41 +08:00
parent 002283899f
commit 8d5bd5a80c
5 changed files with 106 additions and 39 deletions

View File

@ -13,7 +13,7 @@ namespace v2rayN.Handler
{
private Config _config;
private V2rayHandler _v2rayHandler;
private List<int> _selecteds;
private List<ServerTestItem> _selecteds;
Action<int, string> _updateFunc;
public SpeedtestHandler(ref Config config)
@ -25,9 +25,22 @@ namespace v2rayN.Handler
{
_config = config;
_v2rayHandler = v2rayHandler;
_selecteds = Utils.DeepCopy(selecteds);
//_selecteds = Utils.DeepCopy(selecteds);
_updateFunc = update;
_selecteds = new List<ServerTestItem>();
foreach (var it in selecteds)
{
_selecteds.Add(new ServerTestItem()
{
selected = it,
indexId = config.vmess[it].indexId,
address = config.vmess[it].address,
port = config.vmess[it].port,
configType = config.vmess[it].configType
});
}
if (actionType == "ping")
{
Task.Run(() => RunPing());
@ -46,19 +59,19 @@ namespace v2rayN.Handler
}
}
private void RunPingSub(Action<int> updateFun)
private void RunPingSub(Action<ServerTestItem> updateFun)
{
try
{
foreach (int index in _selecteds)
foreach (var it in _selecteds)
{
if (_config.vmess[index].configType == (int)EConfigType.Custom)
if (it.configType == (int)EConfigType.Custom)
{
continue;
}
try
{
updateFun(index);
updateFun(it);
}
catch (Exception ex)
{
@ -77,18 +90,22 @@ namespace v2rayN.Handler
private void RunPing()
{
RunPingSub((int index) =>
RunPingSub((ServerTestItem it) =>
{
long time = Utils.Ping(_config.vmess[index].address);
long time = Utils.Ping(it.address);
var index = _config.FindIndexId(it.indexId);
if (index < 0) return;
_updateFunc(index, FormatOut(time, "ms"));
});
}
private void RunTcping()
{
RunPingSub((int index) =>
RunPingSub((ServerTestItem it) =>
{
int time = GetTcpingTime(_config.vmess[index].address, _config.vmess[index].port);
int time = GetTcpingTime(it.address, it.port);
var index = _config.FindIndexId(it.indexId);
if (index < 0) return;
_updateFunc(index, FormatOut(time, "ms"));
});
}
@ -103,16 +120,19 @@ namespace v2rayN.Handler
pid = _v2rayHandler.LoadV2rayConfigString(_config, _selecteds);
if (pid < 0)
{
_updateFunc(_selecteds[0], UIRes.I18N("OperationFailed"));
_updateFunc(_selecteds[0].selected, UIRes.I18N("OperationFailed"));
return;
}
//Thread.Sleep(5000);
int httpPort = _config.GetLocalPort("speedtest");
List<Task> tasks = new List<Task>();
foreach (int itemIndex in _selecteds)
foreach (var it in _selecteds)
{
if (_config.vmess[itemIndex].configType == (int)EConfigType.Custom)
if (it.configType == (int)EConfigType.Custom)
{
continue;
}
if (it.port <= 0)
{
continue;
}
@ -120,11 +140,13 @@ namespace v2rayN.Handler
{
try
{
WebProxy webProxy = new WebProxy(Global.Loopback, httpPort + itemIndex);
WebProxy webProxy = new WebProxy(Global.Loopback, it.port);
int responseTime = -1;
string status = GetRealPingTime(_config.constItem.speedPingTestUrl, webProxy, out responseTime);
string output = Utils.IsNullOrEmpty(status) ? FormatOut(responseTime, "ms") : FormatOut(status, "");
_updateFunc(itemIndex, output);
string output = Utils.IsNullOrEmpty(status) ? FormatOut(responseTime, "ms") : status;
var index = _config.FindIndexId(it.indexId);
if (index < 0) return;
_updateFunc(index, output);
}
catch (Exception ex)
{
@ -178,7 +200,7 @@ namespace v2rayN.Handler
private void RunSpeedTest()
{
int testCounter = 0;
string testIndexId = string.Empty;
int pid = -1;
if (_config.vmess.Count <= 0)
@ -189,7 +211,7 @@ namespace v2rayN.Handler
pid = _v2rayHandler.LoadV2rayConfigString(_config, _selecteds);
if (pid < 0)
{
_updateFunc(_selecteds[0], UIRes.I18N("OperationFailed"));
_updateFunc(_selecteds[0].selected, UIRes.I18N("OperationFailed"));
return;
}
@ -197,29 +219,32 @@ namespace v2rayN.Handler
DownloadHandle downloadHandle2 = new DownloadHandle();
downloadHandle2.UpdateCompleted += (sender2, args) =>
{
_updateFunc(testCounter, args.Msg);
var index = _config.FindIndexId(testIndexId);
if (index < 0) return;
_updateFunc(index, args.Msg);
};
downloadHandle2.Error += (sender2, args) =>
{
_updateFunc(testCounter, args.GetException().Message);
var index = _config.FindIndexId(testIndexId);
if (index < 0) return;
_updateFunc(index, args.GetException().Message);
};
var timeout = 10;
foreach (int itemIndex in _selecteds)
foreach (var it in _selecteds)
{
if (itemIndex >= _config.vmess.Count)
{
break;
}
if (_config.vmess[itemIndex].configType == (int)EConfigType.Custom)
if (it.configType == (int)EConfigType.Custom)
{
continue;
}
testCounter = itemIndex;
int httpPort = _config.GetLocalPort("speedtest");
WebProxy webProxy = new WebProxy(Global.Loopback, httpPort + itemIndex);
if (it.port <= 0)
{
continue;
}
testIndexId = it.indexId;
if (_config.FindIndexId(it.indexId) < 0) continue;
WebProxy webProxy = new WebProxy(Global.Loopback, it.port);
var ws = downloadHandle2.DownloadDataAsync(url, webProxy, timeout - 2);
Thread.Sleep(1000 * timeout);

View File

@ -364,7 +364,14 @@ namespace v2rayN.Handler
//远程服务器用户ID
usersItem.id = config.id();
usersItem.email = Global.userEMail;
usersItem.security = config.security();
if (Global.vmessSecuritys.Contains(config.security()))
{
usersItem.security = config.security();
}
else
{
usersItem.security = Global.DefaultSecurity;
}
//Mux
outbound.mux.enabled = config.muxEnabled;
@ -1383,7 +1390,7 @@ namespace v2rayN.Handler
#region Gen speedtest config
public static string GenerateClientSpeedtestConfigString(Config config, List<int> selecteds, out string msg)
public static string GenerateClientSpeedtestConfigString(Config config, List<ServerTestItem> selecteds, out string msg)
{
try
{
@ -1429,21 +1436,26 @@ namespace v2rayN.Handler
int httpPort = configCopy.GetLocalPort("speedtest");
foreach (int index in selecteds)
foreach (var it in selecteds)
{
if (configCopy.vmess[index].configType == (int)EConfigType.Custom)
if (it.configType == (int)EConfigType.Custom)
{
continue;
}
if (it.port <= 0)
{
continue;
}
configCopy.index = index;
var port = httpPort + index;
configCopy.index = it.selected;
var port = httpPort + it.selected;
//Port In Used
if (lstIpEndPoints != null && lstIpEndPoints.FindIndex(_it => _it.Port == port) >= 0)
{
continue;
}
it.port = port;
Inbounds inbound = new Inbounds
{

View File

@ -73,7 +73,7 @@ namespace v2rayN.Handler
/// 新建进程载入V2ray配置文件字符串
/// 返回新进程pid。
/// </summary>
public int LoadV2rayConfigString(Config config, List<int> _selecteds)
public int LoadV2rayConfigString(Config config, List<ServerTestItem> _selecteds)
{
int pid = -1;
string configStr = V2rayConfigHandler.GenerateClientSpeedtestConfigString(config, _selecteds, out string msg);

View File

@ -0,0 +1,29 @@
using System;
namespace v2rayN.Mode
{
[Serializable]
class TestItem
{
public int selected
{
get; set;
}
public string indexId
{
get; set;
}
public string address
{
get; set;
}
public int port
{
get; set;
}
public int configType
{
get; set;
}
}
}

View File

@ -214,6 +214,7 @@
<Compile Include="Mode\ServerStatistics.cs" />
<Compile Include="Mode\SysproxyConfig.cs" />
<Compile Include="Mode\EConfigType.cs" />
<Compile Include="Mode\TestItem.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>