Skip to content

Commit

Permalink
添加对IPv6的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Ponderfly committed Jan 16, 2023
1 parent 1c33302 commit d5cb4d1
Show file tree
Hide file tree
Showing 30 changed files with 306 additions and 6,894 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="ip.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="IPv6.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

Expand Down
211 changes: 211 additions & 0 deletions src/GoogleTranslateIpCheck/GoogleTranslateIpCheck/IPv6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
2404:6800:4008:c15::95
2404:6800:4008:c15::8f
2404:6800:4008:c13::8f
2404:6800:4008:c15::bf
2404:6800:4008:c15::84
2404:6800:4008:c15::d3
2404:6800:4008:c13::b0
2404:6800:4008:c15::a5
2404:6800:4008:c13::91
2404:6800:4008:c13::89
2404:6800:4008:c13::cb
2404:6800:4008:c15::78
2404:6800:4008:c13::c9
2404:6800:4008:c13::84
2404:6800:4008:c13::94
2404:6800:4008:c15::1f
2404:6800:4008:c15::8e
2404:6800:4008:c13::74
2404:6800:4008:c13::d3
2404:6800:4008:c13::62
2404:6800:4008:c15::62
2404:6800:4008:c13::c5
2404:6800:4008:c15::75
2404:6800:4008:c15::d6
2404:6800:4008:c15::c9
2404:6800:4008:c13::a7
2404:6800:4008:c15::61
2404:6800:4008:c15::6b
2404:6800:4008:c15::92
2404:6800:4008:c15::a6
2404:6800:4008:c15::7b
2404:6800:4008:c13::7a
2404:6800:4008:c13::95
2404:6800:4008:c15::52
2404:6800:4008:c15::6e
2404:6800:4008:c13::bf
2404:6800:4008:c13::c3
2404:6800:4008:c13::b5
2404:6800:4008:c15::85
2404:6800:4008:c13::a6
2404:6800:4008:c15::70
2404:6800:4008:c15::b0
2404:6800:4008:c15::c2
2404:6800:4008:c15::ca
2404:6800:4008:c13::c4
2404:6800:4008:c15::cb
2404:6800:4008:c13::5a
2404:6800:4008:c15::8a
2404:6800:4008:c15::68
2404:6800:4008:c13::52
2404:6800:4008:c13::98
2404:6800:4008:c15::94
2404:6800:4008:c15::c5
2404:6800:4008:c13::72
2404:6800:4008:c15::c8
2404:6800:4008:c13::c8
2404:6800:4008:c15::7c
2404:6800:4008:c15::c1
2404:6800:4008:c13::92
2404:6800:4008:c15::7a
2404:6800:4008:c13::1f
2404:6800:4008:c13::ca
2404:6800:4008:c13::61
2404:6800:4008:c15::a4
2404:6800:4008:c13::60
2404:6800:4008:c15::5a
2404:6800:4008:c13::85
2404:6800:4008:c13::d6
2404:6800:4008:c15::8d
2404:6800:4008:c13::8e
2404:6800:4008:c15::b5
2404:6800:4008:c15::9c
2404:6800:4008:c15::60
2404:6800:4008:c15::98
2404:6800:4008:c15::11
2404:6800:4008:c13::6b
2404:6800:4008:c15::91
2404:6800:4008:c13::a5
2404:6800:4008:c13::70
2404:6800:4008:c15::6a
2404:6800:4008:c13::a4
2404:6800:4008:c15::89
2404:6800:4008:c13::90
2404:6800:4003:c01::a5
2404:6800:4003:c01::8e
2404:6800:4003:c01::c3
2404:6800:4003:c02::6b
2404:6800:4003:c01::6e
2404:6800:4003:c01::d6
2404:6800:4003:c01::ca
2404:6800:4003:c01::72
2404:6800:4003:c00::78
2404:6800:4003:c00::c2
2404:6800:4003:c02::70
2404:6800:4003:c01::61
2404:6800:4003:c01::90
2404:6800:4003:c00::c8
2404:6800:4003:c02::90
2404:6800:4003:c02::a5
2404:6800:4003:c00::70
2a00:1450:4001:829::201a
2a00:1450:4001:802::2017
2a00:1450:4001:803::2009
2a00:1450:4001:811::2008
2a00:1450:4001:829::2009
2a00:1450:4001:811::2006
2a00:1450:4001:831::2008
2a00:1450:4001:829::2006
2a00:1450:4001:802::2000
2a00:1450:4001:803::2000
2a00:1450:4001:830::2018
2a00:1450:4001:829::2018
2a00:1450:4001:829::2008
2a00:1450:4001:830::2017
2a00:1450:4001:829::2012
2a00:1450:4001:827::2001
2a00:1450:4001:830::201a
2a00:1450:4001:802::2019
2a00:1450:4001:828::2017
2a00:1450:4001:811::201b
2a00:1450:4001:803::2006
2a00:1450:4001:830::2000
2a00:1450:4001:831::201f
2a00:1450:4001:830::2009
2a00:1450:4001:829::2001
2a00:1450:4001:827::201b
2a00:1450:4001:830::2006
2a00:1450:4001:811::201f
2a00:1450:4001:830::201f
2a00:1450:4001:829::2015
2a00:1450:4001:830::2008
2a00:1450:4001:830::2012
2a00:1450:4001:811::2012
2a00:1450:4001:829::2007
2a00:1450:4001:828::201b
2a00:1450:4001:802::2015
2a00:1450:4001:827::201a
2a00:1450:4001:827::2012
2a00:1450:4001:827::2000
2a00:1450:4001:803::2012
2a00:1450:4001:831::2009
2a00:1450:4001:829::201b
2a00:1450:4001:811::2019
2a00:1450:4001:827::2006
2a00:1450:4001:803::2001
2a00:1450:4001:828::2006
2a00:1450:4001:802::2007
2a00:1450:4001:828::2007
2a00:1450:4001:829::2019
2a00:1450:4001:828::2001
2a00:1450:4001:811::2009
2a00:1450:4001:829::2000
2a00:1450:4001:831::2012
2a00:1450:4001:827::2019
2a00:1450:4001:811::2000
2a00:1450:4001:831::2001
2a00:1450:4001:811::2015
2a00:1450:4001:827::2015
2a00:1450:4001:802::2018
2a00:1450:4001:803::2008
2a00:1450:4001:803::2015
2a00:1450:4001:830::2007
2a00:1450:4001:811::2007
2a00:1450:4001:831::201b
2a00:1450:4001:802::201b
2a00:1450:4001:829::201f
2a00:1450:4001:827::201f
2a00:1450:4001:803::2018
2a00:1450:4001:828::2015
2a00:1450:4001:811::201a
2a00:1450:4001:831::2015
2a00:1450:4001:802::2009
2a00:1450:4001:811::2018
2a00:1450:4001:831::2006
2a00:1450:4001:831::201a
2a00:1450:4001:831::2000
2a00:1450:4001:827::2017
2a00:1450:4001:828::2009
2a00:1450:4001:830::2019
2a00:1450:4001:828::2019
2a00:1450:4001:828::201f
2a00:1450:4001:802::2006
2a00:1450:4001:828::2000
2a00:1450:4001:803::2007
2a00:1450:4001:811::2017
2a00:1450:4001:827::2008
2a00:1450:4001:803::201a
2a00:1450:4001:828::201a
2a00:1450:4001:803::201f
2a00:1450:4001:831::2007
2a00:1450:4001:803::201b
2a00:1450:4001:827::2009
2a00:1450:4001:802::2001
2a00:1450:4001:803::2019
2a00:1450:4001:830::2015
2a00:1450:4001:830::201b
2a00:1450:4001:802::2012
2a00:1450:4001:829::2017
2a00:1450:4001:831::2017
2a00:1450:4001:802::201f
2a00:1450:4001:803::2017
2a00:1450:4001:828::2018
2a00:1450:4001:802::201a
2a00:1450:4001:827::2018
2a00:1450:4001:827::2007
2a00:1450:4001:831::2019
2a00:1450:4001:802::2008
2a00:1450:4001:828::2008
2a00:1450:4001:828::2012
2a00:1450:4001:831::2018
2a00:1450:4001:828::200f
52 changes: 42 additions & 10 deletions src/GoogleTranslateIpCheck/GoogleTranslateIpCheck/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
using System.Runtime.InteropServices;

