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 configparser
import argparse import argparse
from datetime import datetime from datetime import datetime
from os import environ
parser = argparse.ArgumentParser(description='Broadcast SSH hidden service hostname via MQTT') parser = argparse.ArgumentParser(description='Broadcast SSH hidden service hostname via MQTT')
@ -18,9 +19,14 @@ args = parser.parse_args()
configPath = args.configPath configPath = args.configPath
if "TORCH_CONFIG_DIR" in environ:
configPath = environ.get("TORCH_CONFIG_DIR")
if not configPath.endswith("/"): if not configPath.endswith("/"):
configPath = configPath + "/" configPath = configPath + "/"
print("Using torch configuration path: " + configPath)
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(configPath + "torch.conf") config.read(configPath + "torch.conf")
@ -51,12 +57,14 @@ with Controller.from_port(port = torControllerPort) as controller:
controller, controller,
protocolInfo.cookie_path) protocolInfo.cookie_path)
service = controller.create_ephemeral_hidden_service( print("Connected to Tor on port %s" % (torControllerPort))
sshPort,
detached = True) service = controller.create_ephemeral_hidden_service(sshPort)
onionAddress = "%s.onion" % (service.service_id) onionAddress = "%s.onion" % (service.service_id)
print("Created Tor Hidden Service for local port %s at %s" % (sshPort, onionAddress))
payload = { payload = {
'clientId': clientID, 'clientId': clientID,
'timestamp': datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)"), 'timestamp': datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)"),
@ -65,6 +73,7 @@ payload = {
} }
client = mqtt.Client() client = mqtt.Client()
protocol = "mqtt"
if mqttRequireCertificate: if mqttRequireCertificate:
client.tls_set( client.tls_set(
@ -72,6 +81,7 @@ if mqttRequireCertificate:
certfile = mqttCertFile, certfile = mqttCertFile,
keyfile = mqttKeyFile, keyfile = mqttKeyFile,
cert_reqs=ssl.CERT_REQUIRED) cert_reqs=ssl.CERT_REQUIRED)
protocol = "mqtts"
if mqttBrokerHost.endswith(".onion"): if mqttBrokerHost.endswith(".onion"):
client.proxy_set(proxy_type=socks.SOCKS5, proxy_addr="localhost", proxy_port=torProxyPort) 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.connect(mqttBrokerHost, mqttBrokerPort, 60)
client.publish(mqttTopic, json.dumps(payload)) 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() client.disconnect()
print("Disconnected from MQTT Broker")