Skip to content

Commit

Permalink
Mosquitto auth improvements (#61)
Browse files Browse the repository at this point in the history
* Mention CAN-Interface name on connection error

* Rework MQTT Authentication

* use net/url package to parse URL
* Set username and password directly instead of function
* remove one useless variable layer
* client ID is now lowercase "can2mqtt"

---------

Co-authored-by: Malte Muench <[email protected]>
  • Loading branch information
mxmxchere and Malte Muench authored Jul 6, 2024
1 parent eec11c0 commit b48e3b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/canbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func canStart(canInterface string) {
slog.Debug("canbus: initializing CAN-Bus", "interface", canInterface)
bus, err = can.NewBusForInterfaceWithName(canInterface)
if err != nil {
slog.Error("canbus: error while initializing CAN-Bus", "error", err)
slog.Error("canbus: error while initializing CAN-Bus", "interface", canInterface, "error", err)
os.Exit(1)
}
slog.Info("canbus: connected to CAN")
Expand Down Expand Up @@ -70,4 +70,4 @@ func canPublish(frame can.Frame) {
if err != nil {
slog.Error("canbus: error while publishing CAN-Frame", "error", err)
}
}
}
44 changes: 20 additions & 24 deletions src/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,44 @@ package main
import (
MQTT "github.com/eclipse/paho.mqtt.golang"
"log/slog"
"net/url"
"os"
"strings"
)

var client MQTT.Client
var user, pw string

// uses the connectString to establish a connection to the MQTT
// broker
func mqttStart(suppliedString string) {
connectString := suppliedString
if strings.Contains(suppliedString, "@") {
// looks like authentication is required for this server
userPasswordHost := strings.TrimPrefix(suppliedString, "tcp://")
userPassword, host, found := strings.Cut(userPasswordHost, "@")
user, pw, found = strings.Cut(userPassword, ":")
if !found {
slog.Error("mqtt: missing colon(:) between username and password", "connect string", suppliedString)
os.Exit(1)
}
connectString = "tcp://" + host
func mqttStart(URL string) {
// parse the supplied URL
u, err := url.Parse(URL)
if err != nil {
slog.Error("while parsing URL", "url", URL, "error", err)
os.Exit(1)
}
clientSettings := MQTT.NewClientOptions().AddBroker(connectString)
clientSettings.SetClientID("CAN2MQTT")

// create MQTT Client
clientSettings := MQTT.NewClientOptions().AddBroker(u.Scheme + "://" + u.Host)
clientSettings.SetClientID("can2mqtt")
clientSettings.SetDefaultPublishHandler(handleMQTT)
if strings.Contains(suppliedString, "@") {
clientSettings.SetCredentialsProvider(userPwCredProv)
if u.User != nil {
clientSettings.SetUsername(u.User.Username())
password, passwdSet := u.User.Password()
if passwdSet {
clientSettings.SetPassword(password)
}
}

client = MQTT.NewClient(clientSettings)
slog.Debug("mqtt: starting connection", "connectString", connectString)
slog.Debug("mqtt: starting connection", "connectString", URL)
if token := client.Connect(); token.Wait() && token.Error() != nil {
slog.Error("mqtt: could not connect to mqtt", "error", token.Error())
os.Exit(1)
}
slog.Info("mqtt: connected to mqtt")
}

// credentialsProvider
func userPwCredProv() (username, password string) {
return user, pw
}

// subscribe to a new topic
func mqttSubscribe(topic string) {
if token := client.Subscribe(topic, 0, nil); token.Wait() && token.Error() != nil {
Expand All @@ -69,4 +65,4 @@ func mqttPublish(topic string, payload []byte) {
token.Wait()
slog.Debug("mqtt: published message", "payload", payload, "topic", topic)
mqttSubscribe(topic)
}
}

0 comments on commit b48e3b8

Please sign in to comment.