diff --git a/onionr/communicator.py b/onionr/communicator.py index 1a6ceb64..e8a27b24 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -20,7 +20,7 @@ and code to operate as a daemon, getting commands from the command queue databas along with this program. If not, see . ''' import sqlite3, requests, hmac, hashlib, time, sys, os, math, logger, urllib.parse, random -import core, onionrutils, onionrcrypto, onionrproofs, btc, config, onionrplugins as plugins +import core, onionrutils, onionrcrypto, netcontroller, onionrproofs, btc, config, onionrplugins as plugins class OnionrCommunicate: def __init__(self, debug, developmentMode): @@ -33,6 +33,7 @@ class OnionrCommunicate: self._core = core.Core() self._utils = onionrutils.OnionrUtils(self._core) self._crypto = onionrcrypto.OnionrCrypto(self._core) + self._netController = netcontroller.NetController(0) # arg is the HS port but not needed rn in this file self.highFailureAmount = 7 ''' @@ -58,6 +59,9 @@ class OnionrCommunicate: logger.debug('Communicator debugging enabled.') torID = open('data/hs/hostname').read() + apiRunningCheckRate = 10 + apiRunningCheckCount = 0 + self.peerData = {} # Session data for peers (recent reachability, speed, etc) if os.path.exists(self._core.queueDB): @@ -103,9 +107,23 @@ class OnionrCommunicate: if announceAttemptCount >= announceAttempts: logger.warn('Unable to announce to ' + command[1]) break + apiRunningCheckCount += 1 + # check if local API is up + if apiRunningCheckCount > apiRunningCheckRate: + if self._core._utils.localCommand('ping') != 'pong': + for i in range(4): + if self._utils.localCommand('ping') == 'pong': + apiRunningCheckCount = 0 + break # break for loop + time.sleep(1) + else: + # This executes if the api is NOT detected to be running + logger.error('Daemon detected API crash (or otherwise unable to reach API after long time, stopping)') + break # break main daemon loop + apiRunningCheckCount = 0 time.sleep(1) - + self._netController.killTor() return def getNewPeers(self): diff --git a/onionr/netcontroller.py b/onionr/netcontroller.py index af20f335..77149477 100644 --- a/onionr/netcontroller.py +++ b/onionr/netcontroller.py @@ -52,6 +52,7 @@ class NetController: torrcData = '''SocksPort ''' + str(self.socksPort) + ''' HiddenServiceDir data/hs/ HiddenServicePort 80 127.0.0.1:''' + str(self.hsPort) + ''' +DataDirectory data/tordata/ ''' torrc = open(self.torConfigLocation, 'w') torrc.write(torrcData) diff --git a/onionr/onionr.py b/onionr/onionr.py index a92eb77e..395dcb75 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -550,6 +550,11 @@ class Onionr: return def get_hostname(self): - return open('./data/hs/hostname', 'r').read() + retVal = '' + try: + with open('./data/hs/hostname', 'r') as hostname: + retval = retVal.read() + except FileNotFoundError: + return retVal Onionr()