Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit adb4ecc

Browse files
committed
Merge branch 'master' into steam
2 parents 99b4edb + 7b01cb1 commit adb4ecc

File tree

18 files changed

+370
-415
lines changed

18 files changed

+370
-415
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* Windows 10 1703 或以上
1515
* Microsoft.NET Framework 4.7.2 或以上
1616

17-
可能兼容 Microsoft.NET Framework 4(未测试)
18-
1917
# [Steam 上购买](https://store.steampowered.com/app/828090)
2018

2119
## 这是做什么的
@@ -28,14 +26,21 @@
2826

2927
## TXT列表格式举例
3028
```
31-
www.youtube.com:80 YouTube 80端口 注释可包括空格
29+
# Google
30+
[2607:f8b0:4007:801::2004]:443 IPv6 地址
31+
172.217.14.68:80
3232
www.google.com 谷歌,默认443端口
33+
34+
# Youtube
35+
[2607:f8b0:4007:80e::200e] IPv6 地址
36+
172.217.14.78 IPv4 地址
37+
www.youtube.com:80 80端口 注释可包括空格
3338
```
3439

3540
## 应该支持
3641
* 高 DPI
3742
* 兼容 Win7
38-
43+
* IPv6
3944

4045
## 待完成
4146
* 超时提示

TCPingInfoView.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.27703.2042
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TCPingInfoView", "TCPingInfoView\TCPingInfoView.csproj", "{9CC24A76-98CD-4824-942F-C617373F8428}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TCPingInfoViewTests", "TCPingInfoViewTests\TCPingInfoViewTests.csproj", "{CFD67279-1B90-4F03-9BDB-5C4C86722D63}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{9CC24A76-98CD-4824-942F-C617373F8428}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{9CC24A76-98CD-4824-942F-C617373F8428}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{9CC24A76-98CD-4824-942F-C617373F8428}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{CFD67279-1B90-4F03-9BDB-5C4C86722D63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{CFD67279-1B90-4F03-9BDB-5C4C86722D63}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{CFD67279-1B90-4F03-9BDB-5C4C86722D63}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{CFD67279-1B90-4F03-9BDB-5C4C86722D63}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

TCPingInfoView/Forms/LogForm.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Windows.Forms;
3-
using TCPingInfoView.Collection;
43
using TCPingInfoView.I18n;
54
using TCPingInfoView.Model;
65
using TCPingInfoView.Util;

TCPingInfoView/Forms/MainForm.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Linq;
77
using System.Net;
8+
using System.Net.Sockets;
89
using System.Reflection;
910
using System.Threading;
1011
using System.Threading.Tasks;
@@ -38,8 +39,7 @@ public MainForm()
3839

3940
private static string ExeName => Assembly.GetExecutingAssembly().GetName().Name;
4041

41-
private readonly AppConfig Config =
42-
new AppConfig(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"{ExeName}.json"));
42+
private readonly AppConfig Config = new AppConfig(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"{ExeName}.json"));
4343

4444
private static string ListPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"{ExeName}.txt");
4545

@@ -435,7 +435,7 @@ private void FirstPing()
435435
return;
436436
}
437437

438-
if (IPFormatter.IsIPv4Address(mainTable[i].HostsName)) //反查DNS
438+
if (IPFormatter.IsIPAddress(mainTable[i].HostsName)) //反查DNS
439439
{
440440
PingOne(i);
441441

@@ -459,7 +459,7 @@ private void FirstPing()
459459

460460
if (ip != null)
461461
{
462-
mainTable[i].Endpoint = $@"{ip}:{rawTable[i].Port}";
462+
mainTable[i].Endpoint = ip.AddressFamily == AddressFamily.InterNetwork ? $@"{ip}:{rawTable[i].Port}" : $@"[{ip}]:{rawTable[i].Port}";
463463
}
464464
PingOne(i);
465465
}
@@ -483,15 +483,15 @@ private void PingOne(int index)
483483

484484
if (ip != null)
485485
{
486-
mainTable[index].Endpoint = $@"{ip}:{rawTable[index].Port}";
486+
mainTable[index].Endpoint = ip.AddressFamily == AddressFamily.InterNetwork ? $@"{ip}:{rawTable[index].Port}" : $@"[{ip}]:{rawTable[index].Port}";
487487
}
488488
else
489489
{
490490
return;
491491
}
492492
}
493493

494-
var ipe = IPFormatter.ToIPEndPoint(mainTable[index].Endpoint, 443);
494+
var ipe = IPFormatter.ToIPEndPoint(mainTable[index].Endpoint);
495495
double? latency = null;
496496
var res = Timeout;
497497
var time = DateTime.Now;
Lines changed: 28 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Net;
5-
using System.Net.Sockets;
1+
using System.Net;
62
using System.Text.RegularExpressions;
73

