can override config dir using environment variable; block while creating Tor hidden service in case Tor isn't quite ready; added logging

This commit is contained in:
B.J. Dweck 2020-10-06 11:49:02 +02:00
parent 4502060a8c
commit 2948cf7f07

View File

@ -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")