-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoop.go
523 lines (491 loc) · 12.1 KB
/
scoop.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/miekg/dns"
"golang.org/x/net/proxy"
"io"
"log"
"net"
"net/url"
"os"
"strings"
)
var names = map[string]uint16{
"A": dns.TypeA,
"NS": dns.TypeNS,
"MD": dns.TypeMD,
"MF": dns.TypeMF,
"CNAME": dns.TypeCNAME,
"SOA": dns.TypeSOA,
"MB": dns.TypeMB,
"MG": dns.TypeMG,
"MR": dns.TypeMR,
"NULL": dns.TypeNULL,
"PTR": dns.TypePTR,
"HINFO": dns.TypeHINFO,
"MINFO": dns.TypeMINFO,
"MX": dns.TypeMX,
"TXT": dns.TypeTXT,
"RP": dns.TypeRP,
"AFSDB": dns.TypeAFSDB,
"X25": dns.TypeX25,
"ISDN": dns.TypeISDN,
"RT": dns.TypeRT,
"NSAPPTR": dns.TypeNSAPPTR,
"SIG": dns.TypeSIG,
"KEY": dns.TypeKEY,
"PX": dns.TypePX,
"GPOS": dns.TypeGPOS,
"AAAA": dns.TypeAAAA,
"LOC": dns.TypeLOC,
"NXT": dns.TypeNXT,
"EID": dns.TypeEID,
"NIMLOC": dns.TypeNIMLOC,
"SRV": dns.TypeSRV,
"ATMA": dns.TypeATMA,
"NAPTR": dns.TypeNAPTR,
"KX": dns.TypeKX,
"CERT": dns.TypeCERT,
"DNAME": dns.TypeDNAME,
"OPT": dns.TypeOPT, // EDNS
"APL": dns.TypeAPL,
"DS": dns.TypeDS,
"SSHFP": dns.TypeSSHFP,
"RRSIG": dns.TypeRRSIG,
"NSEC": dns.TypeNSEC,
"DNSKEY": dns.TypeDNSKEY,
"DHCID": dns.TypeDHCID,
"NSEC3": dns.TypeNSEC3,
"NSEC3PARAM": dns.TypeNSEC3PARAM,
"TLSA": dns.TypeTLSA,
"SMIMEA": dns.TypeSMIMEA,
"HIP": dns.TypeHIP,
"NINFO": dns.TypeNINFO,
"RKEY": dns.TypeRKEY,
"TALINK": dns.TypeTALINK,
"CDS": dns.TypeCDS,
"CDNSKEY": dns.TypeCDNSKEY,
"OPENPGPKEY": dns.TypeOPENPGPKEY,
"CSYNC": dns.TypeCSYNC,
"ZONEMD": dns.TypeZONEMD,
"SVCB": dns.TypeSVCB,
"HTTPS": dns.TypeHTTPS,
"SPF": dns.TypeSPF,
"UINFO": dns.TypeUINFO,
"UID": dns.TypeUID,
"GID": dns.TypeGID,
"UNSPEC": dns.TypeUNSPEC,
"NID": dns.TypeNID,
"L32": dns.TypeL32,
"L64": dns.TypeL64,
"LP": dns.TypeLP,
"EUI48": dns.TypeEUI48,
"EUI64": dns.TypeEUI64,
"URI": dns.TypeURI,
"CAA": dns.TypeCAA,
"AVC": dns.TypeAVC,
"TKEY": dns.TypeTKEY,
"TSIG": dns.TypeTSIG,
"IXFR": dns.TypeIXFR,
"AXFR": dns.TypeAXFR,
"MAILB": dns.TypeMAILB,
"MAILA": dns.TypeMAILA,
"ANY": dns.TypeANY,
"TA": dns.TypeTA,
"DLV": dns.TypeDLV,
}
func GetDnsTypeFromName(name string) uint16 {
return names[name]
}
func failArgs(format string, a ...interface{}) {
outfile := os.Stderr
_, _ = outfile.WriteString("ERROR: ")
_, _ = fmt.Fprintf(outfile, format, a...)
_, _ = outfile.WriteString("\n")
os.Exit(1)
}
type DnsProtocol string
const (
ProtocolTCP = DnsProtocol("tcp")
ProtocolUDP = DnsProtocol("udp")
)
type ServerConfig struct {
Protocol DnsProtocol
UseTLS bool
ServerHost string
ServerPort string
TlsServerName string
}
func (s ServerConfig) Address() string {
return s.ServerHost + ":" + s.ServerPort
}
func (s ServerConfig) Network() string {
return string(s.Protocol)
}
func parseServerArg(server string) (*ServerConfig, error) {
var defaultPort = "53"
var err error
var result = new(ServerConfig)
result.Protocol = "udp"
var schemeEnd = strings.Index(server, "://")
if schemeEnd > -1 {
scheme := server[:schemeEnd]
server = server[schemeEnd+3:]
switch scheme {
case "tls", "dot", "tcp-tls":
result.Protocol = ProtocolTCP
result.UseTLS = true
defaultPort = "853"
case "tcp":
result.Protocol = ProtocolTCP
defaultPort = "53"
case "udp":
result.Protocol = ProtocolUDP
defaultPort = "53"
default:
return nil, fmt.Errorf("invalid scheme: %s", scheme)
}
}
tlsNameIndex := strings.IndexByte(server, '#')
if tlsNameIndex >= 0 {
result.TlsServerName = server[tlsNameIndex+1:]
server = server[:tlsNameIndex]
}
if len(server) == 0 {
failArgs("got empty server")
}
result.ServerHost, result.ServerPort, err = net.SplitHostPort(server)
if err != nil {
addrError := err.(*net.AddrError)
if addrError.Err == "missing port in address" {
result.ServerPort = defaultPort
result.ServerHost = server
} else {
return nil, fmt.Errorf("invalid server: %s", err)
}
}
if result.Protocol == "" {
switch result.ServerPort {
case "853":
result.Protocol = ProtocolTCP
result.UseTLS = true
default:
fallthrough
case "53":
result.Protocol = ProtocolUDP
}
}
return result, nil
}
func parseProxyArg(proxyValue string) (Dialer, error) {
var proxyDialer Dialer
proxyUrl, err := url.Parse(proxyValue)
if err != nil {
return nil, fmt.Errorf("invalid proxy url: %s", err)
}
proxyDialer, err = proxy.FromURL(proxyUrl, &net.Dialer{})
if err != nil {
return nil, fmt.Errorf("invalid proxy: %s", err)
}
return proxyDialer, nil
}
// A Dialer is a means to establish a connection.
type Dialer interface {
Dial(network, address string) (net.Conn, error)
}
type Scoop struct {
Dialer Dialer
TLSConfig *tls.Config
}
func (s Scoop) Exchange(server *ServerConfig, msg *dns.Msg) (*dns.Msg, error) {
conn, err := s.Dial(server)
if err != nil {
return nil, err
}
err = conn.WriteMsg(msg)
if err != nil {
closeError := conn.Close()
if closeError != nil {
return nil, fmt.Errorf("failed to close connection: %s - after error %s", closeError, err)
} else {
return nil, err
}
}
response, err := conn.ReadMsg()
if err != nil {
closeError := conn.Close()
if closeError != nil {
return nil, fmt.Errorf("failed to reaad connection: %s - after error %s", closeError, err)
} else {
return nil, err
}
}
return response, conn.Close()
}
func (s Scoop) Dial(server *ServerConfig) (*dns.Conn, error) {
var err error
address := server.Address()
network := server.Network()
conn := new(dns.Conn)
var dialer Dialer
if s.Dialer == nil {
dialer = &net.Dialer{}
} else {
dialer = s.Dialer
}
if server.UseTLS {
var rawCon net.Conn
var tlsConfig *tls.Config
var tlsConn *tls.Conn
if s.TLSConfig == nil {
tlsConfig = &tls.Config{}
} else {
tlsConfig = s.TLSConfig.Clone()
}
// Do not overwrite ServerName if specified in the tlsConfig already
if tlsConfig.ServerName == "" {
if server.TlsServerName != "" {
tlsConfig.ServerName = server.ServerHost
} else {
tlsConfig.ServerName, _, err = net.SplitHostPort(address)
if err != nil {
return nil, err
}
}
}
rawCon, err = dialer.Dial(network, address)
if err != nil {
return nil, err
}
tlsConn = tls.Client(rawCon, tlsConfig)
err = tlsConn.Handshake()
if err != nil {
return nil, err
}
conn.Conn = tlsConn
} else {
conn.Conn, err = dialer.Dial(network, address)
if err != nil {
return nil, err
}
}
return conn, nil
}
func main() {
var err error
var dnsTypeName string
var outputType string
var name string
var serverArg *ServerConfig
var args = os.Args[1:]
var proxyUrl string
for i := 0; i < len(args); i++ {
arg := args[i]
if len(arg) == 0 {
failArgs("got empty argument")
}
if arg[0] == '-' {
if len(arg) == 1 {
failArgs("invalid argument '-'")
} else if arg[1] == '-' {
arg = arg[2:]
} else {
arg = arg[1:]
}
switch arg {
case "tor":
if proxyUrl != "" {
failArgs("proxy already specified")
}
proxyUrl = "socks5h://127.0.0.1:9050"
continue
}
var argValue string
argParts := strings.SplitN(arg, "=", 2)
if len(argParts) == 2 {
arg = argParts[0]
argValue = argParts[1]
} else if i+1 == len(args) {
failArgs("'%s' expects a value or is invalid flag", arg)
} else {
i++
argValue = args[i]
}
switch arg {
case "proxy":
if proxyUrl != "" {
failArgs("proxy already specified")
}
proxyUrl = argValue
case "out":
if outputType != "" {
failArgs("'out' type already specified")
}
outputType = argValue
default:
failArgs("invalid argument: %s", arg)
}
} else if arg[0] == '@' {
if serverArg != nil {
failArgs("got multiple server")
}
serverArg, err = parseServerArg(arg[1:])
if err != nil {
failArgs(err.Error())
}
} else if names[arg] != dns.TypeNone {
dnsTypeName = arg
} else {
if name != "" {
failArgs("got multiple names: %s and %s", name, arg)
}
name = arg
}
}
if name == "" {
failArgs("got no name")
}
if serverArg == nil {
if proxyUrl != "" {
failArgs("server must be specified when using a proxy")
}
log.Println("got no server, using resolv.conf")
config, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
failArgs("failed to load resolv.conf: %s", err)
}
serverArg = new(ServerConfig)
serverArg.ServerHost = config.Servers[0]
serverArg.ServerPort = config.Port
serverArg.Protocol = ProtocolUDP
}
log.Printf("using server %s on port %s \n", serverArg.ServerHost, serverArg.ServerPort)
log.Printf("using network %s\n", serverArg.Network())
if dnsTypeName == "" {
var arpa string
arpa, err = dns.ReverseAddr(name)
if err != nil {
dnsTypeName = "A"
} else {
name = arpa
dnsTypeName = "PTR"
}
}
dnsType := GetDnsTypeFromName(dnsTypeName)
if dnsType == 0 {
failArgs("invalid dns type: %s", dnsTypeName)
}
log.Printf("using dns type %s", dnsTypeName)
scoop := new(Scoop)
if proxyUrl != "" {
scoop.Dialer, err = parseProxyArg(proxyUrl)
if err != nil {
failArgs(err.Error())
}
log.Printf("using proxy %s\n", proxyUrl)
}
if serverArg.TlsServerName == "" && isOnionAddress(serverArg.ServerHost) {
log.Println("skipping TLS verification for .onion host")
scoop.TLSConfig = &tls.Config{
InsecureSkipVerify: true,
}
} else if serverArg.TlsServerName != "" {
scoop.TLSConfig = &tls.Config{
ServerName: serverArg.TlsServerName,
}
}
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(name), dnsType)
m.RecursionDesired = true
response, err := scoop.Exchange(serverArg, m)
if err != nil {
fmt.Println(err)
return
}
var writer DnsWriter = defaultWriter
switch outputType {
case "json":
writer = &jsonWriter{
Indent: " ",
}
}
err = writer.Write(os.Stdout, response)
if err != nil {
fmt.Println(" *** error:", err)
}
}
type DnsWriter interface {
Write(w io.Writer, answer *dns.Msg) error
}
type jsonWriter struct {
Prefix string
Indent string
}
func (writer jsonWriter) Write(w io.Writer, answer *dns.Msg) error {
encoder := json.NewEncoder(w)
encoder.SetIndent(writer.Prefix, writer.Indent)
err := encoder.Encode(answer)
return err
}
type writerFunc func(w io.Writer, answer *dns.Msg) error
func (fnc writerFunc) Write(w io.Writer, answer *dns.Msg) error {
return fnc(w, answer)
}
var defaultWriter writerFunc = func(w io.Writer, answer *dns.Msg) (err error) {
const (
bold = "\033[1m"
unbold = "\033[0m"
inlineFmt = bold + "%-9s: " + unbold
headerFmt = bold + "\n> %s " + unbold + "\n"
answerHighlightFormat = "\033[0;32m%v\u001B[0m\n"
answerFailFormat = "\033[0;31m%v\u001B[0m\n"
)
if _, err = fmt.Fprintf(w, inlineFmt+"%s (%d)\n", "Rcode", dns.RcodeToString[answer.Rcode], answer.Rcode); err != nil {
return
}
if _, err = fmt.Fprintf(w, inlineFmt+"%d\n", "Id", answer.Id); err != nil {
return
}
if _, err = fmt.Fprintf(w, inlineFmt+"%t\n", "Compress", answer.Compress); err != nil {
return
}
if len(answer.Ns) > 0 {
if _, err = fmt.Fprintf(w, headerFmt, "Nameservers"); err != nil {
return
}
for _, nameserver := range answer.Ns {
if _, err = fmt.Fprintf(w, "%v\n", nameserver); err != nil {
return
}
}
}
if len(answer.Extra) > 0 {
if _, err = fmt.Fprintf(w, headerFmt, "Extras"); err != nil {
return
}
for _, extra := range answer.Extra {
if _, err = fmt.Fprintf(w, "%v\n", extra); err != nil {
return
}
}
}
if _, err = fmt.Fprintf(w, headerFmt, "Answers"); err != nil {
return
}
if len(answer.Answer) == 0 {
if _, err = fmt.Fprintf(w, answerFailFormat, "no answers"); err != nil {
return
}
}
for _, answer := range answer.Answer {
if _, err = fmt.Fprintf(w, answerHighlightFormat, answer); err != nil {
return
}
}
return nil
}
func isOnionAddress(host string) bool {
return strings.HasSuffix(host, ".onion")
}