From a9824fe6ec8926c8df84c014d94f74b23ee70c23 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Wed, 20 May 2026 15:42:47 +0800 Subject: [PATCH] Improve ConnectionHandler DownloadService --- .../ServiceLib/Handler/ConnectionHandler.cs | 79 +++++++++++++------ v2rayN/ServiceLib/Services/DownloadService.cs | 48 ++++++++--- 2 files changed, 89 insertions(+), 38 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/ConnectionHandler.cs b/v2rayN/ServiceLib/Handler/ConnectionHandler.cs index 05825d93..6fd7d453 100644 --- a/v2rayN/ServiceLib/Handler/ConnectionHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConnectionHandler.cs @@ -4,6 +4,9 @@ public static class ConnectionHandler { private static readonly string _tag = "ConnectionHandler"; + /// + /// Runs ping and IP checks and returns a formatted result string. + /// public static async Task RunAvailabilityCheck() { var time = await GetRealPingTimeInfo(); @@ -12,40 +15,24 @@ public static class ConnectionHandler return string.Format(ResUI.TestMeOutput, time, ip); } + /// + /// Gets IP information using the default local proxy. + /// private static async Task GetIPInfo() { - var url = AppManager.Instance.Config.SpeedTestItem.IPAPIUrl; - if (url.IsNullOrEmpty()) - { - return null; - } - - var downloadHandle = new DownloadService(); - var result = await downloadHandle.TryDownloadString(url, true, ""); - if (result == null) - { - return null; - } - - var ipInfo = JsonUtils.Deserialize(result); - if (ipInfo == null) - { - return null; - } - - var ip = ipInfo.ip ?? ipInfo.clientIp ?? ipInfo.ip_addr ?? ipInfo.query; - var country = ipInfo.country_code ?? ipInfo.country ?? ipInfo.countryCode ?? ipInfo.location?.country_code; - - return $"({country ?? "unknown"}) {ip}"; + var webProxy = await GetWebProxy(); + return await GetIPInfo(webProxy); } + /// + /// Measures real ping time using configured test URL. + /// private static async Task GetRealPingTimeInfo() { var responseTime = -1; try { - var port = AppManager.Instance.GetLocalPort(EInboundProtocol.socks); - var webProxy = new WebProxy($"socks5://{Global.Loopback}:{port}"); + var webProxy = await GetWebProxy(); var url = AppManager.Instance.Config.SpeedTestItem.SpeedPingTestUrl; for (var i = 0; i < 2; i++) @@ -66,6 +53,18 @@ public static class ConnectionHandler return responseTime; } + /// + /// Creates local SOCKS proxy instance. + /// + private static async Task GetWebProxy() + { + var port = AppManager.Instance.GetLocalPort(EInboundProtocol.socks); + return new WebProxy($"socks5://{Global.Loopback}:{port}"); + } + + /// + /// Measures response time by sending HTTP requests through proxy. + /// public static async Task GetRealPingTime(string url, IWebProxy? webProxy, int downloadTimeout) { var responseTime = -1; @@ -95,4 +94,34 @@ public static class ConnectionHandler } return responseTime; } + + /// + /// Gets IP and country information through specified proxy. + /// + public static async Task GetIPInfo(IWebProxy? webProxy) + { + var url = AppManager.Instance.Config.SpeedTestItem.IPAPIUrl; + if (url.IsNullOrEmpty()) + { + return null; + } + + var downloadHandle = new DownloadService(); + var result = await downloadHandle.TryDownloadString(url, webProxy, ""); + if (result == null) + { + return null; + } + + var ipInfo = JsonUtils.Deserialize(result); + if (ipInfo == null) + { + return null; + } + + var ip = ipInfo.ip ?? ipInfo.clientIp ?? ipInfo.ip_addr ?? ipInfo.query; + var country = ipInfo.country_code ?? ipInfo.country ?? ipInfo.countryCode ?? ipInfo.location?.country_code; + + return $"({country ?? "unknown"}) {ip}"; + } } diff --git a/v2rayN/ServiceLib/Services/DownloadService.cs b/v2rayN/ServiceLib/Services/DownloadService.cs index 77d3a7c1..1dbdfae2 100644 --- a/v2rayN/ServiceLib/Services/DownloadService.cs +++ b/v2rayN/ServiceLib/Services/DownloadService.cs @@ -3,7 +3,7 @@ using System.Net.Http.Headers; namespace ServiceLib.Services; /// -///Download +/// Download /// public class DownloadService { @@ -13,7 +13,10 @@ public class DownloadService private static readonly string _tag = "DownloadService"; - public async Task DownloadDataAsync(string url, WebProxy webProxy, int downloadTimeout, Func updateFunc) + /// + /// Downloads data with the specified proxy and reports progress messages. + /// + public async Task DownloadDataAsync(string url, IWebProxy webProxy, int downloadTimeout, Func updateFunc) { try { @@ -36,6 +39,9 @@ public class DownloadService return 0; } + /// + /// Downloads a file and reports progress through events. + /// public async Task DownloadFileAsync(string url, string fileName, bool blProxy, int downloadTimeout) { try @@ -64,6 +70,9 @@ public class DownloadService } } + /// + /// Gets redirect target URL without following redirects automatically. + /// public async Task UrlRedirectAsync(string url, bool blProxy) { var webRequestHandler = new SocketsHttpHandler @@ -86,11 +95,23 @@ public class DownloadService } } + /// + /// Tries to download string content using proxy switch setting. + /// public async Task TryDownloadString(string url, bool blProxy, string userAgent) + { + var webProxy = await GetWebProxy(blProxy); + return await TryDownloadString(url, webProxy, userAgent); + } + + /// + /// Tries to download string content with a specified proxy. + /// + public async Task TryDownloadString(string url, IWebProxy? webProxy, string userAgent) { try { - var result1 = await DownloadStringAsync(url, blProxy, userAgent, 15); + var result1 = await DownloadStringAsync(url, webProxy, userAgent, 15); if (result1.IsNotEmpty()) { return result1; @@ -108,7 +129,7 @@ public class DownloadService try { - var result2 = await DownloadStringViaDownloader(url, blProxy, userAgent, 15); + var result2 = await DownloadStringViaDownloader(url, webProxy, userAgent, 15); if (result2.IsNotEmpty()) { return result2; @@ -128,14 +149,12 @@ public class DownloadService } /// - /// DownloadString + /// Downloads string content via HttpClient. /// - /// - private async Task DownloadStringAsync(string url, bool blProxy, string userAgent, int timeout) + private async Task DownloadStringAsync(string url, IWebProxy? webProxy, string userAgent, int timeout) { try { - var webProxy = await GetWebProxy(blProxy); var client = new HttpClient(new SocketsHttpHandler() { Proxy = webProxy, @@ -172,15 +191,12 @@ public class DownloadService } /// - /// DownloadString + /// Downloads string content via DownloaderHelper. /// - /// - private async Task DownloadStringViaDownloader(string url, bool blProxy, string userAgent, int timeout) + private async Task DownloadStringViaDownloader(string url, IWebProxy? webProxy, string userAgent, int timeout) { try { - var webProxy = await GetWebProxy(blProxy); - if (userAgent.IsNullOrEmpty()) { userAgent = Utils.GetVersion(false); @@ -200,6 +216,9 @@ public class DownloadService return null; } + /// + /// Creates local SOCKS proxy when proxy switch is enabled. + /// private async Task GetWebProxy(bool blProxy) { if (!blProxy) @@ -215,6 +234,9 @@ public class DownloadService return new WebProxy($"socks5://{Global.Loopback}:{port}"); } + /// + /// Checks whether the specified TCP endpoint is reachable. + /// private async Task SocketCheck(string ip, int port) { try