-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalive.go
44 lines (37 loc) · 1.17 KB
/
alive.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
package checkers
import (
"github.com/gocolly/colly"
"log"
"net/url"
)
func CheckIsAlive(args *Wrapper) {
var onErrorCallback = func(response *colly.Response, err error) {
log.Printf("[DEBUG CheckIsAlive] Bad proxy found error status %d [%s] [%s]", response.StatusCode, response.Request.ProxyURL, err)
}
var onResponseCallback = func(response *colly.Response) {
log.Printf("[DEBUG CheckIsAlive] Good proxy found [%s]", response.Request.ProxyURL)
args.AddGoodProxy(response.Request.ProxyURL)
}
var collectors []*colly.Collector
for _, proxy := range args.ProxiesToTest {
newCollector := NewCollector(args)
newCollector.OnResponse(onResponseCallback)
newCollector.OnError(onErrorCallback)
u, err := url.Parse(proxy)
if err != nil {
log.Printf("[CheckIsAlive] error while parseing proxy %s %s", proxy, err)
newCollector = nil
continue
}
newCollector.SetProxyFunc(ProxyURL(u))
if err = newCollector.Visit(args.WebsiteToCrawl); err != nil {
log.Printf("[CheckIsAlive] error happening doing Visit %s", err)
newCollector = nil
continue
}
collectors = append(collectors, newCollector)
}
for _, collec := range collectors {
collec.Wait()
}
}