-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
resolver.go
204 lines (177 loc) · 4.83 KB
/
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
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
package main
import (
"crypto/tls"
"fmt"
"net"
"strings"
"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
"github.com/natesales/q/cli"
"github.com/natesales/q/transport"
)
// createQuery creates a slice of DNS queries
func createQuery(opts cli.Flags, rrTypes []uint16) []dns.Msg {
var queries []dns.Msg
// Query for each requested RR type
for _, qType := range rrTypes {
req := dns.Msg{}
if opts.ID != -1 {
req.Id = uint16(opts.ID)
} else {
req.Id = dns.Id()
}
req.Authoritative = opts.AuthoritativeAnswer
req.AuthenticatedData = opts.AuthenticData
req.CheckingDisabled = opts.CheckingDisabled
req.RecursionDesired = opts.RecursionDesired
req.RecursionAvailable = opts.RecursionAvailable
req.Zero = opts.Zero
req.Truncated = opts.Truncated
if opts.DNSSEC || opts.NSID || opts.Pad || opts.ClientSubnet != "" || opts.Cookie != "" {
opt := &dns.OPT{
Hdr: dns.RR_Header{
Name: ".",
Class: opts.UDPBuffer,
Rrtype: dns.TypeOPT,
},
}
if opts.DNSSEC {
opt.SetDo()
}
if opts.NSID {
opt.Option = append(opt.Option, &dns.EDNS0_NSID{
Code: dns.EDNS0NSID,
})
}
if opts.Pad {
paddingOpt := new(dns.EDNS0_PADDING)
msgLen := req.Len()
padLen := 128 - msgLen%128
// Truncate padding to fit in UDP buffer
if msgLen+padLen > int(opt.UDPSize()) {
padLen = int(opt.UDPSize()) - msgLen
if padLen < 0 { // Stop padding
padLen = 0
}
}
log.Debugf("Padding with %d bytes", padLen)
paddingOpt.Padding = make([]byte, padLen)
opt.Option = append(opt.Option, paddingOpt)
}
if opts.ClientSubnet != "" {
ip, ipNet, err := net.ParseCIDR(opts.ClientSubnet)
if err != nil {
log.Fatalf("parsing subnet %s", opts.ClientSubnet)
}
mask, _ := ipNet.Mask.Size()
log.Debugf("EDNS0 client subnet %s/%d", ip, mask)
ednsSubnet := &dns.EDNS0_SUBNET{
Code: dns.EDNS0SUBNET,
Address: ip,
Family: 1, // IPv4
SourceNetmask: uint8(mask),
}
if ednsSubnet.Address.To4() == nil {
ednsSubnet.Family = 2 // IPv6
}
opt.Option = append(opt.Option, ednsSubnet)
}
if opts.Cookie != "" {
cookie := &dns.EDNS0_COOKIE{
Code: dns.EDNS0COOKIE,
Cookie: opts.Cookie,
}
opt.Option = append(opt.Option, cookie)
}
req.Extra = append(req.Extra, opt)
}
req.Question = []dns.Question{{
Name: dns.Fqdn(opts.Name),
Qtype: qType,
Qclass: opts.Class,
}}
queries = append(queries, req)
}
return queries
}
// newTransport creates a new transport based on local options
func newTransport(server string, transportType transport.Type, tlsConfig *tls.Config) (*transport.Transport, error) {
var ts transport.Transport
common := transport.Common{
Server: server,
ReuseConn: opts.ReuseConn,
}
switch transportType {
case transport.TypeHTTP:
if opts.ODoHProxy != "" {
log.Debugf("Using ODoH transport with target %s proxy %s", server, opts.ODoHProxy)
ts = &transport.ODoH{
Proxy: opts.ODoHProxy,
TLSConfig: tlsConfig,
}
} else {
log.Debugf("Using HTTP(s) transport: %s", server)
ts = &transport.HTTP{
Common: common,
TLSConfig: tlsConfig,
UserAgent: opts.HTTPUserAgent,
Method: opts.HTTPMethod,
HTTP2: opts.HTTP2,
HTTP3: opts.HTTP3,
NoPMTUd: !opts.PMTUD,
}
}
case transport.TypeDNSCrypt:
log.Debugf("Using DNSCrypt transport: %s", server)
if strings.HasPrefix(server, "sdns://") {
log.Traceln("Using provided DNS stamp for DNSCrypt")
ts = &transport.DNSCrypt{
Common: common,
ServerStamp: server,
TCP: opts.DNSCryptTCP,
UDPSize: opts.DNSCryptUDPSize,
}
} else {
log.Traceln("Using manual DNSCrypt configuration")
ts = &transport.DNSCrypt{Common: common,
TCP: opts.DNSCryptTCP,
UDPSize: opts.DNSCryptUDPSize,
PublicKey: opts.DNSCryptPublicKey,
ProviderName: opts.DNSCryptProvider,
}
}
case transport.TypeQUIC:
log.Debugf("Using QUIC transport: %s", server)
tc := tlsConfig.Clone()
tlsConfig.NextProtos = opts.QUICALPNTokens
ts = &transport.QUIC{
Common: common,
TLSConfig: tc,
PMTUD: opts.PMTUD,
AddLengthPrefix: opts.QUICLengthPrefix,
}
case transport.TypeTLS:
log.Debugf("Using TLS transport: %s", server)
ts = &transport.TLS{
Common: common,
TLSConfig: tlsConfig,
}
case transport.TypeTCP:
log.Debugf("Using TCP transport: %s", server)
ts = &transport.Plain{
Common: common,
PreferTCP: true,
UDPBuffer: opts.UDPBuffer,
}
case transport.TypePlain:
log.Debugf("Using UDP with TCP fallback: %s", server)
ts = &transport.Plain{
Common: common,
PreferTCP: false,
UDPBuffer: opts.UDPBuffer,
}
default:
return nil, fmt.Errorf("unknown transport protocol %s", transportType)
}
return &ts, nil
}