forked from rs/dnstrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
198 lines (185 loc) · 4.12 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
package main
import (
"flag"
"fmt"
"net"
"os"
"strings"
"time"
"github.com/miekg/dns"
"github.com/rs/dnstrace/client"
)
const (
cReset = 0
cBold = 1
cRed = 31
cGreen = 32
cYellow = 33
cBlue = 34
cMagenta = 35
cCyan = 36
cGray = 37
cDarkGray = 90
maxRetry = 10 // limit retry of unresolved name to 10 times
)
func colorize(s interface{}, color int, enabled bool) string {
if !enabled {
return fmt.Sprintf("%v", s)
}
return fmt.Sprintf("\x1b[%dm%v\x1b[0m", color, s)
}
func init() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: dnstrace [qtype] <domain>\n\n")
flag.PrintDefaults()
}
}
func main() {
color := flag.Bool("color", true, "Enable/disable colors")
nsonly := flag.Bool("nsonly", false, "Only print nameservers")
flag.Parse()
if flag.NArg() < 1 || flag.NArg() > 2 {
flag.Usage()
os.Exit(1)
}
qname := ""
qtype := dns.TypeA
for _, arg := range flag.Args() {
if t, found := dns.StringToType[arg]; found {
qtype = t
continue
}
if qname != "" {
flag.Usage()
os.Exit(1)
}
qname = dns.Fqdn(arg)
}
col := func(s interface{}, c int) string {
return colorize(s, c, *color)
}
m := &dns.Msg{}
m.SetQuestion(qname, qtype)
// Set DNSSEC opt to better emulate the default queries from a nameserver.
o := &dns.OPT{
Hdr: dns.RR_Header{
Name: ".",
Rrtype: dns.TypeOPT,
},
}
o.SetDo()
o.SetUDPSize(dns.DefaultMsgSize)
m.Extra = append(m.Extra, o)
c := client.New(maxRetry)
c.Client.Timeout = 500 * time.Millisecond
t := client.Tracer{
GotIntermediaryResponse: func(i int, m *dns.Msg, rs client.Responses, rtype client.ResponseType) {
fr := rs.Fastest()
var r *dns.Msg
if fr != nil {
r = fr.Msg
}
qname := m.Question[0].Name
qtype := dns.TypeToString[m.Question[0].Qtype]
if i > 1 {
if !*nsonly {
fmt.Println()
}
}
if !*nsonly {
fmt.Printf("%d - query %s %s", i, qtype, qname)
}
if r != nil {
if !*nsonly {
fmt.Printf(": %s", strings.Replace(strings.Replace(r.MsgHdr.String(), ";; ", "", -1), "\n", ", ", -1))
}
}
if !*nsonly {
fmt.Println()
}
fmt.Println()
for _, pr := range rs {
ln := 0
if pr.Msg != nil {
ln = pr.Msg.Len()
}
rtt := float64(pr.RTT) / float64(time.Millisecond)
lrtt := "0ms (from cache)"
if pr.Server.HasGlue {
lrtt = "0ms (from glue)"
} else if pr.Server.LookupRTT > 0 {
if !*nsonly {
lrtt = fmt.Sprintf("%.2fms", float64(pr.Server.LookupRTT)/float64(time.Millisecond))
}
}
if !*nsonly {
fmt.Printf(col(" - %d bytes in %.2fms + %s lookup on %s(%s)", cDarkGray), ln, rtt, lrtt, pr.Server.Name, pr.Addr)
}
if pr.Err != nil {
err := pr.Err
if oerr, ok := err.(*net.OpError); ok {
err = oerr.Err
}
if !*nsonly {
fmt.Printf(": %v", col(err, cRed))
}
}
if !*nsonly {
fmt.Print("\n")
}
}
switch rtype {
case client.ResponseTypeDelegation:
var label string
for _, rr := range r.Ns {
if ns, ok := rr.(*dns.NS); ok {
label = ns.Header().Name
break
}
}
_, ns := c.DCache.Get(label)
for _, s := range ns {
var glue string
if s.HasGlue {
if !*nsonly {
glue = col("glue: "+strings.Join(s.Addrs, ","), cDarkGray)
}
} else {
if !*nsonly {
glue = col("no glue", cYellow)
}
}
if !*nsonly {
fmt.Printf("%s %d NS %s (%s)\n", label, s.TTL, s.Name, glue)
} else {
// just print the nameserver hostname without any details
fmt.Printf("%s\n", s.Name)
}
}
case client.ResponseTypeCNAME:
for _, rr := range r.Answer {
if !*nsonly {
fmt.Println(rr)
}
}
}
},
FollowingCNAME: func(domain, target string) {
fmt.Printf(col("\n~ following CNAME %s -> %s\n", cBlue), domain, target)
},
}
r, rtt, err := c.RecursiveQuery(m, t)
if err != nil {
fmt.Printf(col("*** error: %v\n", cRed), err)
os.Exit(1)
}
if !*nsonly {
fmt.Println()
fmt.Printf(col(";; Cold best path time: %s\n\n", cGray), rtt)
}
for _, rr := range r.Answer {
if !*nsonly {
fmt.Println(rr)
}
}
}