-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
455 lines (414 loc) · 13.1 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package main
import (
"context"
"fmt"
"net"
"os"
"strings"
"time"
"github.com/alecthomas/kong"
"github.com/jimmystewpot/dns-preload/pkg/confighandlers"
"github.com/jimmystewpot/dns-preload/pkg/dns"
"golang.org/x/sync/errgroup"
)
const (
// these const strings are used to store the DNS query types for reuse.
queryTypeAStr string = "A, AAAA"
queryTypeCNAMEStr string = "CNAME"
queryTypeMXStr string = "MX"
queryTypeNSStr string = "NS"
queryTypeTXTStr string = "TXT"
queryTypePTRStr string = "PTR"
// print messages that are used more than once.
infoMessage string = "Preloading Nameserver: %s with query type: %s for domains: %s"
batchMessage string = "Preloaded batch for query type: %s completed in: %s"
qTypeEmptyErrMessage string = "Preloading error: query type %s has no entries in the configuration"
qTypeErrMessage string = "preloading error: query type %s is not a valid query type"
completedMessage string = "Preload completed in %s"
)
var (
cli struct {
All Preload `cmd:"" help:"preload all of the following types from the configuration file"`
Cname Preload `cmd:"" help:"preload only the cname entries from the configuration file"`
Hosts Preload `cmd:"" help:"preload only the hosts entries from the configuration file, this does an A and AAAA lookup"`
Mx Preload `cmd:"" help:"preload only the mx entries from the configuration file"`
Ns Preload `cmd:"" help:"preload only the ns entries from the configuration file"`
Txt Preload `cmd:"" help:"preload only the txt entries from the configuration file"`
Ptr Preload `cmd:"" help:"preload only the ptr entries from the configuration file"`
Config Config `cmd:"" help:"generate an empty configuration file to stdout"`
Delay time.Duration `default:"0s" help:"How long to wait until the queries are executed"`
Sleep time.Duration `default:"100ms" help:"Sleep between the different tests when query type all has been chosen"`
}
start time.Time
quiet bool
)
type Preload struct {
ConfigFile string `required:"" help:"The configuration file to read the domain list to query from"`
Server string `default:"localhost" help:"The server to query to seed the domain list into"`
Port string `default:"53" help:"The port the DNS server listens for requests on"`
Workers uint8 `default:"2" help:"The number of concurrent goroutines used to query the DNS server"`
Mute bool `default:"false" help:"Suppress the preload task output to the console"`
Quiet bool `default:"false" help:"Suppress the preload response output to the console"`
Full bool `default:"true" help:"For record types that return a Hostname ensure that these are resolved"`
Debug bool `default:"false" help:"Debug mode"`
Timeout time.Duration `default:"30s" help:"The timeout for each DNS query to succeed (not implemented)"`
resolver dns.CustomResolver
nameserver string
}
type Config struct {
Quiet bool `default:"false" help:"Suppress the info output to the console"`
Generate struct {
Generate bool `default:"true" help:"Generate an empty configuration and output it to stdout"`
} `cmd:"" help:"Generate a configuration file"`
Validate struct {
ConfigFile string `required:"" help:"The configuration file to load"`
} `cmd:"" help:"Validate a configuration file"`
}
// Config Run() prints an empty YAML configuration to stdout.
func (c *Config) Run(cmd string) error {
switch cmd {
case "config generate":
cfg := confighandlers.Configuration{}
return cfg.PrintEmptyConfigration(c.Quiet)
case "config validate":
return fmt.Errorf("not yet implemented")
}
return fmt.Errorf("unknown command %s", cmd)
}
func (p *Preload) Run(cmd string) error {
quiet = p.Quiet
cfg, err := confighandlers.LoadConfigFromFile(&p.ConfigFile)
if err != nil {
return err
}
p.nameserver = net.JoinHostPort(p.Server, p.Port)
p.resolver = dns.NewResolver(p.nameserver, p.Timeout)
ctx := context.Background()
return p.RunQueries(ctx, cmd, cfg)
}
// RunQueries breaks out the command switch statement allowing me to write better tests by adding a mock resolver.
func (p *Preload) RunQueries(ctx context.Context, cmd string, cfg *confighandlers.Configuration) error {
switch cmd {
case confighandlers.Cname:
if cfg.QueryType.CnameCount != 0 {
p.IntroPrinter(queryTypeCNAMEStr, cfg.QueryType.Cname)
return p.CNAME(ctx, cfg.QueryType.Cname)
}
case confighandlers.Hosts:
if cfg.QueryType.HostsCount != 0 {
p.IntroPrinter(queryTypeAStr, cfg.QueryType.Hosts)
return p.Hosts(ctx, cfg.QueryType.Hosts)
}
case confighandlers.Mx:
if cfg.QueryType.MXCount != 0 {
p.IntroPrinter(queryTypeMXStr, cfg.QueryType.MX)
return p.MX(ctx, cfg.QueryType.MX)
}
case confighandlers.Ns:
if cfg.QueryType.NSCount != 0 {
p.IntroPrinter(queryTypeNSStr, cfg.QueryType.NS)
return p.NS(ctx, cfg.QueryType.NS)
}
case confighandlers.Txt:
if cfg.QueryType.TXTCount != 0 {
p.IntroPrinter(queryTypeTXTStr, cfg.QueryType.TXT)
return p.TXT(ctx, cfg.QueryType.TXT)
}
case confighandlers.Ptr:
if cfg.QueryType.PTRCount != 0 {
p.IntroPrinter(queryTypePTRStr, cfg.QueryType.PTR)
return p.PTR(ctx, cfg.QueryType.PTR)
}
default: // no known query type fallback error handling.
return fmt.Errorf(qTypeErrMessage, cmd)
}
if p.Debug {
fmt.Printf(qTypeEmptyErrMessage+"\n", cmd)
}
return nil
}
// CNAME preload the nameserver with CNAME lookups for a given list of hostnames.
//
//nolint:dupl // duplication of logic but not functionality
func (p *Preload) CNAME(ctx context.Context, hosts []string) error {
batch := time.Now()
g := createErrGroup(p.Workers)
for i := 0; i < len(hosts); i++ {
host := hosts[i]
g.Go(func() error {
s := time.Now()
deadline, cancel := context.WithDeadline(ctx, time.Now().Add(p.Timeout))
defer cancel()
result, err := p.resolver.LookupCNAME(deadline, host)
if err != nil {
return err
}
err = p.ResultsPrinter(host, queryTypeCNAMEStr, time.Since(s), result)
if err != nil {
return err
}
return nil
})
}
if err := g.Wait(); err != nil {
return err
}
if !p.Quiet {
fmt.Printf(batchMessage+"\n", queryTypeCNAMEStr, time.Since(batch))
}
return nil
}
// Hosts preload the nameserver with IP addresses for a given list of hostnames.
//
//nolint:dupl // duplication of logic but not functionality
func (p *Preload) Hosts(ctx context.Context, hosts []string) error {
batch := time.Now()
g := createErrGroup(p.Workers)
for i := 0; i < len(hosts); i++ {
host := hosts[i]
g.Go(func() error {
s := time.Now()
deadline, cancel := context.WithDeadline(ctx, time.Now().Add(p.Timeout))
defer cancel()
result, err := p.resolver.LookupIPAddr(deadline, host)
if err != nil {
return err
}
err = p.ResultsPrinter(host, queryTypeAStr, time.Since(s), result)
if err != nil {
return err
}
return nil
})
}
// wait for all of the goroutines in the error group to complete, any errors are handled uniformly.
if err := g.Wait(); err != nil {
return err
}
if !p.Quiet {
fmt.Printf(batchMessage+"\n", queryTypeAStr, time.Since(batch))
}
return nil
}
// MX preloads the nameserver with the MX records for a given list of hostnames.
//
//nolint:dupl // duplication of logic but not functionality
func (p *Preload) MX(ctx context.Context, hosts []string) error {
batch := time.Now()
g := createErrGroup(p.Workers)
for i := 0; i < len(hosts); i++ {
host := hosts[i]
g.Go(func() error {
s := time.Now()
deadline, cancel := context.WithDeadline(ctx, time.Now().Add(p.Timeout))
defer cancel()
result, err := p.resolver.LookupMX(deadline, host)
if err != nil {
return err
}
err = p.ResultsPrinter(host, queryTypeMXStr, time.Since(s), result)
if err != nil {
return err
}
return nil
})
}
if err := g.Wait(); err != nil {
return err
}
if !p.Quiet {
fmt.Printf(batchMessage+"\n", queryTypeMXStr, time.Since(batch))
}
return nil
}
// NS preloads the nameserver records for a given list of hostnames.
func (p *Preload) NS(ctx context.Context, hosts []string) error {
batch := time.Now()
g := createErrGroup(p.Workers)
for i := 0; i < len(hosts); i++ {
host := hosts[i]
g.Go(func() error {
s := time.Now()
deadline, cancel := context.WithDeadline(ctx, time.Now().Add(p.Timeout))
defer cancel()
result, err := p.resolver.LookupNS(deadline, host)
if err != nil {
return err
}
err = p.ResultsPrinter(host, queryTypeNSStr, time.Since(s), result)
if err != nil {
completedPrinter(quiet, start)
return err
}
return nil
})
}
if err := g.Wait(); err != nil {
return err
}
if !p.Quiet {
fmt.Printf(batchMessage+"\n", queryTypeNSStr, time.Since(batch))
}
return nil
}
// TXT preloads the nameserver with the TXT records for a given list of hostnames.
//
//nolint:dupl // duplication of logic but not functionality
func (p *Preload) TXT(ctx context.Context, hosts []string) error {
batch := time.Now()
g := createErrGroup(p.Workers)
for i := 0; i < len(hosts); i++ {
host := hosts[i]
g.Go(func() error {
s := time.Now()
deadline, cancel := context.WithDeadline(ctx, time.Now().Add(p.Timeout))
defer cancel()
result, err := p.resolver.LookupTXT(deadline, host)
if err != nil {
return err
}
err = p.ResultsPrinter(host, queryTypeTXTStr, time.Since(s), result)
if err != nil {
return err
}
return nil
})
}
if err := g.Wait(); err != nil {
return err
}
if !p.Quiet {
fmt.Printf(batchMessage+"\n", queryTypeTXTStr, time.Since(batch))
}
return nil
}
// PTR preloads the nameserver with the PTR records for a given list of hostnames.
//
//nolint:dupl // duplication of logic but not functionality
func (p *Preload) PTR(ctx context.Context, hosts []string) error {
batch := time.Now()
g := createErrGroup(p.Workers)
for i := 0; i < len(hosts); i++ {
host := hosts[i]
g.Go(func() error {
s := time.Now()
deadline, cancel := context.WithDeadline(ctx, time.Now().Add(p.Timeout))
defer cancel()
result, err := p.resolver.LookupAddr(deadline, host)
if err != nil {
return err
}
err = p.ResultsPrinter(host, queryTypePTRStr, time.Since(s), result)
if err != nil {
return err
}
return nil
})
}
if err := g.Wait(); err != nil {
return err
}
if !p.Quiet {
fmt.Printf(batchMessage+"\n", queryTypePTRStr, time.Since(batch))
}
return nil
}
// String provides output to the console for the results of the preloading.
func (p *Preload) ResultsPrinter(hostname string, qtype string, duration time.Duration, results interface{}) error {
// str is used to store the string conversions of the results.
str := make([]string, 0)
switch r := results.(type) {
case string:
str = append(str, results.(string))
case []string:
str = append(str, results.([]string)...)
case []*net.MX:
for _, mx := range results.([]*net.MX) {
str = append(str, mx.Host)
}
case []*net.NS:
for _, ns := range results.([]*net.NS) {
str = append(str, ns.Host)
}
case []net.IPAddr:
for _, ip := range results.([]net.IPAddr) {
str = append(str, ip.IP.String())
}
default:
return fmt.Errorf("error: unknown type %T", r)
}
if p.Full {
// mx and ns record types return hostnames, if full is on we should resolve the final targets.
if (qtype == queryTypeMXStr) || (qtype == queryTypeNSStr) {
err := p.Hosts(context.Background(), str)
if err != nil {
return err
}
}
}
if !p.Quiet {
fmt.Printf("Preloaded %s type %s in %s to %+s\n", hostname, qtype, duration, strings.Join(str, ", "))
}
return nil
}
// IntroPrinter outputs the info on what domains and servers are being reloaded.
func (p *Preload) IntroPrinter(queryType string, hosts []string) {
if !p.Mute {
fmt.Printf("\n"+infoMessage, p.nameserver, queryType, strings.Join(hosts, ", ")+"\n")
}
}
// CompletedPrinter prints out a completion message and a timer to stdout.
func completedPrinter(quiet bool, t time.Time) {
if !quiet {
fmt.Printf(completedMessage+"\n", time.Since(t))
}
}
// createErrGroup handles the logic to setup an errgroup for concurrency.
func createErrGroup(limit uint8) *errgroup.Group {
g := new(errgroup.Group)
// if limit is not set, i.e. in testing set it.
if limit < 1 {
g.SetLimit(1)
return g
}
g.SetLimit(int(limit))
return g
}
func main() {
start = time.Now()
cmd := kong.Parse(&cli,
kong.Name(os.Args[0]),
kong.Description("Preload a DNS cache with a list of hostnames from a YAML configuration file."),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
}),
)
time.Sleep(cli.Delay)
switch cmd.Command() {
case "all":
errGrp := createErrGroup(1)
for _, queryType := range confighandlers.QueryTypes {
errGrp.Go(func() error {
err := cmd.Run(queryType)
if err != nil {
if !quiet {
fmt.Printf("%s\n", err)
}
return err
}
return nil
})
// add a small sleep so that the queries can complete before the next batch.
time.Sleep(cli.Sleep)
}
completedPrinter(quiet, start)
if err := errGrp.Wait(); err != nil {
cmd.FatalIfErrorf(err)
}
default:
err := cmd.Run(cmd.Command())
completedPrinter(quiet, start)
cmd.FatalIfErrorf(err)
}
}