Skip to content

Commit 02df329

Browse files
committed
dump add inFile
1 parent e6bb67c commit 02df329

File tree

8 files changed

+131
-20
lines changed

8 files changed

+131
-20
lines changed

HISTORY.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v0.1.13 dump add inFile
2+
3+
- dump add inFile/json argument, which can be used to dump data from queries file. ```./fofa dump -inFile a.txt -outFile out.json -j```
4+
15
## v0.1.12 dump
26

37
- dump is used to perform large-scale data retrieval for the same search query, https://en.fofa.info/api/batches_pages

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,25 @@ UpdateTime: 2022-05-30 17:00:00
168168
./fofa dump --format json -fixUrl -outFile a.json -batchSize 10000 'title=phpinfo'
169169
```
170170

171+
- dump large-scale data by queries file (line by line)
172+
173+
```shell
174+
175+
cat queries.txt
176+
port=13344
177+
port=23455
178+
179+
# csv
180+
./fofa dump -outFile out.csv -inFile queries.txt
181+
182+
# json
183+
./fofa dump -inFile queries.txt -outFile out.json -j
184+
2023/08/09 10:05:33 dump data of query: port=13344
185+
2023/08/09 10:05:35 size: 11/11, 100.00%
186+
2023/08/09 10:05:35 dump data of query: port=23455
187+
2023/08/09 10:05:37 size: 499/499, 100.00%
188+
```
189+
171190
### Utils
172191

173192
- random subcommand

account.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ const (
4141
)
4242

4343
const (
44-
VipLevelRed VipLevel = 20 // 红队版
45-
VipLevelStudent VipLevel = 22 // 教育账户
44+
VipLevelRed VipLevel = 20 // 红队版
45+
VipLevelStudent VipLevel = 22 // 教育账户
46+
VipLevelNever VipLevel = 100 // 不可能的等级
4647
)
4748

4849
// AccountInfo fofa account info

account_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ func TestAccountInfo_String(t *testing.T) {
4343
RemainApiQuery: 0,
4444
RemainApiData: 0,
4545
}, ai)
46+
assert.Equal(t, `{
47+
"error": false,
48+
"fcoin": 0,
49+
"fofa_point": 0,
50+
"isvip": true,
51+
"vip_level": 3,
52+
"remain_api_query": 0,
53+
"remain_api_data": 0
54+
}`, ai.String())
4655
}
4756

4857
func TestClient_AccountInfo(t *testing.T) {
@@ -98,4 +107,31 @@ func TestClient_AccountInfo(t *testing.T) {
98107
assert.Equal(t, VipLevelSubPersonal, cli.Account.VIPLevel)
99108
assert.Equal(t, 10, cli.Account.FCoin)
100109
assert.Equal(t, 100, cli.freeSize())
110+
111+
// 红队?
112+
account = validAccounts[8]
113+
cli, err = NewClient(WithURL(ts.URL + "?email=" + account.Email + "&key=" + account.Key))
114+
assert.Nil(t, err)
115+
assert.True(t, cli.Account.IsVIP)
116+
assert.Equal(t, VipLevelRed, cli.Account.VIPLevel)
117+
assert.Equal(t, 0, cli.Account.FCoin)
118+
assert.Equal(t, 10000, cli.freeSize())
119+
120+
// 学生
121+
account = validAccounts[9]
122+
cli, err = NewClient(WithURL(ts.URL + "?email=" + account.Email + "&key=" + account.Key))
123+
assert.Nil(t, err)
124+
assert.True(t, cli.Account.IsVIP)
125+
assert.Equal(t, VipLevelStudent, cli.Account.VIPLevel)
126+
assert.Equal(t, 0, cli.Account.FCoin)
127+
assert.Equal(t, 10000, cli.freeSize())
128+
129+
// 不可能的等级
130+
account = validAccounts[10]
131+
cli, err = NewClient(WithURL(ts.URL + "?email=" + account.Email + "&key=" + account.Key))
132+
assert.Nil(t, err)
133+
assert.True(t, cli.Account.IsVIP)
134+
assert.Equal(t, VipLevelNever, cli.Account.VIPLevel)
135+
assert.Equal(t, 0, cli.Account.FCoin)
136+
assert.Equal(t, -1, cli.freeSize())
101137
}

client_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ var (
3838
{"[email protected]", "99999", true, VipLevelSubBuss, 0, 0, 0}, // 订阅商业
3939

4040
{"[email protected]", "10001", true, VipLevelRed, 0, 0, 0}, // 红队
41-
{"[email protected]", "10001", true, VipLevelStudent, 0, 0, 0}, // 教育
41+
{"[email protected]", "10002", true, VipLevelStudent, 0, 0, 0}, // 教育
42+
43+
{"[email protected]", "10003", true, VipLevelNever, 0, 0, 0}, // 不可能的等级
4244
}
4345

4446
queryHander = func(w http.ResponseWriter, r *http.Request) {

cmd/fofa/cmd/dump.go

+63-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"bufio"
45
"errors"
56
"fmt"
67
"github.com/LubyRuffy/gofofa"
@@ -32,12 +33,24 @@ var dumpCmd = &cli.Command{
3233
Usage: "can be csv/json/xml",
3334
Destination: &format,
3435
},
36+
&cli.BoolFlag{
37+
Name: "json",
38+
Aliases: []string{"j"},
39+
Usage: "output use json format",
40+
Destination: &json,
41+
},
3542
&cli.StringFlag{
3643
Name: "outFile",
3744
Aliases: []string{"o"},
3845
Usage: "if not set, wirte to stdout",
3946
Destination: &outFile,
4047
},
48+
&cli.StringFlag{
49+
Name: "inFile",
50+
Aliases: []string{"i"},
51+
Usage: "queries line by line",
52+
Destination: &inFile,
53+
},
4154
&cli.IntFlag{
4255
Name: "size",
4356
Aliases: []string{"s"},
@@ -77,9 +90,35 @@ var dumpCmd = &cli.Command{
7790
// DumpAction search action
7891
func DumpAction(ctx *cli.Context) error {
7992
// valid same config
93+
var queries []string
8094
query := ctx.Args().First()
81-
if len(query) == 0 {
82-
return errors.New("fofa query cannot be empty")
95+
if len(query) > 0 {
96+
queries = append(queries, query)
97+
}
98+
if len(inFile) > 0 {
99+
// 打开文件
100+
file, err := os.Open(inFile)
101+
if err != nil {
102+
log.Fatal(err)
103+
}
104+
defer file.Close()
105+
106+
// 创建一个 Scanner 对象来逐行读取文件
107+
scanner := bufio.NewScanner(file)
108+
109+
// 逐行读取并打印
110+
for scanner.Scan() {
111+
queries = append(queries, scanner.Text())
112+
}
113+
114+
// 检查是否有读取错误
115+
if err = scanner.Err(); err != nil {
116+
log.Fatal(err)
117+
}
118+
}
119+
120+
if len(queries) == 0 {
121+
return errors.New("fofa query cannot be empty, use args or -inFile")
83122
}
84123
fields := strings.Split(fieldString, ",")
85124
if len(fields) == 0 {
@@ -100,6 +139,9 @@ func DumpAction(ctx *cli.Context) error {
100139
outTo = os.Stdout
101140
}
102141

142+
if json {
143+
format = "json"
144+
}
103145
// gen writer
104146
var writer outformats.OutWriter
105147
if hasBodyField(fields) && format == "csv" {
@@ -119,20 +161,25 @@ func DumpAction(ctx *cli.Context) error {
119161
}
120162

121163
// do search
122-
fetchedSize := 0
123-
err := fofaCli.DumpSearch(query, size, batchSize, fields, func(res [][]string, allSize int) (err error) {
124-
fetchedSize += len(res)
125-
log.Printf("size: %d/%d, %.2f%%", fetchedSize, allSize, 100*float32(fetchedSize)/float32(allSize))
126-
// output
127-
err = writer.WriteAll(res)
128-
return err
129-
}, gofofa.SearchOptions{
130-
FixUrl: fixUrl,
131-
UrlPrefix: urlPrefix,
132-
Full: full,
133-
})
134-
if err != nil {
135-
return err
164+
for _, query := range queries {
165+
log.Println("dump data of query:", query)
166+
167+
fetchedSize := 0
168+
err := fofaCli.DumpSearch(query, size, batchSize, fields, func(res [][]string, allSize int) (err error) {
169+
fetchedSize += len(res)
170+
log.Printf("size: %d/%d, %.2f%%", fetchedSize, allSize, 100*float32(fetchedSize)/float32(allSize))
171+
// output
172+
err = writer.WriteAll(res)
173+
return err
174+
}, gofofa.SearchOptions{
175+
FixUrl: fixUrl,
176+
UrlPrefix: urlPrefix,
177+
Full: full,
178+
})
179+
if err != nil {
180+
log.Println("fetch error:", err)
181+
//return err
182+
}
136183
}
137184

138185
return nil

cmd/fofa/cmd/search.go

+2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ var (
1717
size int // fetch size
1818
format string // out format
1919
outFile string // out file
20+
inFile string // in file
2021
deductMode string // deduct Mode
2122
fixUrl bool // each host fix as url, like 1.1.1.1,80 will change to http://1.1.1.1
2223
urlPrefix string // each host fix as url, like 1.1.1.1,80 will change to http://1.1.1.1
2324
full bool // search result for over a year
2425
batchSize int // amount of data contained in each batch, only for dump
26+
json bool // out format as json for short
2527
)
2628

2729
// search subcommand

cmd/fofa/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
var (
12-
version = "v0.1.12"
12+
version = "v0.1.13"
1313
commit = "none"
1414
date = "unknown"
1515
builtBy = "unknown" // goreleaser fill

0 commit comments

Comments
 (0)