diff --git a/v2rayN/ServiceLib/Handler/ConnectionHandler.cs b/v2rayN/ServiceLib/Handler/ConnectionHandler.cs index 0d0afc3a..38f3ae51 100644 --- a/v2rayN/ServiceLib/Handler/ConnectionHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConnectionHandler.cs @@ -1,17 +1,20 @@ +using System.Net; + namespace ServiceLib.Handler; public static class ConnectionHandler { + private static readonly string _tag = "ConnectionHandler"; + public static async Task RunAvailabilityCheck() { - var downloadHandle = new DownloadService(); - var time = await downloadHandle.RunAvailabilityCheck(null); - var ip = time > 0 ? await GetIPInfo(downloadHandle) ?? Global.None : Global.None; + var time = await GetRealPingTime(); + var ip = time > 0 ? await GetIPInfo() ?? Global.None : Global.None; return string.Format(ResUI.TestMeOutput, time, ip); } - private static async Task GetIPInfo(DownloadService downloadHandle) + private static async Task GetIPInfo() { var url = AppManager.Instance.Config.SpeedTestItem.IPAPIUrl; if (url.IsNullOrEmpty()) @@ -19,6 +22,7 @@ public static class ConnectionHandler return null; } + var downloadHandle = new DownloadService(); var result = await downloadHandle.TryDownloadString(url, true, ""); if (result == null) { @@ -36,4 +40,31 @@ public static class ConnectionHandler return $"({country ?? "unknown"}) {ip}"; } + + private static async Task GetRealPingTime() + { + var responseTime = -1; + try + { + var port = AppManager.Instance.GetLocalPort(EInboundProtocol.socks); + var webProxy = new WebProxy($"socks5://{Global.Loopback}:{port}"); + var url = AppManager.Instance.Config.SpeedTestItem.SpeedPingTestUrl; + + for (var i = 0; i < 2; i++) + { + responseTime = await HttpClientHelper.Instance.GetRealPingTime(url, webProxy, 10); + if (responseTime > 0) + { + break; + } + await Task.Delay(500); + } + } + catch (Exception ex) + { + Logging.SaveLog(_tag, ex); + return -1; + } + return responseTime; + } } diff --git a/v2rayN/ServiceLib/Helper/HttpClientHelper.cs b/v2rayN/ServiceLib/Helper/HttpClientHelper.cs index 3dfb45cc..a559800f 100644 --- a/v2rayN/ServiceLib/Helper/HttpClientHelper.cs +++ b/v2rayN/ServiceLib/Helper/HttpClientHelper.cs @@ -1,3 +1,5 @@ +using System.Diagnostics; +using System.Net; using System.Net.Http.Headers; using System.Net.Mime; using System.Text; @@ -202,4 +204,35 @@ public class HttpClientHelper } } while (isMoreToRead); } + + public async Task GetRealPingTime(string url, IWebProxy? webProxy, int downloadTimeout) + { + var responseTime = -1; + try + { + using var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromSeconds(downloadTimeout)); + using var client = new HttpClient(new SocketsHttpHandler() + { + Proxy = webProxy, + UseProxy = webProxy != null + }); + + List oneTime = new(); + for (var i = 0; i < 2; i++) + { + var timer = Stopwatch.StartNew(); + await client.GetAsync(url, cts.Token).ConfigureAwait(false); + timer.Stop(); + oneTime.Add((int)timer.Elapsed.TotalMilliseconds); + await Task.Delay(100); + } + responseTime = oneTime.Where(x => x > 0).OrderBy(x => x).FirstOrDefault(); + } + catch //(Exception ex) + { + //Utile.SaveLog(ex.Message, ex); + } + return responseTime; + } } diff --git a/v2rayN/ServiceLib/Services/DownloadService.cs b/v2rayN/ServiceLib/Services/DownloadService.cs index f2f406ae..fe17d523 100644 --- a/v2rayN/ServiceLib/Services/DownloadService.cs +++ b/v2rayN/ServiceLib/Services/DownloadService.cs @@ -210,63 +210,6 @@ public class DownloadService return null; } - public async Task RunAvailabilityCheck(IWebProxy? webProxy) - { - var responseTime = -1; - try - { - webProxy ??= await GetWebProxy(true); - var config = AppManager.Instance.Config; - - for (var i = 0; i < 2; i++) - { - responseTime = await GetRealPingTime(config.SpeedTestItem.SpeedPingTestUrl, webProxy, 10); - if (responseTime > 0) - { - break; - } - await Task.Delay(500); - } - } - catch (Exception ex) - { - Logging.SaveLog(_tag, ex); - return -1; - } - return responseTime; - } - - public async Task GetRealPingTime(string url, IWebProxy? webProxy, int downloadTimeout) - { - var responseTime = -1; - try - { - using var cts = new CancellationTokenSource(); - cts.CancelAfter(TimeSpan.FromSeconds(downloadTimeout)); - using var client = new HttpClient(new SocketsHttpHandler() - { - Proxy = webProxy, - UseProxy = webProxy != null - }); - - List oneTime = new(); - for (var i = 0; i < 2; i++) - { - var timer = Stopwatch.StartNew(); - await client.GetAsync(url, cts.Token).ConfigureAwait(false); - timer.Stop(); - oneTime.Add((int)timer.Elapsed.TotalMilliseconds); - await Task.Delay(100); - } - responseTime = oneTime.Where(x => x > 0).OrderBy(x => x).FirstOrDefault(); - } - catch //(Exception ex) - { - //Utile.SaveLog(ex.Message, ex); - } - return responseTime; - } - private async Task GetWebProxy(bool blProxy) { if (!blProxy) diff --git a/v2rayN/ServiceLib/Services/SpeedtestService.cs b/v2rayN/ServiceLib/Services/SpeedtestService.cs index 7b4c8ebd..67b9bc94 100644 --- a/v2rayN/ServiceLib/Services/SpeedtestService.cs +++ b/v2rayN/ServiceLib/Services/SpeedtestService.cs @@ -198,8 +198,6 @@ public class SpeedtestService } await Task.Delay(1000); - var downloadHandle = new DownloadService(); - List tasks = new(); foreach (var it in selecteds) { @@ -213,7 +211,7 @@ public class SpeedtestService } tasks.Add(Task.Run(async () => { - await DoRealPing(downloadHandle, it); + await DoRealPing(it); })); } await Task.WhenAll(tasks); @@ -263,7 +261,7 @@ public class SpeedtestService else { await Task.Delay(1000); - var delay = await DoRealPing(downloadHandle, it); + var delay = await DoRealPing(it); if (blSpeedTest) { if (delay > 0) @@ -294,10 +292,10 @@ public class SpeedtestService await Task.WhenAll(tasks); } - private async Task DoRealPing(DownloadService downloadHandle, ServerTestItem it) + private async Task DoRealPing(ServerTestItem it) { var webProxy = new WebProxy($"socks5://{Global.Loopback}:{it.Port}"); - var responseTime = await downloadHandle.GetRealPingTime(_config.SpeedTestItem.SpeedPingTestUrl, webProxy, 10); + var responseTime = await HttpClientHelper.Instance.GetRealPingTime(_config.SpeedTestItem.SpeedPingTestUrl, webProxy, 10); ProfileExManager.Instance.SetTestDelay(it.IndexId, responseTime); UpdateFunc(it.IndexId, responseTime.ToString());