Skip to content

Commit

Permalink
Addedd support for h2c requests (HTTP/2 without TLS) (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzdeb authored and tsenart committed May 8, 2018
1 parent cd8b31b commit a744dc7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func attackCmd() command {
fs.StringVar(&opts.keyf, "key", "", "TLS client PEM encoded private key file")
fs.Var(&opts.rootCerts, "root-certs", "TLS root certificate files (comma separated list)")
fs.BoolVar(&opts.http2, "http2", true, "Send HTTP/2 requests when supported by the server")
fs.BoolVar(&opts.h2c, "h2c", false, "Send HTTP/2 requests without TLS encryption")
fs.BoolVar(&opts.insecure, "insecure", false, "Ignore invalid server TLS certificates")
fs.BoolVar(&opts.lazy, "lazy", false, "Read targets lazily")
fs.DurationVar(&opts.duration, "duration", 0, "Duration of the test [0 = forever]")
Expand Down Expand Up @@ -62,6 +63,7 @@ type attackOpts struct {
keyf string
rootCerts csl
http2 bool
h2c bool
insecure bool
lazy bool
duration time.Duration
Expand Down Expand Up @@ -133,6 +135,7 @@ func attack(opts *attackOpts) (err error) {
vegeta.KeepAlive(opts.keepalive),
vegeta.Connections(opts.connections),
vegeta.HTTP2(opts.http2),
vegeta.H2C(opts.h2c),
)

res := atk.Attack(tr, opts.rate, opts.duration)
Expand Down
15 changes: 15 additions & 0 deletions lib/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ func HTTP2(enabled bool) func(*Attacker) {
}
}

// H2C returns a functional option which enables H2C support on requests
// performed by an Attacker
func H2C(enabled bool) func(*Attacker) {
return func(a *Attacker) {
if tr := a.client.Transport.(*http.Transport); enabled {
a.client.Transport = &http2.Transport{
AllowHTTP: true,
DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
return tr.Dial(network, addr)
},
}
}
}
}

// Attack reads its Targets from the passed Targeter and attacks them at
// the rate specified for duration time. When the duration is zero the attack
// runs until Stop is called. Results are put into the returned channel as soon
Expand Down

0 comments on commit a744dc7

Please sign in to comment.