-
Notifications
You must be signed in to change notification settings - Fork 7
/
resolver.go
82 lines (73 loc) · 1.98 KB
/
resolver.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
package dane
import (
"net"
"time"
"github.com/miekg/dns"
)
//
// DNS resolver defaults
//
var (
defaultDNSTimeout = 2
defaultDNSRetries = 3
defaultTCPTimeout = 3
defaultResolverPort = 53
defaultResolvConf = "/etc/resolv.conf"
defaultBufsize uint16 = 1460
)
//
// Resolver contains a DNS resolver configuration
//
type Resolver struct {
Servers []*Server // list of resolvers
Rdflag bool // set RD flag
Adflag bool // set AD flag
Cdflag bool // set CD flag
Timeout time.Duration // query timeout
Retries int // query retries
Payload uint16 // EDNS0 UDP payload size
IPv6 bool // lookup AAAA records in getAddresses()
IPv4 bool // look A records in getAddresses()
Pkixfallback bool // whether to fallback to PKIX in getTLSA()
}
//
// NewResolver initializes a new Resolver structure from a given IP
// address (net.IP) and port number.
//
func NewResolver(servers []*Server) *Resolver {
r := new(Resolver)
r.Servers = servers
r.Rdflag = true
r.Adflag = true
r.Timeout = time.Second * time.Duration(defaultDNSTimeout)
r.Retries = defaultDNSRetries
r.Payload = defaultBufsize
r.IPv6 = true
r.IPv4 = true
r.Pkixfallback = true
return r
}
//
// GetResolver returns a Resolver configuration structure containing
// a list of DNS resolver addresses obtained from a custom resolver
// configuration file or from the system default (/etc/resolv.conf)
// if the config file is unspecified.
//
func GetResolver(resconf string) (*Resolver, error) {
var ip net.IP
var resolver *Resolver
var servers []*Server
if resconf == "" {
resconf = defaultResolvConf
}
c, err := dns.ClientConfigFromFile(resconf)
if err != nil {
return nil, err
}
for _, s := range c.Servers {
ip = net.ParseIP(s)
servers = append(servers, NewServer("", ip, defaultResolverPort))
}
resolver = NewResolver(servers)
return resolver, err
}