about summary refs log tree commit diff
path: root/src/httpRequest.go
blob: 4fe8b4416e586c71be97d964dd858352d69edbc2 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
)

// httpRequest fetches a "fuzzword" from the wordlistChannel and replaces the
// string "FUZZ" in the given URL by the fetched fuzzword. A HTTP request to the
// created url is then issued resulting in a response that is pushed into the
// printChannel for printing.
func httpRequest(channels channels, threadNr int) {
	for {
		// replace the first instance of "FUZZ" in the given url by the next
		// value from the wordlistChannel
		fuzzWord := <-channels.wordlistChannel
		requestURL := strings.Replace(url, "FUZZ", fuzzWord, 1)

		if dryrun == true {
			log.Printf("req: %s", requestURL)
			continue
		}

		// make the http get request
		resp, err := http.Get(requestURL)
		if err != nil {
			if verbose == true {
				log.Println(err)
			}
		} else {
			defer resp.Body.Close()
			body, err := ioutil.ReadAll(resp.Body)

			if err != nil {
				if verbose == true {
					log.Println(err)
				}
			}

			responseLines := len(strings.Split(string(body), "\n")) - 1
			responseWords := len(strings.Split(string(body), " "))
			responseChars := len(string(body))

			// define the response
			var response = Response{
				StatusCode:    resp.StatusCode,
				ResponseLines: responseLines,
				ResponseWords: responseWords,
				ResponseChars: responseChars,
				FuzzWord:      fuzzWord,
			}

			// insert the response into the print channel for further printing
			channels.printChannel <- response
		}
	}

	if channels.doneChannel != nil {
		channels.doneChannel <- threadNr
	}
}

func httpHandler(channels channels) {
	if verbose == true {
		log.Printf("%s Starting the http handlers", green("[i]"))
	}

	// handle one or more theads
	if threads <= 1 {
		log.Printf("%s 1 thread", yellow("[i]"))
		httpRequest(channels, 0)

	} else {
		if verbose == true {
			log.Printf("%s Starting %d threads...", yellow("[i]"), threads)
		}

		// loop over all the threads starting a go routine fetching a word from
		// the wordlistChannel, making the request and inserting the result into
		// the printChannel
		for i := 0; i < threads; i++ {
			go httpRequest(channels, i)
		}
		if verbose == true {
			log.Printf("%s Done starting %d threads", green("[i]"), threads)
		}

		for i := 0; i < threads; i++ {
			threadNum := <-channels.doneChannel
			if verbose == true {
				fmt.Printf("Thread %d done!\n", threadNum)
			}
		}
	}

	if verbose == true {
		log.Printf("%s Done starting the http handlers", boldGreen("[+]"))
	}
}