Repository navigation
Expand file tree
/
Copy pathcli.go
More file actions
332 lines (292 loc) · 10 KB
/
Copy pathcli.go
File metadata and controls
332 lines (292 loc) · 10 KB
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
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log/slog"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"text/tabwriter"
"time"
"github.com/handsomefox/dnsbench/internal/bench"
"github.com/handsomefox/dnsbench/internal/catalog"
"github.com/handsomefox/dnsbench/internal/report"
)
// Config holds all CLI configuration
type Config struct {
// What to test
ResolversFile string
SitesFile string
// Test setup
LookupTimeout time.Duration
Repeats int
OnlyMajorResolvers bool
PrimaryOnly bool
Family catalog.Family
Transport catalog.Transport
Kind string
Providers []string
MaxConcurrency int
Retries int
// Output and logging
OutputType report.Format
LogType LogType
WarmupRuns int
// List prints the resolvers a run would use and exits.
List bool
// Web UI
ServeUI bool
ListenAddr string
}
type LogType int
const (
LogDefault LogType = iota
LogVerbose
LogDisabled
)
func (l LogType) String() string {
switch l {
case LogVerbose:
return "verbose"
case LogDisabled:
return "disabled"
default:
return "default"
}
}
// errInterrupted ends a run that the user stopped with Ctrl+C after its
// partial report is written.
var errInterrupted = errors.New("interrupted")
func run(ctx context.Context, config *Config) error {
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer stop()
// After the first interrupt, a second one ends the process at once.
context.AfterFunc(ctx, stop)
domains, err := catalog.LoadDomains(config.SitesFile)
if err != nil {
return fmt.Errorf("loading domains: %w", err)
}
servers, err := catalog.LoadServers(config.ResolversFile, config.filter())
if err != nil {
return fmt.Errorf("loading servers: %w", err)
}
var reporter bench.Reporter = bench.NoopReporter{}
var p *progress
if config.LogType == LogDefault && isTerminal(os.Stderr) {
p = newProgress(os.Stderr, len(servers)*len(domains)*config.Repeats)
slog.SetDefault(slog.New(progressHandler{next: slog.Default().Handler(), p: p}))
p.run()
reporter = p
}
results, err := bench.Run(ctx, config.benchOptions(), servers, domains, reporter)
if p != nil {
p.finish()
}
interrupted := err != nil && ctx.Err() != nil && len(results) > 0
if err != nil && !interrupted {
return fmt.Errorf("benchmark run failed: %w", err)
}
if interrupted {
fmt.Fprintln(os.Stderr, "Interrupted. The report covers the lookups that finished.")
}
if err := report.Write(os.Stdout, results, config.OutputType); err != nil {
return fmt.Errorf("writing the report: %w", err)
}
if interrupted {
return errInterrupted
}
return nil
}
func parseFlags() *Config {
var config Config
var (
outputType string
logType string
family string
transport string
kind string
providers string
warmupRuns int
serveUI bool
listenAddr string
)
flag.StringVar(&config.ResolversFile, "f", "", "Resolver file that replaces the built-in list, one name;ip per line, with an optional\nthird field: a TLS name for DoT, an https:// URL for DoH, or quic://name for DoQ")
flag.DurationVar(&config.LookupTimeout, "t", 3*time.Second, "Timeout for one lookup attempt, such as 1500ms or 2s")
flag.IntVar(&config.Repeats, "n", 10, "Measured lookups of each domain per resolver")
flag.StringVar(&config.SitesFile, "s", "", "Domain file that replaces the built-in list, one domain per line")
flag.StringVar(&outputType, "output", "table", "Report format: table, csv, or json")
flag.StringVar(&logType, "log", "default", "Logging: default, verbose, or disabled")
flag.IntVar(&config.Retries, "retries", 2, "Retries of a failed lookup attempt, after a short wait. 0 disables them")
flag.IntVar(&config.MaxConcurrency, "c", max(runtime.NumCPU()/2, 2), "Maximum lookups in flight at once, across all resolvers")
flag.BoolVar(&config.OnlyMajorResolvers, "major", false, "Only the major providers: Cloudflare, Google, Quad9, NextDNS, and AdGuard")
flag.BoolVar(&config.PrimaryOnly, "primary", false, "Only the first address of each service, such as Cloudflare-1")
flag.StringVar(&family, "family", "ipv4", "Address family: ipv4, ipv6, or all")
flag.StringVar(&transport, "proto", "plain", "Transport: plain, dot, doh, doq, or all")
flag.StringVar(&providers, "provider", "", "Only the services of these companies, separated by commas, such as cloudflare,google,quad9")
flag.StringVar(&kind, "kind", "all", "Kind of resolver: global, filtering (malware, ads, or family filters), privacy, regional, or all")
flag.IntVar(&warmupRuns, "warmup", 0, "Unmeasured lookups of a domain right before a resolver's first measured lookup of it")
flag.BoolVar(&config.List, "list", false, "Print the resolvers a run would use, after the filters or from -f, and exit")
flag.BoolVar(&serveUI, "ui", false, "Serve the dashboard instead of running a benchmark")
flag.StringVar(&listenAddr, "listen", "127.0.0.1:8080", "Dashboard address. :8080 accepts connections from other machines")
flag.Usage = func() { printUsage(flag.CommandLine) }
flag.Parse()
// Validate configuration
if config.Repeats < 1 {
fmt.Fprintf(os.Stderr, "Error: repeats must be at least 1\n")
os.Exit(1)
}
if config.Retries < 0 {
fmt.Fprintf(os.Stderr, "Error: retries must be 0 or more\n")
os.Exit(1)
}
if config.MaxConcurrency < 1 {
fmt.Fprintf(os.Stderr, "Error: concurrency must be at least 1\n")
os.Exit(1)
}
if config.LookupTimeout < 100*time.Millisecond {
fmt.Fprintf(os.Stderr, "Error: timeout must be at least 100ms\n")
os.Exit(1)
}
format, err := report.ParseFormat(outputType)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
config.OutputType = format
fam, err := catalog.ParseFamily(family)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
config.Family = fam
tr, err := catalog.ParseTransport(transport)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
config.Transport = tr
k, err := catalog.ParseKind(kind)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
config.Kind = k
config.Providers, err = catalog.ParseProviders(providers)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
config.WarmupRuns = warmupRuns
config.ServeUI = serveUI
config.ListenAddr = listenAddr
// Parse log type
switch strings.ToLower(logType) {
case "default":
config.LogType = LogDefault
case "verbose":
config.LogType = LogVerbose
case "disabled":
config.LogType = LogDisabled
default:
fmt.Fprintf(os.Stderr, "Error: invalid log type %q\n", logType)
os.Exit(1)
}
return &config
}
// benchOptions returns the options of a run that the flags set.
func (c *Config) benchOptions() bench.Options {
return bench.Options{
Repeats: c.Repeats,
Timeout: c.LookupTimeout,
Concurrency: c.MaxConcurrency,
Retries: c.Retries,
Warmup: c.WarmupRuns,
}
}
// filter returns the built-in resolver filter that the flags set.
func (c *Config) filter() catalog.Filter {
return catalog.Filter{
Major: c.OnlyMajorResolvers,
Primary: c.PrimaryOnly,
Family: c.Family,
Transport: c.Transport,
Kind: c.Kind,
Providers: c.Providers,
}
}
// listServers prints the resolvers that a run with config would use, one
// per line: name, address, transport, and the TLS name or DoH URL.
func listServers(w io.Writer, config *Config) error {
servers, err := catalog.LoadServers(config.ResolversFile, config.filter())
if err != nil {
return err
}
tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
fmt.Fprintln(tw, "Resolver\tAddress\tTransport\tEndpoint") //nolint:errcheck // Flush reports write errors
for _, s := range servers {
endpoint := s.TLSName + s.DoHURL + s.DoQName // at most one is set
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", s.Name, s.Addr, s.Transport(), endpoint) //nolint:errcheck // Flush reports write errors
}
return tw.Flush()
}
// usageGroups orders the flags in -h by what they control.
var usageGroups = []struct {
title string
flags []string
}{
{"Resolvers", []string{"provider", "major", "primary", "family", "proto", "kind", "f", "list"}},
{"Domains", []string{"s"}},
{"Measurement", []string{"n", "t", "c", "retries", "warmup"}},
{"Output", []string{"output", "log"}},
{"Dashboard", []string{"ui", "listen"}},
}
// printUsage writes -h: the ways to run dnsbench, the flags by group, and
// examples. Each flag's line comes from its definition, so the text cannot
// drift from the flags.
func printUsage(fs *flag.FlagSet) {
var b strings.Builder
b.WriteString(`dnsbench measures how fast and how reliably DNS resolvers answer.
Usage:
dnsbench [flags] Run a benchmark and print a report
dnsbench -ui [flags] Serve the dashboard and open it in a browser
dnsbench -list [flags] Print the resolvers a run would use
`)
for _, g := range usageGroups {
fmt.Fprintf(&b, "\n%s:\n", g.title)
for _, name := range g.flags {
f := fs.Lookup(name)
if f == nil {
continue
}
arg, usage := flag.UnquoteUsage(f)
head := " -" + f.Name
if arg != "" {
head += " " + arg
}
// A usage with several lines keeps its column.
usage = strings.ReplaceAll(usage, "\n", "\n"+strings.Repeat(" ", 23))
fmt.Fprintf(&b, "%-22s %s", head, usage)
if f.DefValue != "" && f.DefValue != "false" && f.DefValue != "0" && f.DefValue != "default" {
fmt.Fprintf(&b, " (default %s)", f.DefValue)
}
b.WriteString("\n")
}
}
b.WriteString(`
Flags take one dash or two: -n 5 and --n 5 are the same.
Examples:
dnsbench -major -primary One address of each major provider
dnsbench -provider cloudflare,quad9 Cloudflare and Quad9, filtering too
dnsbench -major -proto all -family all Every transport and family they offer
dnsbench -f resolvers.txt -s domains.txt Your own resolvers and domains
dnsbench -output csv > results.csv Save a report
dnsbench -ui The dashboard at http://127.0.0.1:8080
`)
//nolint:errcheck // best-effort help output
_, _ = io.WriteString(fs.Output(), b.String())
}