84
namespace TCPingInfoView.NetUtils
95
{
106
public static class IPFormatter
117
{
12-
private static readonly Regex Ipv4Pattern = new Regex("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){1}(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){2}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
8+
public static readonly Regex EndPointRegexStr = new Regex(@"^\[(.*)\]:(\d{1,5})|(.*):(\d{1,5})$");
139

14-
public static bool IsIPv4Address(string input)
10+
public static bool IsIPAddress(string input)
1511
{
16-
return Ipv4Pattern.IsMatch(input);
17-
}
18-
19-
public static bool IsIPv4Address(IPAddress ipAddress)
20-
{
21-
return ipAddress.AddressFamily == AddressFamily.InterNetwork;
22-
}
23-
24-
public static bool IsIPv6Address(IPAddress ipAddress)
25-
{
26-
return ipAddress.AddressFamily == AddressFamily.InterNetworkV6;
12+
return IPAddress.TryParse(input, out _);
2713
}
2814

2915
public static bool IsPort(int port)
@@ -36,70 +22,48 @@ public static bool IsPort(int port)
3622
return false;
3723
}
3824

39-
public static IPEndPoint ToIPEndPoint(string str, int defaultport)
25+
public static IPEndPoint ToIPEndPoint(string str, int defaultPort = 443)
4026
{
41-
if (string.IsNullOrWhiteSpace(str) || !IsPort(defaultport))
27+
if (string.IsNullOrWhiteSpace(str) || !IsPort(defaultPort))
4228
{
4329
return null;
4430
}
4531

46-
var s = str.Split(':');
47-
if (s.Length == 1 || s.Length == 2)
32+
var sp = EndPointRegexStr.Match(str).Groups;
33+
if (sp.Count == 5)
4834
{
49-
if (!IsIPv4Address(s[0]))
35+
var hostname = string.IsNullOrWhiteSpace(sp[1].Value) ? sp[3].Value : sp[1].Value;
36+
if (IPAddress.TryParse(hostname, out var ip))
5037
{
51-
return null;
38+
if (int.TryParse(string.IsNullOrWhiteSpace(sp[2].Value) ? sp[4].Value : sp[2].Value, out var port))
39+
{
40+
if (IsPort(port))
41+
{
42+
return new IPEndPoint(ip, port);
43+
}
44+
}
5245
}
53-
54-
var ip = IPAddress.Parse(s[0]);
55-
if (s.Length == 2)
46+
}
47+
else if (sp.Count == 1)
48+
{
49+
var groups = Regex.Match(str, @"^\[(.*)\]$").Groups;
50+
if (groups.Count == 2)
5651
{
57-
var port = Convert.ToInt32(s[1]);
58-
if (IsPort(port))
52+
if (IPAddress.TryParse(groups[1].Value, out var ip))
5953
{
60-
return ToIPEndPoint(ip, port);
54+
return new IPEndPoint(ip, defaultPort);
6155
}
6256
}
6357
else
6458
{
65-
return ToIPEndPoint(ip, defaultport);
59+
if (IPAddress.TryParse(str, out var ip))
60+
{
61+
return new IPEndPoint(ip, defaultPort);
62+
}
6663
}
6764
}
6865

6966
return null;
7067
}
71-
72-
public static IPEndPoint ToIPEndPoint(IPAddress ip, int port)
73-
{
74-
return new IPEndPoint(ip, port);
75-
}
76-
77-
public static IEnumerable<IPEndPoint> ToIPEndPoints(string str, int defaultport, char[] separator)
78-
{
79-
var s = str.Split(separator, StringSplitOptions.RemoveEmptyEntries);
80-
return s.Select(ipEndPointsStr => ToIPEndPoint(ipEndPointsStr, defaultport)).Where(ipend => ipend != null);
81-
}
82-
83-
public static IEnumerable<IPEndPoint> ToIPEndPoints(IEnumerable<IPAddress> ips, int port)
84-
{
85-
return ips.Select(ip => ToIPEndPoint(ip, port));
86-
}
87-
88-
public static IPAddress PTRName2IP(string str)
89-
{
90-
var s = str.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
91-
return IPAddress.Parse($@"{s[3]}.{s[2]}.{s[1]}.{s[0]}");
92-
}
93-
94-
public static string IPStr2PTRName(string str)
95-
{
96-
if (!IsIPv4Address(str))
97-
{
98-
return string.Empty;
99-
}
100-
101-
var s = str.Split('.');
102-
return $@"{s[3]}.{s[2]}.{s[1]}.{s[0]}.in-addr.arpa";
103-
}
10468
}
10569
}

0 commit comments

Comments
 (0)