mirror of
https://github.com/huiyadanli/RevokeMsgPatcher.git
synced 2025-05-23 22:06:06 +08:00
[#] 修复QQNT插件更新前对代理地址没有正确测速的问题
This commit is contained in:
parent
25532df301
commit
7cbb8939e1
|
@ -31,17 +31,17 @@ namespace RevokeMsgPatcher.Forms
|
|||
// 添加代理 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));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -127,11 +127,11 @@ namespace RevokeMsgPatcher.Model
|
|||
}
|
||||
}
|
||||
|
||||
public async Task<string> GetRemoteVersion(string proxyUrl = null)
|
||||
public async Task<string> 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 @@ namespace RevokeMsgPatcher.Model
|
|||
}
|
||||
}
|
||||
|
||||
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 @@ namespace RevokeMsgPatcher.Model
|
|||
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($"下载成功,解压中...");
|
||||
|
|
|
@ -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<string> ProxyUrls = new List<string>()
|
||||
{
|
||||
"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<string> GetFastestProxyAsync()
|
||||
/// <summary>
|
||||
/// 获得最快的代理地址
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <returns>最快的代理地址,结果</returns>
|
||||
public static async Task<Tuple<string, string>> GetFastestProxyAsync(string target)
|
||||
{
|
||||
return await GetFastestProxyAsync(ProxyUrls);
|
||||
return await GetFastestProxyAsync(ProxyUrls, target);
|
||||
}
|
||||
|
||||
public static async Task<string> GetFastestProxyAsync(List<string> proxyAddresses)
|
||||
public static async Task<Tuple<string, string>> GetFastestProxyAsync(List<string> proxyAddresses, string target)
|
||||
{
|
||||
var tasks = new List<Task<string>>();
|
||||
var tasks = new List<Task<Tuple<string, string, bool>>>(); // 修改为包含成功标志的元组
|
||||
var cts = new CancellationTokenSource();
|
||||
|
||||
foreach (var proxy in proxyAddresses)
|
||||
{
|
||||
tasks.Add(TestProxyAsync(proxy, cts.Token));
|
||||
// 如果目标地址为空且代理地址为默认地址,则跳过
|
||||
if (string.IsNullOrEmpty(target) && proxy == "{0}")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
tasks.Add(TestProxyAsync(proxy, target, cts.Token));
|
||||
}
|
||||
|
||||
var firstCompletedTask = await Task.WhenAny(tasks);
|
||||
cts.Cancel(); // 取消所有其他请求
|
||||
while (tasks.Count > 0)
|
||||
{
|
||||
var firstCompletedTask = await Task.WhenAny(tasks);
|
||||
tasks.Remove(firstCompletedTask);
|
||||
|
||||
try
|
||||
{
|
||||
return await firstCompletedTask; // 返回第一个完成的代理地址
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return null; // 如果第一个任务被取消,返回 null
|
||||
var result = await firstCompletedTask;
|
||||
if (result.Item3) // 检查是否成功
|
||||
{
|
||||
cts.Cancel(); // 取消所有其他请求
|
||||
return new Tuple<string, string>(result.Item1, result.Item2); // 返回第一个成功的代理地址
|
||||
}
|
||||
}
|
||||
|
||||
return new Tuple<string, string>(string.Empty, string.Empty); // 如果没有成功的结果,返回空
|
||||
}
|
||||
|
||||
private static async Task<string> TestProxyAsync(string proxyAddress, CancellationToken cancellationToken)
|
||||
private static async Task<Tuple<string, string, bool>> 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<string, string, bool>(proxyAddress.Replace("{0}", ""), await response.Content.ReadAsStringAsync(), true);
|
||||
}
|
||||
catch (Exception)
|
||||
catch (Exception e)
|
||||
{
|
||||
return null;
|
||||
return new Tuple<string, string, bool>(proxyAddress.Replace("{0}", ""), e.Message, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user