-
Notifications
You must be signed in to change notification settings - Fork 28
/
dns_resolver.go
151 lines (127 loc) · 3.95 KB
/
dns_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
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
// Package dns_resolver is a simple dns resolver
// based on miekg/dns
package dns_resolver
import (
"errors"
"math/rand"
"net"
"os"
"strings"
"time"
"github.com/miekg/dns"
)
// DnsResolver represents a dns resolver
type DnsResolver struct {
Servers []string
RetryTimes int
r *rand.Rand
}
// New initializes DnsResolver.
func New(servers []string) *DnsResolver {
for i := range servers {
servers[i] = net.JoinHostPort(servers[i], "53")
}
return &DnsResolver{servers, len(servers) * 2, rand.New(rand.NewSource(time.Now().UnixNano()))}
}
// NewFromResolvConf initializes DnsResolver from resolv.conf like file.
func NewFromResolvConf(path string) (*DnsResolver, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return &DnsResolver{}, errors.New("no such file or directory: " + path)
}
config, err := dns.ClientConfigFromFile(path)
servers := []string{}
for _, ipAddress := range config.Servers {
servers = append(servers, net.JoinHostPort(ipAddress, "53"))
}
return &DnsResolver{servers, len(servers) * 2, rand.New(rand.NewSource(time.Now().UnixNano()))}, err
}
// LookupHost returns IP addresses of provied host.
// In case of timeout retries query RetryTimes times.
func (r *DnsResolver) LookupHost(host string) ([]net.IP, error) {
return r.lookupHost(host, r.RetryTimes)
}
func (r *DnsResolver) lookupHost(host string, triesLeft int) ([]net.IP, error) {
m1 := new(dns.Msg)
m1.Id = dns.Id()
m1.RecursionDesired = true
m1.Question = make([]dns.Question, 1)
m1.Question[0] = dns.Question{dns.Fqdn(host), dns.TypeA, dns.ClassINET}
in, err := dns.Exchange(m1, r.Servers[r.r.Intn(len(r.Servers))])
result := []net.IP{}
if err != nil {
if strings.HasSuffix(err.Error(), "i/o timeout") && triesLeft > 0 {
triesLeft--
return r.lookupHost(host, triesLeft)
}
return result, err
}
if in != nil && in.Rcode != dns.RcodeSuccess {
return result, errors.New(dns.RcodeToString[in.Rcode])
}
for _, record := range in.Answer {
if t, ok := record.(*dns.A); ok {
result = append(result, t.A)
}
}
return result, err
}
// LookupTXT returns TXT records of provied host.
// In case of timeout retries query RetryTimes times.
func (r *DnsResolver) LookupTXT(host string) ([]string, error) {
return r.lookupTXT(host, r.RetryTimes)
}
func (r *DnsResolver) lookupTXT(host string, triesLeft int) ([]string, error) {
m1 := new(dns.Msg)
m1.Id = dns.Id()
m1.RecursionDesired = true
m1.Question = make([]dns.Question, 1)
m1.Question[0] = dns.Question{dns.Fqdn(host), dns.TypeTXT, dns.ClassINET}
in, err := dns.Exchange(m1, r.Servers[r.r.Intn(len(r.Servers))])
result := []string{}
if err != nil {
if strings.HasSuffix(err.Error(), "i/o timeout") && triesLeft > 0 {
triesLeft--
return r.lookupTXT(host, triesLeft)
}
return result, err
}
if in != nil && in.Rcode != dns.RcodeSuccess {
return result, errors.New(dns.RcodeToString[in.Rcode])
}
for _, record := range in.Answer {
if t, ok := record.(*dns.TXT); ok {
result = append(result, t.Txt...)
}
}
return result, err
}
// LookupMX returns MX records of provied host.
// In case of timeout retries query RetryTimes times.
func (r *DnsResolver) LookupMX(host string) ([]string, error) {
return r.lookupMX(host, r.RetryTimes)
}
func (r *DnsResolver) lookupMX(host string, triesLeft int) ([]string, error) {
m1 := new(dns.Msg)
m1.Id = dns.Id()
m1.RecursionDesired = true
m1.Question = make([]dns.Question, 1)
m1.Question[0] = dns.Question{dns.Fqdn(host), dns.TypeMX, dns.ClassINET}
in, err := dns.Exchange(m1, r.Servers[r.r.Intn(len(r.Servers))])
result := []string{}
if err != nil {
if strings.HasSuffix(err.Error(), "i/o timeout") && triesLeft > 0 {
triesLeft--
return r.lookupTXT(host, triesLeft)
}
return result, err
}
if in != nil && in.Rcode != dns.RcodeSuccess {
return result, errors.New(dns.RcodeToString[in.Rcode])
}
for _, record := range in.Answer {
if t, ok := record.(*dns.MX); ok {
result = append(result, t.Mx)
}
}
return result, err
}