-
Notifications
You must be signed in to change notification settings - Fork 5
/
stats.go
83 lines (74 loc) · 1.79 KB
/
stats.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
package gofofa
import (
"encoding/base64"
"errors"
"strconv"
"strings"
)
// StatsResults /search/stats api results
type StatsResults struct {
Error bool `json:"error"`
Errmsg string `json:"errmsg"`
Distinct map[string]interface{} `json:"distinct"`
Aggs map[string]interface{} `json:"aggs"`
LastUpdateTime string `json:"lastupdatetime"`
}
// StatsItem one stats item
type StatsItem struct {
Name string
Count int
}
// StatsObject one stats object
type StatsObject struct {
Name string
Items []StatsItem
}
// Stats aggs fofa host data
// query fofa query string
// size data size
// fields' field of fofa host struct
func (c *Client) Stats(query string, size int, fields []string) (res []StatsObject, err error) {
if len(fields) == 0 {
fields = []string{"title", "country"}
}
var sr StatsResults
err = c.Fetch("search/stats",
map[string]string{
"qbase64": base64.StdEncoding.EncodeToString([]byte(query)),
"size": strconv.Itoa(size),
"fields": strings.Join(fields, ","),
"full": "false", // 是否全部数据,非一年内
},
&sr)
if err != nil {
return
}
if len(sr.Errmsg) > 0 {
err = errors.New(sr.Errmsg)
return
}
for _, rawField := range fields {
field := rawField
// 有些字段要进行改名
switch rawField {
case "country":
field = "countries"
}
if v, ok := sr.Aggs[field]; ok {
if objArray, ok := v.([]interface{}); ok && len(objArray) > 0 {
so := StatsObject{
Name: rawField,
}
for _, obj := range objArray {
obj := obj.(map[string]interface{})
so.Items = append(so.Items, StatsItem{
Name: obj["name"].(string),
Count: int(obj["count"].(float64)),
})
}
res = append(res, so)
}
}
}
return
}