Skip to content

Commit 0644b21

Browse files
committed
Refactor main function to improve error handling and exit strategy
- Introduced a new `run` function to encapsulate the main logic and return exit codes instead of using `os.Exit`. - Updated error handling to return appropriate exit codes for various validation failures, enhancing the program's robustness.
1 parent 1051b1e commit 0644b21

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

main.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ func validateDomain(domain string) error {
7676
}
7777

7878
func main() {
79+
os.Exit(run())
80+
}
81+
82+
func run() int {
7983
wordlistFile := flag.String("w", "", "Path to the wordlist file")
8084
concurrency := flag.Int("t", 100, "Number of concurrent workers")
8185
timeoutMs := flag.Int("timeout", 1000, "DNS lookup timeout in milliseconds")
@@ -97,7 +101,7 @@ func main() {
97101

98102
if err != nil {
99103
out.Error("%v", err)
100-
os.Exit(1)
104+
return 1
101105
}
102106

103107
if *testMode {
@@ -114,46 +118,46 @@ func main() {
114118
if *testMode {
115119
fmt.Fprintln(os.Stderr, "Running in SIMULATION mode")
116120
}
117-
os.Exit(0)
121+
return 0
118122
}
119123

120124
if *wordlistFile == "" || flag.NArg() == 0 {
121125
fmt.Fprintln(os.Stderr, "Usage: subenum -w <wordlist_file> [options] <domain>")
122126
flag.PrintDefaults()
123-
os.Exit(1)
127+
return 1
124128
}
125129

126130
if *concurrency <= 0 {
127131
out.Error("Concurrency level (-t) must be greater than 0")
128-
os.Exit(1)
132+
return 1
129133
}
130134

131135
if *timeoutMs <= 0 {
132136
out.Error("Timeout (-timeout) must be greater than 0")
133-
os.Exit(1)
137+
return 1
134138
}
135139

136140
if *testHitRate < 1 || *testHitRate > 100 {
137141
out.Error("Hit rate (-hit-rate) must be between 1 and 100")
138-
os.Exit(1)
142+
return 1
139143
}
140144

141145
if maxAttempts < 1 {
142146
out.Error("Attempts (-attempts) must be at least 1")
143-
os.Exit(1)
147+
return 1
144148
}
145149

146150
if !*testMode {
147151
if err := validateDNSServer(*dnsServer); err != nil {
148152
out.Error("DNS server %s: %v", *dnsServer, err)
149-
os.Exit(1)
153+
return 1
150154
}
151155
}
152156

153157
domain := flag.Arg(0)
154158
if err := validateDomain(domain); err != nil {
155159
out.Error("%v", err)
156-
os.Exit(1)
160+
return 1
157161
}
158162

159163
timeout := time.Duration(*timeoutMs) * time.Millisecond
@@ -163,7 +167,7 @@ func main() {
163167
f, err := os.Create(*outputFile)
164168
if err != nil {
165169
out.Error("creating output file: %v", err)
166-
os.Exit(1)
170+
return 1
167171
}
168172
outWriter = bufio.NewWriter(f)
169173
out = output.New(outWriter, *testMode)
@@ -204,13 +208,13 @@ func main() {
204208
isWildcard, err := dns.CheckWildcard(context.Background(), domain, timeout, *dnsServer)
205209
if err != nil {
206210
out.Error("wildcard detection failed: %v", err)
207-
os.Exit(1)
211+
return 1
208212
}
209213
if isWildcard {
210214
out.Info("WARNING: Wildcard DNS detected for %s — all subdomains resolve.", domain)
211215
if !*force {
212216
out.Info("Results would be meaningless. Use -force to scan anyway.")
213-
os.Exit(1)
217+
return 1
214218
}
215219
out.Info("Continuing because -force is set. Results may contain false positives.")
216220
}
@@ -219,7 +223,7 @@ func main() {
219223
entries, duplicates, err := wordlist.LoadWordlist(*wordlistFile)
220224
if err != nil {
221225
out.Error("reading wordlist file: %v", err)
222-
os.Exit(1)
226+
return 1
223227
}
224228

225229
totalWords := int64(len(entries))
@@ -332,6 +336,7 @@ done:
332336
out.Info("This mode is intended for educational and testing purposes only.")
333337
}
334338
}
339+
return 0
335340
}
336341

337342
// resolveAttempts merges the -attempts and deprecated -retries flags.

0 commit comments

Comments
 (0)