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

Add TLS support for MQTT connection #98

Merged
merged 1 commit into from
Mar 14, 2025
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: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ services:
- PATH_PREFIX=/abrp # optional, default is /
- TZ=Europe/Berlin # set timezone
- MQTT=tcp://localhost:1883 # mqtt URL
- MQTT_USERNAME=username # optional, MQTT username
- MQTT_PASSWORD=password # optional, MQTT password
- MQTT_TLS=false # optional, set to true to enable TLS
- MQTT_TLS_SKIP_VERIFY=false # optional, set to true to skip TLS certificate verification
- TM_CAR_NUMBER=1 # teslamate car number
- ABRP_CAR_MODEL=xyz # check values via https://api.iternio.com/1/tlm/get_carmodels_list
- ABRP_TOKEN=xyz # car token
Expand Down
28 changes: 27 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"os"
"strconv"
"tm-to-abrp/internal/app"
)

Expand All @@ -16,6 +17,31 @@ func main() {
mqttAddress = "tcp://localhost:1883"
}

// Get MQTT credentials
mqttUsername := os.Getenv("MQTT_USERNAME")
mqttPassword := os.Getenv("MQTT_PASSWORD")

// Get TLS settings
mqttUseTLS := false
useTLSStr := os.Getenv("MQTT_TLS")
if useTLSStr != "" {
var err error
mqttUseTLS, err = strconv.ParseBool(useTLSStr)
if err != nil {
mqttUseTLS = false
}
}

mqttTlsSkipVerify := false
tlsSkipStr := os.Getenv("MQTT_TLS_SKIP_VERIFY")
if tlsSkipStr != "" {
var err error
mqttTlsSkipVerify, err = strconv.ParseBool(tlsSkipStr)
if err != nil {
mqttTlsSkipVerify = false
}
}

if os.Getenv("TZ") == "" {
panic("TZ environment variable not set")
}
Expand All @@ -40,6 +66,6 @@ func main() {
os.Getenv("ABRP_API_KEY"),
)

app.MessagesSubscribe(mqttAddress, &car)
app.MessagesSubscribe(mqttAddress, mqttUsername, mqttPassword, mqttUseTLS, mqttTlsSkipVerify, &car)
app.WebStart(port, &car)
}
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
golang.org/x/net v0.36.0 h1:vWF2fRbw4qslQsQzgFqZff+BItCvGFQqKzKIzx1rmoA=
golang.org/x/net v0.36.0/go.mod h1:bFmbeoIPfrw4sMHNhb4J9f6+tPziuGjq7Jk/38fxi1I=
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
19 changes: 18 additions & 1 deletion internal/app/messages.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
package app

import (
"crypto/tls"
"log"
"strings"

mqtt "github.com/eclipse/paho.mqtt.golang"
)

func MessagesSubscribe(mqttAddress string, car *Car) {
func MessagesSubscribe(mqttAddress string, username string, password string, useTLS bool, tlsSkipVerify bool, car *Car) {
log.Println("Connecting to MQTT server on " + mqttAddress)

opts := mqtt.NewClientOptions().AddBroker(mqttAddress).SetClientID("tm-to-abrp-car" + car.number)

// Add username and password if provided
if username != "" {
opts.SetUsername(username)
if password != "" {
opts.SetPassword(password)
}
}

// Configure TLS if needed
if useTLS {
tlsConfig := &tls.Config{
InsecureSkipVerify: tlsSkipVerify,
}
opts.SetTLSConfig(tlsConfig)
}

client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
Expand Down