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

add --ca-cert option #81

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,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 @@ -29,6 +29,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 @@ -164,6 +164,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 @@ -154,8 +155,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),
}
}

// bind to source IP address if given, or if ipv4/ipv6 is forced
if src := c.String(defs.OptionSource); src != "" || (forceIPv4 || forceIPv6) {
Expand Down