From 1ae27253199522ff0ee3b4e7f8cadd4261441912 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Mon, 6 Aug 2018 02:50:08 -0500 Subject: [PATCH] improved onionr startup and port binding --- onionr/api.py | 1 - onionr/netcontroller.py | 2 +- onionr/onionr.py | 51 ++++++++++++++++++++++++++--------------- onionr/onionrutils.py | 1 + 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/onionr/api.py b/onionr/api.py index 3ccebff5..d41245d1 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -492,7 +492,6 @@ class API: self.http_server.serve_forever() except KeyboardInterrupt: pass - #app.run(host=self.host, port=bindPort, debug=False, threaded=True) except Exception as e: logger.error(str(e)) logger.fatal('Failed to start client on ' + self.host + ':' + str(bindPort) + ', exiting...') diff --git a/onionr/netcontroller.py b/onionr/netcontroller.py index 386c334b..19f355e4 100644 --- a/onionr/netcontroller.py +++ b/onionr/netcontroller.py @@ -97,7 +97,7 @@ DataDirectory data/tordata/ elif 'Opening Socks listener' in line.decode(): logger.debug(line.decode().replace('\n', '')) else: - logger.fatal('Failed to start Tor. Try killing any other Tor processes owned by this user.') + logger.fatal('Failed to start Tor. Maybe a stray instance of Tor used by Onionr is still running?') return False except KeyboardInterrupt: logger.fatal("Got keyboard interrupt.") diff --git a/onionr/onionr.py b/onionr/onionr.py index 8ed144ee..0de17850 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -138,10 +138,9 @@ class Onionr: if type(config.get('client.hmac')) is type(None): config.set('client.hmac', base64.b16encode(os.urandom(32)).decode('utf-8'), savefile=True) if type(config.get('client.port')) is type(None): - #while True: - randomPort = random.randint(1024, 65535) - # if self.onionrUtils.checkPort(randomPort): - # break + randomPort = 0 + while randomPort < 1024: + randomPort = self.onionrCore._crypto.secrets.randbelow(65535) config.set('client.port', randomPort, savefile=True) if type(config.get('client.participate')) is type(None): config.set('client.participate', True, savefile=True) @@ -544,22 +543,36 @@ class Onionr: Starts the Onionr communication daemon ''' communicatorDaemon = './communicator2.py' - if not os.environ.get("WERKZEUG_RUN_MAIN") == "true": - if self._developmentMode: - logger.warn('DEVELOPMENT MODE ENABLED (THIS IS LESS SECURE!)', timestamp = False) - net = NetController(config.get('client.port', 59496)) - logger.info('Tor is starting...') - if not net.startTor(): - sys.exit(1) - logger.info('Started .onion service: ' + logger.colors.underline + net.myID) - logger.info('Our Public key: ' + self.onionrCore._crypto.pubKey) - time.sleep(1) - #TODO make runable on windows - subprocess.Popen([communicatorDaemon, "run", str(net.socksPort)]) - logger.debug('Started communicator') - events.event('daemon_start', onionr = self) - self.api = api.API(self.debug) + apiThread = Thread(target=api.API, args=(self.debug,)) + apiThread.start() + try: + time.sleep(3) + except KeyboardInterrupt: + logger.info('Got keyboard interrupt') + time.sleep(1) + self.onionrUtils.localCommand('shutdown') + else: + if apiThread.isAlive(): + if self._developmentMode: + logger.warn('DEVELOPMENT MODE ENABLED (THIS IS LESS SECURE!)', timestamp = False) + net = NetController(config.get('client.port', 59496)) + logger.info('Tor is starting...') + if not net.startTor(): + sys.exit(1) + logger.info('Started .onion service: ' + logger.colors.underline + net.myID) + logger.info('Our Public key: ' + self.onionrCore._crypto.pubKey) + time.sleep(1) + #TODO make runable on windows + subprocess.Popen([communicatorDaemon, "run", str(net.socksPort)]) + logger.debug('Started communicator') + events.event('daemon_start', onionr = self) + try: + while True: + time.sleep(5) + except KeyboardInterrupt: + self.onionrCore.daemonQueueAdd('shutdown') + self.onionrUtils.localCommand('shutdown') return def killDaemon(self): diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 531568ee..afa86ef6 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -553,6 +553,7 @@ class OnionrUtils: ''' Do a get request through a local tor or i2p instance ''' + retData = False if proxyType == 'tor': if port == 0: raise onionrexceptions.MissingPort('Socks port required for Tor HTTP get request')