|
6 | 6 | "errors" |
7 | 7 | "fmt" |
8 | 8 | "math/rand/v2" |
| 9 | + "path/filepath" |
9 | 10 | "strings" |
10 | 11 | "time" |
11 | 12 |
|
@@ -47,6 +48,7 @@ type Model struct { |
47 | 48 | vals formValues // the running scan's settings, for its report |
48 | 49 | seed uint64 |
49 | 50 | started time.Time |
| 51 | + final scan.Event // the event that ended the scan, for a saved file's summary (#332) |
50 | 52 |
|
51 | 53 | // What the Sources and Permute fields add around the scan (#274). |
52 | 54 | lookups []sourceLookup // the passive sources asked, in order |
@@ -254,16 +256,9 @@ func (m Model) beginScan(loaded wordlistLoadedMsg) (tea.Model, tea.Cmd) { |
254 | 256 | return m, nil |
255 | 257 | } |
256 | 258 | m.outFile = f |
257 | | - m.out = output.NewFile(f.Writer, vals.Simulate, vals.format) |
258 | | - m.out.SetSeed(seed) |
259 | | - // Same file shape as the CLI's: a takeover column when CNAME |
260 | | - // records are requested (#118). |
261 | | - m.out.SetTakeoverColumn(dns.HintsTakeover(vals.Types)) |
262 | | - // With sources or a permutation pass, each result says what proposed |
263 | | - // it, as the CLI's do. |
264 | | - m.out.SetSourcesColumn(len(vals.sources) > 0 || vals.permute) |
265 | | - } |
266 | | - m.vals, m.seed, m.started = vals, seed, time.Now() |
| 259 | + m.out = newFileWriter(f, vals, vals.format, seed) |
| 260 | + } |
| 261 | + m.vals, m.seed, m.started, m.final = vals, seed, time.Now(), scan.Event{} |
267 | 262 | m.lookups, m.from, m.foundBy = loaded.lookups, loaded.from, map[string]int{} |
268 | 263 | m.entries, m.names, m.pool, m.deadline = entries, nil, pool, time.Time{} |
269 | 264 |
|
@@ -325,15 +320,40 @@ func (m Model) beginScan(loaded wordlistLoadedMsg) (tea.Model, tea.Cmd) { |
325 | 320 | return m, listenForEvents(events) |
326 | 321 | } |
327 | 322 |
|
| 323 | +// newFileWriter returns a writer for a results file in the same shape as the |
| 324 | +// CLI's: a takeover column when CNAME records are requested (#118), and with |
| 325 | +// sources or a permutation pass, what proposed each result (#274). |
| 326 | +func newFileWriter(f *output.File, vals formValues, format output.Format, seed uint64) *output.Writer { |
| 327 | + w := output.NewFile(f.Writer, vals.Simulate, format) |
| 328 | + w.SetSeed(seed) |
| 329 | + w.SetTakeoverColumn(dns.HintsTakeover(vals.Types)) |
| 330 | + w.SetSourcesColumn(len(vals.sources) > 0 || vals.permute) |
| 331 | + return w |
| 332 | +} |
| 333 | + |
328 | 334 | func (m Model) updateScan(msg tea.Msg) (tea.Model, tea.Cmd) { |
329 | 335 | switch msg := msg.(type) { |
330 | 336 | case tea.KeyPressMsg: |
| 337 | + // The save prompt (#332) takes every key but ctrl+c while it is open. |
| 338 | + if m.scanView.saving && msg.String() != "ctrl+c" { |
| 339 | + if m.scanView.handleSaveKey(msg) { |
| 340 | + m.saveResults(expandHome(strings.TrimSpace(m.scanView.savePath))) |
| 341 | + } |
| 342 | + return m, nil |
| 343 | + } |
331 | 344 | // The result filter (#221) takes its keys first; while it is being |
332 | 345 | // typed it takes every key but ctrl+c, so q and r can be typed. |
333 | 346 | if m.scanView.handleFilterKey(msg) { |
334 | 347 | return m, nil |
335 | 348 | } |
336 | 349 | switch msg.String() { |
| 350 | + case "s": |
| 351 | + if m.scanView.done { |
| 352 | + m.scanView.saving = true |
| 353 | + if m.scanView.savePath == "" { |
| 354 | + m.scanView.savePath = m.defaultSavePath() |
| 355 | + } |
| 356 | + } |
337 | 357 | case "ctrl+c": |
338 | 358 | // Once the scan has finished there is nothing to abort; quit |
339 | 359 | // instead of relabelling a completed scan as "Aborted" (#43). |
@@ -399,6 +419,7 @@ func (m Model) updateScan(msg tea.Msg) (tea.Model, tea.Cmd) { |
399 | 419 | // The scan stopped before testing candidates (an early error such as a |
400 | 420 | // wildcard zone without Force, #99): close the file without finalizing, |
401 | 421 | // mirroring the CLI, which skips Finish so no empty JSON array is written. |
| 422 | + m.final = ev.Event |
402 | 423 | m.reportOutputErr(m.finalizeOutput(false)) |
403 | 424 | return m, svCmd |
404 | 425 | case ev.Kind == scan.EventDone: |
@@ -437,6 +458,11 @@ func (m *Model) record(r scan.Result) { |
437 | 458 | for _, src := range res.Sources { |
438 | 459 | m.foundBy[src]++ |
439 | 460 | } |
| 461 | + // The scan view has just added this result as its last line; keep what |
| 462 | + // proposed it there too, so a saved file says so (#332). |
| 463 | + if n := len(m.scanView.results); n > 0 { |
| 464 | + m.scanView.results[n-1].res = res |
| 465 | + } |
440 | 466 | if m.out != nil { |
441 | 467 | m.out.Emit(res) |
442 | 468 | } |
@@ -488,12 +514,83 @@ func (m *Model) finishOutput(final scan.Event) { |
488 | 514 | if main := m.scanView.main; main != nil { |
489 | 515 | final = *main // the report's counts are the main pass's (#298) |
490 | 516 | } |
| 517 | + m.final = final |
491 | 518 | if m.out != nil { |
492 | 519 | m.out.Summary(m.summary(final)) |
493 | 520 | } |
494 | 521 | m.reportOutputErr(m.finalizeOutput(true)) |
495 | 522 | } |
496 | 523 |
|
| 524 | +// defaultSavePath is the save prompt's starting path: the form's Output |
| 525 | +// file if it had one, else subenum-<domain> with the form's format's |
| 526 | +// extension (#332). |
| 527 | +func (m *Model) defaultSavePath() string { |
| 528 | + if m.vals.outputFile != "" { |
| 529 | + return m.vals.outputFile |
| 530 | + } |
| 531 | + ext := map[output.Format]string{output.FormatJSON: ".json", output.FormatJSONL: ".jsonl", output.FormatCSV: ".csv"}[m.vals.format] |
| 532 | + if ext == "" { |
| 533 | + ext = ".txt" |
| 534 | + } |
| 535 | + return "subenum-" + m.vals.Domain + ext |
| 536 | +} |
| 537 | + |
| 538 | +// saveFormat picks a saved file's format from its extension, falling back |
| 539 | +// to the form's Format for any other name (#332). |
| 540 | +func saveFormat(path string, fallback output.Format) output.Format { |
| 541 | + switch strings.ToLower(filepath.Ext(path)) { |
| 542 | + case ".txt": |
| 543 | + return output.FormatText |
| 544 | + case ".json": |
| 545 | + return output.FormatJSON |
| 546 | + case ".jsonl", ".ndjson": |
| 547 | + return output.FormatJSONL |
| 548 | + case ".csv": |
| 549 | + return output.FormatCSV |
| 550 | + } |
| 551 | + return fallback |
| 552 | +} |
| 553 | + |
| 554 | +// saveResults writes the results the filter shows to path, in the format its |
| 555 | +// extension names, the way the Output file would have (#332). A text file |
| 556 | +// carries each result's records while the records view is on, as with |
| 557 | +// -show-records. The outcome is a notice on the scan screen; a failed save |
| 558 | +// leaves an existing file unchanged and does not change the exit code. |
| 559 | +func (m *Model) saveResults(path string) { |
| 560 | + sv := &m.scanView |
| 561 | + results := sv.toSave() |
| 562 | + format := saveFormat(path, m.vals.format) |
| 563 | + err := func() error { |
| 564 | + f, err := output.CreateFile(path) |
| 565 | + if err != nil { |
| 566 | + return err |
| 567 | + } |
| 568 | + w := newFileWriter(f, m.vals, format, m.seed) |
| 569 | + w.SetShowRecords(sv.records) |
| 570 | + for _, r := range results { |
| 571 | + w.Emit(r) |
| 572 | + } |
| 573 | + w.Summary(m.summary(m.final)) |
| 574 | + err = w.Finish() |
| 575 | + if cerr := f.Close(err == nil); err == nil { |
| 576 | + err = cerr |
| 577 | + } |
| 578 | + return err |
| 579 | + }() |
| 580 | + if err != nil { |
| 581 | + sv.addMessage(errorStyle.Render("✗ cannot save results: " + err.Error())) |
| 582 | + return |
| 583 | + } |
| 584 | + text := fmt.Sprintf("Saved %d results to %s", len(results), path) |
| 585 | + if sv.filtered() { |
| 586 | + text += fmt.Sprintf(" (the %d of %d the filter shows)", len(results), len(sv.results)) |
| 587 | + } |
| 588 | + if sv.dropped > 0 { |
| 589 | + text += fmt.Sprintf("; %d earlier results were no longer kept", sv.dropped) |
| 590 | + } |
| 591 | + sv.addMessage(dimStyle.Render(text)) |
| 592 | +} |
| 593 | + |
497 | 594 | // finalizeOutput closes the optional output file. When finish is true the |
498 | 595 | // structured writer is finalized first (streamed JSON array closed, CSV |
499 | 596 | // flushed) and the file replaces any existing one; when false it is discarded |
|
0 commit comments