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

Mosquitto auth improvements #61

Merged
merged 2 commits into from
Jul 6, 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
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)
}
}
Loading