From 2948cf7f07588288d0c06a234c5eb3b50d46b9ec Mon Sep 17 00:00:00 2001 From: BJ Dweck Date: Tue, 6 Oct 2020 11:49:02 +0200 Subject: [PATCH] can override config dir using environment variable; block while creating Tor hidden service in case Tor isn't quite ready; added logging --- torch-pub.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/torch-pub.py b/torch-pub.py index 9bd7888..aec7adb 100644 --- a/torch-pub.py +++ b/torch-pub.py @@ -8,6 +8,7 @@ import json import configparser import argparse from datetime import datetime +from os import environ parser = argparse.ArgumentParser(description='Broadcast SSH hidden service hostname via MQTT') @@ -18,9 +19,14 @@ args = parser.parse_args() configPath = args.configPath +if "TORCH_CONFIG_DIR" in environ: + configPath = environ.get("TORCH_CONFIG_DIR") + if not configPath.endswith("/"): configPath = configPath + "/" +print("Using torch configuration path: " + configPath) + config = configparser.ConfigParser() config.read(configPath + "torch.conf") @@ -51,12 +57,14 @@ with Controller.from_port(port = torControllerPort) as controller: controller, protocolInfo.cookie_path) - service = controller.create_ephemeral_hidden_service( - sshPort, - detached = True) + print("Connected to Tor on port %s" % (torControllerPort)) + + service = controller.create_ephemeral_hidden_service(sshPort) onionAddress = "%s.onion" % (service.service_id) + print("Created Tor Hidden Service for local port %s at %s" % (sshPort, onionAddress)) + payload = { 'clientId': clientID, 'timestamp': datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)"), @@ -65,6 +73,7 @@ payload = { } client = mqtt.Client() +protocol = "mqtt" if mqttRequireCertificate: client.tls_set( @@ -72,6 +81,7 @@ if mqttRequireCertificate: certfile = mqttCertFile, keyfile = mqttKeyFile, cert_reqs=ssl.CERT_REQUIRED) + protocol = "mqtts" if mqttBrokerHost.endswith(".onion"): client.proxy_set(proxy_type=socks.SOCKS5, proxy_addr="localhost", proxy_port=torProxyPort) @@ -79,4 +89,8 @@ if mqttBrokerHost.endswith(".onion"): client.connect(mqttBrokerHost, mqttBrokerPort, 60) client.publish(mqttTopic, json.dumps(payload)) +print("Connected to MQTT Broker at %s://%s:%s/%s" % (protocol, mqttBrokerHost, mqttBrokerPort, mqttTopic)) +print("Published payload: " + json.dumps(payload)) + client.disconnect() +print("Disconnected from MQTT Broker")