Added log messaging

This commit is contained in:
B.J. Dweck 2020-10-21 12:31:45 +02:00
parent 16085fd594
commit 43bece3c57

View File

@ -30,8 +30,12 @@ def main():
print("Using torch configuration path: " + config_path) print("Using torch configuration path: " + config_path)
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(config_path + "torch.conf")
configuration_file_path = config_path + "torch.conf"
print("Reading configuration file at '%s'" % (configuration_file_path))
config.read(configuration_file_path)
tor_proxy_host = config['tor'].get('ProxyHost', fallback="localhost")
tor_proxy_port = config['tor'].getint('ProxyPort', fallback=9050) tor_proxy_port = config['tor'].getint('ProxyPort', fallback=9050)
tor_controller_port = config['tor'].getint('ControllerPort', fallback=9051) tor_controller_port = config['tor'].getint('ControllerPort', fallback=9051)
@ -40,6 +44,7 @@ def main():
mqtt_config = config['mqtt'] mqtt_config = config['mqtt']
mqtt_broker_host = mqtt_config.get('BrokerHost', fallback="localhost") mqtt_broker_host = mqtt_config.get('BrokerHost', fallback="localhost")
mqtt_broker_port = mqtt_config.getint('BrokerPort', fallback=1883) mqtt_broker_port = mqtt_config.getint('BrokerPort', fallback=1883)
mqtt_broker_using_tor = mqtt_broker_host.endswith(".onion")
client_id = mqtt_config.get('ClientID', fallback=socket.gethostname()) client_id = mqtt_config.get('ClientID', fallback=socket.gethostname())
mqtt_topic = mqtt_config.get('Topic', fallback="torch/%s/onion_url" % client_id) mqtt_topic = mqtt_config.get('Topic', fallback="torch/%s/onion_url" % client_id)
@ -51,34 +56,27 @@ def main():
mqtt_cert_file = config_path + mqtt_config.get('CertFile') mqtt_cert_file = config_path + mqtt_config.get('CertFile')
mqtt_key_file = config_path + mqtt_config.get('KeyFile') mqtt_key_file = config_path + mqtt_config.get('KeyFile')
print("Connecting to local TOR controller on port %s" % tor_proxy_port)
with Controller.from_port(port=tor_controller_port) as controller: with Controller.from_port(port=tor_controller_port) as controller:
protocol_info = stem.connection.get_protocolinfo(controller) protocol_info = stem.connection.get_protocolinfo(controller)
stem.connection.authenticate_safecookie( stem.connection.authenticate_safecookie(controller, protocol_info.cookie_path)
controller,
protocol_info.cookie_path)
print("Connected to Tor on port %s" % tor_controller_port) print("Creating TOR Hidden Service...")
service = controller.create_ephemeral_hidden_service(ssh_port, detached=True) service = controller.create_ephemeral_hidden_service(ssh_port, detached=True)
onion_address = "%s.onion" % service.service_id onion_address = "%s.onion" % service.service_id
print("Created Tor Hidden Service for local port %s at %s" % (ssh_port, onion_address)) print("Created Tor Hidden Service for local service on port %s at %s" % (ssh_port, onion_address))
payload = {
'clientId': client_id,
'timestamp': datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)"),
'onionAddress': onion_address,
'sshPort': ssh_port
}
client = mqtt.Client() client = mqtt.Client()
protocol = "mqtt" protocol = "mqtt"
if mqtt_broker_host.endswith(".onion"): if mqtt_broker_using_tor:
client.proxy_set(proxy_type=socks.SOCKS5, proxy_addr="localhost", proxy_port=tor_proxy_port) client.proxy_set(proxy_type=socks.SOCKS5, proxy_addr=tor_proxy_host, proxy_port=tor_proxy_port)
else: else:
if mqtt_require_certificate: if mqtt_require_certificate:
protocol = "mqtts" protocol = "mqtts"
@ -88,10 +86,23 @@ def main():
keyfile=mqtt_key_file, keyfile=mqtt_key_file,
cert_reqs=ssl.CERT_REQUIRED) cert_reqs=ssl.CERT_REQUIRED)
print("Connecting to MQTT broker: %s://%s:%s/%s" % (protocol, mqtt_broker_host, mqtt_broker_port, mqtt_topic))
if mqtt_broker_using_tor:
print("--> Using TOR proxy: %s:%s" % (tor_proxy_host, tor_proxy_port))
client.connect(mqtt_broker_host, mqtt_broker_port, 60) client.connect(mqtt_broker_host, mqtt_broker_port, 60)
client.publish(mqtt_topic, json.dumps(payload)) print("Connected to MQTT Broker")
print("Connected to MQTT Broker at %s://%s:%s/%s" % (protocol, mqtt_broker_host, mqtt_broker_port, mqtt_topic))
print("Published payload: " + json.dumps(payload)) payload = json.dumps({
'clientId': client_id,
'timestamp': datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)"),
'onionAddress': onion_address,
'sshPort': ssh_port
})
print("Publishing payload: " + payload)
client.publish(mqtt_topic, payload)
print("Published payload!")
client.disconnect() client.disconnect()
print("Disconnected from MQTT Broker") print("Disconnected from MQTT Broker")