Include ClientID (default: hostname) and default topic

This commit is contained in:
B.J. Dweck 2020-10-05 13:49:34 +02:00
parent d58745ca2e
commit 58eb806b95

View File

@ -3,6 +3,8 @@ import stem.connection
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
import ssl import ssl
import socks import socks
import socket
import json
import configparser import configparser
configPath = "/etc/torch/" configPath = "/etc/torch/"
@ -16,7 +18,8 @@ sshPort = config['ssh'].getint('Port', fallback = 22)
mqttConfig = config['mqtt'] mqttConfig = config['mqtt']
mqttBrokerHost = mqttConfig.get('BrokerHost', fallback = "localhost") mqttBrokerHost = mqttConfig.get('BrokerHost', fallback = "localhost")
mqttBrokerPort = mqttConfig.getint('BrokerPort', fallback = 1883) mqttBrokerPort = mqttConfig.getint('BrokerPort', fallback = 1883)
mqttTopic = mqttConfig.get('Topic', fallback = "default/topic") clientID = mqttConfig.get('ClientID', fallback = socket.gethostname())
mqttTopic = mqttConfig.get('Topic', fallback = "torch/%s/onion_url" % (clientID))
mqttRequireCertificate = mqttConfig.getboolean( mqttRequireCertificate = mqttConfig.getboolean(
'RequireCertificate', 'RequireCertificate',
@ -40,6 +43,8 @@ with Controller.from_port(port = torControllerPort) as controller:
onionAddress = "%s.onion" % (service.service_id) onionAddress = "%s.onion" % (service.service_id)
payload = { 'clientId': clientID, 'onionAddress': onionAddress }
client = mqtt.Client() client = mqtt.Client()
if mqttRequireCertificate: if mqttRequireCertificate:
@ -54,5 +59,5 @@ if mqttBrokerHost.endswith(".onion"):
client.tls_insecure_set(True) client.tls_insecure_set(True)
client.connect(mqttBrokerHost, mqttBrokerPort, 60) client.connect(mqttBrokerHost, mqttBrokerPort, 60)
client.publish(mqttTopic, onionAddress) client.publish(mqttTopic, json.dumps(payload))
client.disconnect() client.disconnect()