now kill tor more often when shutting down, use own tor data directory, and check for api crash to stop daemon

master
Kevin 2018-04-21 20:53:12 -05:00
parent c0e08eae79
commit 0ce3c7d940
3 changed files with 27 additions and 3 deletions

View File

@ -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 <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import sqlite3, requests, hmac, hashlib, time, sys, os, math, logger, urllib.parse, random 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: class OnionrCommunicate:
def __init__(self, debug, developmentMode): def __init__(self, debug, developmentMode):
@ -33,6 +33,7 @@ class OnionrCommunicate:
self._core = core.Core() self._core = core.Core()
self._utils = onionrutils.OnionrUtils(self._core) self._utils = onionrutils.OnionrUtils(self._core)
self._crypto = onionrcrypto.OnionrCrypto(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 self.highFailureAmount = 7
''' '''
@ -58,6 +59,9 @@ class OnionrCommunicate:
logger.debug('Communicator debugging enabled.') logger.debug('Communicator debugging enabled.')
torID = open('data/hs/hostname').read() torID = open('data/hs/hostname').read()
apiRunningCheckRate = 10
apiRunningCheckCount = 0
self.peerData = {} # Session data for peers (recent reachability, speed, etc) self.peerData = {} # Session data for peers (recent reachability, speed, etc)
if os.path.exists(self._core.queueDB): if os.path.exists(self._core.queueDB):
@ -103,9 +107,23 @@ class OnionrCommunicate:
if announceAttemptCount >= announceAttempts: if announceAttemptCount >= announceAttempts:
logger.warn('Unable to announce to ' + command[1]) logger.warn('Unable to announce to ' + command[1])
break 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) time.sleep(1)
self._netController.killTor()
return return
def getNewPeers(self): def getNewPeers(self):

View File

@ -52,6 +52,7 @@ class NetController:
torrcData = '''SocksPort ''' + str(self.socksPort) + ''' torrcData = '''SocksPort ''' + str(self.socksPort) + '''
HiddenServiceDir data/hs/ HiddenServiceDir data/hs/
HiddenServicePort 80 127.0.0.1:''' + str(self.hsPort) + ''' HiddenServicePort 80 127.0.0.1:''' + str(self.hsPort) + '''
DataDirectory data/tordata/
''' '''
torrc = open(self.torConfigLocation, 'w') torrc = open(self.torConfigLocation, 'w')
torrc.write(torrcData) torrc.write(torrcData)

View File

@ -550,6 +550,11 @@ class Onionr:
return return
def get_hostname(self): 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() Onionr()