-
Notifications
You must be signed in to change notification settings - Fork 73
/
main.go
402 lines (327 loc) · 8.73 KB
/
main.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Package main is the command-line tool that does DNS lookups using
// dnsproxy/upstream. See README.md for more information.
package main
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"net"
"net/netip"
"os"
"strconv"
"strings"
"time"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil/sysresolv"
"github.com/ameshkov/dnsstamps"
"github.com/miekg/dns"
)
type jsonMsg struct {
dns.Msg
Elapsed time.Duration `json:"elapsed"`
}
// VersionString -- see the makefile
var VersionString = "master"
// nolint: gocyclo
func main() {
// parse env variables
machineReadable := os.Getenv("JSON") == "1"
insecureSkipVerify := os.Getenv("VERIFY") == "0"
timeoutStr := os.Getenv("TIMEOUT")
http3Enabled := os.Getenv("HTTP3") == "1"
verbose := os.Getenv("VERBOSE") == "1"
padding := os.Getenv("PAD") == "1"
do := os.Getenv("DNSSEC") == "1"
subnetOpt := getSubnet()
ednsOpt := getEDNSOpt()
if verbose {
log.SetLevel(log.DEBUG)
}
timeout := 10
if !machineReadable {
_, _ = os.Stdout.WriteString(fmt.Sprintf("dnslookup %s\n", VersionString))
if len(os.Args) == 2 && (os.Args[1] == "-v" || os.Args[1] == "--version") {
os.Exit(0)
}
}
if insecureSkipVerify {
_, _ = os.Stdout.WriteString("TLS verification has been disabled\n")
}
if len(os.Args) == 2 && (os.Args[1] == "-h" || os.Args[1] == "--help") {
usage()
os.Exit(0)
}
if len(os.Args) < 2 || len(os.Args) > 5 {
log.Printf("Wrong number of arguments")
usage()
os.Exit(1)
}
question := getQuestion()
if timeoutStr != "" {
i, err := strconv.Atoi(timeoutStr)
if err != nil {
log.Printf("Wrong timeout value: %s", timeoutStr)
usage()
os.Exit(1)
}
timeout = i
}
var server string
if len(os.Args) > 2 {
server = os.Args[2]
} else {
sysr, err := sysresolv.NewSystemResolvers(nil, 53)
if err != nil {
log.Printf("Cannot get system resolvers: %v", err)
os.Exit(1)
}
server = sysr.Addrs()[0].String()
}
var httpVersions []upstream.HTTPVersion
if http3Enabled {
httpVersions = []upstream.HTTPVersion{
upstream.HTTPVersion3,
upstream.HTTPVersion2,
upstream.HTTPVersion11,
}
}
opts := &upstream.Options{
Timeout: time.Duration(timeout) * time.Second,
InsecureSkipVerify: insecureSkipVerify,
HTTPVersions: httpVersions,
}
if len(os.Args) == 4 {
ip := net.ParseIP(os.Args[3])
if ip == nil {
log.Fatalf("invalid IP specified: %s", os.Args[3])
}
opts.Bootstrap = &singleIPResolver{ip: ip}
}
if len(os.Args) == 5 {
// DNSCrypt parameters
providerName := os.Args[3]
serverPkStr := os.Args[4]
serverPk, err := hex.DecodeString(strings.ReplaceAll(serverPkStr, ":", ""))
if err != nil {
log.Fatalf("Invalid server PK %s: %s", serverPkStr, err)
}
var stamp dnsstamps.ServerStamp
stamp.Proto = dnsstamps.StampProtoTypeDNSCrypt
stamp.ServerAddrStr = server
stamp.ProviderName = providerName
stamp.ServerPk = serverPk
server = stamp.String()
}
u, err := upstream.AddressToUpstream(server, opts)
if err != nil {
log.Fatalf("Cannot create an upstream: %s", err)
}
req := &dns.Msg{}
req.Id = dns.Id()
req.RecursionDesired = true
req.Question = []dns.Question{question}
if subnetOpt != nil {
opt := getOrCreateOpt(req, do)
opt.Option = append(opt.Option, subnetOpt)
}
if ednsOpt != nil {
opt := getOrCreateOpt(req, do)
opt.Option = append(opt.Option, ednsOpt)
}
if padding {
opt := getOrCreateOpt(req, do)
opt.Option = append(opt.Option, newEDNS0Padding(req))
}
startTime := time.Now()
reply, err := u.Exchange(req)
if err != nil {
log.Fatalf("Cannot make the DNS request: %s", err)
}
if !machineReadable {
msg := fmt.Sprintf("dnslookup result (elapsed %v):\n", time.Now().Sub(startTime))
_, _ = os.Stdout.WriteString(fmt.Sprintf("Server: %s\n\n", server))
_, _ = os.Stdout.WriteString(msg)
_, _ = os.Stdout.WriteString(reply.String() + "\n")
} else {
// Prevent JSON parsing from skewing results
endTime := time.Now()
var JSONreply jsonMsg
JSONreply.Msg = *reply
JSONreply.Elapsed = endTime.Sub(startTime)
var b []byte
b, err = json.MarshalIndent(JSONreply, "", " ")
if err != nil {
log.Fatalf("Cannot marshal json: %s", err)
}
_, _ = os.Stdout.WriteString(string(b) + "\n")
}
}
func getOrCreateOpt(req *dns.Msg, do bool) (opt *dns.OPT) {
opt = req.IsEdns0()
if opt == nil {
req.SetEdns0(udpBufferSize, do)
opt = req.IsEdns0()
}
return opt
}
func getEDNSOpt() (option *dns.EDNS0_LOCAL) {
ednsOpt := os.Getenv("EDNSOPT")
if ednsOpt == "" {
return nil
}
parts := strings.Split(ednsOpt, ":")
code, err := strconv.Atoi(parts[0])
if err != nil {
log.Printf("invalid EDNSOPT %s: %v", ednsOpt, err)
usage()
os.Exit(1)
}
var value []byte
if len(parts) > 1 {
value, err = hex.DecodeString(parts[1])
if err != nil {
log.Printf("invalid EDNSOPT %s: %v", ednsOpt, err)
usage()
os.Exit(1)
}
}
return &dns.EDNS0_LOCAL{
Code: uint16(code),
Data: value,
}
}
// getQuestion returns a DNS question for the query.
func getQuestion() (q dns.Question) {
domain := os.Args[1]
rrType := getRRType()
qClass := getClass()
// If the user tries to query an IP address and does not specify any
// query type, convert to PTR automatically.
ip := net.ParseIP(domain)
if os.Getenv("RRTYPE") == "" && ip != nil {
domain = ipToPtr(ip)
rrType = dns.TypePTR
}
q.Name = dns.Fqdn(domain)
q.Qtype = rrType
q.Qclass = qClass
return q
}
func ipToPtr(ip net.IP) (ptr string) {
if ip.To4() != nil {
return ip4ToPtr(ip)
}
return ip6ToPtr(ip)
}
func ip4ToPtr(ip net.IP) (ptr string) {
parts := strings.Split(ip.String(), ".")
for i := range parts {
ptr = parts[i] + "." + ptr
}
ptr = ptr + "in-addr.arpa."
return
}
func ip6ToPtr(ip net.IP) (ptr string) {
addr, _ := netip.ParseAddr(ip.String())
str := addr.StringExpanded()
// Remove colons and reverse the order of characters.
str = strings.ReplaceAll(str, ":", "")
reversed := ""
for i := len(str) - 1; i >= 0; i-- {
reversed += string(str[i])
if i != 0 {
reversed += "."
}
}
ptr = reversed + ".ip6.arpa."
return ptr
}
func getSubnet() (option *dns.EDNS0_SUBNET) {
subnetStr := os.Getenv("SUBNET")
if subnetStr == "" {
return nil
}
_, ipNet, err := net.ParseCIDR(subnetStr)
if err != nil {
log.Printf("invalid SUBNET %s: %v", subnetStr, err)
usage()
os.Exit(1)
}
ones, _ := ipNet.Mask.Size()
return &dns.EDNS0_SUBNET{
Code: dns.EDNS0SUBNET,
Family: 1,
SourceNetmask: uint8(ones),
SourceScope: 0,
Address: ipNet.IP,
}
}
func getClass() (class uint16) {
classStr := os.Getenv("CLASS")
var ok bool
class, ok = dns.StringToClass[classStr]
if !ok {
if classStr != "" {
log.Printf("Invalid CLASS: %q", classStr)
usage()
os.Exit(1)
}
class = dns.ClassINET
}
return class
}
func getRRType() (rrType uint16) {
rrTypeStr := os.Getenv("RRTYPE")
var ok bool
rrType, ok = dns.StringToType[rrTypeStr]
if !ok {
if rrTypeStr != "" {
log.Printf("Invalid RRTYPE: %q", rrTypeStr)
usage()
os.Exit(1)
}
rrType = dns.TypeA
}
return rrType
}
func usage() {
_, _ = os.Stdout.WriteString("Usage: dnslookup <domain> <server> [<providerName> <serverPk>]\n")
_, _ = os.Stdout.WriteString("<domain>: mandatory, domain name to lookup\n")
_, _ = os.Stdout.WriteString("<server>: mandatory, server address. Supported: plain, tcp:// (TCP), tls:// (DOT), https:// (DOH), sdns:// (DNSCrypt), quic:// (DOQ)\n")
_, _ = os.Stdout.WriteString("<providerName>: optional, DNSCrypt provider name\n")
_, _ = os.Stdout.WriteString("<serverPk>: optional, DNSCrypt server public key\n")
}
// requestPaddingBlockSize is used to pad responses over DoT and DoH according
// to RFC 8467.
const requestPaddingBlockSize = 128
const udpBufferSize = dns.DefaultMsgSize
// newEDNS0Padding constructs a new OPT RR EDNS0 Padding for the extra section.
func newEDNS0Padding(req *dns.Msg) (option *dns.EDNS0_PADDING) {
msgLen := req.Len()
padLen := requestPaddingBlockSize - msgLen%requestPaddingBlockSize
// Truncate padding to fit in UDP buffer.
if msgLen+padLen > udpBufferSize {
padLen = udpBufferSize - msgLen
if padLen < 0 {
padLen = 0
}
}
return &dns.EDNS0_PADDING{Padding: make([]byte, padLen)}
}
// singleIPResolver represents a resolver that resolves a single IP address.
// This type implements the upstream.Resolver interface.
type singleIPResolver struct {
ip net.IP
}
// type check
var _ upstream.Resolver = (*singleIPResolver)(nil)
// LookupNetIP implements the upstream.Resolver interface for *singleIPResolver.
func (s *singleIPResolver) LookupNetIP(_ context.Context, _ string, _ string) (addrs []netip.Addr, err error) {
ip, ok := netip.AddrFromSlice(s.ip)
if !ok {
return nil, fmt.Errorf("invalid IP: %s", s.ip)
}
return []netip.Addr{ip}, nil
}