-
Notifications
You must be signed in to change notification settings - Fork 3
/
host_test.go
423 lines (368 loc) · 12.3 KB
/
host_test.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package gofofa
import (
"context"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestClient_HostSearch(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(queryHander))
defer ts.Close()
var cli *Client
var err error
var account accountInfo
var res [][]string
// 注册用户,没有F币
account = validAccounts[0]
cli, err = NewClient(WithURL(ts.URL + "?email=" + account.Email + "&key=" + account.Key))
assert.Nil(t, err)
res, err = cli.HostSearch("port=80", 10, []string{"ip", "port"})
assert.Contains(t, err.Error(), "insufficient privileges")
// 注册用户,有F币
account = validAccounts[4]
cli, err = NewClient(WithURL(ts.URL + "?email=" + account.Email + "&key=" + account.Key))
assert.Nil(t, err)
res, err = cli.HostSearch("port=80", 10, []string{"ip", "port"})
assert.Contains(t, err.Error(), "DeductModeFCoin")
// 参数错误
account = validAccounts[1]
cli, err = NewClient(WithURL(ts.URL + "?email=" + account.Email + "&key=" + account.Key))
assert.Nil(t, err)
assert.True(t, cli.Account.IsVIP)
res, err = cli.HostSearch("", 10, []string{"ip", "port"})
assert.Contains(t, err.Error(), "[-4] Params Error")
assert.Equal(t, 0, len(res))
// 数量超出限制
res, err = cli.HostSearch("port=80", 10000, []string{"ip", "port"})
assert.Equal(t, 100, len(res))
account = validAccounts[2]
cli, err = NewClient(WithURL(ts.URL + "?email=" + account.Email + "&key=" + account.Key))
res, err = cli.HostSearch("port=80", 10000, []string{"ip", "port"})
assert.Equal(t, 10000, len(res))
// 多字段
account = validAccounts[1]
cli, err = NewClient(WithURL(ts.URL + "?email=" + account.Email + "&key=" + account.Key))
res, err = cli.HostSearch("port=80", 10, []string{"ip", "port"})
assert.Equal(t, 10, len(res))
assert.Equal(t, "94.130.128.248", res[0][0])
assert.Equal(t, "80", res[0][1])
// 没有字段,跟host, ip,port一样
res, err = cli.HostSearch("port=80", 10, nil)
assert.Equal(t, "1.1.1.1:81", res[0][0])
assert.Equal(t, "1.1.1.1", res[0][1])
assert.Equal(t, "81", res[0][2])
// 单字段
res, err = cli.HostSearch("port=80", 10, []string{"host"})
assert.Nil(t, err)
assert.Equal(t, 10, len(res))
// 请求0数据
res, err = cli.HostSearch("port=80", 0, nil)
assert.Contains(t, err.Error(), "The Size value `0` must be between")
// 返回0条数据
res, err = cli.HostSearch("port=100000", 10, nil)
assert.Nil(t, err)
assert.Nil(t, res)
// 返回非正常格式数据
res, err = cli.HostSearch("port=100001", 10, nil)
assert.Nil(t, err)
// 数据不够
res, err = cli.HostSearch("port=50000", 10000, nil)
assert.Nil(t, err)
assert.Equal(t, 9, len(res))
// 错误语句
res, err = cli.HostSearch("aaa=bbb", 10, nil)
assert.Contains(t, err.Error(), "[820000] FOFA Query Syntax Incorrect")
// search full result
res, err = cli.HostSearch("port=5354", 100, []string{"ip", "port"}, SearchOptions{
Full: false,
})
assert.Nil(t, err)
res2, err := cli.HostSearch("port=5354", 100, []string{"ip", "port"}, SearchOptions{
Full: true,
})
assert.Nil(t, err)
assert.Greater(t, len(res2), len(res))
// 没有权限
res, err = cli.HostSearch("port=1231", 10, []string{"fid"})
assert.Contains(t, err.Error(), "没有权限搜索fid字段")
// 带有fixurl
res, err = cli.HostSearch("port=80", 10, []string{"host"}, SearchOptions{
FixUrl: true,
UrlPrefix: "",
})
assert.Nil(t, err)
assert.Equal(t, 10, len(res))
assert.Contains(t, res[0][0], "https://")
res, err = cli.HostSearch("port=80", 10, []string{"host"}, SearchOptions{
FixUrl: true,
UrlPrefix: "redis://",
})
assert.Nil(t, err)
assert.Equal(t, 10, len(res))
assert.Contains(t, res[0][0], "redis://")
// 请求失败
cli = &Client{
Server: "http://fofa.info:66666",
httpClient: &http.Client{},
Account: AccountInfo{
FCoin: 0,
IsVIP: true,
VIPLevel: 1,
},
logger: logrus.New(),
}
res, err = cli.HostSearch("port=80", 10, []string{"host"})
assert.Error(t, err)
// search all data
account = validAccounts[3]
cli, err = NewClient(WithURL(ts.URL + "?email=" + account.Email + "&key=" + account.Key))
res, err = cli.HostSearch("port=80", -1, []string{"host"}, SearchOptions{
FixUrl: true,
})
assert.Nil(t, err)
assert.Equal(t, 10, len(res))
// 过滤UniqByIP
res, err = cli.HostSearch("port=80", 500, []string{"ip", "port"}, SearchOptions{})
assert.Nil(t, err)
assert.Equal(t, 1000, len(res))
res, err = cli.HostSearch("port=80", 500, []string{"ip", "port"}, SearchOptions{
UniqByIP: true,
})
assert.Nil(t, err)
assert.Equal(t, 986, len(res))
// 域名泛解析保留
res, err = cli.HostSearch("domain=huashunxinan.net", 500, []string{"link"}, SearchOptions{})
assert.Nil(t, err)
assert.Equal(t, 100, len(res))
res, err = cli.HostSearch("domain=huashunxinan.net", 500, []string{"link"}, SearchOptions{
DeWildcard: 3,
})
assert.Nil(t, err)
assert.Equal(t, 11, len(res))
// subdomain去重
res, err = cli.HostSearch("domain=huashunxinan.net", 500, []string{"link"}, SearchOptions{})
assert.Nil(t, err)
assert.Equal(t, 100, len(res))
res, err = cli.HostSearch("domain=huashunxinan.net", 500, []string{"link"}, SearchOptions{
DedupHost: true,
})
assert.Nil(t, err)
assert.Equal(t, 99, len(res))
// checkActive探活
res, err = cli.HostSearch("ip=127.0.0.1", 1, []string{"link"}, SearchOptions{})
assert.Nil(t, err)
assert.Equal(t, 1, len(res[0]))
res, err = cli.HostSearch("ip=127.0.0.1", 1, []string{"link"}, SearchOptions{
CheckActive: 3,
})
assert.Nil(t, err)
assert.Equal(t, 2, len(res[0])) // 多一个isActive字段
// filter过滤器
res, err = cli.HostSearch("domain=huashunxinan.net", 500, []string{"ip", "host", "status_code"}, SearchOptions{})
assert.Nil(t, err)
assert.Equal(t, 100, len(res))
res, err = cli.HostSearch("domain=huashunxinan.net", 500, []string{"ip", "host", "status_code"}, SearchOptions{
Filter: "status_code=='200'",
})
assert.Nil(t, err)
assert.Equal(t, 57, len(res))
}
func TestClient_HostSearch_UniqIP(t *testing.T) {
fofaQueryTest := `title=test`
ts := httptest.NewServer(http.HandlerFunc(bindSearchAllQueryHandle(fofaQueryTest, "ip,port",
`{"error":false,"size":3,"page":1,"mode":"extended","query":"title=\"test\"","results":[["1.1.1.1","80"],["1.1.1.1","81"],["2.2.2.2","81"]]}`,
)))
defer ts.Close()
var cli *Client
var err error
var account accountInfo
var res [][]string
account = validAccounts[3]
cli, err = NewClient(WithURL(ts.URL + "?email=" + account.Email + "&key=" + account.Key))
// fixUrl support socks5/redis
res, err = cli.HostSearch(fofaQueryTest, 10, []string{"ip", "port"}, SearchOptions{
UniqByIP: true,
})
assert.Nil(t, err)
assert.Equal(t, 2, len(res))
assert.Equal(t, "1.1.1.1", res[0][0])
assert.Equal(t, "2.2.2.2", res[1][0])
}
func TestClient_HostSearch_FixUrl(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(queryHander))
defer ts.Close()
var cli *Client
var err error
var account accountInfo
var res [][]string
account = validAccounts[3]
cli, err = NewClient(WithURL(ts.URL + "?email=" + account.Email + "&key=" + account.Key))
// fixUrl support socks5/redis
res, err = cli.HostSearch("port=1080", 10, []string{"ip", "port"}, SearchOptions{
FixUrl: true,
})
assert.EqualError(t, err, NoHostWithFixURL)
// fixUrl support socks5/redis
res, err = cli.HostSearch("port=1080", 10, []string{"host", "ip", "port"}, SearchOptions{
FixUrl: true,
})
assert.Nil(t, err)
assert.Equal(t, 3, len(res))
assert.Equal(t, "socks5://1.1.1.1:1080", res[0][0])
assert.Equal(t, "redis://2.2.2.2:1080", res[1][0])
assert.Equal(t, "https://3.3.3.3:1080", res[2][0])
// fixUrl support socks5/redis
res, err = cli.HostSearch("port=1080", 10, []string{"host", "ip", "port"}, SearchOptions{
FixUrl: true,
UrlPrefix: "",
})
assert.Nil(t, err)
assert.Equal(t, 3, len(res))
assert.Equal(t, "socks5://1.1.1.1:1080", res[0][0])
assert.Equal(t, "redis://2.2.2.2:1080", res[1][0])
assert.Equal(t, "https://3.3.3.3:1080", res[2][0])
// no fields
res, err = cli.HostSearch("port=1080", 10, nil, SearchOptions{
FixUrl: true,
UrlPrefix: "",
})
assert.Nil(t, err)
assert.Equal(t, 3, len(res))
assert.Equal(t, "socks5://1.1.1.1:1080", res[0][0])
assert.Equal(t, "redis://2.2.2.2:1080", res[1][0])
assert.Equal(t, "https://3.3.3.3:1080", res[2][0])
// has protocol
res, err = cli.HostSearch("port=1080", 10, []string{"host", "ip", "port", "protocol"}, SearchOptions{
FixUrl: true,
UrlPrefix: "",
})
assert.Nil(t, err)
assert.Equal(t, 3, len(res))
assert.Equal(t, "socks5://1.1.1.1:1080", res[0][0])
assert.Equal(t, "redis://2.2.2.2:1080", res[1][0])
assert.Equal(t, "https://3.3.3.3:1080", res[2][0])
// no url prefix and no protocol
hosts := fixHostToUrl([][]string{{"1.1.1.1:80", "1.1.1.1", "80"}}, []string{"host", "ip", "port"}, 0, "", -1)
assert.Equal(t, "http://1.1.1.1:80", hosts[0][0])
// todo:确保返回的字段数跟用户要求的一致
}
func TestClient_HostSize(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(queryHander))
defer ts.Close()
var cli *Client
var err error
var account accountInfo
var count int
account = validAccounts[1]
cli, err = NewClient(WithURL(ts.URL + "?email=" + account.Email + "&key=" + account.Key))
assert.Nil(t, err)
count, err = cli.HostSize("port=80")
assert.Nil(t, err)
assert.Equal(t, 12345678, count)
// 请求失败
cli = &Client{
Server: "http://fofa.info:66666",
httpClient: &http.Client{},
logger: logrus.New(),
}
count, err = cli.HostSize("port=80")
assert.Error(t, err)
}
func TestClient_HostStats(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(queryHander))
defer ts.Close()
var cli *Client
var err error
var account accountInfo
var hostStat HostStatsData
account = validAccounts[1]
cli, err = NewClient(WithURL(ts.URL + "?email=" + account.Email + "&key=" + account.Key))
assert.Nil(t, err)
hostStat, err = cli.HostStats("1.1.1.1")
assert.Nil(t, err)
assert.Equal(t, 4, len(hostStat.Ports))
hostStat, err = cli.HostStats("fofa.info")
assert.Nil(t, err)
assert.Equal(t, 4, len(hostStat.Ports))
// 请求失败
cli = &Client{
Server: "http://fofa.info:66666",
httpClient: &http.Client{},
logger: logrus.New(),
}
hostStat, err = cli.HostStats("1.1.1.1")
assert.Error(t, err)
}
func TestClient_SetContext(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(queryHander))
defer ts.Close()
onDataCh := make(chan struct{}, 1)
onResults := func([][]string) {
onDataCh <- struct{}{}
}
account := validAccounts[3]
cli, err := NewClient(WithURL(ts.URL+"?email="+account.Email+"&key="+account.Key), WithOnResults(onResults))
assert.Nil(t, err)
cli.DeductMode = DeductModeFCoin
ctx, cancel := context.WithCancel(context.Background())
cli.SetContext(ctx)
stopCh := make(chan struct{}, 1)
go func() {
defer func() {
stopCh <- struct{}{}
}()
res, err := cli.HostSearch("port=80", 100000000, []string{"ip", "port"})
assert.Equal(t, context.Canceled, err)
assert.True(t, len(res) > 0)
}()
<-onDataCh
cancel()
<-stopCh
}
func TestClient_DumpSearch(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(queryHander))
defer ts.Close()
var cli *Client
var err error
var account accountInfo
var res [][]string
// 多字段
account = validAccounts[1]
cli, err = NewClient(WithURL(ts.URL + "?email=" + account.Email + "&key=" + account.Key))
err = cli.DumpSearch("port=80", 10000, 10, []string{"ip", "port"}, func(i [][]string, i2 int) error {
res = append(res, i...)
return nil
})
assert.Nil(t, err)
assert.Equal(t, 100, len(res))
assert.Equal(t, "1.1.1.1", res[0][0])
assert.Equal(t, "81", res[0][1])
res = nil
err = cli.DumpSearch("port=80", 10000, 10, nil, func(i [][]string, i2 int) error {
res = append(res, i...)
return nil
}, SearchOptions{FixUrl: true})
assert.Nil(t, err)
assert.Equal(t, 100, len(res))
assert.Equal(t, "http://1.1.1.1", res[0][0])
assert.Equal(t, "1.1.1.1", res[0][1])
assert.Equal(t, "81", res[0][2])
// 数据范围报错
res = nil
err = cli.DumpSearch("port=80", 10000, 10000000, nil, func(i [][]string, i2 int) error {
res = append(res, i...)
return nil
}, SearchOptions{FixUrl: true})
assert.NotNil(t, err)
// 取消
ctx, cancel := context.WithCancel(context.Background())
cli.SetContext(ctx)
res = nil
err = cli.DumpSearch("port=80", 10000, 10, nil, func(i [][]string, i2 int) error {
cancel()
return nil
}, SearchOptions{FixUrl: true})
assert.NotNil(t, err)
}