forked from michenriksen/aquatone
/
url_requester.go
106 lines (93 loc) · 3.05 KB
/
url_requester.go
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
103
104
105
106
package agents
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/michenriksen/aquatone/core"
"github.com/parnurzeal/gorequest"
)
type URLRequester struct {
session *core.Session
}
func NewURLRequester() *URLRequester {
return &URLRequester{}
}
func (d *URLRequester) ID() string {
return "agent:url_requester"
}
func (a *URLRequester) Register(s *core.Session) error {
s.EventBus.SubscribeAsync(core.URL, a.OnURL, false)
a.session = s
return nil
}
func (a *URLRequester) OnURL(url string) {
a.session.Out.Debug("[%s] Received new URL %s\n", a.ID(), url)
a.session.WaitGroup.Add()
go func(url string) {
defer a.session.WaitGroup.Done()
http := Gorequest(a.session.Options)
resp, _, errs := http.Get(url).
Set("User-Agent", RandomUserAgent()).
Set("X-Forwarded-For", RandomIPv4Address()).
Set("Via", fmt.Sprintf("1.1 %s", RandomIPv4Address())).
Set("Forwarded", fmt.Sprintf("for=%s;proto=http;by=%s", RandomIPv4Address(), RandomIPv4Address())).End()
var status string
if errs != nil {
a.session.Stats.IncrementRequestFailed()
for _, err := range errs {
a.session.Out.Debug("[%s] Error: %v\n", a.ID(), err)
if os.IsTimeout(err) {
a.session.Out.Error("%s: request timeout\n", url)
return
}
}
a.session.Out.Debug("%s: failed\n", url)
return
}
a.session.Stats.IncrementRequestSuccessful()
if resp.StatusCode >= 500 {
a.session.Stats.IncrementResponseCode5xx()
status = Red(resp.Status)
} else if resp.StatusCode >= 400 {
a.session.Stats.IncrementResponseCode4xx()
status = Yellow(resp.Status)
} else if resp.StatusCode >= 300 {
a.session.Stats.IncrementResponseCode3xx()
status = Green(resp.Status)
} else {
a.session.Stats.IncrementResponseCode2xx()
status = Green(resp.Status)
}
a.session.Out.Info("%s: %s\n", url, status)
a.writeHeaders(url, resp)
if *a.session.Options.SaveBody {
a.writeBody(url, resp)
}
a.session.EventBus.Publish(core.URLResponsive, url)
}(url)
}
func (a *URLRequester) writeHeaders(url string, resp gorequest.Response) {
filepath := a.session.GetFilePath(fmt.Sprintf("headers/%s.txt", BaseFilenameFromURL(url)))
headers := fmt.Sprintf("%s\n", resp.Status)
for name, value := range resp.Header {
headers += fmt.Sprintf("%v: %v\n", name, strings.Join(value, " "))
}
if err := ioutil.WriteFile(filepath, []byte(headers), 0644); err != nil {
a.session.Out.Debug("[%s] Error: %v\n", a.ID(), err)
a.session.Out.Error("Failed to write HTTP response headers for %s to %s\n", url, filepath)
}
}
func (a *URLRequester) writeBody(url string, resp gorequest.Response) {
filepath := a.session.GetFilePath(fmt.Sprintf("html/%s.html", BaseFilenameFromURL(url)))
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
a.session.Out.Debug("[%s] Error: %v\n", a.ID(), err)
a.session.Out.Error("Failed to read response body for %s\n", url)
return
}
if err := ioutil.WriteFile(filepath, body, 0644); err != nil {
a.session.Out.Debug("[%s] Error: %v\n", a.ID(), err)
a.session.Out.Error("Failed to write HTTP response body for %s to %s\n", url, filepath)
}
}