From 7cbb8939e1b480f88e0b107ce8378ac6d485354c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Thu, 7 Nov 2024 00:27:14 +0800 Subject: [PATCH] =?UTF-8?q?[#]=20=E4=BF=AE=E5=A4=8DQQNT=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=89=8D=E5=AF=B9=E4=BB=A3=E7=90=86=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E6=B2=A1=E6=9C=89=E6=AD=A3=E7=A1=AE=E6=B5=8B=E9=80=9F?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RevokeMsgPatcher/Forms/FormLiteLoaderQQNT.cs | 10 +-- RevokeMsgPatcher/Model/LiteLoaderRowData.cs | 30 ++++++++- RevokeMsgPatcher/Utils/ProxySpeedTester.cs | 67 ++++++++++++-------- 3 files changed, 73 insertions(+), 34 deletions(-) diff --git a/RevokeMsgPatcher/Forms/FormLiteLoaderQQNT.cs b/RevokeMsgPatcher/Forms/FormLiteLoaderQQNT.cs index 49740fcc..159e663c 100644 --- a/RevokeMsgPatcher/Forms/FormLiteLoaderQQNT.cs +++ b/RevokeMsgPatcher/Forms/FormLiteLoaderQQNT.cs @@ -31,17 +31,17 @@ private void InitCboProxyList() // 添加代理 URL 到下拉菜单 foreach (var proxy in ProxySpeedTester.ProxyUrls) { - cboGithubProxy.Items.Add(proxy); + cboGithubProxy.Items.Add(proxy.Replace("{0}","")); } // 异步测试代理速度并设置默认选项 Task.Run(async () => { - var fastestProxy = await ProxySpeedTester.GetFastestProxyAsync(); - Debug.WriteLine(fastestProxy); - if (!string.IsNullOrEmpty(fastestProxy)) + var fastestProxy = await ProxySpeedTester.GetFastestProxyAsync(ProxySpeedTester.TargetUrl); + Debug.WriteLine(fastestProxy.Item1); + if (!string.IsNullOrEmpty(fastestProxy.Item1)) { - cboGithubProxy.Invoke(new Action(() => cboGithubProxy.SelectedItem = fastestProxy)); + cboGithubProxy.Invoke(new Action(() => cboGithubProxy.SelectedItem = fastestProxy.Item1)); } }); } diff --git a/RevokeMsgPatcher/Model/LiteLoaderRowData.cs b/RevokeMsgPatcher/Model/LiteLoaderRowData.cs index 76b539c2..41eb74da 100644 --- a/RevokeMsgPatcher/Model/LiteLoaderRowData.cs +++ b/RevokeMsgPatcher/Model/LiteLoaderRowData.cs @@ -127,11 +127,11 @@ public void GetLocalVersionAndUpdateStatus() } } - public async Task GetRemoteVersion(string proxyUrl = null) + public async Task GetRemoteVersion(string proxyUrl) { using (var client = new HttpClient()) { - var url = string.IsNullOrEmpty(proxyUrl) ? VersionJsonUrl : proxyUrl + "/" + VersionJsonUrl; + var url = FormatUrl(proxyUrl, VersionJsonUrl); var response = await client.GetAsync(url); if (response.IsSuccessStatusCode) { @@ -144,6 +144,30 @@ public async Task GetRemoteVersion(string proxyUrl = null) } } + private string FormatUrl(string proxyUrl, string target) + { + if (string.IsNullOrEmpty(proxyUrl)) + { + return target; + } + else + { + if (proxyUrl.Contains("{0}")) + { + return string.Format(proxyUrl, target); + } + else + { + if (!proxyUrl.EndsWith("/")) + { + proxyUrl += "/"; + } + + return proxyUrl + target; + } + } + } + public async Task CheckAndUpdate(string proxyUrl = null) { try @@ -161,7 +185,7 @@ public async Task CheckAndUpdate(string proxyUrl = null) UpdateStatus($"存在新版本{remoteVersion},正在下载..."); Debug.WriteLine("发现新版本,正在下载..."); var url = DownloadUrl.Replace("#{version}", remoteVersion); - url = string.IsNullOrEmpty(proxyUrl) ? url : proxyUrl + "/" + url; + url = FormatUrl(proxyUrl, url); string downloadedFilePath = await DownloadLatestPackage(url, Path.Combine(Application.StartupPath, "Public/Download")); Debug.WriteLine("下载到:" + downloadedFilePath); UpdateStatus($"下载成功,解压中..."); diff --git a/RevokeMsgPatcher/Utils/ProxySpeedTester.cs b/RevokeMsgPatcher/Utils/ProxySpeedTester.cs index 1f4a9587..01123b5e 100644 --- a/RevokeMsgPatcher/Utils/ProxySpeedTester.cs +++ b/RevokeMsgPatcher/Utils/ProxySpeedTester.cs @@ -9,60 +9,75 @@ namespace RevokeMsgPatcher.Utils { public class ProxySpeedTester { - private static readonly string TargetUrl = "https://raw.githubusercontent.com/LiteLoaderQQNT/LiteLoaderQQNT/refs/heads/main/package.json"; + public static readonly string TargetUrl = "https://raw.githubusercontent.com/LiteLoaderQQNT/LiteLoaderQQNT/refs/heads/main/package.json"; private static readonly HttpClient _httpClient = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) }; public static readonly List ProxyUrls = new List() { - "https://mirror.ghproxy.com", - "https://hub.gitmirror.com", - "https://ghproxy.cc", - "https://www.ghproxy.cc", - "https://ghproxy.cn", - "https://ghproxy.net", + "{0}", + "https://mirror.ghproxy.com/{0}", + "https://hub.gitmirror.com/{0}", + "https://ghproxy.cc/{0}", + "https://www.ghproxy.cc/{0}", + "https://ghproxy.cn/{0}", + "https://ghproxy.net/{0}" }; - public static async Task GetFastestProxyAsync() + /// + /// 获得最快的代理地址 + /// + /// + /// 最快的代理地址,结果 + public static async Task> GetFastestProxyAsync(string target) { - return await GetFastestProxyAsync(ProxyUrls); + return await GetFastestProxyAsync(ProxyUrls, target); } - public static async Task GetFastestProxyAsync(List proxyAddresses) + public static async Task> GetFastestProxyAsync(List proxyAddresses, string target) { - var tasks = new List>(); + var tasks = new List>>(); // 修改为包含成功标志的元组 var cts = new CancellationTokenSource(); foreach (var proxy in proxyAddresses) { - tasks.Add(TestProxyAsync(proxy, cts.Token)); - } - - var firstCompletedTask = await Task.WhenAny(tasks); - cts.Cancel(); // 取消所有其他请求 + // 如果目标地址为空且代理地址为默认地址,则跳过 + if (string.IsNullOrEmpty(target) && proxy == "{0}") + { + continue; + } - try - { - return await firstCompletedTask; // 返回第一个完成的代理地址 + tasks.Add(TestProxyAsync(proxy, target, cts.Token)); } - catch (OperationCanceledException) + + while (tasks.Count > 0) { - return null; // 如果第一个任务被取消,返回 null + var firstCompletedTask = await Task.WhenAny(tasks); + tasks.Remove(firstCompletedTask); + + var result = await firstCompletedTask; + if (result.Item3) // 检查是否成功 + { + cts.Cancel(); // 取消所有其他请求 + return new Tuple(result.Item1, result.Item2); // 返回第一个成功的代理地址 + } } + + return new Tuple(string.Empty, string.Empty); // 如果没有成功的结果,返回空 } - private static async Task TestProxyAsync(string proxyAddress, CancellationToken cancellationToken) + private static async Task> TestProxyAsync(string proxyAddress, string target, CancellationToken cancellationToken) { try { // 模拟代理测试请求 - var response = await _httpClient.GetAsync(proxyAddress, cancellationToken); + var response = await _httpClient.GetAsync(string.Format(proxyAddress, target), cancellationToken); response.EnsureSuccessStatusCode(); - return proxyAddress; + return new Tuple(proxyAddress.Replace("{0}", ""), await response.Content.ReadAsStringAsync(), true); } - catch (Exception) + catch (Exception e) { - return null; + return new Tuple(proxyAddress.Replace("{0}", ""), e.Message, false); } } }