Skip to content

Commit

Permalink
Merge pull request #54 from markruler/support-local-address
Browse files Browse the repository at this point in the history
Support local address option
  • Loading branch information
nakabonne committed Oct 8, 2020
2 parents cccdee3 + 6a2592d commit 2a36d9a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
6 changes: 6 additions & 0 deletions attacker/attacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package attacker
import (
"context"
"math"
"net"
"net/http"
"time"

Expand Down Expand Up @@ -39,6 +40,7 @@ type Options struct {
KeepAlive bool
Connections int
HTTP2 bool
LocalAddr net.IPAddr

Attacker Attacker
}
Expand Down Expand Up @@ -72,6 +74,9 @@ func Attack(ctx context.Context, target string, resCh chan *Result, metricsCh ch
if opts.Connections == 0 {
opts.Connections = DefaultConnections
}
if opts.LocalAddr.IP == nil {
opts.LocalAddr = vegeta.DefaultLocalAddr
}
if opts.Attacker == nil {
opts.Attacker = vegeta.NewAttacker(
vegeta.Timeout(opts.Timeout),
Expand All @@ -81,6 +86,7 @@ func Attack(ctx context.Context, target string, resCh chan *Result, metricsCh ch
vegeta.Connections(opts.Connections),
vegeta.KeepAlive(opts.KeepAlive),
vegeta.HTTP2(opts.HTTP2),
vegeta.LocalAddr(opts.LocalAddr),
)
}

Expand Down
30 changes: 18 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
Expand All @@ -30,18 +31,19 @@ var (
)

type cli struct {
rate int
duration time.Duration
timeout time.Duration
method string
headers []string
body string
bodyFile string
maxBody int64
workers uint64
maxWorkers uint64
connections int
http2 bool
rate int
duration time.Duration
timeout time.Duration
method string
headers []string
body string
bodyFile string
maxBody int64
workers uint64
maxWorkers uint64
connections int
http2 bool
localAddress string

debug bool
version bool
Expand Down Expand Up @@ -78,6 +80,7 @@ func parseFlags(stdout, stderr io.Writer) (*cli, error) {
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.http2, "http2", true, "Issue HTTP/2 requests to servers which support it. (default true)")
flagSet.StringVarP(&c.localAddress, "local-address", "L", "0.0.0.0", "Local IP address.")
flagSet.Usage = c.usage
if err := flagSet.Parse(os.Args[1:]); err != nil {
if !errors.Is(err, flag.ErrHelp) {
Expand Down Expand Up @@ -169,6 +172,8 @@ func (c *cli) makeOptions() (*attacker.Options, error) {
body = b
}

localAddr := net.IPAddr{IP: net.ParseIP(c.localAddress)}

return &attacker.Options{
Rate: c.rate,
Duration: c.duration,
Expand All @@ -182,6 +187,7 @@ func (c *cli) makeOptions() (*attacker.Options, error) {
MaxWorkers: c.maxWorkers,
Connections: c.connections,
HTTP2: c.http2,
LocalAddr: localAddr,
}, nil
}

Expand Down
27 changes: 14 additions & 13 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,20 @@ func TestParseFlags(t *testing.T) {
{
name: "with default options",
want: &cli{
rate: 50,
duration: time.Second * 10,
timeout: time.Second * 30,
method: "GET",
headers: []string{},
maxBody: -1,
keepAlive: true,
workers: 10,
maxWorkers: math.MaxUint64,
connections: 10000,
stdout: new(bytes.Buffer),
stderr: new(bytes.Buffer),
http2: true,
rate: 50,
duration: time.Second * 10,
timeout: time.Second * 30,
method: "GET",
headers: []string{},
maxBody: -1,
keepAlive: true,
workers: 10,
maxWorkers: math.MaxUint64,
connections: 10000,
stdout: new(bytes.Buffer),
stderr: new(bytes.Buffer),
http2: true,
localAddress: "0.0.0.0",
},
wantErr: false,
},
Expand Down

0 comments on commit 2a36d9a

Please sign in to comment.