-
Notifications
You must be signed in to change notification settings - Fork 8
/
crawler.go
72 lines (66 loc) · 1.7 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
package rotateproxy
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"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) (err error) {
req, err := http.NewRequest("GET", "https://fofa.so/api/v1/search/all", nil)
if err != nil {
return err
}
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", "500")
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)
if err != nil {
return err
}
fmt.Printf("start to parse proxy url from response\n")
defer resp.Body.Close()
var res fofaAPIResponse
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return err
}
fmt.Printf("get %d host\n", len(res.Results))
for _, value := range res.Results {
host := value[0]
addProxyURL(fmt.Sprintf("socks5://%s", host))
}
crawlDone <- struct{}{}
return
}
func StartRunCrawler(fofaApiKey, fofaEmail, rule string, pageCount int) {
go func() {
for i := 1; i <= pageCount; i++ {
RunCrawler(fofaApiKey, fofaEmail, rule, i)
}
ticker := time.NewTicker(600 * time.Second)
for range ticker.C {
for i := 1; i <= pageCount; i++ {
RunCrawler(fofaApiKey, fofaEmail, rule, i)
}
}
}()
}