-
Notifications
You must be signed in to change notification settings - Fork 248
/
upstreamresolver.go
203 lines (170 loc) · 5.16 KB
/
upstreamresolver.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
package upstream
import (
"context"
"fmt"
"net"
"net/netip"
"github.com/AdguardTeam/dnsproxy/internal/bootstrap"
proxynetutil "github.com/AdguardTeam/dnsproxy/internal/netutil"
"github.com/AdguardTeam/dnsproxy/proxyutil"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns"
)
// Resolver is an alias for bootstrap.Resolver to avoid the import cycle.
type Resolver = bootstrap.Resolver
// NewResolver creates a Resolver. resolverAddress should be either a plain IP
// address or empty. If it is empty, the default [net.Resolver] is used, and
// sorting the resolved addresses is the caller's responsibility. Otherwise, it
// creates an Upstream using opts.
//
// TODO(e.burkov): Require resolverAddress not being empty and rename into
// NewUpstreamResolver.
func NewResolver(resolverAddress string, opts *Options) (r Resolver, err error) {
if resolverAddress == "" {
return &net.Resolver{}, nil
}
upsOpts := &Options{
// Avoid recursion in case the bootstrap resolver is not valid.
Bootstrap: []string{""},
}
// TODO(ameshkov): Aren't other options needed here?
if opts != nil {
upsOpts.Timeout = opts.Timeout
upsOpts.VerifyServerCertificate = opts.VerifyServerCertificate
}
ur := upstreamResolver{}
ur.Upstream, err = AddressToUpstream(resolverAddress, upsOpts)
if err != nil {
err = fmt.Errorf("creating upstream: %w", err)
log.Error("upstream bootstrap: %s", err)
return ur, err
}
if err = validateBootstrap(ur.Upstream); err != nil {
log.Error("upstream bootstrap %s: %s", resolverAddress, err)
ur.Upstream = nil
return ur, err
}
return ur, err
}
// validateBootstrap returns error if the upstream is not eligible to be a
// bootstrap DNS server. DNSCrypt is always okay. Plain DNS, DNS-over-TLS,
// DNS-over-HTTPS, and DNS-over-QUIC are okay only if those are defined by IP.
func validateBootstrap(upstream Upstream) (err error) {
switch upstream := upstream.(type) {
case *dnsCrypt:
return nil
case *dnsOverTLS:
_, err = netip.ParseAddr(upstream.addr.Hostname())
case *dnsOverHTTPS:
_, err = netip.ParseAddr(upstream.addr.Hostname())
case *dnsOverQUIC:
_, err = netip.ParseAddr(upstream.addr.Hostname())
case *plainDNS:
_, err = netip.ParseAddr(upstream.addr.Hostname())
default:
err = fmt.Errorf("unknown upstream type: %T", upstream)
}
return errors.Annotate(err, "bootstrap %s: %w", upstream.Address())
}
// upstreamResolver is a wrapper around Upstream that implements the
// [bootstrap.Resolver] interface. It sorts the resolved addresses preferring
// IPv4.
type upstreamResolver struct {
// Upstream is embedded here to avoid implementing another Upstream's
// methods.
Upstream
}
// type check
var _ Resolver = upstreamResolver{}
// LookupNetIP implements the [Resolver] interface for upstreamResolver.
//
// TODO(e.burkov): Use context.
func (r upstreamResolver) LookupNetIP(
_ context.Context,
network string,
host string,
) (ipAddrs []netip.Addr, err error) {
// TODO(e.burkov): Investigate when [r.Upstream] is nil and why.
if r.Upstream == nil || host == "" {
return []netip.Addr{}, nil
}
host = dns.Fqdn(host)
answers := make([][]dns.RR, 1, 2)
var errs []error
switch network {
case "ip4", "ip6":
qtype := dns.TypeA
if network == "ip6" {
qtype = dns.TypeAAAA
}
var resp *dns.Msg
resp, err = r.resolve(host, qtype)
if err != nil {
return []netip.Addr{}, err
}
answers[0] = resp.Answer
case "ip":
resCh := make(chan *resolveResult, 2)
go r.resolveAsync(resCh, host, dns.TypeA)
go r.resolveAsync(resCh, host, dns.TypeAAAA)
answers = answers[:0:cap(answers)]
for i := 0; i < 2; i++ {
res := <-resCh
if res.err != nil {
errs = append(errs, res.err)
continue
}
answers = append(answers, res.resp.Answer)
}
default:
return []netip.Addr{}, fmt.Errorf("unsupported network %s", network)
}
for _, ans := range answers {
for _, rr := range ans {
if addr, ok := netip.AddrFromSlice(proxyutil.IPFromRR(rr)); ok {
ipAddrs = append(ipAddrs, addr)
}
}
}
// TODO(e.burkov): Use [errors.Join] in Go 1.20.
if len(ipAddrs) == 0 && len(errs) > 0 {
return []netip.Addr{}, errs[0]
}
// Use the previous dnsproxy behavior: prefer IPv4 by default.
//
// TODO(a.garipov): Consider unexporting this entire method or
// documenting that the order of addrs is undefined.
proxynetutil.SortNetIPAddrs(ipAddrs, false)
return ipAddrs, nil
}
// resolve performs a single DNS lookup of host.
func (r upstreamResolver) resolve(host string, qtype uint16) (resp *dns.Msg, err error) {
req := &dns.Msg{
MsgHdr: dns.MsgHdr{
Id: dns.Id(),
RecursionDesired: true,
},
Question: []dns.Question{{
Name: host,
Qtype: qtype,
Qclass: dns.ClassINET,
}},
}
return r.Exchange(req)
}
// resolveResult is the result of a single concurrent lookup.
type resolveResult = struct {
resp *dns.Msg
err error
}
// resolveAsync performs a single DNS lookup and sends the result to ch. It's
// intended to be used as a goroutine.
func (r upstreamResolver) resolveAsync(
resCh chan<- *resolveResult,
host string,
qtype uint16,
) {
resp, err := r.resolve(host, qtype)
resCh <- &resolveResult{resp: resp, err: err}
}