Skip to content

Commit 0cbf30b

Browse files
committed
Refactorings for life
1 parent 4eb8f42 commit 0cbf30b

File tree

7 files changed

+206
-185
lines changed

7 files changed

+206
-185
lines changed

fourohme.go

Lines changed: 25 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,18 @@ For example: cat domains.txt | httpx -silent -mc 401,402,403,404,405 | fourohme
2727
package main
2828

2929
import (
30-
"bufio"
31-
"flag"
3230
"fmt"
33-
"net/http"
3431
"net/url"
35-
"os"
36-
"strings"
3732
"sync"
38-
)
39-
40-
type Header struct {
41-
Key string
42-
Value string
43-
}
4433

45-
type Request struct {
46-
Verb string
47-
Url string
48-
Headers []Header
49-
}
34+
"github.com/topscoder/fourohme/libs/fourohme"
35+
)
5036

5137
func main() {
52-
urlPtr, filePtr, silentPtr, threadsPtr := parseCommandLineFlags()
38+
urlPtr, filePtr, silentPtr, threadsPtr := fourohme.ParseCommandLineFlags()
5339

5440
if !*silentPtr {
55-
showBanner()
41+
fourohme.ShowBanner()
5642
}
5743

5844
headerKeysList := []string{
@@ -122,10 +108,10 @@ func main() {
122108
"0",
123109
}
124110

125-
var composedHeadersList []Header
111+
var composedHeadersList []fourohme.Header
126112
for _, key := range headerKeysList {
127113
for _, value := range headerValuesList {
128-
header := Header{Key: key, Value: value}
114+
header := fourohme.Header{Key: key, Value: value}
129115
composedHeadersList = append(composedHeadersList, header)
130116
}
131117
}
@@ -140,7 +126,7 @@ func main() {
140126
}
141127

142128
// Let's Rock
143-
urls := readUrlsFromInput(urlPtr, filePtr)
129+
urls := fourohme.ReadUrlsFromInput(urlPtr, filePtr)
144130

145131
for _, pUrl := range urls {
146132
parsedURL, err := url.Parse(pUrl)
@@ -150,75 +136,75 @@ func main() {
150136

151137
// Try each header in composedHeadersList
152138
var wg sync.WaitGroup
153-
ch := make(chan Request, *threadsPtr)
139+
ch := make(chan fourohme.Request, *threadsPtr)
154140
for _, header := range composedHeadersList {
155141
wg.Add(1)
156142

157-
var headerList []Header
143+
var headerList []fourohme.Header
158144
headerList = append(headerList, header)
159145

160-
request := Request{Verb: "GET", Url: pUrl, Headers: headerList}
146+
request := fourohme.Request{Verb: "GET", Url: pUrl, Headers: headerList}
161147

162148
ch <- request
163149

164-
go talkHttpBaby(ch, &wg)
150+
go fourohme.TalkHttpBaby(ch, &wg)
165151
}
166152

167153
// Try each header with %URL% variable
168154
for _, headerKey := range headerKeysList {
169155
wg.Add(1)
170156

171-
var headerList []Header
172-
header := Header{Key: headerKey, Value: pUrl}
157+
var headerList []fourohme.Header
158+
header := fourohme.Header{Key: headerKey, Value: pUrl}
173159
headerList = append(headerList, header)
174160

175-
request := Request{Verb: "GET", Url: pUrl, Headers: headerList}
161+
request := fourohme.Request{Verb: "GET", Url: pUrl, Headers: headerList}
176162

177163
ch <- request
178164

179-
go talkHttpBaby(ch, &wg)
165+
go fourohme.TalkHttpBaby(ch, &wg)
180166
}
181167

182-
sUrl, sPath := getHostAndPath(parsedURL)
168+
sUrl, sPath := fourohme.GetHostAndPath(parsedURL)
183169

184170
// Try each header with %PATH% variable
185171
for _, headerKey := range headerKeysList {
186172
wg.Add(1)
187173

188-
var headerList []Header
189-
header := Header{Key: headerKey, Value: sPath}
174+
var headerList []fourohme.Header
175+
header := fourohme.Header{Key: headerKey, Value: sPath}
190176
headerList = append(headerList, header)
191177

192-
request := Request{Verb: "GET", Url: pUrl, Headers: headerList}
178+
request := fourohme.Request{Verb: "GET", Url: pUrl, Headers: headerList}
193179

194180
ch <- request
195181

196-
go talkHttpBaby(ch, &wg)
182+
go fourohme.TalkHttpBaby(ch, &wg)
197183
}
198184

199185
// Try each URL payload in urlPayloadsList
200-
var headerList []Header
186+
var headerList []fourohme.Header
201187
for _, payload := range urlPayloadsList {
202188
wg.Add(1)
203189

204190
loadedUrl := fmt.Sprintf("%s%s%s", sUrl, sPath, payload)
205191

206-
request := Request{Verb: "GET", Url: loadedUrl, Headers: headerList}
192+
request := fourohme.Request{Verb: "GET", Url: loadedUrl, Headers: headerList}
207193

208194
ch <- request
209195

210-
go talkHttpBaby(ch, &wg)
196+
go fourohme.TalkHttpBaby(ch, &wg)
211197
}
212198

213199
// Try with different HTTP Verbs
214200
for _, verb := range httpVerbsList {
215201
wg.Add(1)
216202

217-
request := Request{Verb: verb, Url: pUrl, Headers: headerList}
203+
request := fourohme.Request{Verb: verb, Url: pUrl, Headers: headerList}
218204

219205
ch <- request
220206

221-
go talkHttpBaby(ch, &wg)
207+
go fourohme.TalkHttpBaby(ch, &wg)
222208
}
223209

224210
close(ch)
@@ -227,148 +213,3 @@ func main() {
227213
fmt.Println("")
228214
}
229215
}
230-
231-
func talkHttpBaby(ch chan Request, wg *sync.WaitGroup) {
232-
defer wg.Done() // Schedule the wg.Done() function call to be executed when the function returns
233-
234-
request := <-ch
235-
236-
statusCode := executeHttpRequest(request)
237-
238-
printOutput(statusCode, request.Verb, request.Url, request.Headers)
239-
}
240-
241-
func executeHttpRequest(request Request) int {
242-
verb := request.Verb
243-
url := request.Url
244-
headers := request.Headers
245-
246-
req := createRequest(verb, url)
247-
248-
if req == nil {
249-
return -1
250-
}
251-
252-
for _, header := range headers {
253-
req.Header.Add(header.Key, header.Value)
254-
}
255-
256-
resp, err := http.DefaultClient.Do(req)
257-
if err != nil {
258-
fmt.Println(err)
259-
resp.Body.Close()
260-
return -1
261-
}
262-
263-
resp.Body.Close()
264-
return resp.StatusCode
265-
}
266-
267-
func parseCommandLineFlags() (*string, *string, *bool, *int) {
268-
urlPtr := flag.String("url", "", "URL to make requests to")
269-
filePtr := flag.String("file", "", "Path to a file containing URLs")
270-
silentPtr := flag.Bool("silent", false, "Don't print shizzle. Only what matters.")
271-
threadsPtr := flag.Int("threads", 4, "The amount of threads to be used to execute the HTTP requests. Be gentle or get blocked.")
272-
flag.Parse()
273-
274-
return urlPtr, filePtr, silentPtr, threadsPtr
275-
}
276-
277-
func readUrlsFromInput(urlPtr, filePtr *string) []string {
278-
var urls []string
279-
280-
urls = readUrlsFromStdin()
281-
282-
if urls != nil {
283-
return urls
284-
}
285-
286-
if *filePtr != "" {
287-
urls = readUrlsFromFile(*filePtr)
288-
} else if *urlPtr != "" {
289-
urls = strings.Split(*urlPtr, ",")
290-
}
291-
292-
return urls
293-
}
294-
295-
func readUrlsFromStdin() []string {
296-
stat, _ := os.Stdin.Stat()
297-
if (stat.Mode() & os.ModeCharDevice) == 0 {
298-
// Read from stdin
299-
urls := make([]string, 0)
300-
scanner := bufio.NewScanner(os.Stdin)
301-
for scanner.Scan() {
302-
urls = append(urls, scanner.Text())
303-
}
304-
305-
return urls
306-
}
307-
308-
return nil
309-
}
310-
311-
func readUrlsFromFile(filepath string) []string {
312-
file, err := os.Open(filepath)
313-
if err != nil {
314-
fmt.Println(err)
315-
os.Exit(1)
316-
}
317-
defer file.Close()
318-
319-
var urls []string
320-
scanner := bufio.NewScanner(file)
321-
for scanner.Scan() {
322-
urls = append(urls, scanner.Text())
323-
}
324-
325-
return urls
326-
}
327-
328-
func getHostAndPath(parsedURL *url.URL) (string, string) {
329-
sUrl := parsedURL.Scheme + "://" + parsedURL.Host
330-
sPath := parsedURL.Path
331-
if sPath == "" {
332-
sPath = "/"
333-
}
334-
335-
return sUrl, sPath
336-
}
337-
338-
func createRequest(verb string, pUrl string) *http.Request {
339-
req, err := http.NewRequest(verb, pUrl, nil)
340-
341-
if err != nil {
342-
fmt.Println(err)
343-
return nil
344-
}
345-
346-
return req
347-
}
348-
349-
func printOutput(statusCode int, verb string, url string, headers []Header) {
350-
// Print in green if it's 200
351-
if statusCode == 200 {
352-
fmt.Printf("\033[32m%d => HTTP %s %s %v\033[0m\n", statusCode, verb, url, headers)
353-
} else {
354-
fmt.Printf("\033[31m%d => HTTP %s %s %v\033[0m\n", statusCode, verb, url, headers)
355-
}
356-
}
357-
358-
func showBanner() {
359-
const banner = `
360-
361-
362-
███████╗ ██████╗ ██╗ ██╗██████╗ ██████╗ ██╗ ██╗ ███╗ ███╗███████╗
363-
██╔════╝██╔═══██╗██║ ██║██╔══██╗ ██╔═══██╗██║ ██║ ████╗ ████║██╔════╝
364-
█████╗ ██║ ██║██║ ██║██████╔╝ ██║ ██║███████║ ██╔████╔██║█████╗
365-
██╔══╝ ██║ ██║██║ ██║██╔══██╗ ██║ ██║██╔══██║ ██║╚██╔╝██║██╔══╝
366-
██║ ╚██████╔╝╚██████╔╝██║ ██║ ╚██████╔╝██║ ██║ ██║ ╚═╝ ██║███████╗
367-
╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚══════╝
368-
369-
by @topscoder
370-
371-
`
372-
373-
fmt.Println(banner)
374-
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/topscoder/fourohme
22

3-
go 1.18
3+
go 1.20

libs/fourohme/header.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package fourohme
2+
3+
type Header struct {
4+
Key string
5+
Value string
6+
}

libs/fourohme/input.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package fourohme
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strings"
8+
)
9+
10+
func ReadUrlsFromInput(urlPtr, filePtr *string) []string {
11+
var urls []string
12+
13+
urls = readUrlsFromStdin()
14+
15+
if urls != nil {
16+
return urls
17+
}
18+
19+
if *filePtr != "" {
20+
urls = readUrlsFromFile(*filePtr)
21+
} else if *urlPtr != "" {
22+
urls = strings.Split(*urlPtr, ",")
23+
}
24+
25+
return urls
26+
}
27+
28+
func readUrlsFromStdin() []string {
29+
stat, _ := os.Stdin.Stat()
30+
if (stat.Mode() & os.ModeCharDevice) == 0 {
31+
// Read from stdin
32+
urls := make([]string, 0)
33+
scanner := bufio.NewScanner(os.Stdin)
34+
for scanner.Scan() {
35+
urls = append(urls, scanner.Text())
36+
}
37+
38+
return urls
39+
}
40+
41+
return nil
42+
}
43+
44+
func readUrlsFromFile(filepath string) []string {
45+
file, err := os.Open(filepath)
46+
if err != nil {
47+
fmt.Println(err)
48+
os.Exit(1)
49+
}
50+
defer file.Close()
51+
52+
var urls []string
53+
scanner := bufio.NewScanner(file)
54+
for scanner.Scan() {
55+
urls = append(urls, scanner.Text())
56+
}
57+
58+
return urls
59+
}

0 commit comments

Comments
 (0)