dhcp4: ensure MQTT topic names are printable (for mosquitto_sub)

This commit is contained in:
Michael Stapelberg 2021-06-03 21:05:33 +02:00
parent 3834acfa2b
commit cbadfe5128

View File

@ -34,6 +34,7 @@ import (
"sync"
"syscall"
"time"
"unicode"
"github.com/gokrazy/gokrazy"
"github.com/google/renameio"
@ -419,6 +420,15 @@ func newSrv(permDir string) (*srv, error) {
// MQTT requires valid UTF-8 and some brokers dont cope well with
// invalid UTF-8: https://github.com/fhmq/hmq/issues/104
identifier := strings.ToValidUTF8(latest.Hostname, "")
// Some MQTT clients (e.g. mosquitto_pub) dont cope well with topic
// names containing non-printable characters (see also
// https://twitter.com/zekjur/status/1347295676909158400):
identifier = strings.Map(func(r rune) rune {
if unicode.IsPrint(r) {
return r
}
return -1
}, identifier)
if identifier == "" {
identifier = latest.HardwareAddr
}