-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreporter.go
231 lines (191 loc) · 5.1 KB
/
reporter.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
package main
import (
"fmt"
"strings"
"time"
"github.com/happal/taifun/cli"
)
// Reporter prints the Results to a terminal.
type Reporter struct {
term cli.Terminal
width int
}
// NewReporter returns a new reporter, width is the length of the hostname
// template (used for the first column).
func NewReporter(term cli.Terminal, width int) *Reporter {
return &Reporter{term: term, width: width}
}
// Stats collects statistics about several responses.
type Stats struct {
Start time.Time
Errors, Results int
Empty, Delegated int
A, AAAA, MX, CNAME, PTR map[string]struct{}
ShownResults int
Count int
lastRPS time.Time
rps float64
}
func formatSeconds(secs float64) string {
sec := int(secs)
hours := sec / 3600
sec -= hours * 3600
min := sec / 60
sec -= min * 60
if hours > 0 {
return fmt.Sprintf("%dh%02dm%02ds", hours, min, sec)
}
return fmt.Sprintf("%dm%02ds", min, sec)
}
// Report returns a report about the received response codes.
func (h *Stats) Report(current string) (res []string) {
res = append(res, "")
status := fmt.Sprintf("%v of %v requests shown", h.ShownResults, h.Results)
dur := time.Since(h.Start) / time.Second
if dur > 0 && time.Since(h.lastRPS) > time.Second {
h.rps = float64(h.Results) / float64(dur)
h.lastRPS = time.Now()
}
if h.rps > 0 {
status += fmt.Sprintf(", %.0f req/s", h.rps)
}
todo := h.Count - h.Results
if todo > 0 {
status += fmt.Sprintf(", %d todo", todo)
if h.rps > 0 {
rem := float64(todo) / h.rps
status += fmt.Sprintf(", %s remaining", formatSeconds(rem))
}
}
if current != "" {
status += fmt.Sprintf(", current: %v", current)
}
res = append(res, status)
if h.Errors > 0 {
res = append(res, fmt.Sprintf("errors: %v", h.Errors))
}
if len(h.A) > 0 {
res = append(res, fmt.Sprintf("unique A: %v", len(h.A)))
}
if len(h.AAAA) > 0 {
res = append(res, fmt.Sprintf("unique AAAA: %v", len(h.AAAA)))
}
if len(h.PTR) > 0 {
res = append(res, fmt.Sprintf("unique PTR: %v", len(h.PTR)))
}
if len(h.MX) > 0 {
res = append(res, fmt.Sprintf("unique MX: %v", len(h.MX)))
}
if len(h.CNAME) > 0 {
res = append(res, fmt.Sprintf("unique CNAME: %v", len(h.CNAME)))
}
if h.Empty > 0 {
res = append(res, fmt.Sprintf("empty: %v", h.Empty))
}
if h.Delegated > 0 {
res = append(res, fmt.Sprintf("delegated: %v", h.Delegated))
}
return res
}
func ljust(s string, width int) string {
if len(s) < width {
return strings.Repeat(" ", width-len(s)) + s
}
return s
}
type printer interface {
Printf(string, ...interface{})
}
func printResult(term printer, width int, result Result) {
if result.Delegation() {
text := fmt.Sprintf("potential delegation, servers: %s", strings.Join(result.Nameservers(), ", "))
term.Printf("%s %8s %8s %6s %s", ljust(result.Hostname, width), "", "", "", text)
return
}
if result.Empty() {
term.Printf("%s %8s %8s %6s %s", ljust(result.Hostname, width), "", "", "", "empty response, potential suffix")
return
}
lastCNAME := ""
request_loop:
for _, request := range result.Requests {
if request.Hide {
continue
}
for _, response := range request.Responses {
if response.Hide {
continue
}
if response.Type == "CNAME" {
// only display the first CNAME response unless the CNAME has changed
if response.Data == lastCNAME {
continue request_loop
}
lastCNAME = response.Data
}
term.Printf("%s %8v %8v %6v %v\n",
ljust(result.Hostname, width),
request.Type,
response.Type,
response.TTL,
response.Data,
)
}
}
}
// Display shows incoming Results.
func (r *Reporter) Display(ch <-chan Result, countChannel <-chan int) error {
r.term.Printf("%s %8s %8s %6s %s", ljust("", r.width), "request", "response", "", "")
r.term.Printf("%s %8s %8s %6s %s", ljust("name ", r.width), "type", "type", "TTL", "response")
stats := &Stats{
Start: time.Now(),
A: make(map[string]struct{}),
AAAA: make(map[string]struct{}),
MX: make(map[string]struct{}),
CNAME: make(map[string]struct{}),
PTR: make(map[string]struct{}),
}
for result := range ch {
select {
case c := <-countChannel:
stats.Count = c
default:
}
stats.Results++
if result.Delegation() {
stats.Delegated++
} else if result.Empty() {
stats.Empty++
}
for _, request := range result.Requests {
if request.Error != nil {
stats.Errors++
}
for _, response := range request.Responses {
switch response.Type {
case "A":
stats.A[response.Data] = struct{}{}
case "AAAA":
stats.AAAA[response.Data] = struct{}{}
case "MX":
stats.MX[response.Data] = struct{}{}
case "CNAME":
stats.CNAME[response.Data] = struct{}{}
case "PTR":
stats.PTR[response.Data] = struct{}{}
}
}
}
if !result.Hide {
printResult(r.term, r.width, result)
stats.ShownResults++
}
r.term.SetStatus(stats.Report(result.Item))
}
r.term.Print("\n")
r.term.Printf("resolved %d DNS requests in %v\n", stats.Results, formatSeconds(time.Since(stats.Start).Seconds()))
for _, line := range stats.Report("")[1:] {
r.term.Print(line)
}
return nil
}