fix: data races and slice preallocation in dirlist and dnslist

add mutex protection for concurrent slice appends, preallocate result
slices with reasonable capacity, use logger instead of direct file i/o
This commit is contained in:
Celeste Hickenlooper
2026-01-02 22:56:06 -08:00
parent 6d505b90a3
commit 534d2605fd
2 changed files with 11 additions and 10 deletions

View File

@@ -99,9 +99,10 @@ func Dirlist(size string, url string, timeout time.Duration, threads int, logdir
}
var wg sync.WaitGroup
var mu sync.Mutex
wg.Add(threads)
results := []DirectoryResult{}
results := make([]DirectoryResult, 0, 64)
for thread := 0; thread < threads; thread++ {
go func(thread int) {
defer wg.Done()
@@ -119,7 +120,6 @@ func Dirlist(size string, url string, timeout time.Duration, threads int, logdir
}
if resp.StatusCode != 404 && resp.StatusCode != 403 {
// log url, directory, and status code
dirlog.Infof("%s [%s]", styles.Status.Render(strconv.Itoa(resp.StatusCode)), styles.Highlight.Render(directory))
if logdir != "" {
logger.Write(sanitizedURL, logdir, fmt.Sprintf("%s [%s]\n", strconv.Itoa(resp.StatusCode), directory))
@@ -129,7 +129,9 @@ func Dirlist(size string, url string, timeout time.Duration, threads int, logdir
Url: resp.Request.URL.String(),
StatusCode: resp.StatusCode,
}
mu.Lock()
results = append(results, result)
mu.Unlock()
}
}
}(thread)

View File

@@ -93,9 +93,10 @@ func Dnslist(size string, url string, timeout time.Duration, threads int, logdir
}
var wg sync.WaitGroup
var mu sync.Mutex
wg.Add(threads)
urls := []string{}
urls := make([]string, 0, 64)
for thread := 0; thread < threads; thread++ {
go func(thread int) {
defer wg.Done()
@@ -110,17 +111,13 @@ func Dnslist(size string, url string, timeout time.Duration, threads int, logdir
if err != nil {
log.Debugf("Error %s: %s", domain, err)
} else {
mu.Lock()
urls = append(urls, resp.Request.URL.String())
mu.Unlock()
dnslog.Infof("%s %s.%s", styles.Status.Render("[http]"), styles.Highlight.Render(domain), sanitizedURL)
if logdir != "" {
f, err := os.OpenFile(logdir+"/"+sanitizedURL+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Errorf("Error creating log file: %s", err)
return
}
defer f.Close()
f.WriteString(fmt.Sprintf("[http] %s.%s\n", domain, sanitizedURL))
logger.Write(sanitizedURL, logdir, fmt.Sprintf("[http] %s.%s\n", domain, sanitizedURL))
}
}
@@ -128,7 +125,9 @@ func Dnslist(size string, url string, timeout time.Duration, threads int, logdir
if err != nil {
log.Debugf("Error %s: %s", domain, err)
} else {
mu.Lock()
urls = append(urls, resp.Request.URL.String())
mu.Unlock()
dnslog.Infof("%s %s.%s", styles.Status.Render("[https]"), styles.Highlight.Render(domain), sanitizedURL)
if logdir != "" {
logger.Write(sanitizedURL, logdir, fmt.Sprintf("[https] %s.%s\n", domain, sanitizedURL))