Skip to content

Commit e67e1b9

Browse files
TMHSDigitalclaude
andcommitted
fix(cli): papercuts in flags, usage errors, -version, -tui and -t
- -attempts usage shows its default of 1 - a missing -w or domain is named before the usage text - -version prints before the simulation banner - -tui is only honoured as a parsed flag and warns that other flags are ignored - the worker pool is capped at the jobs the scan can produce, and -t above 10000 warns Fixes #58 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 00f8b5f commit e67e1b9

6 files changed

Lines changed: 118 additions & 18 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- Wildcards that answer from a rotating CDN or load-balancer pool are now filtered under `-force`. The root fingerprint starts from five probes instead of two and, when their answers differ, keeps probing until the pool stops growing; results that share only some records with the fingerprint are re-validated with fresh probes under the same parent. In a hermetic 8-address pool test, filtering went from 12 of 50 wildcard answers to all 50, with no real names lost (#48).
2323
- A wordlist line longer than 64 KB no longer aborts the run with `bufio.Scanner: token too long`. Overlong lines are truncated, skipped and counted with the other invalid entries (#56).
2424
- `tools/wordlist-gen` no longer overwrites an existing output file without the new `-f` flag, exits 1 instead of writing an empty file when nothing is generated, rejects `-combine` prefixes that are not valid DNS labels, and drops generated entries subenum would skip (for example combinations over 63 characters). `make wordlist` and `examples/demo.sh` pass `-f` for the files they own (#59).
25+
- CLI polish: `-h` shows the `-attempts` default, a missing `-w` or domain is named before the usage text, `-version` prints before the simulation banner, `-tui` is only honoured as a flag (not as `-o -tui` or after `--`) and warns that other flags are ignored, and the worker pool is capped at the number of jobs the scan can produce, with a warning above `-t 10000` (#58).
2526
- 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).
2627

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

‎internal/scan/runner.go‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,20 @@ func RecursionCeiling(n, depth int) float64 {
227227
return total
228228
}
229229

230+
// workerCount caps the pool at the most jobs the scan can produce, so a huge
231+
// -t on a short wordlist does not spawn millions of idle goroutines (#58).
232+
func workerCount(cfg Config, maxDepth int) int {
233+
depth := 1
234+
if cfg.Recursive {
235+
depth = maxDepth
236+
}
237+
work := RecursionCeiling(len(cfg.Entries), depth)
238+
if cfg.MaxQueries > 0 {
239+
work = min(work, float64(cfg.MaxQueries))
240+
}
241+
return max(1, int(min(float64(cfg.Concurrency), work)))
242+
}
243+
230244
// job is a single unit of work: a fully qualified domain to test and its depth
231245
// in the recursion tree (initial entries are depth 1).
232246
type job struct {
@@ -517,7 +531,8 @@ func Run(ctx context.Context, cfg Config, events chan<- Event) {
517531
}()
518532

519533
// Worker pool.
520-
for i := 0; i < cfg.Concurrency; i++ {
534+
workers := workerCount(cfg, maxDepth)
535+
for i := 0; i < workers; i++ {
521536
wg.Add(1)
522537
go func() {
523538
defer wg.Done()

‎internal/scan/runner_test.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,3 +1204,24 @@ func TestRunPreflightRetries(t *testing.T) {
12041204
})
12051205
}
12061206
}
1207+
1208+
// TestWorkerCountCapped covers #58: a huge -t never spawns more workers than
1209+
// the scan has jobs.
1210+
func TestWorkerCountCapped(t *testing.T) {
1211+
for _, tc := range []struct {
1212+
cfg Config
1213+
depth int
1214+
want int
1215+
}{
1216+
{Config{Concurrency: 5_000_000, Entries: makeEntries(8)}, 1, 8},
1217+
{Config{Concurrency: 100, Entries: makeEntries(8), Recursive: true}, 2, 72},
1218+
{Config{Concurrency: 100, Entries: makeEntries(1000), MaxQueries: 10}, 1, 10},
1219+
{Config{Concurrency: 4, Entries: makeEntries(1000)}, 1, 4},
1220+
{Config{Concurrency: 4}, 1, 1},
1221+
} {
1222+
if got := workerCount(tc.cfg, tc.depth); got != tc.want {
1223+
t.Errorf("workerCount(t=%d, n=%d, rec=%v, depth=%d, max=%d) = %d, want %d",
1224+
tc.cfg.Concurrency, len(tc.cfg.Entries), tc.cfg.Recursive, tc.depth, tc.cfg.MaxQueries, got, tc.want)
1225+
}
1226+
}
1227+
}

‎main.go‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ import (
4545
const (
4646
ProgramName = "subenum"
4747
DefaultDNSServer = "8.8.8.8:53"
48+
49+
// highConcurrency is the -t above which the CLI warns (#58).
50+
highConcurrency = 10000
4851
)
4952

5053
// Version is the release identifier. Makefile, CI and the Dockerfile set it
@@ -75,18 +78,12 @@ func formatVersion() string {
7578
}
7679

7780
func main() {
78-
// Fast-path: if -tui is the first argument, launch the TUI immediately
79-
// before flag.Parse() consumes everything else.
80-
for _, arg := range os.Args[1:] {
81-
if arg == "-tui" || arg == "--tui" {
82-
os.Exit(tui.Start())
83-
}
84-
}
8581
os.Exit(run())
8682
}
8783

8884
// cliFlags holds all parsed command-line flag values.
8985
type cliFlags struct {
86+
tui bool
9087
wordlistFile string
9188
domainList string
9289
concurrency int
@@ -117,7 +114,7 @@ type cliFlags struct {
117114
func parseFlags(args []string) (cliFlags, []string, *flag.FlagSet, error) {
118115
var f cliFlags
119116
fs := flag.NewFlagSet(ProgramName, flag.ContinueOnError)
120-
fs.Bool("tui", false, "Launch the interactive terminal UI (all other flags are ignored)")
117+
fs.BoolVar(&f.tui, "tui", false, "Launch the interactive terminal UI (all other flags are ignored)")
121118
fs.StringVar(&f.wordlistFile, "w", "", "Path to the wordlist file (- for stdin)")
122119
fs.StringVar(&f.domainList, "dL", "", "File of apex domains to scan, one per line (- for stdin); replaces the <domain> argument")
123120
fs.IntVar(&f.concurrency, "t", 100, "Number of concurrent workers")
@@ -130,7 +127,7 @@ func parseFlags(args []string) (cliFlags, []string, *flag.FlagSet, error) {
130127
fs.IntVar(&f.testHitRate, "hit-rate", 15, "In simulation mode, percentage of names that resolve (1-100)")
131128
fs.Uint64Var(&f.seed, "seed", 0, "In simulation mode, seed for reproducible results (0 = random; the seed used is printed)")
132129
fs.StringVar(&f.outputFile, "o", "", "Write results to file (in addition to stdout)")
133-
fs.IntVar(&f.attempts, "attempts", 0, "Total DNS resolution attempts per subdomain (1 = no retry)")
130+
fs.IntVar(&f.attempts, "attempts", 0, "Total DNS resolution attempts per subdomain, 1 = no retry (default 1)")
134131
fs.IntVar(&f.retries, "retries", 0, "Deprecated: use -attempts instead")
135132
fs.BoolVar(&f.force, "force", false, "Continue scanning even if wildcard DNS is detected")
136133
fs.StringVar(&f.format, "format", "text", "Output format: text, json, jsonl, or csv")
@@ -195,6 +192,13 @@ func endsWithTerminator(fs *flag.FlagSet, consumed []string) bool {
195192

196193
func validateFlags(f cliFlags, positionals []string, fs *flag.FlagSet, out *output.Writer, maxAttempts int) (string, bool) {
197194
if f.wordlistFile == "" || (len(positionals) == 0 && f.domainList == "") {
195+
// Say what is missing before the full flag list (#58).
196+
if f.wordlistFile == "" {
197+
out.Error("-w <wordlist> is required")
198+
}
199+
if len(positionals) == 0 && f.domainList == "" {
200+
out.Error("missing <domain> (or -dL <domains_file>)")
201+
}
198202
fmt.Fprintln(os.Stderr, "Usage: subenum -w <wordlist_file> [options] <domain>")
199203
fmt.Fprintln(os.Stderr, " subenum -w <wordlist_file> [options] -dL <domains_file>")
200204
fs.SetOutput(os.Stderr)
@@ -217,6 +221,9 @@ func validateFlags(f cliFlags, positionals []string, fs *flag.FlagSet, out *outp
217221
out.Error("Concurrency level (-t) must be greater than 0")
218222
return "", false
219223
}
224+
if f.concurrency > highConcurrency {
225+
out.Info("Warning: -t %d is very high; every worker can hold a socket open, and most resolvers rate-limit long before this", f.concurrency)
226+
}
220227
if f.timeoutMs <= 0 {
221228
out.Error("Timeout (-timeout) must be greater than 0")
222229
return "", false
@@ -383,6 +390,21 @@ func run() (code int) {
383390
return 2
384391
}
385392

393+
// -tui counts only as a real flag, not as the value of another flag
394+
// (-o -tui) or an argument after "--" (#58).
395+
if f.tui {
396+
if fs.NFlag() > 1 || len(positionals) > 0 {
397+
fmt.Fprintln(os.Stderr, "Warning: -tui ignores every other flag and argument; set them in the form instead")
398+
}
399+
return tui.Start()
400+
}
401+
402+
// -version answers before anything else prints, banners included (#58).
403+
if f.showVersion {
404+
fmt.Println(formatVersion())
405+
return 0
406+
}
407+
386408
format, formatErr := output.ParseFormat(f.format)
387409
recordTypes, typesErr := dns.ParseTypes(f.recordTypes)
388410
maxAttempts, err := resolveAttempts(f.attempts, f.retries)
@@ -413,14 +435,6 @@ func run() (code int) {
413435
out.Info("")
414436
}
415437

416-
if f.showVersion {
417-
fmt.Println(formatVersion())
418-
if f.testMode {
419-
fmt.Println("Running in SIMULATION mode")
420-
}
421-
return 0
422-
}
423-
424438
domain, ok := validateFlags(f, positionals, fs, out, maxAttempts)
425439
if !ok {
426440
return 1

‎main_e2e_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,21 @@ func TestE2EDomainFormsGiveIdenticalOutput(t *testing.T) {
282282
}
283283
}
284284
}
285+
286+
// TestE2ECLIPolish covers #58: missing arguments are named before the usage
287+
// text, and -version prints before any simulation banner.
288+
func TestE2ECLIPolish(t *testing.T) {
289+
_, out := runCLIMerged(t, "example.com")
290+
if !strings.Contains(out, "Error: -w <wordlist> is required") {
291+
t.Errorf("missing -w not named:\n%s", out)
292+
}
293+
wl := writeFile(t, "wl.txt", e2eWords)
294+
_, out = runCLIMerged(t, "-w", wl)
295+
if !strings.Contains(out, "Error: missing <domain>") {
296+
t.Errorf("missing domain not named:\n%s", out)
297+
}
298+
code, out := runCLIMerged(t, "-simulate", "-version")
299+
if code != 0 || !strings.HasPrefix(out, ProgramName) || strings.Contains(out, "SIMULATION MODE ACTIVE") {
300+
t.Errorf("-simulate -version: exit %d, output:\n%s", code, out)
301+
}
302+
}

‎main_test.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"path/filepath"
77
"reflect"
8+
"strings"
89
"testing"
910

1011
"github.com/TMHSDigital/subenum/internal/output"
@@ -159,3 +160,33 @@ func TestFormatVersion(t *testing.T) {
159160
t.Errorf("no ldflags: got %q, want %q", got, "subenum dev")
160161
}
161162
}
163+
164+
// TestParseFlagsTUIOnlyAsFlag covers #58: -tui is honoured only as a flag,
165+
// not as another flag's value or an argument after "--".
166+
func TestParseFlagsTUIOnlyAsFlag(t *testing.T) {
167+
for _, tc := range []struct {
168+
args []string
169+
want bool
170+
}{
171+
{[]string{"-tui"}, true},
172+
{[]string{"-v", "--tui"}, true},
173+
{[]string{"-o", "-tui", "example.com"}, false},
174+
{[]string{"-w", "wl.txt", "--", "-tui"}, false},
175+
} {
176+
f, _, _, err := parseFlags(tc.args)
177+
if err != nil {
178+
t.Fatalf("%v: %v", tc.args, err)
179+
}
180+
if f.tui != tc.want {
181+
t.Errorf("%v: tui = %v, want %v", tc.args, f.tui, tc.want)
182+
}
183+
}
184+
}
185+
186+
// TestAttemptsUsageShowsDefault covers #58: -h shows the effective default.
187+
func TestAttemptsUsageShowsDefault(t *testing.T) {
188+
_, _, fs, _ := parseFlags(nil)
189+
if u := fs.Lookup("attempts").Usage; !strings.Contains(u, "default 1") {
190+
t.Errorf("-attempts usage %q does not show the default", u)
191+
}
192+
}

0 commit comments

Comments
 (0)