forked from ncruces/go-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.go
75 lines (68 loc) · 1.73 KB
/
dns.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
// Package dns provides net.Resolver instances implementing caching,
// opportunistic encryption, and DNS over TLS/HTTPS.
//
// To replace the net.DefaultResolver with a caching DNS over HTTPS instance
// using the Google Public DNS resolver:
//
// net.DefaultResolver = dns.NewDoHResolver(
// "https://dns.google/dns-query",
// dns.DoHCache())
package dns
import (
"context"
"crypto/tls"
"net"
"sync"
"time"
)
// OpportunisticResolver opportunistically tries encrypted DNS over TLS
// using the local resolver.
var OpportunisticResolver = &net.Resolver{
Dial: opportunisticDial,
PreferGo: true,
}
func opportunisticDial(ctx context.Context, network, address string) (net.Conn, error) {
host, port, _ := net.SplitHostPort(address)
if (port == "53" || port == "domain") && notBadServer(address) {
deadline, ok := ctx.Deadline()
if ok && deadline.After(time.Now().Add(2*time.Second)) {
var d net.Dialer
d.Timeout = time.Second
tlsAddr := net.JoinHostPort(host, "853")
tlsConf := tls.Config{InsecureSkipVerify: true}
conn, _ := tls.DialWithDialer(&d, "tcp", tlsAddr, &tlsConf)
if conn != nil {
return conn, nil
}
addBadServer(address)
}
}
var d net.Dialer
return d.DialContext(ctx, network, address)
}
var badServers struct {
sync.Mutex
next int
list [4]string
}
func notBadServer(address string) bool {
badServers.Lock()
defer badServers.Unlock()
for _, a := range badServers.list {
if a == address {
return false
}
}
return true
}
func addBadServer(address string) {
badServers.Lock()
defer badServers.Unlock()
for _, a := range badServers.list {
if a == address {
return
}
}
badServers.list[badServers.next] = address
badServers.next = (badServers.next + 1) % len(badServers.list)
}