From 472c169603f07981b1cd169682f5cbf3745f1703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Thu, 6 Feb 2025 01:37:42 +0800 Subject: [PATCH] use R2 to distribute script files --- .../Core/Script/ScriptRepoUpdater.cs | 15 ++--- .../Helpers/Http/ProxySpeedTester.cs | 55 +++++++++++++++---- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs b/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs index 8b697c7e..1add05b0 100644 --- a/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs +++ b/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs @@ -35,7 +35,11 @@ public class ScriptRepoUpdater : Singleton public static readonly string ReposTempPath = Path.Combine(ReposPath, "Temp"); // 中央仓库信息地址 - public static readonly string CenterRepoInfoUrl = "https://raw.githubusercontent.com/babalae/bettergi-scripts-list/refs/heads/main/repo.json"; + public static readonly List CenterRepoInfoUrls = + [ + "https://raw.githubusercontent.com/babalae/bettergi-scripts-list/refs/heads/main/repo.json", + "https://r2-script.bettergi.com/github_mirror/repo.json", + ]; // 中央仓库解压后文件夹名 public static readonly string CenterRepoUnzipName = "bettergi-scripts-list-main"; @@ -88,10 +92,7 @@ public class ScriptRepoUpdater : Singleton public async Task<(string, bool)> UpdateCenterRepo() { // 测速并获取信息 - var res = await ProxySpeedTester.GetFastestProxyAsync(CenterRepoInfoUrl); - // 解析信息 - var fastProxyUrl = res.Item1; - var jsonString = res.Item2; + var (fastUrl, jsonString) = await ProxySpeedTester.GetFastestUrlAsync(CenterRepoInfoUrls); if (string.IsNullOrEmpty(jsonString)) { throw new Exception("从互联网下载最新的仓库信息失败"); @@ -118,7 +119,7 @@ public class ScriptRepoUpdater : Singleton if (needDownload) { - await DownloadRepoAndUnzip(string.Format(fastProxyUrl, url)); + await DownloadRepoAndUnzip(url); updated = true; } @@ -134,7 +135,7 @@ public class ScriptRepoUpdater : Singleton // 检查是否需要更新 if (long.Parse(time) > long.Parse(time2)) { - await DownloadRepoAndUnzip(string.Format(fastProxyUrl, url2)); + await DownloadRepoAndUnzip(url2); updated = true; } diff --git a/BetterGenshinImpact/Helpers/Http/ProxySpeedTester.cs b/BetterGenshinImpact/Helpers/Http/ProxySpeedTester.cs index eafab0d6..eeacaef9 100644 --- a/BetterGenshinImpact/Helpers/Http/ProxySpeedTester.cs +++ b/BetterGenshinImpact/Helpers/Http/ProxySpeedTester.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Net.Http; using System.Threading; using System.Threading.Tasks; @@ -9,14 +10,17 @@ namespace BetterGenshinImpact.Helpers.Http; public class ProxySpeedTester { - private static readonly HttpClient _httpClient = new() { Timeout = TimeSpan.FromSeconds(5) }; + private static readonly HttpClient _httpClient = new() { Timeout = TimeSpan.FromSeconds(15) }; + /// + /// 代理地址已经无法使用在 babalae 的组织下 + /// public static readonly List ProxyUrls = [ "{0}", - "https://mirror.ghproxy.com/{0}", "https://hub.gitmirror.com/{0}", - "https://ghproxy.net/{0}" + "https://ghproxy.net/{0}", + "https://ghproxy.cc/{0}", ]; /// @@ -31,8 +35,19 @@ public class ProxySpeedTester public static async Task<(string, string)> GetFastestProxyAsync(List proxyAddresses, string target) { - var tasks = new List>(); // 修改为包含成功标志的元组 - var cts = new CancellationTokenSource(); + List urlList = []; + foreach (var proxy in proxyAddresses) + { + // 如果目标地址为空且代理地址为默认地址,则跳过 + if (string.IsNullOrEmpty(target) && proxy == "{0}") + { + continue; + } + + urlList.Add(string.Format(proxy, target)); + } + + var (fastUrl, resContent) = await GetFastestUrlAsync(urlList); foreach (var proxy in proxyAddresses) { @@ -42,7 +57,23 @@ public class ProxySpeedTester continue; } - tasks.Add(TestProxyAsync(proxy, target, cts.Token)); + if (fastUrl == string.Format(proxy, target)) + { + return (proxy, resContent); + } + } + + return (string.Empty, string.Empty); // 如果没有成功的结果,返回空 + } + + public static async Task<(string, string)> GetFastestUrlAsync(List urlList) + { + var tasks = new List>(); // 修改为包含成功标志的元组 + var cts = new CancellationTokenSource(); + + foreach (var url in urlList) + { + tasks.Add(TestUrlAsync(url, cts.Token)); } while (tasks.Count > 0) @@ -54,27 +85,29 @@ public class ProxySpeedTester if (result.Item3) // 检查是否成功 { await cts.CancelAsync(); // 取消所有其他请求 - return (result.Item1, result.Item2); // 返回第一个成功的代理地址 + return (result.Item1, result.Item2); // 返回第一个成功的地址 } } return (string.Empty, string.Empty); // 如果没有成功的结果,返回空 } - private static async Task<(string, string, bool)> TestProxyAsync(string proxyAddress, string target, CancellationToken cancellationToken) + private static async Task<(string, string, bool)> TestUrlAsync(string url, CancellationToken cancellationToken) { try { // 模拟代理测试请求 - var response = await _httpClient.GetAsync(string.Format(proxyAddress, target), cancellationToken); + var response = await _httpClient.GetAsync(url, cancellationToken); response.EnsureSuccessStatusCode(); var content = await response.Content.ReadAsStringAsync(cancellationToken); + Debug.WriteLine("SpeedTester", $"Info: {url} - {response.StatusCode} - {content.Length} bytes"); var json = JObject.Parse(content); - return (proxyAddress, content, true); + return (url, content, true); } catch (Exception e) { - return (proxyAddress, e.Message, false); + Debug.WriteLine("SpeedTester", $"Info: {url} - {e.Message}"); + return (url, e.Message, false); } } } \ No newline at end of file