Skip to content

Commit

Permalink
feat: Add TLS Cert File flag
Browse files Browse the repository at this point in the history
  • Loading branch information
dagehuifei committed Oct 12, 2024
1 parent 830de14 commit 00601ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
20 changes: 18 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package main

import (
"crypto/tls"
"flag"
"log"
"os"
Expand All @@ -20,6 +21,8 @@ func main() {
tcpAddr := flag.String("tcp", ":1883", "network address for TCP listener")
wsAddr := flag.String("ws", ":1882", "network address for Websocket listener")
infoAddr := flag.String("info", ":8080", "network address for web info dashboard listener")
tlsCertFile := flag.String("tls-cert-file", "", "TLS certificate file")
tlsKeyFile := flag.String("tls-key-file", "", "TLS key file")
flag.Parse()

sigs := make(chan os.Signal, 1)
Expand All @@ -30,12 +33,25 @@ func main() {
done <- true
}()

var tlsConfig *tls.Config

if tlsCertFile != nil && tlsKeyFile != nil && *tlsCertFile != "" && *tlsKeyFile != "" {
cert, err := tls.LoadX509KeyPair(*tlsCertFile, *tlsKeyFile)
if err != nil {
return
}
tlsConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
}
}

server := mqtt.New(nil)
_ = server.AddHook(new(auth.AllowHook), nil)

tcp := listeners.NewTCP(listeners.Config{
ID: "t1",
Address: *tcpAddr,
ID: "t1",
Address: *tcpAddr,
TLSConfig: tlsConfig,
})
err := server.AddListener(tcp)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion examples/tls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func main() {
done <- true
}()

cert, err := tls.X509KeyPair(testCertificate, testPrivateKey)
// Load tls cert from your cert file
cert, err := tls.LoadX509KeyPair("replace_your_cert.pem", "replace_your_cert.key")

//cert, err := tls.X509KeyPair(testCertificate, testPrivateKey)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 00601ca

Please sign in to comment.