forked from BeyondDimension/SteamTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DnsResolution.cs
84 lines (79 loc) · 2.65 KB
/
DnsResolution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using DnsClient;
using DnsClient.Protocol;
using System.Collections.Generic;
using System.Linq;
using System.Net;
namespace System.Application
{
/// <summary>
/// DNS 解析
/// </summary>
public static class DnsResolution
{
public static IPAddress ResolutionDomainIp(string url)
{
if (!string.IsNullOrEmpty(url))
{
var client = new LookupClient();
var result = client.Query(url, QueryType.A);
if (result.Answers.Count > 0)
{
var address = result.Answers.ARecords().FirstOrDefault()?.Address;
if (address != null) return address;
}
}
return IPAddress.Any;
}
public static int PingDomain(string url)
{
if (!string.IsNullOrEmpty(url))
{
var client = new LookupClient();
var result = client.Query(url, QueryType.A);
if (result.Answers.Count > 0)
{
var value = result.Answers.ARecords().FirstOrDefault()?.TimeToLive;
if (value.HasValue) return value.Value;
}
}
return 0;
}
public static IEnumerable<ARecord> GetPingDomainData(string url)
{
if (!string.IsNullOrEmpty(url))
{
var client = new LookupClient();
var result = client.Query(url, QueryType.A);
if (result.Answers.Count > 0)
{
return result.Answers.ARecords();
}
}
return new List<ARecord>();
}
public static IPAddress ResolutionDomainIpByGoogleDns(string url)
{
if (!string.IsNullOrEmpty(url))
{
var client = new LookupClient();
var dns = new IPAddress[2] { IPAddress.Parse("8.8.8.8"), IPAddress.Parse("8.8.8.4") };
var result = client.Query(new DnsQuestion(url, QueryType.A), new DnsQueryAndServerOptions(dns));
if (result.Answers.Count > 0)
{
var address = result.Answers.ARecords().FirstOrDefault()?.Address;
if (address != null) return address;
}
}
return IPAddress.Any;
}
public static string GetHostByIPAddress(string ip)
{
if (!string.IsNullOrEmpty(ip))
{
var result = Dns.GetHostEntry(IPAddress.Parse(ip)).HostName;
return result;
}
return "";
}
}
}