From 58eb806b952c0b07854e571aac972c3a38f0bbdc Mon Sep 17 00:00:00 2001 From: BJ Dweck Date: Mon, 5 Oct 2020 13:49:34 +0200 Subject: [PATCH] Include ClientID (default: hostname) and default topic --- torch-pub.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/torch-pub.py b/torch-pub.py index b46c894..6420447 100644 --- a/torch-pub.py +++ b/torch-pub.py @@ -3,6 +3,8 @@ import stem.connection import paho.mqtt.client as mqtt import ssl import socks +import socket +import json import configparser configPath = "/etc/torch/" @@ -16,7 +18,8 @@ sshPort = config['ssh'].getint('Port', fallback = 22) mqttConfig = config['mqtt'] mqttBrokerHost = mqttConfig.get('BrokerHost', fallback = "localhost") 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( 'RequireCertificate', @@ -40,6 +43,8 @@ with Controller.from_port(port = torControllerPort) as controller: onionAddress = "%s.onion" % (service.service_id) +payload = { 'clientId': clientID, 'onionAddress': onionAddress } + client = mqtt.Client() if mqttRequireCertificate: @@ -54,5 +59,5 @@ if mqttBrokerHost.endswith(".onion"): client.tls_insecure_set(True) client.connect(mqttBrokerHost, mqttBrokerPort, 60) -client.publish(mqttTopic, onionAddress) +client.publish(mqttTopic, json.dumps(payload)) client.disconnect()