-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.go
More file actions
193 lines (164 loc) · 4.03 KB
/
resolver.go
File metadata and controls
193 lines (164 loc) · 4.03 KB
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
package resolver
import (
"context"
"errors"
"sync"
"github.com/miekg/dns"
)
// Result represents a domain name and its destinations.
type Result struct {
// Name is the domain name.
Name string
// Destination is the list of addresses where it resolves to.
Destination []string
}
// ErrNoResponse is returned when all resolution retries were done and no
// response has been received yet.
var ErrNoResponse = errors.New("no response")
// Resolver represents a DNS resolver.
type Resolver struct {
// Record is the DNS record that is queried.
Record string
// Retries is the number of retries done.
Retries int
// Workers is the number of concurrent goroutines used during resolution.
Workers int
}
// New creates a DNS resolver.
func New(record string, retries, workers int) (*Resolver, error) {
if retries < 0 {
return nil, errors.New("number of retries must be higher or equals to 0")
}
if workers <= 0 {
return nil, errors.New("at least one worker is needed")
}
return &Resolver{
Record: record,
Retries: retries,
Workers: workers,
}, nil
}
// ResolveList resolves a slice of hosts and returns the destintations
// they resolve to over the out channel.
func (r *Resolver) ResolveList(ctx context.Context, domains, servers []string, out chan<- Result) error {
defer close(out)
if len(domains) == 0 {
return errors.New("at least one domain is needed")
}
if len(servers) == 0 {
return errors.New("at least one DNS server is needed")
}
if r.Workers > len(domains) {
r.Workers = len(domains)
}
errs := make(chan error)
done := make(chan struct{})
var wg sync.WaitGroup
wg.Add(r.Workers)
chans := make([]chan string, r.Workers)
for i := 0; i < len(chans); i++ {
chans[i] = make(chan string)
}
for _, ch := range chans {
go func(c chan string) {
defer wg.Done()
select {
case <-done:
return
default: // avoid blocking
}
for v := range c {
// a server from servers slice should be picked in a way that load is
// balanced between them all.
var count int
srv := []string{servers[count%len(servers)]}
for i := 0; i < r.Retries; i++ {
srv = append(srv, servers[(count+i)%len(servers)])
count++
}
dst, err := Resolve(ctx, r.Record, v, r.Retries, srv)
if err != nil {
if err != ErrNoResponse {
errs <- err
}
continue
}
if len(dst) > 0 {
out <- Result{Name: v, Destination: dst}
}
}
}(ch)
}
for k, v := range domains {
select {
case err := <-errs:
done <- struct{}{}
return err
default: // avoid blocking
}
chans[k%r.Workers] <- v
}
for _, c := range chans {
close(c)
}
wg.Wait()
return nil
}
// Resolve tries to resolve a host using the given DNS servers.
// If all servers fail to resolve it, Resolve returns an error
func Resolve(ctx context.Context, record, host string, retries int, srv []string) ([]string, error) {
if len(srv) == 0 {
return nil, errors.New("at least one DNS server is needed")
}
msg := new(dns.Msg)
msg.Id = dns.Id()
msg.RecursionDesired = false
switch record {
case "A":
msg.SetQuestion(dns.Fqdn(host), dns.TypeA)
case "CNAME":
msg.SetQuestion(dns.Fqdn(host), dns.TypeCNAME)
case "PTR":
h, err := reverse(host)
if err != nil {
return nil, err
}
msg.SetQuestion(dns.Fqdn(h), dns.TypePTR)
case "NS":
msg.SetQuestion(dns.Fqdn(host), dns.TypeNS)
default:
return nil, errors.New("invalid record")
}
var in *dns.Msg
var err error
in, err = dns.ExchangeContext(ctx, msg, srv[0])
for err != nil && retries > 0 {
in, err = dns.ExchangeContext(ctx, msg, srv[retries%len(srv)])
if in != nil {
break
}
retries--
}
// if no response was received, return an error.
if in == nil {
return nil, ErrNoResponse
}
var resolution []string
for _, rr := range in.Answer {
var value string
switch v := rr.(type) {
case *dns.A:
value = v.A.String()
case *dns.CNAME:
value = v.Target
case *dns.NS:
value = v.Ns
case *dns.PTR:
value = v.Ptr
default:
continue
}
resolution = append(resolution, value)
}
return resolution, nil
}