Skip to content

Commit

Permalink
Enable to read body from file
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed Sep 16, 2020
1 parent b6045f5 commit 13fc5f5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,28 @@ Give the target URL and press Enter, then the attack will be launched with defau
### Options

#### Rate Limit

The request rate per time unit to issue against the targets.
Give 0 then it will send requests as fast as possible.
Default is `50`.

#### Duration
The amount of time to issue requests to the targets. Give `0s` for an infinite attack. Press `Ctrl-C` to stop.
Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". Default is `10s`.
Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
Default is `10s`.

#### Timeout
The timeout for each request. `0s` means to disable timeouts.
Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
Default is `30s`

**Method**
#### Method
An HTTP request method for each request.

**Header**
#### Header
A request header to be sent.

**Body**
#### Body
The file whose content will be set as the http request body.

## Features

Expand Down
14 changes: 12 additions & 2 deletions attacker/attacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package attacker

import (
"context"
"io/ioutil"
"net/http"
"time"

Expand All @@ -26,7 +27,7 @@ type Options struct {
Duration time.Duration
Timeout time.Duration
Method string
Body []byte
BodyFile string
Header http.Header

Attacker Attacker
Expand All @@ -52,12 +53,21 @@ func Attack(ctx context.Context, target string, resCh chan *Result, opts Options
if opts.Attacker == nil {
opts.Attacker = vegeta.NewAttacker(vegeta.Timeout(opts.Timeout))
}
var body []byte
if opts.BodyFile != "" {
if b, err := ioutil.ReadFile(opts.BodyFile); err != nil {
// TODO: Report to report widget
return nil
} else {
body = b
}
}

rate := vegeta.Rate{Freq: opts.Rate, Per: time.Second}
targeter := vegeta.NewStaticTargeter(vegeta.Target{
Method: opts.Method,
URL: target,
Body: opts.Body,
Body: body,
Header: opts.Header,
})

Expand Down
1 change: 0 additions & 1 deletion attacker/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

// Metrics wraps "vegeta.Metrics" to avoid dependency on it.
// TODO: Add more fields
type Metrics struct {
// Latencies holds computed request latency metrics.
Latencies vegeta.LatencyMetrics `json:"latencies"`
Expand Down
5 changes: 1 addition & 4 deletions gui/keybinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func makeOptions(w *widgets) (attacker.Options, error) {
duration time.Duration
timeout time.Duration
method string
body string
header = make(http.Header)
err error
)
Expand Down Expand Up @@ -97,8 +96,6 @@ func makeOptions(w *widgets) (attacker.Options, error) {
}
}

body = w.bodyInput.Read()

if s := w.headerInput.Read(); s != "" {
parts := strings.SplitN(s, ":", 2)
if len(parts) != 2 {
Expand All @@ -119,7 +116,7 @@ func makeOptions(w *widgets) (attacker.Options, error) {
Duration: duration,
Timeout: timeout,
Method: method,
Body: []byte(body),
BodyFile: w.bodyInput.Read(),
Header: header,
}, nil
}
Expand Down

0 comments on commit 13fc5f5

Please sign in to comment.