/
commands.go
102 lines (88 loc) · 2.56 KB
/
commands.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
package commands
import (
"runtime"
"strconv"
"github.com/activecm/rita/resources"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var (
allCommands []cli.Command
// below are some prebuilt flags that get used often in various commands
// threadFlag allows users to specify how many threads should be used
threadFlag = cli.IntFlag{
Name: "threads, t",
Usage: "Use `N` threads when executing this command",
Value: runtime.NumCPU(),
}
// configFlag allows users to specify an alternate config file to use
configFlag = cli.StringFlag{
Name: "config, c",
Usage: "Use a given `CONFIG_FILE` when running this command",
Value: "",
}
// forceFlag allows users to bypass prompts
forceFlag = cli.BoolFlag{
Name: "force, f",
Usage: "Bypass verification prompt",
}
// resetFlag allows automatic reset of analysis prior to the command
resetFlag = cli.BoolFlag{
Name: "reset, r",
Usage: "Reset database analysis",
}
// for output we often want a human readable option which produces a nice
// report instead of the simple csv style output
humanFlag = cli.BoolFlag{
Name: "human-readable, H",
Usage: "Print a report instead of csv",
}
blSortFlag = cli.StringFlag{
Name: "sort, s",
Usage: "Sort by conn (# of connections), uconn (# of unique connections), total_bytes (# of bytes)",
Value: "conn",
}
blConnFlag = cli.BoolFlag{
Name: "connected, C",
Usage: "Show hosts which were connected to this blacklisted entry",
}
)
// bootstrapCommands simply adds a given command to the allCommands array
func bootstrapCommands(commands ...cli.Command) {
for _, command := range commands {
command.Before = func(c *cli.Context) error {
//Get access to the logger
configFile := c.String("config")
res := resources.InitResources(configFile)
//Display args in logs
fields := log.Fields{
"Arguments": c.Args(),
}
//Display flag info in logs
for _, it := range c.GlobalFlagNames() {
if c.IsSet(it) {
fields["Global Flag("+it+")"] = c.GlobalGeneric(it)
}
}
for _, it := range c.FlagNames() {
if c.IsSet(it) {
fields["Flag("+it+")"] = c.Generic(it)
}
}
res.Log.WithFields(fields).Info("Running Command: " + command.Name)
return nil
}
allCommands = append(allCommands, command)
}
}
// Commands provides all of the defined commands to the front end
func Commands() []cli.Command {
return allCommands
}
//helper functions for formatting floats and integers
func f(f float64) string {
return strconv.FormatFloat(f, 'g', 6, 64)
}
func i(i int64) string {
return strconv.FormatInt(i, 10)
}