-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
482 lines (405 loc) · 12 KB
/
main.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
wildcard "github.com/IGLOU-EU/go-wildcard"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/libnetwork/resolvconf"
"github.com/google/uuid"
"github.com/miekg/dns"
)
func resolve(upstream string, question dns.Question, recursionDesired bool, currentNet string) (*dns.Msg, time.Duration, error) {
c := dns.Client{
DialTimeout: dialTimeout,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
Net: currentNet,
}
query := &dns.Msg{}
query.SetQuestion(question.Name, question.Qtype)
query.RecursionDesired = recursionDesired
if edns0 > -1 {
query.SetEdns0(uint16(edns0), false)
}
return c.Exchange(query, upstream)
}
var eventMutex sync.Mutex
func event(upstream string, name string) {
defer eventMutex.Unlock()
eventMutex.Lock()
events[upstream][name] = events[upstream][name] + 1
}
func harder(id string, question dns.Question, recursionDesired bool, currentUpstreams []string) *dns.Msg {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
shuffledUpstreams := make([]string, len(currentUpstreams))
copy(shuffledUpstreams, currentUpstreams)
rand.Shuffle(len(shuffledUpstreams), func(i, j int) {
shuffledUpstreams[i], shuffledUpstreams[j] = shuffledUpstreams[j], shuffledUpstreams[i]
})
responses := make(chan *dns.Msg, len(shuffledUpstreams))
for i, upstream := range shuffledUpstreams {
go func(index int, upstream string, question dns.Question) {
if concurrencyDelay > 0 {
myConcurrencyDelay := time.Duration(concurrencyDelay) * time.Duration(index)
select {
case <-ctx.Done():
return
case <-time.After(myConcurrencyDelay):
}
}
try := 0
currentNet := netMode
for try < tries {
select {
case <-ctx.Done():
return
default:
}
response, rtt, err := resolve(upstream, question, recursionDesired, currentNet)
select {
case <-ctx.Done():
return
default:
}
if err == nil {
if response.Truncated {
event(upstream, "trunc")
logger(id, "TRUNC", question, upstream, rtt.String(), strconv.Itoa(try))
// https://serverfault.com/questions/991520/how-is-truncation-performed-in-dns-according-to-rfc-1035/991563#991563
currentNet = "tcp"
// log.Println(
// "Answer", response.Answer,
// "AuthenticatedData", response.AuthenticatedData,
// "Authoritative", response.Authoritative,
// "CheckingDisabled", response.CheckingDisabled,
// "Compress", response.Compress,
// "Extra", response.Extra,
// "Id", response.Id,
// "MsgHdr", response.MsgHdr,
// "Ns", response.Ns,
// "Opcode", response.Opcode,
// "Question", response.Question,
// "Rcode", response.Rcode,
// "RecursionAvailable", response.RecursionAvailable,
// "RecursionDesired", response.RecursionDesired,
// "Response", response.Response,
// "Truncated", response.Truncated,
// "Zero", response.Zero,
// )
} else if len(response.Answer) == 0 {
if len(response.Ns) > 0 {
logger(id, "EMPTYNS", question, upstream, rtt.String(), strconv.Itoa(try))
responses <- response
return
} else {
logger(id, "EMPTY", question, upstream, rtt.String(), strconv.Itoa(try))
}
} else {
event(upstream, "got")
logger(id, "GOT", question, upstream, rtt.String(), strconv.Itoa(try))
responses <- response
return
}
} else {
event(upstream, "error")
logger(id, "ERROR", question, upstream, fmt.Sprintf("%v", err)+" "+rtt.String())
}
select {
case <-ctx.Done():
return
default:
}
try = try + 1
// retry truncated instantly
if response == nil {
time.Sleep(delay)
}
select {
case <-ctx.Done():
return
default:
}
if currentNet == "udp" {
currentNet = "tcp"
}
logger(id, "RETRY", question, upstream, currentNet, strconv.Itoa(try))
}
responses <- nil
}(i, upstream, question)
}
received := 0
var final *dns.Msg
for response := range responses {
received = received + 1
if response != nil {
final = response
break
}
if received == len(shuffledUpstreams) {
break
}
}
return final
}
var loggerMutex sync.Mutex
func logger(id string, kind string, question dns.Question, parts ...string) {
var sb strings.Builder
sb.WriteString(id)
sb.WriteString("\t")
sb.WriteString(kind)
sb.WriteString("\t")
sb.WriteString(dns.Type(question.Qtype).String())
sb.WriteString("\t")
sb.WriteString(question.Name)
sb.WriteString(" ")
for _, part := range parts {
sb.WriteString(part)
sb.WriteString("\t")
}
loggerMutex.Lock()
println(sb.String())
loggerMutex.Unlock()
}
func reloadHosts(hostsPath string) {
if hostsPath == "" {
return
}
if b, err := ioutil.ReadFile(hostsPath); err != nil {
log.Fatalln("failed to read", hostsPath, err)
} else {
if err := json.Unmarshal(b, &hosts); err != nil {
log.Fatalln("failed to parse", hostsPath, err)
}
}
}
func createResponse(rrs []dns.RR) *dns.Msg {
response := new(dns.Msg)
response.Compress = false
response.RecursionAvailable = true
for _, rr := range rrs {
response.Answer = append(response.Answer, rr)
}
return response
}
func handleDnsRequest(w dns.ResponseWriter, request *dns.Msg) {
id := uuid.New().String()
var final *dns.Msg
// https://stackoverflow.com/questions/55092830/how-to-perform-dns-lookup-with-multiple-questions
question := request.Question[0]
if request.Opcode != dns.OpcodeQuery {
logger(id, "UNKNOWN", question, dns.OpcodeToString[request.Opcode])
w.WriteMsg(createResponse(nil))
return
}
switch question.Name {
case "localhost.":
logger(id, "LOCAL", question)
var rr dns.RR
switch question.Qtype {
case dns.TypeA:
rr, _ = dns.NewRR(fmt.Sprintf("%s %d IN A %s\n", question.Name, 3600, "127.0.0.1"))
case dns.TypeAAAA:
rr, _ = dns.NewRR(fmt.Sprintf("%s %d IN AAAA %s\n", question.Name, 3600, "::1"))
}
final = createResponse([]dns.RR{rr})
}
if final == nil {
switch question.Qtype {
case dns.TypeA, dns.TypeAAAA:
for host, values := range hosts[dns.Type(question.Qtype).String()] {
if wildcard.Match(host, question.Name) {
logger(id, "HOSTS", question)
var rrs []dns.RR
for _, value := range values {
rr, _ := dns.NewRR(fmt.Sprintf("%s %d IN %s %s\n", question.Name, 3600, dns.Type(question.Qtype).String(), value))
rrs = append(rrs, rr)
}
final = createResponse(rrs)
break
}
}
}
}
if final == nil {
var currentUpstreams []string
if strings.Count(question.Name, ".") == 1 {
if resolvSearch != "" {
question.Name = question.Name + resolvSearch + "."
}
currentUpstreams = resolvUpstreams
} else {
currentUpstreams = upstreams
}
logger(id, "QUERY", question, "recursion", strconv.FormatBool(request.RecursionDesired), w.RemoteAddr().String())
response := harder(id, question, request.RecursionDesired, currentUpstreams)
if response != nil {
final = response
} else {
final = createResponse([]dns.RR{})
final.SetRcode(request, dns.RcodeServerFailure)
}
}
answers := strconv.Itoa(len(final.Answer))
nss := strconv.Itoa(len(final.Ns))
extras := strconv.Itoa(len(final.Extra))
logger(id, "ANSWER", question, dns.RcodeToString[final.Rcode], answers+","+nss+","+extras)
final.SetReply(request)
w.WriteMsg(final)
}
var hosts = make(map[string]map[string][]string)
var upstreams []string
var dialTimeout time.Duration
var readTimeout time.Duration
var writeTimeout time.Duration
var delay time.Duration
var concurrencyDelay time.Duration
var tries int
var retry bool
var netMode string
var edns0 int
var stats int
var events map[string]map[string]int
var resolv bool
var resolvUpstreams []string
var resolvSearch string
var devMode bool
func main() {
log.Println(os.Args)
if len(os.Args) > 1 && os.Args[1] == "test" {
name := os.Args[2]
for {
const timeout = 100 * time.Millisecond
ctx, _ := context.WithTimeout(context.TODO(), timeout)
var r net.Resolver
startedAt := time.Now()
ips, err := r.LookupIP(ctx, "ip4", name)
if err != nil {
log.Println("test error", err)
} else {
log.Println("test", name, "resolves", ips)
os.Exit(0)
}
took := time.Since(startedAt)
remaining := (time.Millisecond * 100) - took
if remaining > 0 {
time.Sleep(remaining)
}
}
}
dialTimeoutMs := flag.Int("dialTimeout", 101, "dialTimeout")
readTimeoutMs := flag.Int("readTimeout", 500, "readTimeout")
writeTimeoutMs := flag.Int("writeTimeout", 500, "writeTimeout")
delayMs := flag.Int("delay", 10, "retry delay in ms")
concurrencyDelayMs := flag.Int("concurrencyDelay", 0, "concurrency delay in ms, first upstream immediately and then add every delay (default 0)")
flag.IntVar(&tries, "tries", 3, "tries")
flag.BoolVar(&retry, "retry", false, "retry")
flag.StringVar(&netMode, "netMode", "udp", "udp, tcp, tcp-tls")
flag.IntVar(&edns0, "edns0", -1, "edns0")
flag.IntVar(&stats, "stats", -1, "print stats every N seconds")
flag.BoolVar(&resolv, "resolv", false, "resolv")
flag.StringVar(&resolvSearch, "resolvSearch", "", "resolvSearch")
flag.BoolVar(&devMode, "devMode", false, "devMode")
var hostsPath string
flag.StringVar(&hostsPath, "hosts", "", "hosts")
flag.Parse()
reloadSigs := make(chan os.Signal, 1)
signal.Notify(reloadSigs, syscall.SIGHUP)
go func() {
for {
<-reloadSigs
reloadHosts(hostsPath)
}
}()
dialTimeout = time.Millisecond * time.Duration(*dialTimeoutMs)
readTimeout = time.Millisecond * time.Duration(*readTimeoutMs)
writeTimeout = time.Millisecond * time.Duration(*writeTimeoutMs)
delay = time.Millisecond * time.Duration(*delayMs)
concurrencyDelay = time.Millisecond * time.Duration(*concurrencyDelayMs)
statsDelay := time.Second * time.Duration(stats)
upstreams = flag.Args()
if len(upstreams) == 0 {
log.Fatalln("no upstreams")
}
if resolv {
var currentResolvConf string
if devMode {
currentResolvConf = "/tmp/resolv.conf"
err := ioutil.WriteFile(currentResolvConf, []byte("# before harderdns\nnameserver 138.197.68.199\n"), 06644)
if err != nil {
log.Fatalln("failed to write ", currentResolvConf, "err", err)
}
} else {
currentResolvConf = "/etc/resolv.conf"
}
resolvContents, err := ioutil.ReadFile(currentResolvConf)
if err != nil {
log.Fatalln("failed to read", currentResolvConf, err)
}
resolvHash, err := ioutils.HashData(bytes.NewReader(resolvContents))
if err != nil {
log.Fatalln("failed to hash", resolvContents, err)
}
f := resolvconf.File{
Content: resolvContents,
Hash: resolvHash,
}
for _, resolvUpstream := range resolvconf.GetNameservers(f.Content) {
if resolvUpstream == "127.0.0.1" {
continue
}
resolvUpstreams = append(resolvUpstreams, resolvUpstream+":53")
}
err = ioutil.WriteFile(currentResolvConf, []byte("# managed by harderdns\nnameserver 127.0.0.1\n"), 06444)
if err != nil {
log.Fatalln("failed to write " + currentResolvConf)
}
}
events = make(map[string]map[string]int)
for _, upstream := range append(upstreams, resolvUpstreams...) {
events[upstream] = make(map[string]int)
}
go func() {
if statsDelay < 0 {
return
}
for {
loggerMutex.Lock()
for _, upstream := range upstreams {
log.Println("upstream", upstream, "got", events[upstream]["got"], "error", events[upstream]["error"], "trunc", events[upstream]["trunc"])
}
for _, resolvUpstream := range resolvUpstreams {
log.Println("upstream", resolvUpstream, "got", events[resolvUpstream]["got"], "error", events[resolvUpstream]["error"], "trunc", events[resolvUpstream]["trunc"])
}
loggerMutex.Unlock()
time.Sleep(statsDelay)
}
}()
reloadHosts(hostsPath)
dns.HandleFunc(".", handleDnsRequest)
port := 53
server := &dns.Server{
Addr: "0.0.0.0:" + strconv.Itoa(port),
Net: "udp",
}
defer server.Shutdown()
log.Printf("Starting at :%d using %s\n", port, netMode)
err := server.ListenAndServe()
if err != nil {
log.Fatalf("Failed to start server: %s\n ", err.Error())
}
}