forked from tomnomnom/meg
/
args.go
166 lines (135 loc) · 3.79 KB
/
args.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
package main
import (
"flag"
"fmt"
"os"
)
// headerArgs is the type used to store the header arguments
type headerArgs []string
func (h *headerArgs) Set(val string) error {
*h = append(*h, val)
return nil
}
func (h headerArgs) String() string {
return "string"
}
type config struct {
concurrency int
delay int
headers headerArgs
followLocation bool
method string
saveStatus int
timeout int
verbose bool
paths string
hosts string
output string
requester requester
}
func processArgs() config {
// concurrency param
concurrency := 20
flag.IntVar(&concurrency, "concurrency", 20, "")
flag.IntVar(&concurrency, "c", 20, "")
// delay param
delay := 5000
flag.IntVar(&delay, "delay", 5000, "")
flag.IntVar(&delay, "d", 5000, "")
// headers param
var headers headerArgs
flag.Var(&headers, "header", "")
flag.Var(&headers, "H", "")
// follow location param
followLocation := false
flag.BoolVar(&followLocation, "location", false, "")
flag.BoolVar(&followLocation, "L", false, "")
// method param
method := "GET"
flag.StringVar(&method, "method", "GET", "")
flag.StringVar(&method, "X", "GET", "")
// savestatus param
saveStatus := 0
flag.IntVar(&saveStatus, "savestatus", 0, "")
flag.IntVar(&saveStatus, "s", 0, "")
// timeout param
timeout := 10000
flag.IntVar(&timeout, "timeout", 10000, "")
flag.IntVar(&timeout, "t", 10000, "")
// rawhttp param
rawHTTP := false
flag.BoolVar(&rawHTTP, "rawhttp", false, "")
flag.BoolVar(&rawHTTP, "r", false, "")
// verbose param
verbose := false
flag.BoolVar(&verbose, "verbose", false, "")
flag.BoolVar(&verbose, "v", false, "")
flag.Parse()
// paths might be in a file, or it might be a single value
paths := flag.Arg(0)
if paths == "" {
paths = defaultPathsFile
}
// hosts are always in a file
hosts := flag.Arg(1)
if hosts == "" {
hosts = defaultHostsFile
}
// default the output directory to ./out
output := flag.Arg(2)
if output == "" {
output = defaultOutputDir
}
// set the requester function to use
requesterFn := goRequest
if rawHTTP {
requesterFn = rawRequest
}
return config{
concurrency: concurrency,
delay: delay,
headers: headers,
followLocation: followLocation,
method: method,
saveStatus: saveStatus,
timeout: timeout,
requester: requesterFn,
verbose: verbose,
paths: paths,
hosts: hosts,
output: output,
}
}
func init() {
flag.Usage = func() {
h := "Request many paths for many hosts\n\n"
h += "Usage:\n"
h += " meg [path|pathsFile] [hostsFile] [outputDir]\n\n"
h += "Options:\n"
h += " -c, --concurrency <val> Set the concurrency level (defaut: 20)\n"
h += " -d, --delay <millis> Milliseconds between requests to the same host (defaut: 5000)\n"
h += " -H, --header <header> Send a custom HTTP header\n"
h += " -L, --location Follow redirects / location header\n"
h += " -r, --rawhttp Use the rawhttp library for requests (experimental)\n"
h += " -s, --savestatus <status> Save only responses with specific status code\n"
h += " -t, --timeout <millis> Set the HTTP timeout (default: 10000)\n"
h += " -v, --verbose Verbose mode\n"
h += " -X, --method <method> HTTP method (default: GET)\n\n"
h += "Defaults:\n"
h += " pathsFile: ./paths\n"
h += " hostsFile: ./hosts\n"
h += " outputDir: ./out\n\n"
h += "Paths file format:\n"
h += " /robots.txt\n"
h += " /package.json\n"
h += " /security.txt\n\n"
h += "Hosts file format:\n"
h += " http://example.com\n"
h += " https://example.edu\n"
h += " https://example.net\n\n"
h += "Examples:\n"
h += " meg /robots.txt\n"
h += " meg paths.txt hosts.txt output\n"
fmt.Fprintf(os.Stderr, h)
}
}