Skip to content

Commit a499dab

Browse files
committed
Refactor tests, no more global state, fixed lack of worker count bug
1 parent a4e2d4c commit a499dab

File tree

9 files changed

+170
-143
lines changed

9 files changed

+170
-143
lines changed

librecursebuster/logic.go

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,27 @@ import (
1010
)
1111

1212
//ManageRequests handles the request workers
13-
func ManageRequests() {
13+
func (gState *State) ManageRequests() {
1414
//manages net request workers
1515
for {
1616
page := <-gState.Chans.pagesChan
1717
if gState.Blacklist[page.URL] {
1818
gState.wg.Done()
19-
PrintOutput(fmt.Sprintf("Not testing blacklisted URL: %s", page.URL), Info, 0)
19+
gState.PrintOutput(fmt.Sprintf("Not testing blacklisted URL: %s", page.URL), Info, 0)
2020
continue
2121
}
2222
for _, method := range gState.Methods {
2323
if page.Result == nil && !gState.Cfg.NoBase {
2424
gState.Chans.workersChan <- struct{}{}
2525
gState.wg.Add(1)
26-
go testURL(method, page.URL, gState.Client)
26+
go gState.testURL(method, page.URL, gState.Client)
2727
}
2828
if gState.Cfg.Wordlist != "" && string(page.URL[len(page.URL)-1]) == "/" { //if we are testing a directory
2929

3030
//check for wildcard response
3131

3232
// maxDirs <- struct{}{}
33-
dirBust(page)
33+
gState.dirBust(page)
3434
}
3535
}
3636
gState.wg.Done()
@@ -39,16 +39,19 @@ func ManageRequests() {
3939
}
4040

4141
//ManageNewURLs will take in any URL, and decide if it should be added to the queue for bustin', or if we discovered something new
42-
func ManageNewURLs() {
42+
func (gState *State) ManageNewURLs() {
4343
//decides on whether to add to the directory list, or add to file output
4444
for {
45-
candidate := <-gState.Chans.newPagesChan
45+
candidate, more := <-gState.Chans.newPagesChan
46+
if !more {
47+
return
48+
}
4649
//check the candidate is an actual URL
4750
u, err := url.Parse(strings.TrimSpace(candidate.URL))
4851

4952
if err != nil {
5053
gState.wg.Done()
51-
PrintOutput(err.Error(), Error, 0)
54+
gState.PrintOutput(err.Error(), Error, 0)
5255
continue //probably a better way of doing this
5356
}
5457

@@ -68,7 +71,7 @@ func ManageNewURLs() {
6871
gState.CMut.Unlock()
6972
gState.wg.Add(1)
7073
gState.Chans.pagesChan <- SpiderPage{URL: actualURL, Reference: candidate.Reference, Result: candidate.Result}
71-
PrintOutput("URL Added: "+actualURL, Debug, 3)
74+
gState.PrintOutput("URL Added: "+actualURL, Debug, 3)
7275

7376
//also add any directories in the supplied path to the 'to be hacked' queue
7477
path := ""
@@ -106,7 +109,7 @@ func ManageNewURLs() {
106109
}
107110
}
108111

109-
func testURL(method string, urlString string, client *http.Client) {
112+
func (gState *State) testURL(method string, urlString string, client *http.Client) {
110113
defer func() {
111114
gState.wg.Done()
112115
atomic.AddUint64(gState.TotalTested, 1)
@@ -115,7 +118,7 @@ func testURL(method string, urlString string, client *http.Client) {
115118
case gState.Chans.testChan <- method + ":" + urlString:
116119
default: //this is to prevent blocking, it doesn't _really_ matter if it doesn't get written to output
117120
}
118-
headResp, content, good := evaluateURL(method, urlString, client)
121+
headResp, content, good := gState.evaluateURL(method, urlString, client)
119122

120123
if !good && !gState.Cfg.ShowAll {
121124
return
@@ -133,15 +136,15 @@ func testURL(method string, urlString string, client *http.Client) {
133136
if !gState.Cfg.NoSpider && good && !gState.Cfg.NoRecursion {
134137
urls, err := getUrls(content)
135138
if err != nil {
136-
PrintOutput(err.Error(), Error, 0)
139+
gState.PrintOutput(err.Error(), Error, 0)
137140
}
138141
for _, x := range urls { //add any found pages into the pool
139142
//add all the directories
140143
newPage := SpiderPage{}
141144
newPage.URL = x
142145
newPage.Reference = headResp.Request.URL
143146

144-
PrintOutput(
147+
gState.PrintOutput(
145148
fmt.Sprintf("Found URL on page: %s", x),
146149
Debug, 3,
147150
)
@@ -152,23 +155,23 @@ func testURL(method string, urlString string, client *http.Client) {
152155
}
153156
}
154157

155-
func dirBust(page SpiderPage) {
158+
func (gState *State) dirBust(page SpiderPage) {
156159
//ugh
157160
u, err := url.Parse(page.URL)
158161
if err != nil {
159-
PrintOutput("This should never occur, url parse error on parsed url?"+err.Error(), Error, 0)
162+
gState.PrintOutput("This should never occur, url parse error on parsed url?"+err.Error(), Error, 0)
160163
return
161164
}
162165
//check to make sure we aren't dirbusting a wildcardyboi (NOTE!!! USES FIRST SPECIFIED MEHTOD TO DO SOFT 404!)
163166
if !gState.Cfg.NoWildcardChecks {
164167
gState.Chans.workersChan <- struct{}{}
165-
h, _, res := evaluateURL(gState.Methods[0], page.URL+RandString(), gState.Client)
168+
h, _, res := gState.evaluateURL(gState.Methods[0], page.URL+RandString(), gState.Client)
166169
//fmt.Println(page.URL, h, res)
167170
if res { //true response indicates a good response for a guid path, unlikely good
168171
if detectSoft404(h, gState.Hosts.Get404(u.Host), gState.Cfg.Ratio404) {
169172
//it's a soft404 probably, guess we can continue (this logic seems wrong??)
170173
} else {
171-
PrintOutput(
174+
gState.PrintOutput(
172175
fmt.Sprintf("Wildcard response detected, skipping dirbusting of %s", page.URL),
173176
Info, 0)
174177
return
@@ -177,7 +180,7 @@ func dirBust(page SpiderPage) {
177180
}
178181

179182
if !gState.Cfg.NoStartStop {
180-
PrintOutput(
183+
gState.PrintOutput(
181184
fmt.Sprintf("Dirbusting %s", page.URL),
182185
Info, 0,
183186
)
@@ -195,7 +198,7 @@ func dirBust(page SpiderPage) {
195198
case <-gState.StopDir:
196199
//<-maxDirs
197200
if !gState.Cfg.NoStartStop {
198-
PrintOutput(fmt.Sprintf("Finished dirbusting: %s", page.URL), Info, 0)
201+
gState.PrintOutput(fmt.Sprintf("Finished dirbusting: %s", page.URL), Info, 0)
199202
}
200203
return
201204
default:
@@ -209,7 +212,7 @@ func dirBust(page SpiderPage) {
209212
}
210213
gState.Chans.workersChan <- struct{}{}
211214
gState.wg.Add(1)
212-
go testURL(method, page.URL+word+"."+ext, gState.Client)
215+
go gState.testURL(method, page.URL+word+"."+ext, gState.Client)
213216
gState.Checked[method+page.URL+word+"."+ext] = true
214217
gState.CMut.Unlock()
215218
}
@@ -222,7 +225,7 @@ func dirBust(page SpiderPage) {
222225
}
223226
gState.Chans.workersChan <- struct{}{}
224227
gState.wg.Add(1)
225-
go testURL(method, page.URL+word+"/", gState.Client)
228+
go gState.testURL(method, page.URL+word+"/", gState.Client)
226229
gState.Checked[method+page.URL+word+"/"] = true
227230
gState.CMut.Unlock()
228231
}
@@ -233,7 +236,7 @@ func dirBust(page SpiderPage) {
233236
}
234237
gState.Chans.workersChan <- struct{}{}
235238
gState.wg.Add(1)
236-
go testURL(method, page.URL+word, gState.Client)
239+
go gState.testURL(method, page.URL+word, gState.Client)
237240
gState.Checked[method+page.URL+word] = true
238241
gState.CMut.Unlock()
239242
//if gState.Cfg.MaxDirs == 1 {
@@ -244,19 +247,19 @@ func dirBust(page SpiderPage) {
244247
}
245248
//<-maxDirs
246249
if !gState.Cfg.NoStartStop {
247-
PrintOutput(fmt.Sprintf("Finished dirbusting: %s", page.URL), Info, 0)
250+
gState.PrintOutput(fmt.Sprintf("Finished dirbusting: %s", page.URL), Info, 0)
248251
}
249252
}
250253

251254
//StartBusting will add a suppllied url to the queue to be tested
252-
func StartBusting(randURL string, u url.URL) {
255+
func (gState *State) StartBusting(randURL string, u url.URL) {
253256
defer gState.wg.Done()
254257
if !gState.Cfg.NoWildcardChecks {
255-
resp, err := HTTPReq("GET", randURL, gState.Client)
258+
resp, err := gState.HTTPReq("GET", randURL, gState.Client)
256259
<-gState.Chans.workersChan
257260
if err != nil {
258261
if gState.Cfg.InputList != "" {
259-
PrintOutput(
262+
gState.PrintOutput(
260263
err.Error(),
261264
Error,
262265
0,
@@ -266,7 +269,7 @@ func StartBusting(randURL string, u url.URL) {
266269
panic("Canary Error, check url is correct: " + randURL + "\n" + err.Error())
267270

268271
}
269-
PrintOutput(
272+
gState.PrintOutput(
270273
fmt.Sprintf("Canary sent: %s, Response: %v", randURL, resp.Status),
271274
Debug, 2,
272275
)
@@ -288,12 +291,12 @@ func StartBusting(randURL string, u url.URL) {
288291
Reference: &u,
289292
}
290293
gState.Checked[u.String()+"/"] = true
291-
PrintOutput("URL Added: "+u.String()+"/", Debug, 3)
294+
gState.PrintOutput("URL Added: "+u.String()+"/", Debug, 3)
292295
}
293296
if ok := gState.Checked[x.URL]; !ok {
294297
gState.wg.Add(1)
295298
gState.Chans.pagesChan <- x
296299
gState.Checked[x.URL] = true
297-
PrintOutput("URL Added: "+x.URL, Debug, 3)
300+
gState.PrintOutput("URL Added: "+x.URL, Debug, 3)
298301
}
299302
}

0 commit comments

Comments
 (0)