From 737d563ebb66d44504c3a9f51b7dcbb382991dfd Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 23 Apr 2020 16:20:30 +0800 Subject: [PATCH] up speedtest --- v2rayN/v2rayN/Handler/DownloadHandle.cs | 12 +++-- v2rayN/v2rayN/Handler/SpeedtestHandler.cs | 64 ++++++++++------------- 2 files changed, 37 insertions(+), 39 deletions(-) diff --git a/v2rayN/v2rayN/Handler/DownloadHandle.cs b/v2rayN/v2rayN/Handler/DownloadHandle.cs index bd09d27f..bd3f4e62 100644 --- a/v2rayN/v2rayN/Handler/DownloadHandle.cs +++ b/v2rayN/v2rayN/Handler/DownloadHandle.cs @@ -136,13 +136,15 @@ namespace v2rayN.Handler string curVersion; string message; string url; - if (type == "Core") { + if (type == "Core") + { curVersion = "v" + getV2rayVersion(); message = string.Format(UIRes.I18N("IsLatestCore"), curVersion); string osBit = Environment.Is64BitProcess ? "64" : "32"; url = string.Format(coreUrl, version, osBit); } - else if (type == "v2rayN") { + else if (type == "v2rayN") + { curVersion = FileVersionInfo.GetVersionInfo(Utils.GetExePath()).FileVersion.ToString(); message = string.Format(UIRes.I18N("IsLatestN"), curVersion); url = string.Format(nUrl, version); @@ -172,8 +174,9 @@ namespace v2rayN.Handler #region Download - public void DownloadFileAsync(string url, WebProxy webProxy, int downloadTimeout) + public WebClientEx DownloadFileAsync(string url, WebProxy webProxy, int downloadTimeout) { + WebClientEx ws = new WebClientEx(); try { Utils.SetSecurityProtocol(); @@ -182,7 +185,7 @@ namespace v2rayN.Handler progressPercentage = -1; totalBytesToReceive = 0; - WebClientEx ws = new WebClientEx(); + //WebClientEx ws = new WebClientEx(); DownloadTimeout = downloadTimeout; if (webProxy != null) { @@ -199,6 +202,7 @@ namespace v2rayN.Handler Error?.Invoke(this, new ErrorEventArgs(ex)); } + return ws; } void ws_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) diff --git a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs index bc50f921..d8556722 100644 --- a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs +++ b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs @@ -11,26 +11,17 @@ namespace v2rayN.Handler { class SpeedtestHandler { - private DownloadHandle downloadHandle2; private Config _config; private V2rayHandler _v2rayHandler; private List _selecteds; Action _updateFunc; - private int testCounter = 0; - private int ItemIndex - { - get - { - return _selecteds[testCounter - 1]; - } - } public SpeedtestHandler(ref Config config, ref V2rayHandler v2rayHandler, List selecteds, string actionType, Action update) { _config = config; _v2rayHandler = v2rayHandler; - _selecteds = selecteds; + _selecteds = Utils.DeepCopy(selecteds); _updateFunc = update; if (actionType == "ping") @@ -178,6 +169,7 @@ namespace v2rayN.Handler private void RunSpeedTest() { + int testCounter = 0; int pid = -1; if (_config.vmess.Count <= 0) @@ -188,39 +180,41 @@ namespace v2rayN.Handler pid = _v2rayHandler.LoadV2rayConfigString(_config, _selecteds); string url = _config.speedTestUrl; - testCounter = 0; - if (downloadHandle2 == null) + DownloadHandle downloadHandle2 = new DownloadHandle(); + downloadHandle2.UpdateCompleted += (sender2, args) => { - downloadHandle2 = new DownloadHandle(); - downloadHandle2.UpdateCompleted += (sender2, args) => - { - _updateFunc(ItemIndex, args.Msg); - if (args.Success) StartNext(); - }; - downloadHandle2.Error += (sender2, args) => - { - _updateFunc(ItemIndex, args.GetException().Message); - StartNext(); - }; - } - - StartNext(); - - void StartNext() + _updateFunc(testCounter, args.Msg); + }; + downloadHandle2.Error += (sender2, args) => { - if (testCounter >= _selecteds.Count) + _updateFunc(testCounter, args.GetException().Message); + }; + + var timeout = 12; + foreach (int itemIndex in _selecteds) + { + if (itemIndex >= _config.vmess.Count) { - if (pid > 0) _v2rayHandler.V2rayStopPid(pid); - return; + break; } + if (_config.vmess[itemIndex].configType == (int)EConfigType.Custom) + { + continue; + } + testCounter = itemIndex; int httpPort = _config.GetLocalPort("speedtest"); - int index = _selecteds[testCounter]; - testCounter++; - WebProxy webProxy = new WebProxy(Global.Loopback, httpPort + index); - downloadHandle2.DownloadFileAsync(url, webProxy, 10); + WebProxy webProxy = new WebProxy(Global.Loopback, httpPort + itemIndex); + var ws = downloadHandle2.DownloadFileAsync(url, webProxy, timeout - 2); + + Thread.Sleep(1000 * timeout); + + ws.CancelAsync(); + + Thread.Sleep(1000 * 2); } + if (pid > 0) _v2rayHandler.V2rayStopPid(pid); }