forked from ameshkov/dnslookup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (109 loc) · 3.07 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
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
"time"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/ameshkov/dnsstamps"
"github.com/miekg/dns"
)
// VersionString -- see the makefile
var VersionString = "undefined"
func main() {
machineReadable := os.Getenv("JSON") == "1"
insecureSkipVerify := os.Getenv("VERIFY") == "0"
timeoutStr := os.Getenv("TIMEOUT")
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) != 3 && len(os.Args) != 4 && len(os.Args) != 5 {
log.Printf("Wrong number of arguments")
usage()
os.Exit(1)
}
if timeoutStr != "" {
i, err := strconv.Atoi(timeoutStr)
if err != nil {
log.Printf("Wrong timeout value: %s", timeoutStr)
usage()
os.Exit(1)
}
timeout = i
}
domain := os.Args[1]
server := os.Args[2]
opts := upstream.Options{
Timeout: time.Duration(timeout) * time.Second,
InsecureSkipVerify: insecureSkipVerify,
}
if len(os.Args) == 4 {
ip := net.ParseIP(os.Args[3])
if ip == nil {
log.Fatalf("invalid IP specified: %s", os.Args[3])
}
opts.ServerIPAddrs = []net.IP{ip}
}
if len(os.Args) == 5 {
// DNSCrypt parameters
providerName := os.Args[3]
serverPkStr := os.Args[4]
serverPk, err := hex.DecodeString(strings.Replace(serverPkStr, ":", "", -1))
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{
{Name: domain + ".", Qtype: dns.TypeA, Qclass: dns.ClassINET},
}
reply, err := u.Exchange(&req)
if err != nil {
log.Fatalf("Cannot make the DNS request: %s", err)
}
if !machineReadable {
os.Stdout.WriteString("dnslookup result:\n")
os.Stdout.WriteString(reply.String() + "\n")
} else {
b, err := json.MarshalIndent(reply, "", " ")
if err != nil {
log.Fatalf("Cannot marshal json: %s", err)
}
os.Stdout.WriteString(string(b) + "\n")
}
}
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, 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")
}