From 8d5bd5a80cc98bd51704e14d56b06ff8a12f05b8 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Fri, 28 Jan 2022 21:05:41 +0800 Subject: [PATCH] Improve server test --- v2rayN/v2rayN/Handler/SpeedtestHandler.cs | 89 +++++++++++++-------- v2rayN/v2rayN/Handler/V2rayConfigHandler.cs | 24 ++++-- v2rayN/v2rayN/Handler/V2rayHandler.cs | 2 +- v2rayN/v2rayN/Mode/ServerTestItem.cs | 29 +++++++ v2rayN/v2rayN/v2rayN.csproj | 1 + 5 files changed, 106 insertions(+), 39 deletions(-) create mode 100644 v2rayN/v2rayN/Mode/ServerTestItem.cs diff --git a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs index d8eb1eee..57ca1456 100644 --- a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs +++ b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs @@ -13,7 +13,7 @@ namespace v2rayN.Handler { private Config _config; private V2rayHandler _v2rayHandler; - private List _selecteds; + private List _selecteds; Action _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(); + 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 updateFun) + private void RunPingSub(Action 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 tasks = new List(); - 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); diff --git a/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs b/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs index f3504c03..909f5904 100644 --- a/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs @@ -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 selecteds, out string msg) + public static string GenerateClientSpeedtestConfigString(Config config, List 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 { diff --git a/v2rayN/v2rayN/Handler/V2rayHandler.cs b/v2rayN/v2rayN/Handler/V2rayHandler.cs index 95951b5e..1b0b99b8 100644 --- a/v2rayN/v2rayN/Handler/V2rayHandler.cs +++ b/v2rayN/v2rayN/Handler/V2rayHandler.cs @@ -73,7 +73,7 @@ namespace v2rayN.Handler /// 新建进程,载入V2ray配置文件字符串 /// 返回新进程pid。 /// - public int LoadV2rayConfigString(Config config, List _selecteds) + public int LoadV2rayConfigString(Config config, List _selecteds) { int pid = -1; string configStr = V2rayConfigHandler.GenerateClientSpeedtestConfigString(config, _selecteds, out string msg); diff --git a/v2rayN/v2rayN/Mode/ServerTestItem.cs b/v2rayN/v2rayN/Mode/ServerTestItem.cs new file mode 100644 index 00000000..304a4c52 --- /dev/null +++ b/v2rayN/v2rayN/Mode/ServerTestItem.cs @@ -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; + } + } +} diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index 203b51d2..84689f59 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -214,6 +214,7 @@ + True True