-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNS.cs
More file actions
135 lines (113 loc) · 5.05 KB
/
DNS.cs
File metadata and controls
135 lines (113 loc) · 5.05 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using ARSoft.Tools.Net.Dns;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
namespace DNSRootServerResolver
{
public static class DNS
{
public static readonly SortedList<String, Entry> GlobalCache = new SortedList<String, Entry>();
public static readonly DnsClient EarthRoot = new DnsClient(IPAddress.Parse("192.228.79.201"), 2000);
public static readonly DnsClient GoogleDns = new DnsClient(IPAddress.Parse("8.8.8.8"), 2000);
public class Entry : Expirable<DnsMessage>
{
public Entry(Func<DnsMessage> renewer, Func<DnsMessage, DateTime> reexpirator) : base(renewer, reexpirator) { }
public long RequestedTimes;
}
private static string getDomainRoot(string domain)
{
return String.Join(".", domain.Split('.').Reverse().Take(2).Reverse());
}
public static List<DnsRecordBase> Resolve(string domain, RecordType type, RecordClass @class, bool useGoogleLookup = false)
{
Entry domainCache;
if (GlobalCache.TryGetValue(domain, out domainCache))
{
domainCache.RequestedTimes++;
return domainCache.Value.AnswerRecords;
}
else
{
if (GlobalCache.TryGetValue("@provider." + getDomainRoot(domain), out domainCache))
{
var providerServers = domainCache.Value;
foreach (var providerServer in providerServers.AdditionalRecords.OfType<ARecord>())
{
var providerServerDns = new DnsClient(providerServer.Address, 2000);
var hostServers = CacheResolver(providerServerDns, domain, domain, type, @class);
if (hostServers != null)
{
return hostServers.AnswerRecords;
}
}
}
}
if (useGoogleLookup)
{
var google = CacheResolver(GoogleDns, domain, domain, type, @class);
if (google != null)
{
return google.AnswerRecords;
}
}
else
{
var tld = domain.Split('.').Last();
var countryServers = CacheResolver(EarthRoot, domain, "@earthroot." + tld, type, @class);
if (countryServers != null)
{
foreach (var countryServer in countryServers.AdditionalRecords.OfType<ARecord>())
{
var countryServerDns = new DnsClient(countryServer.Address, 2000);
var providerServers = CacheResolver(countryServerDns, domain, "@provider." + getDomainRoot(domain), type, @class);
if (providerServers != null)
{
foreach (var providerServer in providerServers.AdditionalRecords.OfType<ARecord>())
{
var providerServerDns = new DnsClient(providerServer.Address, 2000);
var hostServers = CacheResolver(providerServerDns, domain, domain, type, @class);
if (hostServers != null)
{
return hostServers.AnswerRecords;
}
}
}
}
}
}
return new List<DnsRecordBase>();
}
private static DnsMessage CacheResolver(DnsClient client, string domain, string cacheName, RecordType type, RecordClass @class)
{
Entry domainCache;
if (!GlobalCache.TryGetValue(cacheName, out domainCache))
{
Func<DnsMessage> renewer = () => client.Resolve(domain, type, @class);
Func<DnsMessage, DateTime> reexpirator = (dns) =>
{
if (dns != null)
{
if (dns.AnswerRecords.Count != 0)
{
return DateTime.Now.AddSeconds(dns.AnswerRecords.First().TimeToLive);
}
else if (dns.AuthorityRecords.Count != 0)
{
return DateTime.Now.AddSeconds(dns.AuthorityRecords.First().TimeToLive);
}
else if (dns.AdditionalRecords.Count != 0)
{
return DateTime.Now.AddSeconds(dns.AdditionalRecords.First().TimeToLive);
}
}
return DateTime.Now.AddMinutes(1);
};
domainCache = new Entry(renewer, reexpirator);
GlobalCache.Add(cacheName, domainCache);
}
domainCache.RequestedTimes++;
return domainCache.Value;
}
}
}