Skip to content

Commit

Permalink
Adds tls connect support to client
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitja Živković committed Nov 16, 2020
1 parent e576ca4 commit 1acc37a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ The tool supports multiple concurrent clients, configurable message size, etc:
> mqtt-benchmark --help
Usage of mqtt-benchmark:
-broker="tcp://localhost:1883": MQTT broker endpoint as scheme://host:port
-cert="cert.pem": File path to your client certificate in PEM format
-clients=10: Number of clients to start
-count=100: Number of messages to send per client
-format="text": Output format: text|json
-key="key.pem": File path to your private key in PEM format
-password="": MQTT password (empty if auth disabled)
-qos=1: QoS for published messages
-quiet=false : Suppress logs while running (except errors and the result)
Expand Down
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"crypto/tls"
"fmt"
"log"
"time"
Expand All @@ -22,6 +23,7 @@ type Client struct {
MsgQoS byte
Quiet bool
WaitTimeout time.Duration
TlsConfig *tls.Config
}

func (c *Client) Run(res chan *RunResults) {
Expand Down Expand Up @@ -136,6 +138,10 @@ func (c *Client) pubMessages(in, out chan *Message, doneGen, donePub chan bool)
opts.SetUsername(c.BrokerUser)
opts.SetPassword(c.BrokerPass)
}
if c.TlsConfig != nil {
opts.SetTLSConfig(c.TlsConfig)
}

client := mqtt.NewClient(opts)
token := client.Connect()
token.Wait()
Expand Down
25 changes: 25 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
Expand Down Expand Up @@ -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()
Expand All @@ -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++ {
Expand All @@ -97,6 +105,7 @@ func main() {
MsgQoS: byte(*qos),
Quiet: *quiet,
WaitTimeout: time.Duration(*wait) * time.Millisecond,
TlsConfig: tlsConfig,
}
go c.Run(resCh)
}
Expand Down Expand Up @@ -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
}

0 comments on commit 1acc37a

Please sign in to comment.