-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkers.go
More file actions
86 lines (75 loc) · 1.74 KB
/
workers.go
File metadata and controls
86 lines (75 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"bufio"
"fmt"
"net/http"
"os"
"strings"
"sync"
"time"
)
func (c *config) runDirBrute() error {
jobs := make(chan string, c.threads)
var wg sync.WaitGroup
var mu sync.Mutex
client := &http.Client{
Timeout: time.Duration(c.timeout) * time.Second,
}
// open wordlist file
file, err := os.Open(c.wordlistPath)
if err != nil {
return fmt.Errorf("Error opening wordlist: %w", err)
}
defer file.Close()
// Count real paths (skipping comments) for the bar total
scanner := bufio.NewScanner(file)
totalPaths := 0
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line != "" && !strings.HasPrefix(line, "#") {
totalPaths++
}
}
if err := scanner.Err(); err != nil {
return err
}
file.Seek(0, 0) // Reset to start of file
bar := newProgressBar(totalPaths)
// start worker pool
for i := 0; i < c.threads; i++ {
// wg.Go simplfies traditional goroutines management (added in go 1.25)
wg.Go(func() {
for path := range jobs {
u := fmt.Sprintf("%s/%s", strings.TrimRight(c.targetURL, "/"),
strings.TrimLeft(path, "/"))
resp, err := client.Get(u)
if err == nil {
if resp.StatusCode != 404 {
/* ensures only one thread can clear the bar and print at a time */
mu.Lock()
bar.Clear()
fmt.Printf("[%d] %s\n", resp.StatusCode, u)
mu.Unlock()
}
resp.Body.Close()
}
bar.Add(1)
}
})
}
scanner = bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
jobs <- line
}
if err := scanner.Err(); err != nil {
return err
}
close(jobs)
wg.Wait() // Ensures waitgroup counter is zero before exiting
fmt.Println()
return nil
}