Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow request with x509 cert #306

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions hey.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package main

import (
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -64,6 +65,9 @@ var (
disableKeepAlives = flag.Bool("disable-keepalive", false, "")
disableRedirects = flag.Bool("disable-redirects", false, "")
proxyAddr = flag.String("x", "", "")

certFile = flag.String("cert", "", "")
certKey = flag.String("cert-key", "", "")
)

var usage = `Usage: hey [options...] <url>
Expand Down Expand Up @@ -101,6 +105,9 @@ Options:
-disable-redirects Disable following of HTTP redirects
-cpus Number of used cpu cores.
(default for current machine is %d cores)

-cert file path to the X509 certificate
-cert-key file path to the X509 certidicate key
`

func main() {
Expand Down Expand Up @@ -221,6 +228,14 @@ func main() {

req.Header = header

var cert tls.Certificate
if *certFile != "" && *certKey != "" {
cert, err = tls.LoadX509KeyPair(*certFile, *certKey)
if err != nil {
usageAndExit(err.Error())
}
}

w := &requester.Work{
Request: req,
RequestBody: bodyAll,
Expand All @@ -234,6 +249,7 @@ func main() {
H2: *h2,
ProxyAddr: proxyURL,
Output: *output,
Cert: &cert,
}
w.Init()

Expand Down
7 changes: 7 additions & 0 deletions requester/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ type Work struct {
start time.Duration

report *report

// X509 Certificate
Cert *tls.Certificate
}

func (b *Work) writer() io.Writer {
Expand Down Expand Up @@ -245,6 +248,10 @@ func (b *Work) runWorkers() {
DisableKeepAlives: b.DisableKeepAlives,
Proxy: http.ProxyURL(b.ProxyAddr),
}
if b.Cert != nil {
tr.TLSClientConfig.Certificates = []tls.Certificate{*b.Cert}
}

if b.H2 {
http2.ConfigureTransport(tr)
} else {
Expand Down