From 0b1e549e65f67449f10f085a4b7b93f1ffbcb9ba Mon Sep 17 00:00:00 2001 From: BetaFoprhoton <96339528+BetaFoprhoton@users.noreply.github.com> Date: Mon, 29 Jul 2024 20:34:00 +0800 Subject: [PATCH 1/2] Simplify WriteLine, Add remaining IP output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改了部分提示语句输出,在IP请求部分加了剩余IP数输出(强烈建议把中文的变量名称和类名换为英文,用注释替代) --- .../GoogleTranslateIpCheck/Program.cs | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/GoogleTranslateIpCheck/GoogleTranslateIpCheck/Program.cs b/src/GoogleTranslateIpCheck/GoogleTranslateIpCheck/Program.cs index b655836..7eab959 100644 --- a/src/GoogleTranslateIpCheck/GoogleTranslateIpCheck/Program.cs +++ b/src/GoogleTranslateIpCheck/GoogleTranslateIpCheck/Program.cs @@ -46,9 +46,8 @@ if (ips is null || ips?.Count == 0) ips = await ScanIpAsync(); ConcurrentDictionary times = new(); -Console.WriteLine(); -Console.WriteLine("开始检测IP响应时间"); -Console.WriteLine(); +Console.WriteLine("\n开始检测IP响应时间\n"); +var total = ips == null ? 0 : ips.Count; await Parallel.ForEachAsync(ips!, new ParallelOptions() { MaxDegreeOfParallelism = config!.扫描并发数 @@ -65,10 +64,7 @@ Console.ReadKey(); return; } - -Console.WriteLine(); -Console.WriteLine("检测IP完毕,按照响应时间排序结果"); -Console.WriteLine(); +Console.WriteLine("\n检测IP完毕,按照响应时间排序结果\n"); var sortList = times.OrderByDescending(x => x.Value); foreach (var x in sortList) Console.WriteLine($"{x.Key}: 响应时间 {x.Value} ms"); @@ -79,8 +75,7 @@ if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) Console.WriteLine(@"Host文件路径为 C:\Windows\System32\drivers\etc\hosts (需去掉只读属性)"); if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - Console.WriteLine(@"Host文件路径为 /etc/hosts "); -Console.WriteLine(); + Console.WriteLine(@"Host文件路径为 /etc/hosts \n"); foreach (var host in config!.Hosts) Console.WriteLine($"{bestIp} {host}"); Console.WriteLine(); @@ -133,12 +128,14 @@ async Task TestIpAsync(string ip) if (flag) { - Console.WriteLine($"{ip}: 超时"); + Console.WriteLine($"{ip}: 超时, 剩余 {total} 条IP待解析"); + total --; return; } times.TryAdd(ip, time); - Console.WriteLine($"{ip}: 响应时间 {time} ms"); + Console.WriteLine($"{ip}: 响应时间 {time} ms, 剩余 {total} 条IP待解析"); + total --; } async Task?> ScanIpAsync() @@ -193,7 +190,7 @@ async Task TestIpAsync(string ip) } } - Console.WriteLine($"扫描完成,找到 {listIp.Count} 条IP"); + Console.WriteLine($"扫描完成, 共找到 {listIp.Count} 条IP"); return listIp; } @@ -212,8 +209,8 @@ async Task GetResultAsync(string ip) string[]? lines; if (!File.Exists(ipFile)) { - Console.WriteLine("未能找到IP文件"); - Console.WriteLine("尝试从服务器获取IP"); + Console.WriteLine($"未能找到 {ipFile} 文件"); + Console.WriteLine("正在尝试从服务器获取IP"); lines = await ReadRemoteIpAsync(); if (lines is null) return null; @@ -249,7 +246,7 @@ async Task GetResultAsync(string ip) } } - Console.WriteLine($"找到 {listIp.Count} 条IP"); + Console.WriteLine($"共找到 {listIp.Count} 条IP"); return listIp; } From 602723a6f3d37a84473a23b4759c71b622460db8 Mon Sep 17 00:00:00 2001 From: BetaFoprhoton <96339528+BetaFoprhoton@users.noreply.github.com> Date: Tue, 30 Jul 2024 00:35:06 +0800 Subject: [PATCH 2/2] fix thread issue --- .../GoogleTranslateIpCheck/Program.cs | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/GoogleTranslateIpCheck/GoogleTranslateIpCheck/Program.cs b/src/GoogleTranslateIpCheck/GoogleTranslateIpCheck/Program.cs index 7eab959..1e3914f 100644 --- a/src/GoogleTranslateIpCheck/GoogleTranslateIpCheck/Program.cs +++ b/src/GoogleTranslateIpCheck/GoogleTranslateIpCheck/Program.cs @@ -2,11 +2,13 @@ using System.Collections.Concurrent; using System.Diagnostics; using System.Net; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using System.Text.RegularExpressions; +using Flurl.Http.Content; const string configFile = "config.json"; Console.WriteLine("如果支持IPv6推荐优先使用,使用参数 -6 启动"); @@ -47,17 +49,17 @@ ips = await ScanIpAsync(); ConcurrentDictionary times = new(); Console.WriteLine("\n开始检测IP响应时间\n"); -var total = ips == null ? 0 : ips.Count; +var total = ips?.Count + 1 ?? 0; +[MethodImpl(MethodImplOptions.Synchronized)] +Task ReduceTotal() +{ + total --; + return Task.FromResult(total); +} await Parallel.ForEachAsync(ips!, new ParallelOptions() { MaxDegreeOfParallelism = config!.扫描并发数 -}, async (ip, _) => -{ - { - await TestIpAsync(ip); - } -}); - +}, async (ip, _) => await TestIpAsync(ip)); if (times.IsEmpty) { Console.WriteLine("未找到可用IP,可删除 ip.txt 文件直接进入扫描模式"); @@ -128,14 +130,13 @@ async Task TestIpAsync(string ip) if (flag) { - Console.WriteLine($"{ip}: 超时, 剩余 {total} 条IP待解析"); - total --; + Console.WriteLine($"{ip}: 超时, 剩余 {await ReduceTotal()} 条IP待解析"); return; } times.TryAdd(ip, time); - Console.WriteLine($"{ip}: 响应时间 {time} ms, 剩余 {total} 条IP待解析"); - total --; + Console.WriteLine($"{ip}: 响应时间 {time} ms, 剩余 {await ReduceTotal()} 条IP待解析"); + } async Task?> ScanIpAsync()