From b48e3b858b71d332a97d5f32a52e8b28546517f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20M=C3=BCnch?= Date: Sat, 6 Jul 2024 22:08:21 +0200 Subject: [PATCH] Mosquitto auth improvements (#61) * 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 --- src/canbus.go | 4 ++-- src/mqtt.go | 44 ++++++++++++++++++++------------------------ 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/canbus.go b/src/canbus.go index 75c0573..0b1bf8e 100644 --- a/src/canbus.go +++ b/src/canbus.go @@ -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") @@ -70,4 +70,4 @@ func canPublish(frame can.Frame) { if err != nil { slog.Error("canbus: error while publishing CAN-Frame", "error", err) } -} +} \ No newline at end of file diff --git a/src/mqtt.go b/src/mqtt.go index 4a8a062..7fea002 100644 --- a/src/mqtt.go +++ b/src/mqtt.go @@ -3,8 +3,8 @@ package main import ( MQTT "github.com/eclipse/paho.mqtt.golang" "log/slog" + "net/url" "os" - "strings" ) var client MQTT.Client @@ -12,27 +12,28 @@ 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) @@ -40,11 +41,6 @@ func mqttStart(suppliedString string) { 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 { @@ -69,4 +65,4 @@ func mqttPublish(topic string, payload []byte) { token.Wait() slog.Debug("mqtt: published message", "payload", payload, "topic", topic) mqttSubscribe(topic) -} +} \ No newline at end of file