@@ -20,7 +20,7 @@ type HostResults struct {
20
20
Results interface {} `json:"results"`
21
21
}
22
22
23
- // HostStats /host api results
23
+ // HostStatsData /host api results
24
24
type HostStatsData struct {
25
25
Error bool `json:"error"`
26
26
Errmsg string `json:"errmsg"`
@@ -37,11 +37,35 @@ type HostStatsData struct {
37
37
UpdateTime string `json:"update_time"`
38
38
}
39
39
40
+ // SearchOptions options of search, for post processors
41
+ type SearchOptions struct {
42
+ FixUrl bool // each host fix as url, like 1.1.1.1,80 will change to http://1.1.1.1, https://1.1.1.1:8443 will no change
43
+ UrlPrefix string // default is http://
44
+ }
45
+
46
+ // fixHostToUrl 替换host为url
47
+ func fixHostToUrl (res [][]string , fields []string , hostIndex int , urlPrefix string ) [][]string {
48
+ newRes := make ([][]string , 0 , len (res ))
49
+ for _ , row := range res {
50
+ newRow := make ([]string , 0 , len (fields ))
51
+ for j , r := range row {
52
+ if j == hostIndex {
53
+ if ! strings .Contains (r , "://" ) {
54
+ r = urlPrefix + r
55
+ }
56
+ }
57
+ newRow = append (newRow , r )
58
+ }
59
+ newRes = append (newRes , newRow )
60
+ }
61
+ return newRes
62
+ }
63
+
40
64
// HostSearch search fofa host data
41
65
// query fofa query string
42
66
// size data size: -1 means all,0 means just data total info, >0 means actual size
43
67
// fields field of fofa host struct
44
- func (c * Client ) HostSearch (query string , size int , fields []string ) (res [][]string , err error ) {
68
+ func (c * Client ) HostSearch (query string , size int , fields []string , options ... SearchOptions ) (res [][]string , err error ) {
45
69
// check level
46
70
if c .freeSize () == 0 {
47
71
// 不是会员
@@ -139,6 +163,20 @@ func (c *Client) HostSearch(query string, size int, fields []string) (res [][]st
139
163
page ++ // 翻页
140
164
}
141
165
166
+ // 后处理
167
+ if len (options ) > 0 && options [0 ].FixUrl {
168
+ urlPrefix := options [0 ].UrlPrefix
169
+ if urlPrefix == "" {
170
+ urlPrefix = "http://"
171
+ }
172
+ for i , f := range fields {
173
+ if f == "host" {
174
+ res = fixHostToUrl (res , fields , i , urlPrefix )
175
+ break
176
+ }
177
+ }
178
+ }
179
+
142
180
return
143
181
}
144
182
0 commit comments