-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcmd.go
273 lines (260 loc) · 7.41 KB
/
cmd.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
package main
import (
"flag"
"fmt"
"net/url"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"time"
"unsafe"
"github.com/gyyyy/ZoomEye-go/zoomeye"
)
func parseFlags(cmd string, flgs interface{}, examples ...string) []string {
if flgs != nil {
var (
v = reflect.ValueOf(flgs)
elem = v.Elem()
ptr = v.Pointer()
)
for i := 0; i < elem.NumField(); i++ {
var (
f = elem.Type().Field(i)
fname = f.Tag.Get("name")
fvalue = f.Tag.Get("value")
fusage = f.Tag.Get("usage")
fptr = unsafe.Pointer(ptr + f.Offset)
)
if fname == "" {
fname = strings.ToLower(f.Name)
}
switch f.Type.Kind() {
case reflect.String:
flag.StringVar((*string)(fptr), fname, fvalue, fusage)
case reflect.Int:
val, _ := strconv.Atoi(fvalue)
flag.IntVar((*int)(fptr), fname, val, fusage)
case reflect.Bool:
val, _ := strconv.ParseBool(fvalue)
flag.BoolVar((*bool)(fptr), fname, val, fusage)
}
}
}
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "\nUsage of %s (%s):\n", filepath.Base(os.Args[0]), cmd)
flag.PrintDefaults()
if len(examples) > 0 {
example := "Example:\n"
for _, v := range examples {
example += fmt.Sprintf(" ./ZoomEye-go %s %s\n", cmd, v)
}
fmt.Fprintf(flag.CommandLine.Output(), "\n%s\n", example)
}
}
var args []string
for i := 1; i < len(os.Args); {
if p := os.Args[i]; !strings.HasPrefix(p, "-") {
args = append(args, p)
os.Args = append(os.Args[0:i], os.Args[i+1:]...)
} else {
break
}
}
flag.Parse()
return args
}
func checkError(err error) {
switch err.(type) {
case *NoAuthKeyErr:
warnf("not found any Auth Keys, please run <zoomeye init> first")
case *zoomeye.ErrorResult:
errorf("failed to authenticate: %v", err)
case nil:
default:
errorf("something is wrong: %v", err)
}
}
type resultAnalyzer struct {
count bool
facet string
stat string
figure string
filter string
save bool
}
func newResultAnalyzer() *resultAnalyzer {
analyzer := &resultAnalyzer{}
flag.BoolVar(&analyzer.count, "count", false, "The total number of results in ZoomEye database")
flag.StringVar(&analyzer.facet, "facet", "", "Perform statistics on ZoomEye database")
flag.StringVar(&analyzer.stat, "stat", "", "Perform statistics on search results")
flag.StringVar(&analyzer.figure, "figure", "", "Output Pie or bar chart only be used under -facet and -stat")
flag.StringVar(&analyzer.filter, "filter", "", "Output more clearer search results by set filter field")
flag.BoolVar(&analyzer.save, "save", false, "Save data in JSON format")
return analyzer
}
func (a *resultAnalyzer) do(result *zoomeye.SearchResult, saveCallback func([]map[string]interface{})) {
if a.count {
infof("ZoomEye Total", "Count: %d", result.Total)
}
if a.figure != "" {
if a.figure = strings.ToLower(a.figure); a.figure != "pie" {
a.figure = "hist"
}
}
if a.facet != "" {
showFacet(result, strings.Split(a.facet, ","), a.figure)
}
if a.stat != "" {
showStat(result, strings.Split(a.stat, ","), a.figure)
}
var filtered []map[string]interface{}
if a.filter != "" {
filtered = showFilter(result, strings.Split(a.filter, ","))
}
if !a.count && a.facet == "" && a.stat == "" && a.filter == "" {
showData(result)
}
if a.save && saveCallback != nil {
saveCallback(filtered)
}
}
func cmdInit(agent *ZoomEyeAgent) {
var flgs struct {
apiKey string `usage:"ZoomEye API-Key"`
username string `usage:"ZoomEye account username"`
password string `usage:"ZoomEye account password"`
}
parseFlags("init", &flgs, `-apikey "XXXXXXXX-XXXX-XXXXX-XXXX-XXXXXXXXXXX"`,
`-username "username@zoomeye.org" -password "password"`)
var (
result *zoomeye.ResourcesInfoResult
err error
)
if flgs.apiKey != "" {
if result, err = agent.InitByKey(flgs.apiKey); err != nil {
errorf("failed to initialize: %v", err)
return
}
} else if flgs.username != "" && flgs.password != "" {
if result, err = agent.InitByUser(flgs.username, flgs.password); err != nil {
errorf("failed to initialize: %v", err)
return
}
} else if result, err = agent.InitLocal(); err != nil {
warnf("required parameter missing, please run <zoomeye init -h> for help")
return
}
successf("succeed to initialize")
infof("ZoomEye Resources Info", "Role: %s\nQuota: %d", result.Plan, result.Resources.Search)
}
func cmdInfo(agent *ZoomEyeAgent) {
result, err := agent.Info()
if err != nil {
checkError(err)
return
}
successf("succeed to query")
infof("ZoomEye Resources Info", "Role: %s\nQuota: %d", result.Plan, result.Resources.Search)
}
func cmdSearch(agent *ZoomEyeAgent) {
var (
analyzer = newResultAnalyzer()
flgs struct {
num int `value:"20" usage:"The number of search results that should be returned, multiple of 20"`
resource string `name:"type" usage:"Specify the type of resource to search"`
force bool `usage:"Ignore local and cache data"`
}
args = parseFlags("search", &flgs, `"weblogic" -facet "app" -count`)
)
if len(args) == 0 {
warnf("search keyword missing, please run <zoomeye search -h> for help")
return
}
var (
dork = args[0]
start = time.Now()
result, err = agent.Search(dork, flgs.num, flgs.resource, flgs.force)
since = time.Since(start)
)
if err != nil {
checkError(err)
return
}
successf("succeed to search (in %v)", since)
analyzer.do(result, func(filtered []map[string]interface{}) {
name := fmt.Sprintf("%s_%s_%d", flgs.resource, url.QueryEscape(dork), flgs.num)
if path, err := agent.Save(name, result); err != nil {
errorf("failed to save: %v", err)
} else {
successf("succeed to save (%s)", path)
agent.SaveFilterData(filepath.Join(agent.conf.DataPath, name+"_filtered.json"), filtered)
}
})
}
func cmdLoad(agent *ZoomEyeAgent) {
var (
analyzer = newResultAnalyzer()
args = parseFlags("load", nil, `"data/host_weblogic_20.json" -facet "app" -count`)
)
if len(args) == 0 {
warnf("path of local data file missing, please run <zoomeye load -h> for help")
return
}
var (
file = args[0]
result, err = agent.Load(file)
)
if err != nil {
errorf("invalid local data: %v", err)
return
}
successf("succeed to load")
analyzer.do(result, func(filtered []map[string]interface{}) {
var (
ext = filepath.Ext(file)
path = strings.TrimSuffix(file, ext) + "_filtered" + ext
)
if err := agent.SaveFilterData(path, filtered); err != nil {
errorf("failed to save: %v", err)
} else {
path, _ = filepath.Abs(path)
successf("succeed to save (%s)", path)
}
})
}
func cmdHistory(agent *ZoomEyeAgent) {
var (
flgs struct {
filter string `usage:"Output more clearer query results by set filter field"`
num int `value:"20" usage:"The number of results that should be returned"`
force bool `usage:"Ignore cache data"`
}
args = parseFlags("history", &flgs, `"0.0.0.0" -filter "time=^2020-03,port,service" -num 1`)
)
if len(args) == 0 {
warnf("ip missing, please run <zoomeye history -h> for help")
return
}
var (
start = time.Now()
result, err = agent.History(args[0], flgs.force)
since = time.Since(start)
)
if err != nil {
checkError(err)
return
}
successf("succeed to query (in %v)", since)
showHistory(result, strings.Split(flgs.filter, ","), flgs.num)
}
func cmdClear(agent *ZoomEyeAgent) {
var flgs struct {
cache bool
setting bool
}
parseFlags("clear", &flgs, `-cache`, `-cache -setting`)
agent.Clear(flgs.cache, flgs.setting)
successf("succeed to clear data")
}