Skip to content

Commit

Permalink
add --ca-cert option (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
dopstar committed Sep 10, 2024
1 parent 67adaa2 commit b1daf1c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ GLOBAL OPTIONS:
--upload-size value Size of payload being uploaded in KiB (default: 1024)
--secure Use HTTPS instead of HTTP when communicating with
LibreSpeed.org operated servers (default: false)
--ca-cert value Use the specified CA certificate PEM bundle file instead
of the system certificate trust store
--skip-cert-verify Skip verifying SSL certificate for HTTPS connections (self-signed certs) (default: false)
--no-pre-allocate Do not pre allocate upload data. Pre allocation is
enabled by default to improve upload performance. To
Expand Down
1 change: 1 addition & 0 deletions defs/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
OptionUploadSize = "upload-size"
OptionDuration = "duration"
OptionSecure = "secure"
OptionCACert = "ca-cert"
OptionSkipCertVerify = "skip-cert-verify"
OptionNoPreAllocate = "no-pre-allocate"
OptionVersion = "version"
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ func main() {
Usage: "Use HTTPS instead of HTTP when communicating with\n" +
"\tLibreSpeed.org operated servers",
},
&cli.StringFlag{
Name: defs.OptionCACert,
Usage: "Use the specified CA certificate PEM bundle file instead\n" +
"\tof the system certificate trust store",
},
&cli.BoolFlag{
Name: defs.OptionSkipCertVerify,
Usage: "Skip verifying SSL certificate for HTTPS connections (self-signed certs)",
Expand Down
22 changes: 20 additions & 2 deletions speedtest/speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package speedtest
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -160,8 +161,25 @@ func SpeedTest(c *cli.Context) error {
network = "ip"
}

transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: c.Bool(defs.OptionSkipCertVerify)}
transport := http.DefaultTransport.(*http.Transport).Clone()

if caCertFileName := c.String(defs.OptionCACert); caCertFileName != "" {
caCert, err := ioutil.ReadFile(caCertFileName)
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)

transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: c.Bool(defs.OptionSkipCertVerify),
RootCAs: caCertPool,
}
} else {
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: c.Bool(defs.OptionSkipCertVerify),
}
}

dialer := &net.Dialer{
Timeout: 30 * time.Second,
Expand Down

0 comments on commit b1daf1c

Please sign in to comment.