Skip to content

Commit d4fd53b

Browse files
TMHSDigitalclaude
andcommitted
fix(tui): async wordlist load, Failed status, non-zero exit on failure
Reject - as a wordlist (it would block on the stdin Bubble Tea owns), load the wordlist in a tea.Cmd with a loading line, show "Failed: <reason>" when a scan ends with an error instead of "Aborted", and return exit code 1 when the last scan failed. Fixes #55 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1281a45 commit d4fd53b

5 files changed

Lines changed: 168 additions & 23 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- `-max-queries` now stops memory growth in recursive mode: once the cap is reached, remaining and future candidates are counted as skipped without being built or recorded (#51).
1818
- No default command sends DNS traffic at a domain you did not name. `docker compose up`, `make simulate*` and `examples/demo.sh` run in simulation mode; `make run`, `run-verbose`, `run-custom` and `docker-run` require `DOMAIN=` and stop with a message without it; `demo.sh -l <domain>` opts into a live run. `demo.sh` now uses `set -euo pipefail`, `docker-run` uses `$(CURDIR)` so it works under Windows make, and `.PHONY` lists every target (#81).
1919
- Target domains are normalized the same way in the positional argument, `-dL` and the TUI: trimmed, lowercased, trailing dot dropped, so `Example.COM.` no longer yields `api.Example.COM.`. A pasted URL, path, `:port` or leading `*.` is stripped with a one-line note, IDNs such as `bücher.de` are converted to punycode, underscores are accepted as in wordlist entries, and an email address gets a targeted hint instead of `invalid domain format` (#54).
20+
- TUI: a `-` wordlist is rejected on the form instead of freezing the UI on stdin, the wordlist loads in the background with a "Loading wordlist..." line so large lists no longer block input, a scan that ends with an error (preflight failure, wildcard abort, reliability abort) shows "Failed: <reason>" instead of "Aborted", and the TUI exits 1 when the last scan failed (#55).
2021
- Release binaries are built with `CGO_ENABLED=0 -trimpath -s -w`, so `subenum-linux-amd64` is statically linked again and CI fails if a Linux asset is not (#46).
2122

2223
## [0.8.0] - 2026-10-04

‎internal/tui/form.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/TMHSDigital/subenum/internal/dns"
1515
"github.com/TMHSDigital/subenum/internal/output"
1616
"github.com/TMHSDigital/subenum/internal/validate"
17+
"github.com/TMHSDigital/subenum/internal/wordlist"
1718
)
1819

1920
// Field order - Simulate is now field 2 so it's reachable in 2 tabs.
@@ -80,6 +81,7 @@ type formModel struct {
8081
toggles [4]bool // [simulate, force, recursive, noAbort]
8182
focus int
8283
err string
84+
loading bool // wordlist is loading in the background; ctrl+r is ignored
8385
width int
8486
}
8587

@@ -323,6 +325,9 @@ func (m formModel) View() string {
323325
if m.err != "" {
324326
b.WriteString("\n" + errorStyle.Render(" ✗ "+m.err) + "\n")
325327
}
328+
if m.loading {
329+
b.WriteString("\n" + dimStyle.Render(" Loading wordlist...") + "\n")
330+
}
326331

327332
b.WriteString(hintStyle.Render("\n tab/↑↓ navigate • space toggle • ctrl+r run • ctrl+c quit"))
328333

@@ -365,6 +370,10 @@ func (m *formModel) validate() (formValues, string) {
365370
if wl == "" {
366371
return formValues{}, "Wordlist path is required"
367372
}
373+
// The TUI owns stdin, so reading a wordlist from it would block forever (#55).
374+
if wl == wordlist.Stdin {
375+
return formValues{}, "Wordlist from standard input (-) is not supported in the TUI; enter a file path"
376+
}
368377
dnsServer := strings.TrimSpace(m.inputs[3].Value())
369378
if dnsServer == "" {
370379
dnsServer = "8.8.8.8:53"

‎internal/tui/model.go‎

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,20 @@ func New() Model {
4646
}
4747
}
4848

49-
// Start runs the TUI and returns an exit code (0 = ok, 1 = error).
49+
// Start runs the TUI and returns an exit code: 1 if the TUI itself failed or
50+
// the last scan failed (#55), else 0.
5051
func Start() int {
5152
p := tea.NewProgram(New(), tea.WithAltScreen())
52-
if _, err := p.Run(); err != nil {
53+
final, err := p.Run()
54+
if err != nil {
55+
return 1
56+
}
57+
return exitCode(final)
58+
}
59+
60+
// exitCode maps the final model to the process exit code.
61+
func exitCode(final tea.Model) int {
62+
if m, ok := final.(Model); ok && m.scanView.failed() {
5363
return 1
5464
}
5565
return 0
@@ -80,18 +90,32 @@ func (m Model) updateForm(msg tea.Msg) (tea.Model, tea.Cmd) {
8090
return m, tea.Quit
8191

8292
case "ctrl+r":
93+
if m.form.loading {
94+
return m, nil
95+
}
8396
vals, errStr := m.form.validate()
8497
if errStr != "" {
8598
m.form.err = errStr
8699
return m, nil
87100
}
88101
m.form.err = ""
89-
// Kick off the scan.
90-
return m, func() tea.Msg { return startScanMsg{cfg: vals} }
102+
m.form.loading = true
103+
// Load the wordlist off the event loop so a large list cannot
104+
// freeze the UI (#55); beginScan runs when it arrives.
105+
return m, loadWordlistCmd(vals)
91106
}
92107

93-
case startScanMsg:
94-
return m.beginScan(msg.cfg)
108+
case wordlistLoadedMsg:
109+
m.form.loading = false
110+
if msg.err != nil {
111+
m.form.err = "cannot read wordlist: " + msg.err.Error()
112+
return m, nil
113+
}
114+
if len(msg.entries) == 0 {
115+
m.form.err = "wordlist has no valid entries"
116+
return m, nil
117+
}
118+
return m.beginScan(msg.cfg, msg.entries)
95119

96120
case tea.WindowSizeMsg:
97121
m.width = msg.Width
@@ -103,25 +127,21 @@ func (m Model) updateForm(msg tea.Msg) (tea.Model, tea.Cmd) {
103127
return m, cmd
104128
}
105129

106-
func (m Model) beginScan(vals formValues) (tea.Model, tea.Cmd) {
130+
// loadWordlistCmd reads and normalizes the wordlist in a tea.Cmd goroutine.
131+
func loadWordlistCmd(vals formValues) tea.Cmd {
132+
return func() tea.Msg {
133+
entries, _, _, err := wordlist.LoadWordlist(vals.wordlist, vals.domain)
134+
return wordlistLoadedMsg{cfg: vals, entries: entries, err: err}
135+
}
136+
}
137+
138+
func (m Model) beginScan(vals formValues, entries []string) (tea.Model, tea.Cmd) {
107139
// Create the context up front so cancel is always assigned to the model
108140
// before any early return. This satisfies static analysis tools that
109141
// require the cancellation function to be demonstrably reachable.
110142
ctx, cancel := context.WithCancel(context.Background())
111143
m.cancel = cancel
112144

113-
entries, _, _, err := wordlist.LoadWordlist(vals.wordlist, vals.domain)
114-
if err != nil {
115-
cancel() // explicitly cancel before returning on error
116-
m.form.err = "cannot read wordlist: " + err.Error()
117-
return m, nil
118-
}
119-
if len(entries) == 0 {
120-
cancel()
121-
m.form.err = "wordlist has no valid entries"
122-
return m, nil
123-
}
124-
125145
seed := rand.Uint64()
126146

127147
// Open the optional output file before switching screens so a create error
@@ -140,6 +160,7 @@ func (m Model) beginScan(vals formValues) (tea.Model, tea.Cmd) {
140160

141161
m.state = stateScan
142162
m.scanView = newScanViewModel(m.width, m.height, vals.simulate)
163+
m.scanView.noAbort = vals.noAbort
143164

144165
cfg := scan.Config{
145166
Domain: vals.domain,

‎internal/tui/model_test.go‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,92 @@ func TestFinalizeOutputReportsCloseError(t *testing.T) {
120120
t.Fatal("finalizeOutput returned nil although the results file could not be written")
121121
}
122122
}
123+
124+
// TestValidateRejectsStdinWordlist covers #55: "-" would read the stdin that
125+
// Bubble Tea owns and freeze the UI.
126+
func TestValidateRejectsStdinWordlist(t *testing.T) {
127+
m := newFormModel(savedConfig{})
128+
m.inputs[0].SetValue("example.com")
129+
m.inputs[1].SetValue("-")
130+
if _, errStr := m.validate(); !strings.Contains(errStr, "standard input") {
131+
t.Fatalf("validate() error = %q, want a standard-input rejection", errStr)
132+
}
133+
}
134+
135+
// TestRunLoadsWordlistAsync covers #55: ctrl+r returns immediately with the
136+
// wordlist load deferred to a tea.Cmd, and the loaded message starts the scan.
137+
func TestRunLoadsWordlistAsync(t *testing.T) {
138+
redirectConfig(t) // beginScan saves the form values
139+
wl := filepath.Join(t.TempDir(), "wl.txt")
140+
if err := os.WriteFile(wl, []byte("www\nmail\n"), 0o644); err != nil {
141+
t.Fatal(err)
142+
}
143+
m := Model{state: stateForm, form: newFormModel(savedConfig{})}
144+
m.form.inputs[0].SetValue("example.com")
145+
m.form.inputs[1].SetValue(wl)
146+
m.form.inputs[10].SetValue("") // no output file
147+
m.form.toggles[0] = true // simulate: the started scan sends no DNS traffic
148+
149+
next, cmd := m.updateForm(tea.KeyMsg{Type: tea.KeyCtrlR})
150+
nm := next.(Model)
151+
if nm.form.err != "" {
152+
t.Fatalf("form error: %s", nm.form.err)
153+
}
154+
if !nm.form.loading || nm.state != stateForm || cmd == nil {
155+
t.Fatalf("loading=%v state=%v cmd=%v, want a pending load on the form", nm.form.loading, nm.state, cmd != nil)
156+
}
157+
if !strings.Contains(nm.form.View(), "Loading wordlist") {
158+
t.Error("form does not show the loading state")
159+
}
160+
// A second ctrl+r while loading is ignored.
161+
if _, again := nm.updateForm(tea.KeyMsg{Type: tea.KeyCtrlR}); again != nil {
162+
t.Error("ctrl+r during load started a second load")
163+
}
164+
165+
loaded, ok := cmd().(wordlistLoadedMsg)
166+
if !ok || loaded.err != nil || len(loaded.entries) != 2 {
167+
t.Fatalf("load result = %+v", loaded)
168+
}
169+
started, _ := nm.updateForm(loaded)
170+
sm := started.(Model)
171+
defer sm.cancel()
172+
if sm.state != stateScan || sm.form.loading {
173+
t.Fatalf("state=%v loading=%v after load, want the scan screen", sm.state, sm.form.loading)
174+
}
175+
}
176+
177+
// TestScanFailureStatusAndExitCode covers #55: a scan that ends with an error
178+
// shows "Failed", not "Aborted", and the TUI exits non-zero.
179+
func TestScanFailureStatusAndExitCode(t *testing.T) {
180+
update := func(sv scanViewModel, msgs ...tea.Msg) scanViewModel {
181+
for _, msg := range msgs {
182+
sv, _ = sv.Update(msg)
183+
}
184+
return sv
185+
}
186+
187+
// Preflight failure: error, then the channel closes without EventDone.
188+
sv := update(newScanViewModel(120, 40, false), errorMsg{text: "resolver failed preflight"}, abortedMsg{})
189+
if !sv.failed() || !strings.Contains(sv.View(), "Failed: resolver failed preflight") || strings.Contains(sv.View(), "Aborted") {
190+
t.Errorf("preflight failure not shown as Failed:\n%s", sv.View())
191+
}
192+
if code := exitCode(Model{scanView: sv}); code != 1 {
193+
t.Errorf("exit code = %d, want 1", code)
194+
}
195+
196+
// Reliability warning under -no-abort with a finished scan is not a failure.
197+
nv := newScanViewModel(120, 40, false)
198+
nv.noAbort = true
199+
nv = update(nv, errorMsg{text: "warning: 30% failed"}, doneMsg{processed: 5, total: 5})
200+
if nv.failed() || exitCode(Model{scanView: nv}) != 0 {
201+
t.Error("-no-abort warning counted as a failure")
202+
}
203+
204+
// A user abort stays "Aborted" and exits 0.
205+
av := newScanViewModel(120, 40, false)
206+
av.aborted = true
207+
av = update(av, doneMsg{processed: 1, total: 5})
208+
if av.failed() || !strings.Contains(av.View(), "Aborted") || exitCode(Model{scanView: av}) != 0 {
209+
t.Errorf("user abort mis-rendered:\n%s", av.View())
210+
}
211+
}

‎internal/tui/scan_view.go‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ type scanViewModel struct {
5151
found int64
5252
stats scan.Stats
5353
done bool
54-
aborted bool
54+
aborted bool // the user pressed ctrl+c
55+
closed bool // the scan ended without EventDone (preflight, wildcard abort)
56+
errText string // first scan error; the reason shown for a failed scan
57+
noAbort bool // -no-abort: an error with a finished scan is only a warning
5558
width int
5659
height int
5760
simMode bool
@@ -159,6 +162,9 @@ func (m scanViewModel) Update(msg tea.Msg) (scanViewModel, tea.Cmd) {
159162

160163
case errorMsg:
161164
m.addMessage(errorStyle.Render("✗ " + msg.text))
165+
if m.errText == "" {
166+
m.errText = msg.text
167+
}
162168

163169
case doneMsg:
164170
m.done = true
@@ -169,7 +175,7 @@ func (m scanViewModel) Update(msg tea.Msg) (scanViewModel, tea.Cmd) {
169175
m.refresh()
170176

171177
case abortedMsg:
172-
m.aborted = true
178+
m.closed = true
173179
m.done = true
174180
m.refresh()
175181
}
@@ -207,7 +213,14 @@ func (m scanViewModel) View() string {
207213

208214
// Status line
209215
switch {
210-
case m.done && m.aborted:
216+
case m.failed():
217+
b.WriteString(errorStyle.Render("Failed: "+m.errText) + "\n")
218+
b.WriteString(dimStyle.Render(fmt.Sprintf(
219+
"processed %d/%d - found %d - nxdomain %d - timeout %d - refused %d - other %d - wildcard-filtered %d",
220+
m.processed, m.total, m.found, m.stats.NXDomain, m.stats.Timeout, m.stats.Refused, m.stats.Other, m.stats.WildcardFiltered,
221+
)) + "\n")
222+
b.WriteString(hintStyle.Render(" r new scan • q quit"))
223+
case m.done && (m.aborted || m.closed):
211224
b.WriteString(dimStyle.Render(fmt.Sprintf(
212225
"Aborted - processed %d/%d - found %d - nxdomain %d - timeout %d - refused %d - other %d - wildcard-filtered %d",
213226
m.processed, m.total, m.found, m.stats.NXDomain, m.stats.Timeout, m.stats.Refused, m.stats.Other, m.stats.WildcardFiltered,
@@ -230,6 +243,14 @@ func (m scanViewModel) View() string {
230243
return b.String()
231244
}
232245

246+
// failed reports whether the finished scan failed, mirroring the CLI exit
247+
// code: an error fails the scan unless it finished under -no-abort. A scan
248+
// that ended without EventDone after an error (preflight failure, wildcard
249+
// abort) is a failure, not a user abort (#55).
250+
func (m scanViewModel) failed() bool {
251+
return m.done && m.errText != "" && (m.closed || !m.noAbort)
252+
}
253+
233254
// Event message types for Bubble Tea.
234255
type resultMsg struct {
235256
domain string
@@ -243,4 +264,8 @@ type doneMsg struct {
243264
stats scan.Stats
244265
}
245266
type abortedMsg struct{}
246-
type startScanMsg struct{ cfg formValues }
267+
type wordlistLoadedMsg struct {
268+
cfg formValues
269+
entries []string
270+
err error
271+
}

0 commit comments

Comments
 (0)