Skip to content

Commit daf571a

Browse files
committed
TUI: show records and save results after the scan (#332)
A TUI scan run without an Output file could not be saved once it had finished, and the result list showed names only. The scan view keeps each result's records. v toggles them under each name, during or after the scan. On the finished screen s opens a path prompt (default: the form's Output path, or subenum-<domain> with the format's extension) and saves the results the current filter shows, with the run summary, through the same atomic file writer as a scan. The path's extension picks the format. A failed save is reported and leaves any existing file unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Fixes #332
1 parent 27ccdeb commit daf571a

5 files changed

Lines changed: 313 additions & 25 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99

1010
### Added
11+
- The TUI's finished screen can save the results of a scan run without an Output file: `s` asks for a path (starting from the Output file, or `subenum-<domain>` with the Format's extension), and the file is written as the Output file would have been, in the format its extension names (`.txt`, `.json`, `.jsonl`, `.csv`, otherwise the form's Format). With a filter on, only the results it shows are saved. `v` toggles each result's records under it, during or after the scan, and a text file saved while they are shown carries them, as with `-show-records`. Results past the view's 10,000 are not kept and cannot be saved (#332).
1112
- `subenum mcp` offers a read-only `diff` tool that compares two result sets without sending a DNS query: `previous` and `current` each take a `scan` or `lab_scan` report (or its `results` array) or the contents of a results file in any format, and an optional `types` limits the record types compared. It returns `added`, `missing` and `changed` names with their records, as `structuredContent` too, compared as `subenum diff -records` does, so CDN address rotation behind an unchanged CNAME is not a change; missing names are marked unverified. Its input is bounded by the server's 16 MiB message limit, as `explain_stats`'s is (#333).
1213
- `subenum diff [-records] [-type A,AAAA,CNAME] [-format text|json|jsonl|csv] <previous> <current>` compares two saved results files, in any format subenum writes, without sending a DNS query: `+` for names only in the second file, `-` for names only in the first, and with `-records` `~` for names whose records differ, compared as `-diff-records` does. In JSON, JSONL and CSV the `change` is `added`, `missing` or `changed`; a name is `missing` rather than `removed` because nothing was looked up to show it is gone. It exits `4` when the files differ, and is in the shell completions and the man page (#310).
1314
- `-resume` accepts the pacing flags `-rate`, `-rate-per-resolver`, `-t`, `-timeout`, `-attempts`, `-auto-rate`, `-no-abort` and `-max-duration` next to the state file, so a scan stopped because timeouts piled up can continue at a gentler rate instead of the one that caused them. They apply over the saved command line, a changed value of one is no longer a reason to refuse the resume, and the next state file records them. Flags that change the candidates or the wildcard check stay refused (#309).

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,8 @@ It offers `lab_scan` (the lab scenarios, on a local DNS server, always safe), `s
656656
| `q` | Quit after scan completes |
657657
| `/` | Filter the results by name (during or after a scan); `enter` keeps the filter, `esc` clears it. While typing, every key but `ctrl+c` goes to the filter |
658658
| `t` | Show takeover candidates only (toggle) |
659+
| `v` | Show each result's records under it (toggle) |
660+
| `s` | Save the results after the scan: type a path and press `enter` (`esc` cancels). The extension picks the format (`.txt`, `.json`, `.jsonl`, `.csv`; otherwise the form's Format), and with a filter on only the results it shows are saved |
659661
| `esc` | Clear the filter |
660662

661663
</details>

‎internal/tui/model.go‎

Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"math/rand/v2"
9+
"path/filepath"
910
"strings"
1011
"time"
1112

@@ -47,6 +48,7 @@ type Model struct {
4748
vals formValues // the running scan's settings, for its report
4849
seed uint64
4950
started time.Time
51+
final scan.Event // the event that ended the scan, for a saved file's summary (#332)
5052

5153
// What the Sources and Permute fields add around the scan (#274).
5254
lookups []sourceLookup // the passive sources asked, in order
@@ -254,16 +256,9 @@ func (m Model) beginScan(loaded wordlistLoadedMsg) (tea.Model, tea.Cmd) {
254256
return m, nil
255257
}
256258
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{}
267262
m.lookups, m.from, m.foundBy = loaded.lookups, loaded.from, map[string]int{}
268263
m.entries, m.names, m.pool, m.deadline = entries, nil, pool, time.Time{}
269264

@@ -325,15 +320,40 @@ func (m Model) beginScan(loaded wordlistLoadedMsg) (tea.Model, tea.Cmd) {
325320
return m, listenForEvents(events)
326321
}
327322

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+
328334
func (m Model) updateScan(msg tea.Msg) (tea.Model, tea.Cmd) {
329335
switch msg := msg.(type) {
330336
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+
}
331344
// The result filter (#221) takes its keys first; while it is being
332345
// typed it takes every key but ctrl+c, so q and r can be typed.
333346
if m.scanView.handleFilterKey(msg) {
334347
return m, nil
335348
}
336349
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+
}
337357
case "ctrl+c":
338358
// Once the scan has finished there is nothing to abort; quit
339359
// instead of relabelling a completed scan as "Aborted" (#43).
@@ -399,6 +419,7 @@ func (m Model) updateScan(msg tea.Msg) (tea.Model, tea.Cmd) {
399419
// The scan stopped before testing candidates (an early error such as a
400420
// wildcard zone without Force, #99): close the file without finalizing,
401421
// mirroring the CLI, which skips Finish so no empty JSON array is written.
422+
m.final = ev.Event
402423
m.reportOutputErr(m.finalizeOutput(false))
403424
return m, svCmd
404425
case ev.Kind == scan.EventDone:
@@ -437,6 +458,11 @@ func (m *Model) record(r scan.Result) {
437458
for _, src := range res.Sources {
438459
m.foundBy[src]++
439460
}
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+
}
440466
if m.out != nil {
441467
m.out.Emit(res)
442468
}
@@ -488,12 +514,83 @@ func (m *Model) finishOutput(final scan.Event) {
488514
if main := m.scanView.main; main != nil {
489515
final = *main // the report's counts are the main pass's (#298)
490516
}
517+
m.final = final
491518
if m.out != nil {
492519
m.out.Summary(m.summary(final))
493520
}
494521
m.reportOutputErr(m.finalizeOutput(true))
495522
}
496523

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+
497594
// finalizeOutput closes the optional output file. When finish is true the
498595
// structured writer is finalized first (streamed JSON array closed, CSV
499596
// flushed) and the file replaces any existing one; when false it is discarded

‎internal/tui/output_test.go‎

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
tea "charm.land/bubbletea/v2"
11+
"charm.land/lipgloss/v2"
1112

1213
"github.com/TMHSDigital/subenum/internal/output"
1314
"github.com/TMHSDigital/subenum/internal/scan"
@@ -22,6 +23,19 @@ func runTUIScan(t *testing.T, format output.Format, formatName string, types []s
2223
t.Setenv("XDG_CONFIG_HOME", cfg)
2324
t.Setenv("HOME", cfg)
2425
path := filepath.Join(t.TempDir(), "out."+formatName)
26+
runTUIModel(t, path, format, types)
27+
data, err := os.ReadFile(path)
28+
if err != nil {
29+
t.Fatal(err)
30+
}
31+
return string(data)
32+
}
33+
34+
// runTUIModel runs a simulated scan of www, api and mail through the TUI
35+
// model to completion, writing the Output file path if it is not empty, and
36+
// returns the model on the finished screen.
37+
func runTUIModel(t *testing.T, path string, format output.Format, types []string) Model {
38+
t.Helper()
2539
vals := formValues{
2640
Options: scan.Options{
2741
Domain: "example.com", DNSServer: "8.8.8.8:53", Concurrency: 4, TimeoutMs: 1000, Attempts: 1,
@@ -48,11 +62,109 @@ func runTUIScan(t *testing.T, format output.Format, formatName string, types []s
4862
break
4963
}
5064
}
51-
data, err := os.ReadFile(path)
65+
return m
66+
}
67+
68+
// TestTUISavesResultsAfterTheScan covers #332: a scan run without an Output
69+
// file can still be saved from the finished screen, in the format the path's
70+
// extension names and limited to what the filter shows, and v shows each
71+
// result's records. The prompt keeps the screen within the terminal.
72+
func TestTUISavesResultsAfterTheScan(t *testing.T) {
73+
cfg := t.TempDir() // keep saveConfig away from the real user config dir
74+
t.Setenv("APPDATA", cfg)
75+
t.Setenv("XDG_CONFIG_HOME", cfg)
76+
t.Setenv("HOME", cfg)
77+
m := runTUIModel(t, "", output.FormatText, []string{"A"})
78+
if !m.scanView.done {
79+
t.Fatal("scan did not finish")
80+
}
81+
press := func(keys ...tea.KeyPressMsg) {
82+
t.Helper()
83+
for _, k := range keys {
84+
next, _ := m.updateScan(k)
85+
m = next.(Model)
86+
}
87+
}
88+
typeIn := func(s string) {
89+
t.Helper()
90+
for _, r := range s {
91+
press(typeText(string(r)))
92+
}
93+
}
94+
clearPath := func() {
95+
t.Helper()
96+
for range []rune(m.scanView.savePath) {
97+
press(keyBackspace)
98+
}
99+
}
100+
dir := t.TempDir()
101+
102+
// Records: v toggles them under each result.
103+
press(typeText("v"))
104+
if view := m.scanView.viewport.View(); !strings.Contains(view, "A=") {
105+
t.Errorf("v did not show the records:\n%s", view)
106+
}
107+
108+
// Save everything as CSV; the prompt starts from the form's format.
109+
press(typeText("s"))
110+
if !m.scanView.saving || m.scanView.savePath != "subenum-example.com.txt" {
111+
t.Fatalf("s: saving %v, path %q", m.scanView.saving, m.scanView.savePath)
112+
}
113+
if got := lipgloss.Height(m.content()); got != 30 {
114+
t.Errorf("view with the save prompt is %d lines, want 30", got)
115+
}
116+
clearPath()
117+
all := filepath.Join(dir, "all.csv")
118+
typeIn(all) // q and r in the path must not quit or start a new scan
119+
press(keyEnter)
120+
if m.state != stateScan || m.scanView.saving {
121+
t.Fatalf("after enter: state %v, saving %v", m.state, m.scanView.saving)
122+
}
123+
data, err := os.ReadFile(all)
52124
if err != nil {
53125
t.Fatal(err)
54126
}
55-
return string(data)
127+
if lines := strings.Split(strings.TrimSpace(string(data)), "\n"); len(lines) != 4 || !strings.HasPrefix(lines[0], "subdomain,") {
128+
t.Errorf("CSV is not a header and 3 rows:\n%s", data)
129+
}
130+
if !strings.Contains(m.content(), "Saved 3 results to") {
131+
t.Errorf("no saved notice:\n%s", m.content())
132+
}
133+
134+
// Save what a filter shows, as JSONL, which ends with the summary.
135+
press(typeText("/"))
136+
typeIn("www")
137+
press(keyEnter, typeText("s"))
138+
clearPath()
139+
some := filepath.Join(dir, "www.jsonl")
140+
typeIn(some)
141+
press(keyEnter)
142+
data, err = os.ReadFile(some)
143+
if err != nil {
144+
t.Fatal(err)
145+
}
146+
lines := strings.Split(strings.TrimSpace(string(data)), "\n")
147+
var s output.Summary
148+
if len(lines) != 2 || !strings.Contains(lines[0], `"www.example.com"`) || json.Unmarshal([]byte(lines[1]), &s) != nil || s.Type != "summary" {
149+
t.Errorf("filtered JSONL is not www and the summary:\n%s", data)
150+
}
151+
if !strings.Contains(m.content(), "the 1 of 3 the filter shows") {
152+
t.Errorf("no filtered notice:\n%s", m.content())
153+
}
154+
155+
// esc closes the prompt without saving; a path that cannot be created
156+
// is reported and leaves the exit code alone.
157+
press(typeText("s"), keyEsc)
158+
if m.scanView.saving {
159+
t.Error("esc did not close the prompt")
160+
}
161+
press(typeText("s"))
162+
clearPath()
163+
typeIn(filepath.Join(dir, "missing", "x.txt"))
164+
press(keyEnter)
165+
if !strings.Contains(m.content(), "cannot save results") || exitCode(m) != 0 {
166+
t.Errorf("failed save: exit %d, view:\n%s", exitCode(m), m.content())
167+
}
56168
}
57169

58170
// TestTUIOutputMatchesCLI covers #118: a TUI CSV has the takeover column

0 commit comments

Comments
 (0)