-
Notifications
You must be signed in to change notification settings - Fork 133
/
crawler.go
96 lines (89 loc) · 2.21 KB
/
crawler.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
package rotateproxy
import (
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
var crawlDone = make(chan struct{})
type fofaAPIResponse struct {
Err bool `json:"error"`
Mode string `json:"mode"`
Page int `json:"page"`
Query string `json:"query"`
Results [][]string `json:"results"`
Size int `json:"size"`
}
func addProxyURL(url string) {
CreateProxyURL(url)
}
func RunCrawler(fofaApiKey, fofaEmail, rule string, pageNum int, proxy string) (err error) {
req, err := http.NewRequest("GET", "https://fofa.info/api/v1/search/all", nil)
if err != nil {
return err
}
tr := &http.Transport{TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
}}
if proxy != "" {
proxyUrl, err := url.Parse(proxy)
if err == nil { // 使用传入代理
tr.Proxy = http.ProxyURL(proxyUrl)
}
}
rule = base64.StdEncoding.EncodeToString([]byte(rule))
q := req.URL.Query()
q.Add("email", fofaEmail)
q.Add("key", fofaApiKey)
q.Add("qbase64", rule)
q.Add("size", "100")
q.Add("page", fmt.Sprintf("%d", pageNum))
q.Add("fields", "host,title,ip,domain,port,country,city,server,protocol")
req.URL.RawQuery = q.Encode()
// resp, err := http.DefaultClient.Do(req)
resp, err := (&http.Client{Transport: tr}).Do(req)
if err != nil {
return err
}
InfoLog(Noticeln("start to parse proxy url from response"))
defer resp.Body.Close()
var res fofaAPIResponse
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return err
}
InfoLog(Notice("get %d host", len(res.Results)))
for _, value := range res.Results {
host := value[0]
addProxyURL(fmt.Sprintf("socks5://%s", host))
}
crawlDone <- struct{}{}
return
}
func StartRunCrawler(ctx context.Context, fofaApiKey, fofaEmail, rule string, pageCount int, proxy string) {
runCrawlerFunc := func() {
for i := 1; i <= pageCount; i++ {
err := RunCrawler(fofaApiKey, fofaEmail, rule, i, proxy)
if err != nil {
ErrorLog(Warn("[!] error: %v", err))
}
}
}
go func() {
runCrawlerFunc()
ticker := time.NewTicker(600 * time.Second)
defer ticker.Stop()
for {
select {
case <- ticker.C:
runCrawlerFunc()
case <- ctx.Done():
return
}
}
}()
}