forked from tomnomnom/meg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.go
202 lines (165 loc) · 4.5 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
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
package main
import (
"flag"
"fmt"
"os"
"strconv"
)
// 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 saveStatusArgs []int
func (s *saveStatusArgs) Set(val string) error {
i, _ := strconv.Atoi(val)
*s = append(*s, i)
return nil
}
func (s saveStatusArgs) String() string {
return "string"
}
func (s saveStatusArgs) Includes(search int) bool {
for _, status := range s {
if status == search {
return true
}
}
return false
}
type config struct {
body string
concurrency int
delay int
headers headerArgs
followLocation bool
method string
saveStatus saveStatusArgs
timeout int
verbose bool
paths string
hosts string
output string
noHeaders bool
requester requester
}
func processArgs() config {
// body param
body := ""
flag.StringVar(&body, "body", "", "")
flag.StringVar(&body, "b", "", "")
// 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 params
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 params
var saveStatus saveStatusArgs
flag.Var(&saveStatus, "savestatus", "")
flag.Var(&saveStatus, "s", "")
// 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, "")
// no headers
noHeaders := false
flag.BoolVar(&noHeaders, "no-headers", 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{
body: body,
concurrency: concurrency,
delay: delay,
headers: headers,
followLocation: followLocation,
method: method,
saveStatus: saveStatus,
timeout: timeout,
requester: requesterFn,
verbose: verbose,
paths: paths,
hosts: hosts,
output: output,
noHeaders: noHeaders,
}
}
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 += " -b, --body <val> Set the request body\n"
h += " -c, --concurrency <val> Set the concurrency level (default: 20)\n"
h += " -d, --delay <millis> Milliseconds between requests to the same host (default: 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)
}
}