Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2b42f58803 | |||
| f53f2f6309 | |||
| 03cd5ee028 | |||
| fff4b994e1 | |||
| 13c399133c | |||
| 940a41669f | |||
| e9b43da768 | |||
| c14b628558 |
@@ -136,8 +136,8 @@ jobs:
|
|||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Set up QEMU
|
# - name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v3
|
# uses: docker/setup-qemu-action@v3
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.5.2
|
||||||
|
|
||||||
|
- Improved logging and error handling
|
||||||
|
|
||||||
## 0.5.1 - 2026-10-11
|
## 0.5.1 - 2026-10-11
|
||||||
|
|
||||||
- Bug fix
|
- Bug fix
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import datetime
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from flask import Flask, jsonify, render_template, request
|
from flask import Flask, jsonify, render_template, request
|
||||||
@@ -14,6 +15,16 @@ from mqtt_worker import MqttWorker
|
|||||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(name)s %(message)s")
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(name)s %(message)s")
|
||||||
log = logging.getLogger("main")
|
log = logging.getLogger("main")
|
||||||
|
|
||||||
|
|
||||||
|
def _log_uncaught(exc_type, exc, tb):
|
||||||
|
# Python prints uncaught exceptions straight to stderr, bypassing
|
||||||
|
# logging entirely -- which is why a crash traceback had no timestamp
|
||||||
|
# while every other line did. Route it through the logger instead.
|
||||||
|
log.critical("uncaught exception, add-on is exiting", exc_info=(exc_type, exc, tb))
|
||||||
|
|
||||||
|
|
||||||
|
sys.excepthook = _log_uncaught
|
||||||
|
|
||||||
OPTIONS_PATH = "/data/options.json"
|
OPTIONS_PATH = "/data/options.json"
|
||||||
SSL_DIR = "/ssl"
|
SSL_DIR = "/ssl"
|
||||||
BROKER_CERT_FILENAME = "mosquitto-server.crt"
|
BROKER_CERT_FILENAME = "mosquitto-server.crt"
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ See DEVICE_PROTOCOL.md for the full spec devices should implement.
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import threading
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
@@ -38,23 +39,65 @@ class MqttWorker:
|
|||||||
if username:
|
if username:
|
||||||
self.client.username_pw_set(username, password or None)
|
self.client.username_pw_set(username, password or None)
|
||||||
self.client.on_connect = self._on_connect
|
self.client.on_connect = self._on_connect
|
||||||
|
self.client.on_disconnect = self._on_disconnect
|
||||||
self.client.on_message = self._on_message
|
self.client.on_message = self._on_message
|
||||||
|
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self.client.connect(self.host, self.port, keepalive=60)
|
# Returns immediately: the connect (and its retries) happen in the
|
||||||
thread = threading.Thread(target=self.client.loop_forever, daemon=True)
|
# background, so a broker that isn't reachable YET -- e.g. Home
|
||||||
|
# Assistant just booted and Mosquitto hasn't started -- doesn't
|
||||||
|
# crash the whole add-on or block the web UI from coming up.
|
||||||
|
thread = threading.Thread(target=self._run, daemon=True, name="mqtt-worker")
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
|
def _run(self):
|
||||||
|
self.client.reconnect_delay_set(min_delay=1, max_delay=30)
|
||||||
|
delay = 1
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
# This is the plain TCP connect. MQTT itself (CONNECT /
|
||||||
|
# CONNACK, auth) only starts once this succeeds.
|
||||||
|
self.client.connect(self.host, self.port, keepalive=60)
|
||||||
|
except OSError as exc:
|
||||||
|
# ConnectionRefusedError (nothing listening), gaierror
|
||||||
|
# (hostname doesn't resolve), timeouts, etc. all land here.
|
||||||
|
log.warning(
|
||||||
|
"cannot reach MQTT broker at %s:%s (%s: %s) - retrying in %ss. "
|
||||||
|
"Check the mqtt_host / mqtt_port options, and that Mosquitto is running.",
|
||||||
|
self.host, self.port, type(exc).__name__, exc, delay,
|
||||||
|
)
|
||||||
|
time.sleep(delay)
|
||||||
|
delay = min(delay * 2, 30)
|
||||||
|
continue
|
||||||
|
# Connected at the TCP level. From here paho handles drops and
|
||||||
|
# reconnects itself (using reconnect_delay_set above); this only
|
||||||
|
# returns if disconnect() is ever called explicitly.
|
||||||
|
self.client.loop_forever()
|
||||||
|
return
|
||||||
|
|
||||||
def _on_connect(self, client, userdata, flags, rc):
|
def _on_connect(self, client, userdata, flags, rc):
|
||||||
|
if rc != 0:
|
||||||
|
# The TCP connection worked but the BROKER said no -- a different
|
||||||
|
# failure from "connection refused". Usually bad credentials.
|
||||||
|
log.error(
|
||||||
|
"MQTT broker rejected the connection: %s "
|
||||||
|
"(check the mqtt_username / mqtt_password options)",
|
||||||
|
mqtt.connack_string(rc),
|
||||||
|
)
|
||||||
|
return
|
||||||
request_topic = f"{self.topic_prefix}/+/request"
|
request_topic = f"{self.topic_prefix}/+/request"
|
||||||
renew_topic = f"{self.topic_prefix}/+/renew"
|
renew_topic = f"{self.topic_prefix}/+/renew"
|
||||||
client.subscribe(request_topic, qos=1)
|
client.subscribe(request_topic, qos=1)
|
||||||
client.subscribe(renew_topic, qos=1)
|
client.subscribe(renew_topic, qos=1)
|
||||||
log.info("connected to broker, subscribed to %s and %s", request_topic, renew_topic)
|
log.info("connected to broker, subscribed to %s and %s", request_topic, renew_topic)
|
||||||
|
|
||||||
|
def _on_disconnect(self, client, userdata, rc):
|
||||||
|
if rc != 0:
|
||||||
|
log.warning("lost connection to MQTT broker (rc=%s); retrying automatically", rc)
|
||||||
|
|
||||||
def _on_message(self, client, userdata, msg):
|
def _on_message(self, client, userdata, msg):
|
||||||
parts = msg.topic.split("/")
|
parts = msg.topic.split("/")
|
||||||
if len(parts) < 2:
|
if len(parts) < 2:
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
name: "MQTT Certificate Manager"
|
name: "MQTT Certificate Manager"
|
||||||
version: "0.5.1"
|
version: "0.5.2"
|
||||||
slug: "mqtt_cert_manager"
|
slug: "mqtt_cert_manager"
|
||||||
description: "Local CA that issues and manages TLS client certificates for MQTT nodes"
|
description: "Local CA that issues and manages TLS client certificates for MQTT nodes"
|
||||||
url: "https://gitea.viratak.com/bitopa/virarepo/src/branch/main/mqtt-cert-manager"
|
url: "https://gitea.viratak.com/bitopa/virarepo/src/branch/main/mqtt-cert-manager"
|
||||||
|
|||||||
@@ -1,5 +1,17 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.5.5
|
||||||
|
|
||||||
|
- Fixed logging issue
|
||||||
|
|
||||||
|
## 0.5.4
|
||||||
|
|
||||||
|
- Minor bug fix
|
||||||
|
|
||||||
|
## 0.5.1 - 2026-10-11
|
||||||
|
|
||||||
|
- Minor bug fixed
|
||||||
|
|
||||||
## 0.5.0 - 2026-10-11
|
## 0.5.0 - 2026-10-11
|
||||||
|
|
||||||
- Added pre-built docker image
|
- Added pre-built docker image
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
name: Visquitto
|
name: Visquitto
|
||||||
version: "0.5.0"
|
version: "0.5.5"
|
||||||
slug: visquitto
|
slug: visquitto
|
||||||
description: Second private Mosquitto broker for bitopa nodes
|
description: Second private Mosquitto broker for bitopa nodes
|
||||||
url: "https://gitea.viratak.com/bitopa/virarepo/src/branch/main/visquitto"
|
url: "https://gitea.viratak.com/bitopa/virarepo/src/branch/main/visquitto"
|
||||||
|
|||||||
+9
-9
@@ -34,13 +34,8 @@ chmod 600 "$CERTDIR/key.pem"
|
|||||||
|
|
||||||
# ---- Defaults in /config (regenerated at every start) ----
|
# ---- Defaults in /config (regenerated at every start) ----
|
||||||
cat > "$CONFIG/mosquitto.conf" <<EOF
|
cat > "$CONFIG/mosquitto.conf" <<EOF
|
||||||
protocol mqtt
|
$(jq -r '.log_dest[]? | "log_dest \(.)"' /data/options.json)
|
||||||
allow_anonymous false
|
$(jq -r '.log_type[]? | "log_type \(.)"' /data/options.json)
|
||||||
|
|
||||||
log_dest stdout
|
|
||||||
log_type error
|
|
||||||
log_type warning
|
|
||||||
log_type notice
|
|
||||||
log_timestamp_format %Y-%m-%d %H:%M:%S
|
log_timestamp_format %Y-%m-%d %H:%M:%S
|
||||||
|
|
||||||
persistence true
|
persistence true
|
||||||
@@ -55,6 +50,7 @@ include_dir $SHARE
|
|||||||
# Plain listener (host port 1993): password users only
|
# Plain listener (host port 1993): password users only
|
||||||
listener 1883
|
listener 1883
|
||||||
protocol mqtt
|
protocol mqtt
|
||||||
|
allow_anonymous false
|
||||||
plugin_use password_file
|
plugin_use password_file
|
||||||
plugin_opt_password_file $PASSWD
|
plugin_opt_password_file $PASSWD
|
||||||
plugin_use acl_file
|
plugin_use acl_file
|
||||||
@@ -63,6 +59,7 @@ plugin_opt_acl_file $DATA/acl_plain
|
|||||||
# TLS listener (host port 9993): client certificate required
|
# TLS listener (host port 9993): client certificate required
|
||||||
listener 8883
|
listener 8883
|
||||||
protocol mqtt
|
protocol mqtt
|
||||||
|
allow_anonymous false
|
||||||
cafile $CERTDIR/ca.pem
|
cafile $CERTDIR/ca.pem
|
||||||
certfile $CERTDIR/cert.pem
|
certfile $CERTDIR/cert.pem
|
||||||
keyfile $CERTDIR/key.pem
|
keyfile $CERTDIR/key.pem
|
||||||
@@ -76,7 +73,6 @@ plugin_opt_acl_file $DATA/acl_tls
|
|||||||
EOF
|
EOF
|
||||||
|
|
||||||
# plain ACL
|
# plain ACL
|
||||||
echo "# Default ACL for the plain listener: no rules, all access denied" > "$CONFIG/acl_plain"
|
|
||||||
cat > "$CONFIG/acl_plain" <<EOF
|
cat > "$CONFIG/acl_plain" <<EOF
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# Not certified devices
|
# Not certified devices
|
||||||
@@ -100,13 +96,17 @@ topic read cert/enroll/+/renew
|
|||||||
EOF
|
EOF
|
||||||
|
|
||||||
# TLS ACL
|
# TLS ACL
|
||||||
echo "# Default ACL for the TLS listener: no rules, all access denied" > "$CONFIG/acl_tls"
|
|
||||||
cat > "$CONFIG/acl_tls" <<EOF
|
cat > "$CONFIG/acl_tls" <<EOF
|
||||||
# Default rule for every certificate client. %u is the certificate CN.
|
# Default rule for every certificate client. %u is the certificate CN.
|
||||||
pattern readwrite clients/%u/#
|
pattern readwrite clients/%u/#
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# ---- User customization in /share (created once, never overwritten) ----
|
# ---- User customization in /share (created once, never overwritten) ----
|
||||||
|
if grep -qE '^\s*(listener|port|bind_address)\b' "$SHARE/mosquitto.conf"; then
|
||||||
|
echo "ERROR: $SHARE/mosquitto.conf must not define listeners (listener/port/bind_address)." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
if [ ! -f "$SHARE/mosquitto.conf" ]; then
|
if [ ! -f "$SHARE/mosquitto.conf" ]; then
|
||||||
echo "# Extra global Mosquitto options (loaded with include_dir, before the listeners)" > "$SHARE/mosquitto.conf"
|
echo "# Extra global Mosquitto options (loaded with include_dir, before the listeners)" > "$SHARE/mosquitto.conf"
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user