Skip to content

Commit 13e6df3

Browse files
authored
Add TLS support for MQTT connection (#98)
1 parent ed510c1 commit 13e6df3

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ services:
1616
- PATH_PREFIX=/abrp # optional, default is /
1717
- TZ=Europe/Berlin # set timezone
1818
- MQTT=tcp://localhost:1883 # mqtt URL
19+
- MQTT_USERNAME=username # optional, MQTT username
20+
- MQTT_PASSWORD=password # optional, MQTT password
21+
- MQTT_TLS=false # optional, set to true to enable TLS
22+
- MQTT_TLS_SKIP_VERIFY=false # optional, set to true to skip TLS certificate verification
1923
- TM_CAR_NUMBER=1 # teslamate car number
2024
- ABRP_CAR_MODEL=xyz # check values via https://api.iternio.com/1/tlm/get_carmodels_list
2125
- ABRP_TOKEN=xyz # car token

cmd/main.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"os"
5+
"strconv"
56
"tm-to-abrp/internal/app"
67
)
78

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

20+
// Get MQTT credentials
21+
mqttUsername := os.Getenv("MQTT_USERNAME")
22+
mqttPassword := os.Getenv("MQTT_PASSWORD")
23+
24+
// Get TLS settings
25+
mqttUseTLS := false
26+
useTLSStr := os.Getenv("MQTT_TLS")
27+
if useTLSStr != "" {
28+
var err error
29+
mqttUseTLS, err = strconv.ParseBool(useTLSStr)
30+
if err != nil {
31+
mqttUseTLS = false
32+
}
33+
}
34+
35+
mqttTlsSkipVerify := false
36+
tlsSkipStr := os.Getenv("MQTT_TLS_SKIP_VERIFY")
37+
if tlsSkipStr != "" {
38+
var err error
39+
mqttTlsSkipVerify, err = strconv.ParseBool(tlsSkipStr)
40+
if err != nil {
41+
mqttTlsSkipVerify = false
42+
}
43+
}
44+
1945
if os.Getenv("TZ") == "" {
2046
panic("TZ environment variable not set")
2147
}
@@ -40,6 +66,6 @@ func main() {
4066
os.Getenv("ABRP_API_KEY"),
4167
)
4268

43-
app.MessagesSubscribe(mqttAddress, &car)
69+
app.MessagesSubscribe(mqttAddress, mqttUsername, mqttPassword, mqttUseTLS, mqttTlsSkipVerify, &car)
4470
app.WebStart(port, &car)
4571
}

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
44
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
55
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
66
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
7-
golang.org/x/net v0.36.0 h1:vWF2fRbw4qslQsQzgFqZff+BItCvGFQqKzKIzx1rmoA=
8-
golang.org/x/net v0.36.0/go.mod h1:bFmbeoIPfrw4sMHNhb4J9f6+tPziuGjq7Jk/38fxi1I=
97
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
108
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
11-
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
12-
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
139
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
1410
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=

internal/app/messages.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
package app
22

33
import (
4+
"crypto/tls"
45
"log"
56
"strings"
67

78
mqtt "github.com/eclipse/paho.mqtt.golang"
89
)
910

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

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

16+
// Add username and password if provided
17+
if username != "" {
18+
opts.SetUsername(username)
19+
if password != "" {
20+
opts.SetPassword(password)
21+
}
22+
}
23+
24+
// Configure TLS if needed
25+
if useTLS {
26+
tlsConfig := &tls.Config{
27+
InsecureSkipVerify: tlsSkipVerify,
28+
}
29+
opts.SetTLSConfig(tlsConfig)
30+
}
31+
1532
client := mqtt.NewClient(opts)
1633
if token := client.Connect(); token.Wait() && token.Error() != nil {
1734
panic(token.Error())

0 commit comments

Comments
 (0)