Skip to content

Commit

Permalink
[#] 修复QQNT插件更新前对代理地址没有正确测速的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
huiyadanli committed Nov 6, 2024
1 parent 25532df commit 7cbb893
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 34 deletions.
10 changes: 5 additions & 5 deletions RevokeMsgPatcher/Forms/FormLiteLoaderQQNT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});
}
Expand Down
30 changes: 27 additions & 3 deletions RevokeMsgPatcher/Model/LiteLoaderRowData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ public void GetLocalVersionAndUpdateStatus()
}
}

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)
{
Expand All @@ -144,6 +144,30 @@ public async Task<string> 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
Expand All @@ -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($"下载成功,解压中...");
Expand Down
67 changes: 41 additions & 26 deletions RevokeMsgPatcher/Utils/ProxySpeedTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

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<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);
}
}
}
Expand Down

0 comments on commit 7cbb893

Please sign in to comment.