-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
75 lines (63 loc) · 2.11 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
// SPDX-License-Identifier: GPL-3.0-or-later
// Command transport shows how to use the transport to perform a DNS lookup.
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"github.com/miekg/dns"
"github.com/rbmk-project/common/runtimex"
"github.com/rbmk-project/dnscore"
)
// Define command-line flags
var (
serverAddr = flag.String("server", "8.8.8.8:53", "DNS server address")
domain = flag.String("domain", "www.example.com", "Domain to query")
qtype = flag.String("type", "A", "Query type (A, AAAA, CNAME, etc.)")
protocol = flag.String("protocol", "udp", "DNS protocol (udp, tcp, dot, doh)")
)
func main() {
// Parse command-line flags
flag.Parse()
// Set up the JSON logger
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{}))
transport := &dnscore.Transport{}
transport.Logger = logger
// Determine the DNS query type
var dnsType uint16
switch *qtype {
case "A":
dnsType = dns.TypeA
case "AAAA":
dnsType = dns.TypeAAAA
case "CNAME":
dnsType = dns.TypeCNAME
case "HTTPS":
dnsType = dns.TypeHTTPS
default:
panic(fmt.Errorf("transport: unsupported query type: %s", *qtype))
}
// Create the server address
server := dnscore.NewServerAddr(dnscore.Protocol(*protocol), *serverAddr)
flags := 0
maxlength := uint16(dnscore.EDNS0SuggestedMaxResponseSizeUDP)
if *protocol == string(dnscore.ProtocolDoT) || *protocol == string(dnscore.ProtocolDoH) {
flags |= dnscore.EDNS0FlagDO | dnscore.EDNS0FlagBlockLengthPadding
}
if *protocol != string(dnscore.ProtocolUDP) {
maxlength = dnscore.EDNS0SuggestedMaxResponseSizeOtherwise
}
// Create the DNS query
optEDNS0 := dnscore.QueryOptionEDNS0(maxlength, flags)
query := runtimex.Try1(dnscore.NewQuery(*domain, dnsType, optEDNS0))
fmt.Printf(";; Query:\n%s\n", query.String())
// Perform the DNS query
response := runtimex.Try1(transport.Query(context.Background(), server, query))
fmt.Printf("\n;; Response:\n%s\n\n", response.String())
// Validate the DNS response
runtimex.Try0(dnscore.ValidateResponse(query, response))
// Map the RCODE to an error, if any
runtimex.Try0(dnscore.RCodeToError(response))
}