-
Notifications
You must be signed in to change notification settings - Fork 1
/
dnscouch.go
278 lines (259 loc) · 8.07 KB
/
dnscouch.go
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package dnscouch
import (
"errors"
"fmt"
"net"
"os"
"sort"
"strings"
"time"
"github.com/beevik/ntp"
"github.com/miekg/dns"
)
var ServerMap4 = map[string]string{
"1.1.1.1": "Cloudflare One",
"1.0.0.1": "Cloudflare One",
"8.8.8.8": "Google Primary",
"8.8.4.4": "Google Secondary",
"208.67.222.222": "OpenDNS Primary",
"208.67.220.220": "OpenDNS Secondary",
"4.2.2.1": "Level 3",
"209.244.0.3": "Level 3",
"209.244.0.4": "Level 3",
"9.9.9.10": "Quad9 unfiltered",
"149.112.112.10": "Quad9 unfiltered",
"68.94.156.1": "ATT Primary",
"68.94.157.1": "ATT Secondary",
"12.121.117.201": "ATT Services",
"8.26.56.26": "Comodo Primary",
"8.20.247.20": "Comodo Secondary",
"76.76.2.0": "Control D Primary",
"76.76.10.0": "Control D Secondary",
"185.228.168.9": "Clean Browsing Primary",
"185.228.169.9": "Clean Browsing Secondary",
"76.76.19.19": "Alternate DNS Primary",
"76.223.122.150": "Alternate DNS Secondary",
"94.140.14.14": "AdGuard DNS Primary",
"94.140.15.15": "AdGuard DNS Secondary",
}
var ServerMap6 = map[string]string{
"[2606:4700:4700::1111]": "Cloudflare One",
"[2606:4700:4700::1001]": "Cloudflare One",
"[2001:4860:4860::8888]": "Google Primary",
"[2001:4860:4860::8844]": "Google Secondary",
"[2620:119:35::35]": "OpenDNS Primary",
"[2620:119:53::53]": "OpenDNS Secondary",
// Couldn't find IPv6 values for Level 3
"[2620:fe::fe]": "Quad9 unfiltered",
"[2620:fe::9]": "Quad9 unfiltered",
// Couldn't find IPv6 values for ATT
// Couldn't find IPv6 values for Comodo
"[2606:1a40::]": "Control D Primary",
"[2606:1a40:1::]": "Control D Secondary",
"[2a0d:2a00:1::]": "Clean Browsing Primary",
"[2a0d:2a00:2::]": "Clean Browsing Secondary",
"[2602:fcbc::ad]": "Alternate DNS Primary",
"[2602:fcbc:2::ad]": "Alternate DNS Secondary",
"[2a10:50c0::ad1:ff]": "AdGuard DNS Primary",
"[2a10:50c0::ad2:ff]": "AdGuard DNS Secondary",
}
var FilteredServerMap = map[string]string{
"1.1.1.2": "Cloudflare Malware Filtered",
"1.0.0.2": "Cloudflare Malware Filtered",
"1.1.1.3": "Cloudflare Adult Filtered",
"1.0.0.3": "Cloudflare Adult Filtered",
"9.9.9.9": "Quad9 filtered Primary",
"149.112.112.112": "Quad9 filtered Secondary",
// Not technically a filter, but a strange, rare feature:
// EDNS Client-Subnet.
// More about ECS here: https://www.isc.org/blogs/quad9-2020-06/ and
// here: https://www.quad9.net/support/faq/#edns
"9.9.9.11": "Quad9 ecs unfiltered",
"149.112.112.11": "Quad9 ecs unfiltered",
}
// EnableComcast enables comcast DNS servers. They don't response from outside
// the comcast network.
func EnableComcast(servers map[string]string) {
comcast := map[string]string{
"75.75.75.75": "Comcast Primary",
"75.75.76.76": "Comcast Secondary",
// "[2001:558:feed::1]:53": "Comcast Primary IPv6",
// "[2001:558:feed::2]:53": "Comcast Secondary IPv6",
"68.87.85.102": "Comcast older Primary",
"68.87.64.150": "Comcast older Secondary",
}
for s, p := range comcast {
servers[s] = p
}
}
func TimeDNSLookup(server string) (time.Duration, error) {
if _, _, err := net.SplitHostPort(server); err != nil {
if !strings.Contains(err.Error(), "missing port in address") {
return 0, fmt.Errorf("TimeDNSLookup[%q]: %w", server, err)
}
server += ":53"
}
c := new(dns.Client)
m := new(dns.Msg)
m.SetQuestion("google.com.", dns.TypeA)
start := time.Now()
_, _, err := c.Exchange(m, server)
if errors.Is(err, os.ErrDeadlineExceeded) {
return 2 * time.Second, nil // miekg.dns timeout default is 2s.
}
return time.Since(start), err
}
type Result struct {
ServerName, Desc string
D time.Duration
}
type Results []Result
func (a Results) Len() int { return len(a) }
func (a Results) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Results) Less(i, j int) bool { return a[i].D < a[j].D }
func TimeDNSLookupServers(servers map[string]string) (map[string]time.Duration, error) {
times := make(map[string]time.Duration, len(servers))
for s := range servers {
t, err := TimeDNSLookup(s)
if err != nil {
return times, err
}
times[s] = t
}
return times, nil
}
func LookupServers(servers map[string]string) (Results, error) {
var r Results
times, err := TimeDNSLookupServers(servers)
if err != nil {
return nil, err
}
for s, t := range times {
r = append(r, Result{s, servers[s], t})
}
sort.Sort(r)
return r, nil
}
func LookupServersN(servers map[string]string, n int) (Results, error) {
var r Results
var allTimes []map[string]time.Duration
for i := 0; i < n; i++ {
t, err := TimeDNSLookupServers(servers)
if err != nil {
return nil, err
}
allTimes = append(allTimes, t)
}
times := make(map[string]time.Duration, len(servers))
for s := range servers {
sum := int64(0)
for i := 0; i < n; i++ {
sum += int64(allTimes[i][s])
}
avg := sum / int64(n)
times[s] = time.Duration(avg)
}
for s, t := range times {
r = append(r, Result{s, servers[s], t})
}
sort.Sort(r)
return r, nil
}
func TimeDNSLookupServersAvg(servers map[string]string, n int) (map[string]time.Duration, error) {
times := make(map[string]time.Duration, len(servers))
for i := 0; i < n; i++ {
for s := range servers {
t, err := TimeDNSLookup(s)
if err != nil {
return times, err
}
times[s] = t
}
}
return times, nil
}
var NTPServerMap = map[string]string{
"time.cloudflare.com": "Cloudflare time",
"ntp.ubuntu.com": "NTP Ubuntu",
"0.ubuntu.pool.ntp.org": "NTP Ubuntu 0",
"1.ubuntu.pool.ntp.org": "NTP Ubuntu 1",
"2.ubuntu.pool.ntp.org": "NTP Ubuntu 2",
"ntp.nexcess.net": "NexcessNet",
"time.nist.gov": "NIST",
"pool.ntp.org": "NTP org pool",
"0.pool.ntp.org": "NTP org pool 0",
"1.pool.ntp.org": "NTP org pool 1",
"2.pool.ntp.org": "NTP org pool 2",
"time1.google.com": "Google time",
"time2.google.com": "Google time",
"time3.google.com": "Google time",
"time4.google.com": "Google time",
"time.windows.com": "Windows time",
"time.apple.com": "Apple time",
// Intentionally not adding time.facebook.com because facebook is evil AND
// because I DNS sinkhole facebook DNS properties in my home so I cannot
// test it. 😀
// Intentionally not adding russian servers from:
// https://gist.github.com/mutin-sa/eea1c396b1e610a2da1e5550d94b0453
// because Russia is evil and untrustworthy.
// Intentinonlly not adding a lot of stuff because there are so many.
// TODO: regional server list options for EU, NA, ASIA
"ntp1.hetzner.de": "Hetzner Online 1",
"ntp2.hetzner.de": "Hetzner Online 2",
"ntp3.hetzner.de": "Hetzner Online 3",
"ntp.ripe.net": "RIPE",
"clock.isc.org": "ISC",
"0.amazon.pool.ntp.org": "Amazon 0",
"1.amazon.pool.ntp.org": "Amazon 1",
"2.amazon.pool.ntp.org": "Amazon 2",
"3.amazon.pool.ntp.org": "Amazon 3",
}
func LookupNTPServersN(n int) (Results, error) {
var r Results
var allTimes []map[string]time.Duration
for i := 0; i < n; i++ {
if i > 0 {
// NTP Servers can throttle aggressively.
// e.g. kiss of death received: RATE
time.Sleep(2 * time.Second)
}
t, err := TimeNTPLookupServers()
if err != nil {
return nil, err
}
allTimes = append(allTimes, t)
}
times := make(map[string]time.Duration, len(NTPServerMap))
for s := range NTPServerMap {
sum := int64(0)
for i := 0; i < n; i++ {
sum += int64(allTimes[i][s])
}
avg := sum / int64(n)
times[s] = time.Duration(avg)
}
for s, t := range times {
r = append(r, Result{s, NTPServerMap[s], t})
}
sort.Sort(r)
return r, nil
}
func TimeNTPLookupServers() (map[string]time.Duration, error) {
times := make(map[string]time.Duration, len(NTPServerMap))
for s := range NTPServerMap {
t, err := TimeNTPLookup(s)
if err != nil {
return times, err
}
times[s] = t
}
return times, nil
}
func TimeNTPLookup(server string) (time.Duration, error) {
now := time.Now()
_, err := ntp.Time(server)
if errors.Is(err, os.ErrDeadlineExceeded) {
return 5 * time.Second, nil // miekg.dns timeout default is 2s.
}
return -time.Until(now), err
}