From d3f57fe3e78c9f43841b40c1b7abe4877e30dfc6 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Mon, 25 Mar 2019 18:46:25 -0500 Subject: [PATCH] bug fixes for direct connnections --- onionr/api.py | 4 ++-- onionr/communicator.py | 2 +- onionr/onionrservices/bootstrapservice.py | 28 +++++++++++++---------- onionr/onionrservices/connectionserver.py | 11 ++++----- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/onionr/api.py b/onionr/api.py index 4f7276aa..e542bd35 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -39,7 +39,7 @@ class FDSafeHandler(WSGIHandler): except Timeout as ex: raise -def setBindIP(filePath, writeOut=True): +def setBindIP(filePath=''): '''Set a random localhost IP to a specified file (intended for private or public API localhost IPs)''' if config.get('general.random_bind_ip', True): hostOctets = [str(127), str(random.randint(0x02, 0xFF)), str(random.randint(0x02, 0xFF)), str(random.randint(0x02, 0xFF))] @@ -55,7 +55,7 @@ def setBindIP(filePath, writeOut=True): s.close() else: data = '127.0.0.1' - if writeOut: + if filePath != '': with open(filePath, 'w') as bindFile: bindFile.write(data) return data diff --git a/onionr/communicator.py b/onionr/communicator.py index 670e08fa..bb56ca19 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -111,7 +111,7 @@ class OnionrCommunicatorDaemon: if config.get('general.socket_servers', False): self.services = onionrservices.OnionrServices(self._core) self.active_services = [] - OnionrCommunicatorTimers(self, servicecreator.service_creator, 5, maxThreads=10, myArgs=(self,)) + OnionrCommunicatorTimers(self, servicecreator.service_creator, 5, maxThreads=50, myArgs=(self,)) else: self.services = None deniableBlockTimer = OnionrCommunicatorTimers(self, self.daemonTools.insertDeniableBlock, 180, requiresPeer=True, maxThreads=1) diff --git a/onionr/onionrservices/bootstrapservice.py b/onionr/onionrservices/bootstrapservice.py index f1e0b35c..95ae0158 100644 --- a/onionr/onionrservices/bootstrapservice.py +++ b/onionr/onionrservices/bootstrapservice.py @@ -17,10 +17,10 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import time +import time, threading, uuid from gevent.pywsgi import WSGIServer, WSGIHandler from stem.control import Controller -from flask import Flask +from flask import Flask, Response import core from netcontroller import getOpenPort @@ -40,6 +40,7 @@ def bootstrap_client_service(peer, core_inst=None, bootstrap_timeout=300): bootstrap_address = '' shutdown = False + bs_id = str(uuid.uuid4()) @bootstrap_app.route('/ping') def get_ping(): @@ -47,25 +48,28 @@ def bootstrap_client_service(peer, core_inst=None, bootstrap_timeout=300): @bootstrap_app.route('/bs/
', methods=['POST']) def get_bootstrap(address): - if core_inst._utils.validateID(address): + if core_inst._utils.validateID(address + '.onion'): # Set the bootstrap address then close the server - bootstrap_address = address - shutdown = True - return "success" + bootstrap_address = address + '.onion' + core_inst.keyStore.put(bs_id, bootstrap_address) + http_server.stop() + return Response("success") + else: + return Response("") with Controller.from_port(port=core_inst.config.get('tor.controlPort')) as controller: # Connect to the Tor process for Onionr controller.authenticate(core_inst.config.get('tor.controlpassword')) # Create the v3 onion service - response = controller.create_ephemeral_hidden_service({80: bootstrap_port}, key_type = 'NEW', await_publication = True) + response = controller.create_ephemeral_hidden_service({80: bootstrap_port}, key_type = 'NEW', key_content = 'ED25519-V3', await_publication = True) core_inst.insertBlock(response.service_id, header='con', sign=True, encryptType='asym', asymPeer=peer, disableForward=True, expire=(core_inst._utils.getEpoch() + bootstrap_timeout)) - # Run the bootstrap server - threading.Thread(target=http_server.serve_forever).start() + try: + http_server.serve_forever() + except TypeError: + pass # This line reached when server is shutdown by being bootstrapped - while not shutdown and not core_inst.killSockets: - time.sleep(1) # Now that the bootstrap server has received a server, return the address - return bootstrap_address + return core_inst.keyStore.get(bs_id) diff --git a/onionr/onionrservices/connectionserver.py b/onionr/onionrservices/connectionserver.py index 60aff03e..d8a5f773 100644 --- a/onionr/onionrservices/connectionserver.py +++ b/onionr/onionrservices/connectionserver.py @@ -23,7 +23,7 @@ from stem.control import Controller from flask import Flask import core, logger from netcontroller import getOpenPort -from api import setBindIP +import api class ConnectionServer: def __init__(self, peer, address, core_inst=None): @@ -38,9 +38,8 @@ class ConnectionServer: socks = core_inst.config.get('tor.socksport') # Load config for Tor socks port for proxy service_app = Flask(__name__) # Setup Flask app for server. service_port = getOpenPort() - service_ip = setBindIP() + service_ip = api.setBindIP() http_server = WSGIServer(('127.0.0.1', service_port), service_app, log=None) - # TODO define basic endpoints useful for direct connections like stats # TODO load endpoints from plugins @@ -52,10 +51,8 @@ class ConnectionServer: # Connect to the Tor process for Onionr controller.authenticate(core_inst.config.get('tor.controlpassword')) # Create the v3 onion service - response = controller.create_ephemeral_hidden_service({80: service_port}, await_publication = True, key_type='NEW') + response = controller.create_ephemeral_hidden_service({80: service_port}, await_publication = True, key_type='NEW', key_content = 'ED25519-V3') self.core_inst._utils.doPostRequest('http://' + address + '/bs/' + response.service_id, port=socks) logger.info('hosting on ' + response.service_id) - threading.Thread(target=http_server.serve_forever).start() - while not self.core_inst.killSockets: - time.sleep(1) + http_server.serve_forever() http_server.stop() \ No newline at end of file