This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfofa_stat.go
90 lines (77 loc) · 2.12 KB
/
fofa_stat.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
package main
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
)
func getFoFaStat(query string) (result FoFaStatusResp, err error) {
qbase := base64.StdEncoding.EncodeToString([]byte(query))
log.Printf("query:%s,base64:%s", query, qbase)
// create context
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
defer cancel()
if chromePath != "" {
chromedp.ExecPath(chromePath)
}
done := make(chan bool)
var requestID network.RequestID
chromedp.ListenTarget(ctx, func(v interface{}) {
switch ev := v.(type) {
case *network.EventRequestWillBeSent:
if strings.Contains(ev.Request.URL, fmt.Sprintf("https://api.%s/v1/search/stats?qbase64=", fofaDomain)) && ev.Type == "XHR" {
if debug {
log.Printf("EventRequestWillBeSent: %v: %v\n", ev.RequestID, ev.Request.URL)
} else {
log.Printf("request fofa query :%s stat data\n", query)
}
requestID = ev.RequestID
}
case *network.EventLoadingFinished:
if ev.RequestID == requestID {
if debug {
log.Printf("EventLoadingFinished: %v\n", ev.RequestID)
} else {
log.Printf("get fofa query :%s stat data\n", query)
}
close(done)
}
}
})
// run task list
fofaUrl := fmt.Sprintf(`https://%s/result?qbase64=%s&full=%s`, fofaDomain, qbase, isFull)
log.Printf("fofa Query URL:%s \n", fofaUrl)
var res string
err = chromedp.Run(ctx,
network.Enable(),
chromedp.Navigate(fofaUrl),
chromedp.Evaluate(`window.__NUXT__.data[0].url_key`, &res),
)
if err != nil {
log.Fatalf("err: %s", err)
return result, err
}
//log.Printf("window object keys: %v", res)
<-done
// get the downloaded bytes for the request id
var buf []byte
if err = chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
buf, err = network.GetResponseBody(requestID).Do(ctx)
return err
})); err != nil {
return result, err
}
//fmt.Println(string(buf))
err = json.Unmarshal(buf, &result)
if err != nil {
return result, err
}
return result, nil
}