-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
bootstrap.go
314 lines (236 loc) · 7.23 KB
/
bootstrap.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
package resolver
import (
"context"
"fmt"
"math/rand"
"net"
"net/http"
"strings"
"sync/atomic"
"time"
"github.com/0xERR0R/blocky/config"
"github.com/0xERR0R/blocky/log"
"github.com/0xERR0R/blocky/model"
"github.com/0xERR0R/blocky/util"
"github.com/hashicorp/go-multierror"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
)
//nolint:gochecknoglobals
var (
v4v6QTypes = []dns.Type{dns.Type(dns.TypeA), dns.Type(dns.TypeAAAA)}
)
// Bootstrap allows resolving hostnames using the configured bootstrap DNS.
type Bootstrap struct {
log *logrus.Entry
resolver Resolver
bootstraped bootstrapedResolvers
systemResolver *net.Resolver
}
// NewBootstrap creates and returns a new Bootstrap.
// Internally, it uses a CachingResolver and an UpstreamResolver.
func NewBootstrap(cfg *config.Config) (b *Bootstrap, err error) {
log := log.PrefixedLog("bootstrap")
// Create b in multiple steps: Bootstrap and UpstreamResolver have a cyclic dependency
// This also prevents the GC to clean up these two structs, but is not currently an
// issue since they stay allocated until the process terminates
b = &Bootstrap{
log: log,
systemResolver: net.DefaultResolver, // allow replacing it during tests
}
bootstraped, err := newBootstrapedResolvers(b, cfg.BootstrapDNS)
if err != nil {
return nil, err
}
if len(bootstraped) == 0 {
log.Infof("bootstrapDns is not configured, will use system resolver")
return b, nil
}
parallelResolver, err := newParallelBestResolver(bootstraped.ResolverGroups())
if err != nil {
return nil, fmt.Errorf("could not create bootstrap ParallelBestResolver: %w", err)
}
b.bootstraped = bootstraped
b.resolver = Chain(
NewFilteringResolver(cfg.Filtering),
NewCachingResolver(cfg.Caching, nil),
parallelResolver,
)
return b, nil
}
func (b *Bootstrap) UpstreamIPs(r *UpstreamResolver) (*IPSet, error) {
hostname := r.upstream.Host
if ip := net.ParseIP(hostname); ip != nil { // nil-safe when hostname is an IP: makes writing test easier
return newIPSet([]net.IP{ip}), nil
}
ips, err := b.resolveUpstream(r, hostname)
if err != nil {
return nil, err
}
return newIPSet(ips), nil
}
func (b *Bootstrap) resolveUpstream(r Resolver, host string) ([]net.IP, error) {
// Use system resolver if no bootstrap is configured
if b.resolver == nil {
cfg := config.GetConfig()
ctx := context.Background()
timeout := cfg.UpstreamTimeout
if timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Duration(timeout))
defer cancel()
}
return b.systemResolver.LookupIP(ctx, cfg.ConnectIPVersion.Net(), host)
}
if ips, ok := b.bootstraped[r]; ok {
// Special path for bootstraped upstreams to avoid infinite recursion
return ips, nil
}
return b.resolve(host, v4v6QTypes)
}
// NewHTTPTransport returns a new http.Transport that uses b to resolve hostnames
func (b *Bootstrap) NewHTTPTransport() *http.Transport {
if b.resolver == nil {
return &http.Transport{}
}
dialer := net.Dialer{}
return &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
log := b.log.WithField("network", network).WithField("addr", addr)
host, port, err := net.SplitHostPort(addr)
if err != nil {
log.Errorf("dial error: %s", err)
return nil, err
}
connectIPVersion := config.GetConfig().ConnectIPVersion
var qTypes []dns.Type
switch {
case connectIPVersion != config.IPVersionDual: // ignore `network` if a specific version is configured
qTypes = connectIPVersion.QTypes()
case strings.HasSuffix(network, "4"):
qTypes = []dns.Type{dns.Type(dns.TypeA)}
case strings.HasSuffix(network, "6"):
qTypes = []dns.Type{dns.Type(dns.TypeAAAA)}
default:
qTypes = v4v6QTypes
}
// Resolve the host with the bootstrap DNS
ips, err := b.resolve(host, qTypes)
if err != nil {
log.Errorf("resolve error: %s", err)
return nil, err
}
ip := ips[rand.Intn(len(ips))] //nolint:gosec
log.WithField("ip", ip).Tracef("dialing %s", host)
// Use the standard dialer to actually connect
addrWithIP := net.JoinHostPort(ip.String(), port)
return dialer.DialContext(ctx, network, addrWithIP)
},
}
}
func (b *Bootstrap) resolve(hostname string, qTypes []dns.Type) (ips []net.IP, err error) {
ips = make([]net.IP, 0, len(qTypes))
for _, qType := range qTypes {
qIPs, qErr := b.resolveType(hostname, qType)
if qErr != nil {
err = multierror.Append(err, qErr)
continue
}
ips = append(ips, qIPs...)
}
if err == nil && len(ips) == 0 {
return nil, fmt.Errorf("no such host %s", hostname)
}
return
}
func (b *Bootstrap) resolveType(hostname string, qType dns.Type) (ips []net.IP, err error) {
if ip := net.ParseIP(hostname); ip != nil {
return []net.IP{ip}, nil
}
req := model.Request{
Req: util.NewMsgWithQuestion(hostname, qType),
Log: b.log,
}
rsp, err := b.resolver.Resolve(&req)
if err != nil {
return nil, err
}
if rsp.Res.Rcode != dns.RcodeSuccess {
return nil, nil
}
ips = make([]net.IP, 0, len(rsp.Res.Answer))
for _, a := range rsp.Res.Answer {
switch rr := a.(type) {
case *dns.A:
ips = append(ips, rr.A)
case *dns.AAAA:
ips = append(ips, rr.AAAA)
}
}
return ips, nil
}
// map of bootstraped resolvers their hardcoded IPs
type bootstrapedResolvers map[Resolver][]net.IP
func newBootstrapedResolvers(b *Bootstrap, cfg config.BootstrapDNSConfig) (bootstrapedResolvers, error) {
upstreamIPs := make(bootstrapedResolvers, len(cfg))
var multiErr *multierror.Error
for i, upstreamCfg := range cfg {
i := i + 1 // user visible index should start at 1
upstream := upstreamCfg.Upstream
if upstream.IsDefault() {
multiErr = multierror.Append(
multiErr,
fmt.Errorf("item %d: upstream not configured (ips=%v)", i, upstreamCfg.IPs),
)
continue
}
var ips []net.IP
if ip := net.ParseIP(upstream.Host); ip != nil {
ips = append(ips, ip)
} else if upstream.Net == config.NetProtocolTcpUdp {
multiErr = multierror.Append(
multiErr,
fmt.Errorf("item %d: '%s': protocol %s must use IP instead of hostname", i, upstream, upstream.Net),
)
continue
}
ips = append(ips, upstreamCfg.IPs...)
if len(ips) == 0 {
multiErr = multierror.Append(multiErr, fmt.Errorf("item %d: '%s': no IPs configured", i, upstream))
continue
}
resolver := newUpstreamResolverUnchecked(upstream, b)
upstreamIPs[resolver] = ips
}
if multiErr != nil {
return nil, fmt.Errorf("invalid bootstrapDns configuration: %w", multiErr)
}
return upstreamIPs, nil
}
func (br bootstrapedResolvers) ResolverGroups() map[string][]Resolver {
resolvers := make([]Resolver, 0, len(br))
for resolver := range br {
resolvers = append(resolvers, resolver)
}
return map[string][]Resolver{
upstreamDefaultCfgName: resolvers,
}
}
type IPSet struct {
values []net.IP
index uint32
}
func newIPSet(ips []net.IP) *IPSet {
return &IPSet{values: ips}
}
func (ips *IPSet) Current() net.IP {
idx := atomic.LoadUint32(&ips.index)
return ips.values[idx]
}
func (ips *IPSet) Next() {
oldIP := ips.index
newIP := uint32(int(ips.index+1) % len(ips.values))
// We don't care about the result: if the call fails,
// it means the value was incremented by another goroutine
_ = atomic.CompareAndSwapUint32(&ips.index, oldIP, newIP)
}