const string configFile = "config.json";
const string ipFile = "ip.txt";
const string Host = "translate.googleapis.com";
const string Host2 = "translate.google.com";

Console.WriteLine("如果支持IPv6推荐优先使用,使用参数 -6 启动");
var config = new Config();
if (File.Exists(configFile))
{
Expand All @@ -28,21 +28,28 @@
}

var autoSet = false;
var isIPv6 = false;
string ipFile = "ip.txt";
HashSet<string>? ips = null;
if (args.Length > 0)
{
if (args.Any(x => "-6".Equals(x)))
{
isIPv6 = true;
ipFile = "IPv6.txt";
}
if (args.Any(x => "-y".Equals(x, StringComparison.OrdinalIgnoreCase)))
autoSet = true;
if (args.Any(x => "-s".Equals(x, StringComparison.OrdinalIgnoreCase)))
ips = await ScanIpAsync();
}
ips ??= await ReadIpAsync();
if (ips is null || ips?.Count == 0)
{
ips = await ScanIpAsync();
}
ConcurrentDictionary<string, long> times = new();
Console.WriteLine();
Console.WriteLine("开始检测IP响应时间");
Console.WriteLine();
await Parallel.ForEachAsync(ips!, new ParallelOptions()
{
MaxDegreeOfParallelism = config!.扫描并发数
Expand All @@ -61,6 +68,7 @@ await Parallel.ForEachAsync(ips!, new ParallelOptions()
}
Console.WriteLine();
Console.WriteLine("检测IP完毕,按照响应时间排序结果");
Console.WriteLine();
var sortList = times.OrderByDescending(x => x.Value);
foreach (var x in sortList)
Console.WriteLine($"{x.Key}: 响应时间 {x.Value} ms");
Expand Down Expand Up @@ -135,7 +143,8 @@ async Task TestIpAsync(string ip)
var listIp = new HashSet<string>();
CancellationTokenSource cts = new();
cts.Token.Register(() => { Console.WriteLine($"已经找到 {config!.IP扫描限制数量} 条IP,结束扫描"); });
foreach (var ipRange in config!.IP段)
var IP= !isIPv6 ? config!.IP段 : config!.IPv6段;
foreach (var ipRange in IP段)
{
if (string.IsNullOrWhiteSpace(ipRange)) continue;
IPNetwork ipnetwork = IPNetwork.Parse(ipRange);
Expand All @@ -157,7 +166,7 @@ async Task TestIpAsync(string ip)
var result = await GetResultAsync(ip.ToString());
if (!result)
return;
Console.WriteLine($"找到IP: {ip}");
Console.WriteLine($"{ip}");
_ips.Add(ip.ToString());
if (listIp.Count + _ips.Count >= config!.IP扫描限制数量)
cts.Cancel();
Expand All @@ -180,7 +189,8 @@ async Task TestIpAsync(string ip)

async Task<bool> GetResultAsync(string ip)
{
var url = $@"https://{ip}/translate_a/single?client=gtx&sl=zh-CN&tl=en&dt=t&q=你好";
var str = !isIPv6 ? $"{ip}" : $"[{ip}]";
var url = $@"https://{str}/translate_a/single?client=gtx&sl=zh-CN&tl=en&dt=t&q=你好";
return (await url
.WithHeader("host", Host)
.WithTimeout(config!.扫描超时)
Expand Down Expand Up @@ -217,13 +227,13 @@ async Task<bool> GetResultAsync(string ip)
{
foreach (var ip in line.Split(','))
{
if (RegexStuff.IpRegex().IsMatch(ip.Trim()))
if (CheckIp(ip))
listIp.Add(ip.Trim());
}
}
else
{
if (RegexStuff.IpRegex().IsMatch(line.Trim()))
if (CheckIp(line))
listIp.Add(line.Trim());
}
}
Expand All @@ -235,7 +245,8 @@ async Task<bool> GetResultAsync(string ip)
{
try
{
var text = await config!.远程IP文件.WithTimeout(10).GetStringAsync();
var address = !isIPv6 ? config!.远程IP文件 : config!.远程IPv6文件;
var text = await address.WithTimeout(10).GetStringAsync();
return text.Split(new[] { '\n' },
StringSplitOptions.RemoveEmptyEntries);
}
Expand Down Expand Up @@ -289,7 +300,7 @@ void Add(string s)

async Task SaveIpFileAsync()
{
await File.WriteAllLinesAsync(ipFile, times.Keys, Encoding.UTF8);
await File.WriteAllLinesAsync(ipFile, sortList.Reverse().Select(x => x.Key), Encoding.UTF8);
}

async Task FlushDns()
Expand Down Expand Up @@ -343,15 +354,26 @@ async Task FlushDns()
}
}

bool CheckIp(string ip)
{
if (!isIPv6)
return RegexStuff.IpRegex().IsMatch(ip.Trim());
else
return RegexStuff.IPv6Regex().IsMatch(ip.Trim());
}

public partial class RegexStuff
{
[GeneratedRegex(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")]
public static partial Regex IpRegex();
[GeneratedRegex(@"^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$")]
public static partial Regex IPv6Regex();
}

public class Config
{
public string 远程IP文件 { get; set; } = "https://ghproxy.com/https://github.com/Ponderfly/GoogleTranslateIpCheck/releases/download/1.1.1/ips.txt";
public string 远程IPv6文件 { get; set; } = "https://ghproxy.com/https://github.com/Ponderfly/GoogleTranslateIpCheck/releases/download/1.1.1/IPv6.txt";
public int IP扫描限制数量 { get; set; } = 5;
public int 扫描超时 { get; set; } = 4;
public int 扫描并发数 { get; set; } = 80;
Expand All @@ -365,6 +387,16 @@ public class Config
74.125.0.0/16
216.58.192.0/19
"""".Split(Environment.NewLine);
public string[] IPv6段 { get; set; } =
""""
2404:6800:4008:c15::0/112
2a00:1450:4001:802::0/112
2a00:1450:4001:803::0/112
2a00:1450:4001:809::0/112
2a00:1450:4001:811::0/112
2a00:1450:4001:827::0/112
2a00:1450:4001:828::0/112
"""".Split(Environment.NewLine);
}

[JsonSerializable(typeof(Config))]
Expand Down
Loading

0 comments on commit d5cb4d1

Please sign in to comment.