This commit is contained in:
snltty
2024-04-21 08:56:46 +08:00
parent 08c37367bb
commit 035a4a1b0c
29 changed files with 627 additions and 114 deletions

View File

@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
namespace common.libs
{
@@ -46,6 +49,75 @@ namespace common.libs
return new IPEndPoint(ip, port);
}
public static ushort GetRouteLevel(out List<IPAddress> ips)
{
ips = new List<IPAddress>();
try
{
List<string> starts = new() { "10.", "100.", "192.168.", "172." };
var list = GetTraceRoute("www.baidu.com").ToList();
for (ushort i = 0; i < list.Count(); i++)
{
string ip = list.ElementAt(i).ToString();
if (ip.StartsWith(starts[0], StringComparison.Ordinal) || ip.StartsWith(starts[1], StringComparison.Ordinal) || ip.StartsWith(starts[2], StringComparison.Ordinal) || ip.StartsWith(starts[3], StringComparison.Ordinal))
{
if (ip.StartsWith(starts[2], StringComparison.Ordinal) == false)
ips.Add(list.ElementAt(i));
}
else
{
return i;
}
}
}
catch (Exception)
{
}
return 0;
}
public static IEnumerable<IPAddress> GetTraceRoute(string hostNameOrAddress)
{
return GetTraceRoute(hostNameOrAddress, 1);
}
private static IEnumerable<IPAddress> GetTraceRoute(string hostNameOrAddress, int ttl)
{
Ping pinger = new();
// 创建PingOptions对象
PingOptions pingerOptions = new(ttl, true);
int timeout = 100;
byte[] buffer = Encoding.ASCII.GetBytes("11");
// 创建PingReply对象
// 发送ping命令
PingReply reply = pinger.Send(hostNameOrAddress, timeout, buffer, pingerOptions);
// 处理返回结果
List<IPAddress> result = new();
if (reply.Status == IPStatus.Success)
{
result.Add(reply.Address);
}
else if (reply.Status == IPStatus.TtlExpired || reply.Status == IPStatus.TimedOut)
{
//增加当前这个访问地址
if (reply.Status == IPStatus.TtlExpired)
{
result.Add(reply.Address);
}
if (ttl <= 10)
{
//递归访问下一个地址
IEnumerable<IPAddress> tempResult = GetTraceRoute(hostNameOrAddress, ttl + 1);
result.AddRange(tempResult);
}
}
else
{
//失败
}
return result;
}
#if DISABLE_IPV6 || (!UNITY_EDITOR && ENABLE_IL2CPP && !UNITY_2018_3_OR_NEWER)
public static bool IPv6Support = false;