-
Notifications
You must be signed in to change notification settings - Fork 143
/
main.go
376 lines (331 loc) · 10.8 KB
/
main.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package main
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/nakabonne/ali/storage"
flag "github.com/spf13/pflag"
"github.com/nakabonne/ali/attacker"
"github.com/nakabonne/ali/gui"
)
var (
flagSet = flag.NewFlagSet("ali", flag.ContinueOnError)
// Automatically populated by goreleaser during build
version = "unversioned"
commit = "?"
date = "?"
)
type cli struct {
// options for attacker
rate int
duration time.Duration
timeout time.Duration
method string
headers []string
body string
bodyFile string
maxBody int64
workers uint64
maxWorkers uint64
connections int
noHTTP2 bool
localAddress string
noKeepAlive bool
buckets string
resolvers string
insecureSkipVerify bool
tlsCertFile string
tlsKeyFile string
caCert string
//options for gui
queryRange time.Duration
redrawInterval time.Duration
debug bool
version bool
stdout io.Writer
stderr io.Writer
}
func main() {
c, err := parseFlags(os.Stdout, os.Stderr)
if err != nil {
os.Exit(0)
}
os.Exit(c.run(flagSet.Args()))
}
func parseFlags(stdout, stderr io.Writer) (*cli, error) {
c := &cli{
stdout: stdout,
stderr: stderr,
}
flagSet.IntVarP(&c.rate, "rate", "r", attacker.DefaultRate, "The request rate per second to issue against the targets. Give 0 then it will send requests as fast as possible.")
flagSet.DurationVarP(&c.duration, "duration", "d", attacker.DefaultDuration, "The amount of time to issue requests to the targets. Give 0s for an infinite attack.")
flagSet.DurationVarP(&c.timeout, "timeout", "t", attacker.DefaultTimeout, "The timeout for each request. 0s means to disable timeouts.")
flagSet.StringVarP(&c.method, "method", "m", attacker.DefaultMethod, "An HTTP request method for each request.")
flagSet.StringArrayVarP(&c.headers, "header", "H", []string{}, "A request header to be sent. Can be used multiple times to send multiple headers.")
flagSet.StringVarP(&c.body, "body", "b", "", "A request body to be sent.")
flagSet.StringVarP(&c.bodyFile, "body-file", "B", "", "The path to file whose content will be set as the http request body.")
flagSet.Int64VarP(&c.maxBody, "max-body", "M", attacker.DefaultMaxBody, "Max bytes to capture from response bodies. Give -1 for no limit.")
flagSet.BoolVarP(&c.version, "version", "v", false, "Print the current version.")
flagSet.BoolVar(&c.debug, "debug", false, "Run in debug mode.")
flagSet.BoolVarP(&c.noKeepAlive, "no-keepalive", "K", false, "Don't use HTTP persistent connection.")
flagSet.Uint64VarP(&c.workers, "workers", "w", attacker.DefaultWorkers, "Amount of initial workers to spawn.")
flagSet.Uint64VarP(&c.maxWorkers, "max-workers", "W", attacker.DefaultMaxWorkers, "Amount of maximum workers to spawn.")
flagSet.IntVarP(&c.connections, "connections", "c", attacker.DefaultConnections, "Amount of maximum open idle connections per target host")
flagSet.BoolVar(&c.noHTTP2, "no-http2", false, "Don't issue HTTP/2 requests to servers which support it.")
flagSet.StringVar(&c.localAddress, "local-addr", "0.0.0.0", "Local IP address.")
flagSet.BoolVar(&c.insecureSkipVerify, "insecure", false, "Skip TLS verification")
flagSet.StringVar(&c.caCert, "cacert", "", "PEM ca certificate file")
flagSet.StringVar(&c.tlsCertFile, "cert", "", "PEM encoded tls certificate file to use")
flagSet.StringVar(&c.tlsKeyFile, "key", "", "PEM encoded tls private key file to use")
// TODO: Re-enable when making it capable of drawing histogram bar chart.
//flagSet.StringVar(&c.buckets, "buckets", "", "Histogram buckets; comma-separated list.")
flagSet.StringVar(&c.resolvers, "resolvers", "", "Custom DNS resolver addresses; comma-separated list.")
flagSet.DurationVar(&c.queryRange, "query-range", gui.DefaultQueryRange, "The results within the given time range will be drawn on the charts")
flagSet.DurationVar(&c.redrawInterval, "redraw-interval", gui.DefaultRedrawInterval, "Specify how often it redraws the screen")
flagSet.Usage = c.usage
if err := flagSet.Parse(os.Args[1:]); err != nil {
if !errors.Is(err, flag.ErrHelp) {
fmt.Fprintln(c.stderr, err)
}
return nil, err
}
return c, nil
}
func (c *cli) run(args []string) int {
if c.version {
fmt.Fprintf(c.stderr, "version=%s, commit=%s, buildDate=%s, os=%s, arch=%s\n", version, commit, date, runtime.GOOS, runtime.GOARCH)
return 0
}
if len(args) == 0 {
fmt.Fprintln(c.stderr, "no target given")
c.usage()
return 1
}
target := args[0]
if _, err := url.ParseRequestURI(target); err != nil {
fmt.Fprintf(c.stderr, "bad target URL: %v\n", err)
c.usage()
return 1
}
opts, err := c.makeAttackerOptions()
if err != nil {
fmt.Fprintln(c.stderr, err.Error())
c.usage()
return 1
}
// Data points out of query range get flushed to prevent using heap more than need.
s, err := storage.NewStorage(c.queryRange * 2)
if err != nil {
fmt.Fprintf(c.stderr, "failed to initialize time-series storage: %v\n", err)
c.usage()
return 1
}
a, err := attacker.NewAttacker(s, target, opts)
if err != nil {
fmt.Fprintf(c.stderr, "failed to initialize attacker: %v\n", err)
c.usage()
return 1
}
setDebug(nil, c.debug)
if err := gui.Run(target, s, a,
gui.Options{
QueryRange: c.queryRange,
RedrawInternal: c.redrawInterval,
},
); err != nil {
fmt.Fprintf(c.stderr, "failed to start application: %s\n", err.Error())
c.usage()
return 1
}
return 0
}
func (c *cli) usage() {
format := `Usage:
ali [flags] <target URL>
Flags:
%s
Examples:
ali --duration=10m --rate=100 http://host.xz
Author:
Ryo Nakao <[email protected]>
`
fmt.Fprintf(c.stderr, format, flagSet.FlagUsages())
}
// makeAttackerOptions gives back an options for attacker, with the CLI input.
func (c *cli) makeAttackerOptions() (*attacker.Options, error) {
if !validateMethod(c.method) {
return nil, fmt.Errorf("given method %q isn't an HTTP request method", c.method)
}
if c.duration < 0 {
return nil, fmt.Errorf("duration must be greater than or equal to 0s")
}
header := make(http.Header)
for _, hdr := range c.headers {
parts := strings.SplitN(hdr, ":", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("given header %q has a wrong format", hdr)
}
key, val := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
if key == "" || val == "" {
return nil, fmt.Errorf("given header %q has a wrong format", hdr)
}
// NOTE: Add key/value directly to the http.Header (map[string][]string).
// http.Header.Add() canonicalizes keys but the vegeta API is used to test systems that require case-sensitive headers.
header[key] = append(header[key], val)
}
if c.body != "" && c.bodyFile != "" {
return nil, fmt.Errorf(`only one of "--body" and "--body-file" can be specified`)
}
body := []byte(c.body)
if c.bodyFile != "" {
b, err := ioutil.ReadFile(c.bodyFile)
if err != nil {
return nil, fmt.Errorf("unable to open %q: %w", c.bodyFile, err)
}
body = b
}
localAddr := net.IPAddr{IP: net.ParseIP(c.localAddress)}
parsedBuckets, err := parseBucketOptions(c.buckets)
if err != nil {
return nil, fmt.Errorf("wrong buckets format %w", err)
}
parsedResolvers, err := parseResolvers(c.resolvers)
if err != nil {
return nil, err
}
var certs []tls.Certificate
if c.tlsCertFile != "" && c.tlsKeyFile != "" {
cert, err := tls.LoadX509KeyPair(c.tlsCertFile, c.tlsKeyFile)
if err != nil {
return nil, fmt.Errorf("error loading PEM key pair %w", err)
}
certs = append(certs, cert)
}
var caCertPool *x509.CertPool
if c.caCert != "" {
caCertPool = x509.NewCertPool()
caCert, err := ioutil.ReadFile(c.caCert)
if err != nil {
log.Fatal(err)
}
caCertPool.AppendCertsFromPEM(caCert)
}
return &attacker.Options{
Rate: c.rate,
Duration: c.duration,
Timeout: c.timeout,
Method: c.method,
Body: body,
MaxBody: c.maxBody,
Header: header,
KeepAlive: !c.noKeepAlive,
Workers: c.workers,
MaxWorkers: c.maxWorkers,
Connections: c.connections,
HTTP2: !c.noHTTP2,
LocalAddr: localAddr,
Buckets: parsedBuckets,
Resolvers: parsedResolvers,
InsecureSkipVerify: c.insecureSkipVerify,
TLSCertificates: certs,
CACertificatePool: caCertPool,
}, nil
}
func validateMethod(method string) bool {
switch method {
case http.MethodGet, http.MethodHead, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodConnect, http.MethodOptions, http.MethodTrace:
return true
}
return false
}
func parseBucketOptions(rawBuckets string) ([]time.Duration, error) {
if rawBuckets == "" {
return []time.Duration{}, nil
}
stringBuckets := strings.Split(rawBuckets, ",")
result := make([]time.Duration, len(stringBuckets))
for _, bucket := range stringBuckets {
trimmedBucket := strings.TrimSpace(bucket)
d, err := time.ParseDuration(trimmedBucket)
if err != nil {
return nil, err
}
result = append(result, d)
}
return result, nil
}
func parseResolvers(addrs string) ([]string, error) {
if addrs == "" {
return nil, nil
}
stringAddrs := strings.Split(addrs, ",")
result := make([]string, 0, len(stringAddrs))
for _, addr := range stringAddrs {
trimmedAddr := strings.TrimSpace(addr)
// if given address has no port, append "53"
if !strings.Contains(trimmedAddr, ":") {
trimmedAddr += ":53"
}
host, port, err := net.SplitHostPort(trimmedAddr)
if err != nil {
return nil, err
}
// validate port
_, err = strconv.ParseUint(port, 10, 16)
if err != nil {
return nil, fmt.Errorf("port of given address %q has a wrong format", addr)
}
// validate IP
if ip := net.ParseIP(host); ip == nil {
return nil, fmt.Errorf("given address %q has a wrong format", addr)
}
result = append(result, trimmedAddr)
}
return result, nil
}
// Makes a new file under the ~/.config/ali only when debug use.
func setDebug(w io.Writer, debug bool) {
if !debug {
log.SetOutput(io.Discard)
return
}
if w == nil {
dir, err := configDir()
if err != nil {
panic(err)
}
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
panic(err)
}
w, err = os.OpenFile(filepath.Join(dir, "debug.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
if err != nil {
panic(err)
}
}
log.SetOutput(w)
}
func configDir() (string, error) {
usr, err := user.Current()
if err == nil {
return filepath.Join(usr.HomeDir, ".config", "ali"), nil
}
homeDir := os.Getenv("HOME")
if homeDir == "" {
return "", fmt.Errorf("unable to get current user home directory: os/user lookup failed; $HOME is empty")
}
return filepath.Join(homeDir, ".config", "ali"), nil
}