forked from FabulousFabs/GoodReadsCrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpHandler.go
executable file
·221 lines (190 loc) · 6.36 KB
/
HttpHandler.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"net/http"
"net/url"
"io/ioutil"
"github.com/PuerkitoBio/goquery"
"sort"
"fmt"
"strings"
"reflect"
"strconv"
)
type job struct {
url string
index int
}
type response struct {
url string
index int
code int
body string
}
type HttpHandler struct {
chanJob chan job
chanProxy chan proxy
chanResponse chan response
results []response
proxiesLoaded bool
proxies []proxy
}
// supply []string{target urls}, int maximumworkers[, bool useProxy, bool fakeAgent]
func (httphandler HttpHandler) Handle(targets []string, workers int, options ... bool) []response {
useProxy := false
maxProxy := 0
// check for proxy argument
if len(options) > 0 {
// use proxies?
if options[0] {
useProxy = true
maxProxy = workers * 4
httphandler.chanProxy = make(chan proxy, maxProxy)
// make sure proxies are loaded
if !httphandler.proxiesLoaded {
loadProxies(&httphandler, maxProxy)
}
// make sure there can't be more workers than proxies
if workers > len(httphandler.proxies) {
workers = len(httphandler.proxies)
}
}
}
fakeAgent := false
// check for fake agent argument
if len(options) > 1 {
// use fake agents?
fakeAgent = options[1]
}
// setup channels + make sure they are defer-closed
httphandler.chanJob = make(chan job, len(targets))
httphandler.chanResponse = make(chan response, len(targets))
defer func(){
close(httphandler.chanJob)
close(httphandler.chanResponse)
httphandler.results = httphandler.results[:0]
if useProxy {
close(httphandler.chanProxy)
}
}()
// create worker pool
for i := 0; i < workers; i++ {
if !useProxy {
go worker(i, httphandler.chanJob, httphandler.chanResponse, fakeAgent)
} else {
go workerProxy(i, httphandler.chanProxy, httphandler.chanJob, httphandler.chanResponse, fakeAgent)
}
}
// feed job channel
for index, url := range targets {
httphandler.chanJob <- job{url, index}
}
// feed proxy channel
if useProxy {
for _, p := range httphandler.proxies {
httphandler.chanProxy <- p
}
}
// wait and pull results
for {
resp := <-httphandler.chanResponse
httphandler.results = append(httphandler.results, resp)
if (len(httphandler.results) == len(targets)) {
break
}
}
// sort slice before returning
sort.Slice(httphandler.results, func(i, j int) bool {
return httphandler.results[i].index < httphandler.results[j].index
})
return httphandler.results
}
func worker(index int, chanJob <-chan job, chanResponse chan<- response, agent bool){
for job := range chanJob {
client := &http.Client{}
req, err := http.NewRequest("GET", job.url, nil)
if agent {
req.Header.Add("User-Agent", `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.27 Safari/537.36`)
}
if err != nil {
fmt.Println(err)
}
resp, err2 := client.Do(req)
if err2 != nil {
fmt.Println(err2)
}
contents, _ := ioutil.ReadAll(resp.Body)
chanResponse <- response{job.url, job.index, resp.StatusCode, string(contents)}
resp.Body.Close()
}
}
type proxy struct {
ip string
port int
address *url.URL
}
func loadProxies(httphandler *HttpHandler, max int) {
// load web content + initialize goquery
resp, _ := http.Get("https://free-proxy-list.net")
contents, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
document, _ := goquery.NewDocumentFromReader(strings.NewReader(string(contents)))
var p []proxy
// find TRs in document
document.Find("tr").Each(func(index int, tr *goquery.Selection) {
// make sure it's a proxy element
tds := tr.Find("td")
if tds.Length() != 8 {
return
}
// make sure it's a-ok
if reflect.TypeOf(tds.Get(0).FirstChild.Data).Kind() != reflect.String ||
reflect.TypeOf(tds.Get(1).FirstChild.Data).Kind() != reflect.String ||
reflect.TypeOf(tds.Get(4).FirstChild.Data).Kind() != reflect.String ||
reflect.TypeOf(tds.Get(6).FirstChild.Data).Kind() != reflect.String {
return
}
// does this proxy support https?
if string(tds.Get(6).FirstChild.Data) != "yes" {
return
}
// is it transparent?
if string(tds.Get(4).FirstChild.Data) == "transparent" {
return
}
ip := tds.Get(0).FirstChild.Data
port, _ := strconv.Atoi(tds.Get(1).FirstChild.Data)
url, _ := url.Parse(fmt.Sprintf("%s:%d", ip, port))
p = append(p, proxy{ip, port, url})
})
// weed out bad proxies (tests)
for _, pr := range p {
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(pr.address)}}
resp, err := client.Get("https://jsonplaceholder.typicode.com/todos/1")
resp.Body.Close()
if err == nil {
httphandler.proxies = append(httphandler.proxies, pr)
}
if len(httphandler.proxies) >= max {
break
}
}
}
func workerProxy(index int, chanProxy chan proxy, chanJob chan job, chanResponse chan<- response, agent bool){
for job := range chanJob {
proxy := <-chanProxy
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxy.address)}}
req, _ := http.NewRequest("GET", job.url, nil)
if agent {
req.Header.Add("User-Agent", `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.27 Safari/537.36`)
}
resp, errGet := client.Do(req)
contents, errIO := ioutil.ReadAll(resp.Body)
if errGet != nil || errIO != nil {
chanJob <- job
} else {
chanResponse <- response{job.url, job.index, resp.StatusCode, string(contents)}
chanProxy <- proxy
}
resp.Body.Close()
}
}