mirror of
https://github.com/wwhai/mqtt-benchmark.git
synced 2025-10-09 09:30:14 +08:00
Adds tls connect support to client
This commit is contained in:
25
main.go
25
main.go
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
@@ -69,6 +70,8 @@ func main() {
|
||||
clients = flag.Int("clients", 10, "Number of clients to start")
|
||||
format = flag.String("format", "text", "Output format: text|json")
|
||||
quiet = flag.Bool("quiet", false, "Suppress logs while running")
|
||||
cert = flag.String("cert", "", "File path to your client certificate in PEM format")
|
||||
key = flag.String("key", "", "File path to your private key in PEM format")
|
||||
)
|
||||
|
||||
flag.Parse()
|
||||
@@ -80,6 +83,11 @@ func main() {
|
||||
log.Fatalf("Invalid arguments: messages count should be > 1, given: %v", *count)
|
||||
}
|
||||
|
||||
var tlsConfig *tls.Config
|
||||
if *cert != "" && *key != "" {
|
||||
tlsConfig = generateTlsConfig(*cert, *key)
|
||||
}
|
||||
|
||||
resCh := make(chan *RunResults)
|
||||
start := time.Now()
|
||||
for i := 0; i < *clients; i++ {
|
||||
@@ -97,6 +105,7 @@ func main() {
|
||||
MsgQoS: byte(*qos),
|
||||
Quiet: *quiet,
|
||||
WaitTimeout: time.Duration(*wait) * time.Millisecond,
|
||||
TlsConfig: tlsConfig,
|
||||
}
|
||||
go c.Run(resCh)
|
||||
}
|
||||
@@ -192,3 +201,19 @@ func printResults(results []*RunResults, totals *TotalResults, format string) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func generateTlsConfig(certFile string, keyFile string) *tls.Config {
|
||||
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
|
||||
if err != nil {
|
||||
log.Fatalf("Error reading certificate files: %v", err)
|
||||
}
|
||||
|
||||
cfg := tls.Config{
|
||||
ClientAuth: tls.NoClientCert,
|
||||
ClientCAs: nil,
|
||||
InsecureSkipVerify: true,
|
||||
Certificates: []tls.Certificate{cert},
|
||||
}
|
||||
|
||||
return &cfg
|
||||
}
|
||||
|
Reference in New Issue
Block a user