-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathresolver.go
More file actions
213 lines (201 loc) · 5.43 KB
/
Copy pathresolver.go
File metadata and controls
213 lines (201 loc) · 5.43 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package multidns
import (
"bytes"
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"time"
"github.com/miekg/dns"
)
// upstream is the internal interface every resolver transport implements.
type upstream interface {
Exchange(ctx context.Context, q *dns.Msg) (*dns.Msg, error)
Close() error
}
func newUpstream(cfg ResolverConfig) (upstream, error) {
if cfg.Address == "" {
return nil, errors.New("multidns: resolver address is required")
}
timeout := cfg.Timeout
if timeout <= 0 {
timeout = 5 * time.Second
}
switch cfg.Protocol {
case ProtoUDP:
return newDNSClientUpstream("udp", cfg, timeout, nil)
case ProtoTCP:
return newDNSClientUpstream("tcp", cfg, timeout, nil)
case ProtoDoT:
tlsCfg := cfg.TLSConfig
if tlsCfg == nil {
tlsCfg = &tls.Config{}
}
host, _, err := net.SplitHostPort(cfg.Address)
if err == nil && tlsCfg.ServerName == "" {
c := tlsCfg.Clone()
c.ServerName = host
tlsCfg = c
}
return newDNSClientUpstream("tcp-tls", cfg, timeout, tlsCfg)
case ProtoDoH:
return newDoHUpstream(cfg, timeout)
default:
return nil, fmt.Errorf("multidns: unsupported protocol %q", cfg.Protocol)
}
}
// dnsClientUpstream wraps a *dns.Client for UDP/TCP/DoT.
type dnsClientUpstream struct {
client *dns.Client
addr string
dialer DialFunc
}
func newDNSClientUpstream(net string, cfg ResolverConfig, timeout time.Duration, tlsCfg *tls.Config) (*dnsClientUpstream, error) {
c := &dns.Client{
Net: net,
Timeout: timeout,
ReadTimeout: timeout,
WriteTimeout: timeout,
DialTimeout: timeout,
TLSConfig: tlsCfg,
}
return &dnsClientUpstream{
client: c,
addr: cfg.Address,
dialer: cfg.Dialer,
}, nil
}
func (u *dnsClientUpstream) Exchange(ctx context.Context, q *dns.Msg) (*dns.Msg, error) {
if u.dialer == nil {
resp, _, err := u.client.ExchangeContext(ctx, q, u.addr)
return resp, err
}
dialNet := "udp"
switch u.client.Net {
case "tcp", "tcp-tls":
dialNet = "tcp"
}
rawConn, err := u.dialer(ctx, dialNet, u.addr)
if err != nil {
return nil, err
}
var c net.Conn = rawConn
if u.client.Net == "tcp-tls" {
host, _, _ := net.SplitHostPort(u.addr)
tlsCfg := u.client.TLSConfig
if tlsCfg == nil {
tlsCfg = &tls.Config{ServerName: host}
} else if tlsCfg.ServerName == "" {
cloned := tlsCfg.Clone()
cloned.ServerName = host
tlsCfg = cloned
}
tlsConn := tls.Client(rawConn, tlsCfg)
if err := tlsConn.HandshakeContext(ctx); err != nil {
rawConn.Close()
return nil, err
}
c = tlsConn
}
dconn := &dns.Conn{Conn: c}
defer dconn.Close()
// Honor the query's EDNS0 UDP buffer size when the caller advertised
// one; otherwise dns.Conn.ReadMsg caps UDP reads at MinMsgSize (512),
// which would silently truncate large responses on the custom-dialer
// path. The non-custom path goes through dns.Client which does this
// for us.
if u.client.Net == "udp" {
if opt := q.IsEdns0(); opt != nil {
if size := opt.UDPSize(); size >= dns.MinMsgSize {
dconn.UDPSize = size
}
}
}
if dl, ok := ctx.Deadline(); ok {
_ = c.SetDeadline(dl)
} else if u.client.Timeout > 0 {
_ = c.SetDeadline(time.Now().Add(u.client.Timeout))
}
if err := dconn.WriteMsg(q); err != nil {
return nil, err
}
return dconn.ReadMsg()
}
func (u *dnsClientUpstream) Close() error { return nil }
// dohUpstream is an RFC 8484 DNS-over-HTTPS client.
type dohUpstream struct {
endpoint *url.URL
client *http.Client
}
func newDoHUpstream(cfg ResolverConfig, timeout time.Duration) (*dohUpstream, error) {
u, err := url.Parse(cfg.Address)
if err != nil {
return nil, fmt.Errorf("multidns: parse DoH endpoint: %w", err)
}
if u.Scheme != "https" && u.Scheme != "http" {
return nil, fmt.Errorf("multidns: DoH endpoint must be http(s), got %q", u.Scheme)
}
tr := &http.Transport{
ForceAttemptHTTP2: true,
IdleConnTimeout: 30 * time.Second,
TLSHandshakeTimeout: timeout,
TLSClientConfig: cfg.TLSConfig,
}
if cfg.Dialer != nil {
tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return cfg.Dialer(ctx, network, addr)
}
}
return &dohUpstream{
endpoint: u,
client: &http.Client{Transport: tr, Timeout: timeout},
}, nil
}
func (u *dohUpstream) Exchange(ctx context.Context, q *dns.Msg) (*dns.Msg, error) {
wire, err := q.Pack()
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.endpoint.String(), bytes.NewReader(wire))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/dns-message")
req.Header.Set("Accept", "application/dns-message")
resp, err := u.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusTooManyRequests {
io.Copy(io.Discard, resp.Body)
return nil, fmt.Errorf("doh: rate limited (status %d)", resp.StatusCode)
}
if resp.StatusCode >= 500 {
io.Copy(io.Discard, resp.Body)
return nil, fmt.Errorf("doh: server error (status %d)", resp.StatusCode)
}
if resp.StatusCode != http.StatusOK {
io.Copy(io.Discard, resp.Body)
return nil, fmt.Errorf("doh: unexpected status %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
out := new(dns.Msg)
if err := out.Unpack(body); err != nil {
return nil, fmt.Errorf("doh: unpack response: %w", err)
}
return out, nil
}
func (u *dohUpstream) Close() error {
if t, ok := u.client.Transport.(*http.Transport); ok {
t.CloseIdleConnections()
}
return nil
}