|
23 | 23 | package cli |
24 | 24 |
|
25 | 25 | import ( |
26 | | - "bufio" |
27 | 26 | "context" |
28 | 27 | "errors" |
29 | 28 | "flag" |
30 | 29 | "fmt" |
31 | 30 | "io" |
32 | 31 | "os" |
| 32 | + "path/filepath" |
33 | 33 | "runtime/debug" |
| 34 | + "slices" |
34 | 35 | "strings" |
35 | 36 | "time" |
36 | 37 |
|
@@ -130,7 +131,7 @@ type cliFlags struct { |
130 | 131 | testHitRate int |
131 | 132 | seed uint64 |
132 | 133 | simZone string // -simulate-zone: lab scenario served by a local DNS server (#76) |
133 | | - outputFile string |
| 134 | + outputs outputSpecs |
134 | 135 | attempts int |
135 | 136 | retries int |
136 | 137 | force bool |
@@ -229,7 +230,7 @@ func parseFlagsWith(args []string, applyDefaults func(*flag.FlagSet) error, stde |
229 | 230 | fs.IntVar(&f.testHitRate, "hit-rate", scan.DefaultHitRate, "In simulation mode, percentage of names that resolve (1-100)") |
230 | 231 | fs.Uint64Var(&f.seed, "seed", 0, "In simulation mode, seed for reproducible results (0 = random; the seed used is printed)") |
231 | 232 | fs.StringVar(&f.simZone, "simulate-zone", "", "Lab mode: answer every query from this scenario file via a local DNS server, so no traffic leaves the machine (see the Labs page)") |
232 | | - fs.StringVar(&f.outputFile, "o", "", "Write results to file (in addition to stdout)") |
| 233 | + fs.Var(&f.outputs, "o", "Write results to `file` (in addition to stdout); repeat for several files, each in -format or in the format named as a prefix, format:path (jsonl:results.jsonl)") |
233 | 234 | fs.IntVar(&f.attempts, "attempts", 0, "Total DNS resolution attempts per subdomain, 1 = no retry (default 1)") |
234 | 235 | fs.IntVar(&f.retries, "retries", 0, "Deprecated: use -attempts instead") |
235 | 236 | fs.BoolVar(&f.force, "force", false, "Continue scanning even if wildcard DNS is detected") |
@@ -492,16 +493,88 @@ func loadTargets(f cliFlags, in *runtimeInputs, domain string, out *output.Write |
492 | 493 | return targets, true |
493 | 494 | } |
494 | 495 |
|
495 | | -func openOutputFile(path string, testMode bool, format output.Format, out *output.Writer) (*output.Writer, *output.File, bool) { |
496 | | - if path == "" { |
497 | | - return out, nil, true |
| 496 | +// outputSpecs is the repeatable -o flag (#334). Each -o adds a results file, |
| 497 | +// written in -format or in the format its "format:" prefix names, so one run |
| 498 | +// can save bare names for the next tool and JSONL for the next -diff. |
| 499 | +// |
| 500 | +// A config-file or SUBENUM_O value is a default: it is replaced, not added |
| 501 | +// to, by a later source or by the first -o on the command line. |
| 502 | +type outputSpecs struct { |
| 503 | + specs []string |
| 504 | + replace bool // the current specs are a default; the next Set replaces them |
| 505 | +} |
| 506 | + |
| 507 | +func (o *outputSpecs) String() string { |
| 508 | + if o == nil { |
| 509 | + return "" |
498 | 510 | } |
499 | | - f, err := output.CreateFile(path) |
500 | | - if err != nil { |
501 | | - out.Error("creating output file: %v", err) |
502 | | - return out, nil, false |
| 511 | + return strings.Join(o.specs, ",") |
| 512 | +} |
| 513 | + |
| 514 | +// Set adds a file. An empty value adds nothing, so -o "" still means none. |
| 515 | +func (o *outputSpecs) Set(v string) error { |
| 516 | + if o.replace { |
| 517 | + o.specs, o.replace = nil, false |
| 518 | + } |
| 519 | + if v != "" { |
| 520 | + o.specs = append(o.specs, v) |
| 521 | + } |
| 522 | + return nil |
| 523 | +} |
| 524 | + |
| 525 | +// markDefault records that the current specs came from the config file or |
| 526 | +// the environment rather than the command line. |
| 527 | +func (o *outputSpecs) markDefault() { o.replace = true } |
| 528 | + |
| 529 | +// describe names the -o files for notices: the paths, comma-separated. |
| 530 | +func (o *outputSpecs) describe() string { |
| 531 | + paths := make([]string, len(o.specs)) |
| 532 | + for i, spec := range o.specs { |
| 533 | + _, paths[i] = splitOutputSpec(spec) |
| 534 | + } |
| 535 | + return strings.Join(paths, ", ") |
| 536 | +} |
| 537 | + |
| 538 | +// splitOutputSpec splits an -o value into its format prefix, "" for none, |
| 539 | +// and its path. Only a format name counts as a prefix, so a Windows drive |
| 540 | +// letter (C:\results.txt) is part of the path. |
| 541 | +func splitOutputSpec(spec string) (format, path string) { |
| 542 | + prefix, rest, ok := strings.Cut(spec, ":") |
| 543 | + if ok && rest != "" && slices.Contains(append(output.Formats(), "ndjson"), strings.ToLower(prefix)) { |
| 544 | + return prefix, rest |
| 545 | + } |
| 546 | + return "", spec |
| 547 | +} |
| 548 | + |
| 549 | +// outputTarget is one -o file and the format it is written in. |
| 550 | +type outputTarget struct { |
| 551 | + path string |
| 552 | + format output.Format |
| 553 | +} |
| 554 | + |
| 555 | +// parseOutputs resolves the -o values; a file without a prefix takes def, |
| 556 | +// the -format of stdout. The same path given twice is refused: both would |
| 557 | +// replace one file. |
| 558 | +func parseOutputs(specs []string, def output.Format) ([]outputTarget, error) { |
| 559 | + targets := make([]outputTarget, 0, len(specs)) |
| 560 | + seen := map[string]bool{} |
| 561 | + for _, spec := range specs { |
| 562 | + name, path := splitOutputSpec(spec) |
| 563 | + format := def |
| 564 | + if name != "" { |
| 565 | + var err error |
| 566 | + if format, err = output.ParseFormat(name); err != nil { |
| 567 | + return nil, err |
| 568 | + } |
| 569 | + } |
| 570 | + key := filepath.Clean(path) |
| 571 | + if seen[key] { |
| 572 | + return nil, fmt.Errorf("-o %s is given more than once", path) |
| 573 | + } |
| 574 | + seen[key] = true |
| 575 | + targets = append(targets, outputTarget{path: path, format: format}) |
503 | 576 | } |
504 | | - return output.New(f.Writer, testMode, format), f, true |
| 577 | + return targets, nil |
505 | 578 | } |
506 | 579 |
|
507 | 580 | // isTerminal reports whether w is a character device (a terminal) rather than |
@@ -542,16 +615,16 @@ func logVerboseStart(f cliFlags, domain string, maxAttempts int, out *output.Wri |
542 | 615 | if !f.testMode { |
543 | 616 | out.Info("DNS Server: %s", f.dnsServer) |
544 | 617 | } |
545 | | - if f.outputFile != "" { |
546 | | - out.Info("Output file: %s", f.outputFile) |
| 618 | + if len(f.outputs.specs) > 0 { |
| 619 | + out.Info("Output file: %s", f.outputs.describe()) |
547 | 620 | } |
548 | 621 | out.Info("---") |
549 | 622 | } |
550 | 623 |
|
551 | | -func logVerboseDone(ev scan.Event, f cliFlags, outWriter *bufio.Writer, out *output.Writer) { |
| 624 | +func logVerboseDone(ev scan.Event, f cliFlags, out *output.Writer) { |
552 | 625 | out.Info("Processed %d subdomain prefixes", ev.Processed) |
553 | | - if outWriter != nil { |
554 | | - out.Info("Results written to: %s", f.outputFile) |
| 626 | + if len(f.outputs.specs) > 0 { |
| 627 | + out.Info("Results written to: %s", f.outputs.describe()) |
555 | 628 | } |
556 | 629 | if f.testMode { |
557 | 630 | out.Info("\nNOTE: Results were simulated and no actual DNS queries were performed.") |
@@ -768,7 +841,7 @@ func scanTarget(ctx context.Context, s *session, p scanPass, emit func(scan.Even |
768 | 841 | res.resolverAbort = ev.Stats.Aborted |
769 | 842 | logScanBreakdown(p.domain, ev, out) |
770 | 843 | if f.verbose { |
771 | | - logVerboseDone(ev, f, s.outWriter, out) |
| 844 | + logVerboseDone(ev, f, out) |
772 | 845 | } |
773 | 846 | } |
774 | 847 | } |
|
0 commit comments