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: