mirror of
https://github.com/pyMC-dev/pyMC_Repeater.git
synced 2026-08-01 22:42:54 +02:00
Add configurable JWT token expiry and WebSocket transport support for MQTT
This commit is contained in:
+9
-1
@@ -56,6 +56,10 @@ repeater:
|
||||
# JWT secret key for signing tokens (auto-generated if not provided)
|
||||
# Generate with: python -c "import secrets; print(secrets.token_hex(32))"
|
||||
jwt_secret: ""
|
||||
|
||||
# JWT token expiry time in minutes (default: 60 minutes / 1 hour)
|
||||
# Controls how long users stay logged in before needing to re-authenticate
|
||||
jwt_expiry_minutes: 60
|
||||
|
||||
# Mesh Network Configuration
|
||||
mesh:
|
||||
@@ -171,7 +175,11 @@ mqtt:
|
||||
|
||||
# MQTT broker settings
|
||||
broker: "localhost"
|
||||
port: 1883 # Use 8883 for TLS/SSL
|
||||
port: 1883 # Use 8883 for TLS/SSL, 80/443/9001 for WebSockets
|
||||
|
||||
# Use WebSocket transport instead of standard TCP
|
||||
# Typically uses ports: 80 (ws://), 443 (wss://), or 9001
|
||||
use_websockets: false
|
||||
|
||||
# Authentication (optional)
|
||||
username: null
|
||||
|
||||
@@ -29,7 +29,12 @@ class MQTTHandler:
|
||||
return
|
||||
|
||||
try:
|
||||
self.client = mqtt.Client()
|
||||
# Use WebSocket transport if configured, otherwise use standard TCP
|
||||
transport = "websockets" if self.mqtt_config.get("use_websockets", False) else "tcp"
|
||||
self.client = mqtt.Client(transport=transport)
|
||||
|
||||
if transport == "websockets":
|
||||
logger.info("Using WebSocket transport for MQTT")
|
||||
|
||||
# Configure TLS/SSL if enabled
|
||||
tls_config = self.mqtt_config.get("tls", {})
|
||||
@@ -71,11 +76,12 @@ class MQTTHandler:
|
||||
broker = self.mqtt_config.get("broker", "localhost")
|
||||
port = self.mqtt_config.get("port", 1883)
|
||||
|
||||
secure = "(TLS)" if tls_config.get("enabled", False) else ""
|
||||
logger.info(f"Connecting to MQTT broker {broker}:{port} {secure}...")
|
||||
|
||||
self.client.connect(broker, port, 60)
|
||||
self.client.loop_start()
|
||||
|
||||
secure = "(TLS)" if tls_config.get("enabled", False) else ""
|
||||
logger.info(f"MQTT client connected to {broker}:{port} {secure}")
|
||||
logger.info(f"MQTT client successfully connected")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to initialize MQTT: {e}")
|
||||
|
||||
@@ -218,9 +218,10 @@ class HTTPStatsServer:
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save JWT secret to config: {e}")
|
||||
|
||||
# Initialize JWT handler (15 minute expiry)
|
||||
self.jwt_handler = JWTHandler(jwt_secret, expiry_minutes=15)
|
||||
logger.info("JWT handler initialized")
|
||||
# Initialize JWT handler with configurable expiry (default 1 hour)
|
||||
jwt_expiry_minutes = security_config.get("jwt_expiry_minutes", 60)
|
||||
self.jwt_handler = JWTHandler(jwt_secret, expiry_minutes=jwt_expiry_minutes)
|
||||
logger.info(f"JWT handler initialized (token expiry: {jwt_expiry_minutes} minutes)")
|
||||
|
||||
# Initialize API token manager
|
||||
storage_dir = self.config.get("storage", {}).get("storage_dir", ".")
|
||||
|
||||
Reference in New Issue
Block a user