From 9fa06bbde747192dd901aaac54b34bcda89c3a2b Mon Sep 17 00:00:00 2001 From: j Date: Wed, 20 Jan 2016 11:15:11 +0530 Subject: [PATCH] use ephemeral_hidden_service if available --- oml/tor.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/oml/tor.py b/oml/tor.py index d43df76..9544ad4 100644 --- a/oml/tor.py +++ b/oml/tor.py @@ -8,6 +8,7 @@ import distutils import ox import stem from stem.control import Controller +from Crypto.PublicKey import RSA import settings import state @@ -164,24 +165,29 @@ class Tor(object): if not self.connected: return False controller = self.controller - logger.debug("FIXME: dont remove/add service if already defined") - controller.remove_hidden_service(self.dir) - result = controller.create_hidden_service( - self.dir, - settings.server_defaults['node_port'], - target_port=settings.server['node_port'] - ) - logger.debug('published node as https://%s:%s', result.hostname, settings.server_defaults['node_port']) - ''' - with open(settings.ssl_key_path) as fd: - key_content = fd.read() - ports = {9851: settings.server['node_port']} - response = controller.create_ephemeral_hidden_service(ports, - key_type='RSA1024', key_content=key_content, - detached=True, await_publication = True) - logger.debug('published node as https://%s.onion:%s', - settings.USER_ID, settings.server_defaults['node_port']) - ''' + if controller.get_version() >= stem.version.Requirement.ADD_ONION: + with open(settings.ssl_key_path, 'rb') as fd: + private_key = fd.read() + key_content = RSA.importKey(private_key).exportKey().decode() + key_content = ''.join(key_content.strip().split('\n')[1:-1]) + ports = {9851: settings.server['node_port']} + controller.remove_ephemeral_hidden_service(settings.USER_ID) + response = controller.create_ephemeral_hidden_service(ports, + key_type='RSA1024', key_content=key_content, + detached=True) + if response.is_ok(): + logger.debug('published node as https://%s.onion:%s', + settings.USER_ID, settings.server_defaults['node_port']) + else: + logger.debug('failed to publish node to tor') + else: + controller.remove_hidden_service(self.dir) + result = controller.create_hidden_service( + self.dir, + settings.server_defaults['node_port'], + target_port=settings.server['node_port'] + ) + logger.debug('published node as https://%s:%s', result.hostname, settings.server_defaults['node_port']) def depublish(self): if not self.connected: