@@ -2757,58 +2757,108 @@ private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
27572757 // 创建专用方法用于异步检测GitHub代理
27582758 private async Task < string > DetectGitHubProxyAsync ( List < string > proxyItems )
27592759 {
2760- bool proxyFound = false ;
2761- string detectedProxyURL = "" ;
2762-
27632760 Log ( "检测可用 GitHub 代理..." , GetRichTextBoxAllLog ( ) ) ;
27642761
2765- // 遍历随机排序后的代理列表
2766- foreach ( string proxyItem in proxyItems )
2762+ // 固定 commit,确保内容不变
2763+ const string testTarget = "https://raw.githubusercontent.com/golang/go/080aa8e9647e5211650f34f3a93fb493afbe396d/src/net/http/transport.go" ;
2764+ const string expectedHash = "cbb44007f7cc4cd862acfdb70fbbf5bd89cd800de78a2905bfbc71900e7639e2" ;
2765+ const long minSizeBytes = 50 * 1024 ;
2766+
2767+ // 并发检测所有代理
2768+ var tasks = proxyItems . Select ( async proxyItem =>
27672769 {
2768- string checkUrl = $ "https://{ proxyItem } /https://raw.githubusercontent.com/sinspired/SubsCheck-Win-GUI/master/packages.config";
2769- Log ( $ "正在测试 GitHub 代理: { proxyItem } ", GetRichTextBoxAllLog ( ) ) ;
2770+ string proxyBase = proxyItem . StartsWith ( "http://" ) || proxyItem . StartsWith ( "https://" )
2771+ ? proxyItem
2772+ : $ "https://{ proxyItem } ";
2773+ if ( ! proxyBase . EndsWith ( "/" ) )
2774+ proxyBase += "/" ;
2775+
2776+ string checkUrl = proxyBase + testTarget ;
2777+ // Log($"正在测试 GitHub 代理: {proxyBase}", GetRichTextBoxAllLog());
27702778 richTextBoxAllLog . Refresh ( ) ;
27712779
27722780 try
27732781 {
2774- using ( HttpClient client = new HttpClient ( ) )
2782+ using ( var handler = new HttpClientHandler { Proxy = null , UseProxy = false } )
2783+ using ( var client = new HttpClient ( handler ) )
27752784 {
2776- client . Timeout = TimeSpan . FromSeconds ( 5 ) ; // 设置5秒超时
2777- // 添加User-Agent头,避免被拒绝访问
2778- client . DefaultRequestHeaders . UserAgent . ParseAdd ( "Mozilla/5.0 (Windows NT 10.0; Win32; x86) AppleWebKit/537.36 (KHTML, like Gecko) cmliu/SubsCheck-Win-GUI " ) ;
2785+ client . Timeout = TimeSpan . FromSeconds ( 15 ) ;
2786+ client . DefaultRequestHeaders . UserAgent . ParseAdd (
2787+ "Mozilla/5.0 (Windows NT 10.0; Win32; x86) AppleWebKit/537.36" ) ;
27792788
2780- // 使用异步方式
2781- HttpResponseMessage response = await client . GetAsync ( checkUrl ) ;
2782- if ( response . IsSuccessStatusCode )
2789+ var sw = System . Diagnostics . Stopwatch . StartNew ( ) ;
2790+ var response = await client . GetAsync ( checkUrl , HttpCompletionOption . ResponseContentRead ) ;
2791+ sw . Stop ( ) ;
2792+
2793+ if ( ! response . IsSuccessStatusCode )
2794+ return null ;
2795+
2796+ byte [ ] content = await response . Content . ReadAsByteArrayAsync ( ) ;
2797+
2798+ // 校验文件大小
2799+ if ( content . Length < minSizeBytes )
2800+ {
2801+ // Log($"代理 {proxyBase} 返回内容过小({content.Length} bytes),疑似无效响应", GetRichTextBoxAllLog(), true);
2802+ return null ;
2803+ }
2804+
2805+ // 校验 SHA256
2806+ string actualHash ;
2807+ using ( var sha256 = System . Security . Cryptography . SHA256 . Create ( ) )
27832808 {
2784- // 找到可用代理
2785- detectedProxyURL = $ "https://{ proxyItem } /";
2786- //richTextBoxAllLog.Clear(); 暂时禁用
2787- Log ( $ "找到可用 GitHub 代理: { proxyItem } ", GetRichTextBoxAllLog ( ) ) ;
2788- proxyFound = true ;
2789- break ;
2809+ var hashBytes = sha256 . ComputeHash ( content ) ;
2810+ actualHash = BitConverter . ToString ( hashBytes ) . Replace ( "-" , "" ) . ToLowerInvariant ( ) ;
27902811 }
2812+
2813+ if ( actualHash != expectedHash )
2814+ {
2815+ // Log($"代理 {proxyBase} 内容校验失败,疑似虚假代理", GetRichTextBoxAllLog(), true);
2816+ return null ;
2817+ }
2818+
2819+ double speedKBps = content . Length / 1024.0 / sw . Elapsed . TotalSeconds ;
2820+ // Log($"代理 {proxyBase} 可用,耗时 {sw.ElapsedMilliseconds}ms,速度 {speedKBps:F1}KB/s", GetRichTextBoxAllLog());
2821+
2822+ return new
2823+ {
2824+ ProxyURL = proxyBase ,
2825+ Elapsed = sw . Elapsed ,
2826+ SpeedKBps = speedKBps
2827+ } ;
27912828 }
27922829 }
27932830 catch ( Exception ex )
27942831 {
2795- // 记录错误但继续尝试下一个
2796- Log ( $ "代理 { proxyItem } 测试失败: { ex . Message } ", GetRichTextBoxAllLog ( ) , true ) ;
2832+ // Log($"代理 {proxyItem} 测试失败: {ex.Message}", GetRichTextBoxAllLog(), true);
27972833 richTextBoxAllLog . Refresh ( ) ;
2834+ return null ;
27982835 }
2799- }
2836+ } ) . ToList ( ) ;
2837+
2838+ // 等待所有检测完成
2839+ var results = await Task . WhenAll ( tasks ) ;
2840+
2841+ // 综合评分:速度 70% + 延迟 30%
2842+ var best = results
2843+ . Where ( r => r != null )
2844+ . OrderByDescending ( r => r . SpeedKBps * 0.7 + ( 1.0 / r . Elapsed . TotalSeconds ) * 0.3 )
2845+ . FirstOrDefault ( ) ;
28002846
2801- // 如果没有找到可用的代理
2802- if ( ! proxyFound )
2847+ if ( best != null )
28032848 {
2804- Log ( "未找到可用的 GitHub 代理,请在高级设置中手动设置。" , GetRichTextBoxAllLog ( ) , true ) ;
2805- MessageBox . Show ( "未找到可用的 GitHub 代理。\n \n 请打开高级设置手动填入一个可用的Github Proxy,或检查您的网络连接。" ,
2806- "代理检测失败" ,
2807- MessageBoxButtons . OK ,
2808- MessageBoxIcon . Warning ) ;
2849+ Log ( $ "最佳 GitHub 代理: { best . ProxyURL } ,速度 { best . SpeedKBps : F1} KB/s,耗时 { best . Elapsed . TotalMilliseconds : F0} ms",
2850+ GetRichTextBoxAllLog ( ) ) ;
2851+ return best . ProxyURL ;
28092852 }
28102853
2811- return detectedProxyURL ;
2854+ Log ( "未找到可用的 GitHub 代理,请手动设置。" , GetRichTextBoxAllLog ( ) , true ) ;
2855+ MessageBox . Show (
2856+ "未找到可用的 GitHub 代理。\n \n 请手动填入可用的 Github Proxy,或检查您的网络连接。" ,
2857+ "代理检测失败" ,
2858+ MessageBoxButtons . OK ,
2859+ MessageBoxIcon . Warning ) ;
2860+
2861+ return "" ;
28122862 }
28132863
28142864 private async void buttonUpdateKernel_Click ( object sender , EventArgs e )
0 commit comments