From d2e7ced776f00708387648da068b3327bbd48421 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sun, 20 Jan 2019 12:09:53 -0600 Subject: [PATCH] changed communicator to be in the same process --- onionr/api.py | 4 ++-- onionr/communicator2.py | 37 +++++++++++++------------------------ onionr/onionr.py | 13 ++++++++++--- onionr/onionrusers.py | 2 +- onionr/serializeddata.py | 2 +- 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/onionr/api.py b/onionr/api.py index 88f5f69f..c6047dee 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -406,8 +406,8 @@ class API: @app.route('/getstats') def getStats(): - return Response("disabled") - #return Response(self._core.serializer.getStats()) + #return Response("disabled") + return Response(self._core.serializer.getStats()) @app.route('/getuptime') def showUptime(): diff --git a/onionr/communicator2.py b/onionr/communicator2.py index de55a4fc..873e40c7 100755 --- a/onionr/communicator2.py +++ b/onionr/communicator2.py @@ -25,11 +25,13 @@ import onionrdaemontools, onionrsockets, onionr, onionrproofs, proofofmemory import binascii from dependencies import secrets from defusedxml import minidom - +config.reload() class OnionrCommunicatorDaemon: - def __init__(self, debug, developmentMode): + def __init__(self, onionrInst, proxyPort, developmentMode=config.get('general.dev_mode', False)): + onionrInst.communicatorInst = self # configure logger and stuff onionr.Onionr.setupConfig('data/', self = self) + self.proxyPort = proxyPort self.isOnline = True # Assume we're connected to the internet @@ -37,7 +39,7 @@ class OnionrCommunicatorDaemon: self.timers = [] # initalize core with Tor socks port being 3rd argument - self.proxyPort = sys.argv[2] + self.proxyPort = proxyPort self._core = core.Core(torPort=self.proxyPort) # intalize NIST beacon salt and time @@ -49,9 +51,6 @@ class OnionrCommunicatorDaemon: # loop time.sleep delay in seconds self.delay = 1 - # time app started running for info/statistics purposes - self.startTime = self._core._utils.getEpoch() - # lists of connected peers and peers we know we can't reach currently self.onlinePeers = [] self.offlinePeers = [] @@ -90,8 +89,11 @@ class OnionrCommunicatorDaemon: # intended only for use by OnionrCommunicatorDaemon self.daemonTools = onionrdaemontools.DaemonTools(self) + # time app started running for info/statistics purposes + self.startTime = self._core._utils.getEpoch() - if debug or developmentMode: + if developmentMode: + print('enabling heartbeat') OnionrCommunicatorTimers(self, self.heartbeat, 30) # Set timers, function reference, seconds @@ -129,8 +131,6 @@ class OnionrCommunicatorDaemon: self.socketServer.start() self.socketClient = onionrsockets.OnionrSocketClient(self._core) - # Loads chat messages into memory - threading.Thread(target=self._chat.chatHandler).start() # Main daemon loop, mainly for calling timers, don't do any complex operations here to avoid locking try: @@ -513,6 +513,8 @@ class OnionrCommunicatorDaemon: response = '\n'.join(list(self.onlinePeers)).strip() if response == '': response = 'none' + elif cmd[0] == 'localCommand': + response = self._core._utils.localCommand(cmd[1]) elif cmd[0] == 'pex': for i in self.timers: if i.timerFunction.__name__ == 'lookupAdders': @@ -643,18 +645,5 @@ class OnionrCommunicatorTimers: self.count = -1 # negative 1 because its incremented at bottom self.count += 1 -shouldRun = False -debug = True -developmentMode = False -if config.get('general.dev_mode', True): - developmentMode = True -try: - if sys.argv[1] == 'run': - shouldRun = True -except IndexError: - pass -if shouldRun: - try: - OnionrCommunicatorDaemon(debug, developmentMode) - except Exception as e: - logger.error('Error occured in Communicator', error = e, timestamp = False) +def startCommunicator(onionrInst, proxyPort): + OnionrCommunicatorDaemon(onionrInst, proxyPort) \ No newline at end of file diff --git a/onionr/onionr.py b/onionr/onionr.py index 6ec90725..8523ec77 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -33,7 +33,7 @@ import onionrutils import netcontroller from netcontroller import NetController from onionrblockapi import Block -import onionrproofs, onionrexceptions, onionrusers +import onionrproofs, onionrexceptions, onionrusers, communicator2 try: from urllib3.contrib.socks import SOCKSProxyManager @@ -72,6 +72,7 @@ class Onionr: logger.error('Tor is not installed') sys.exit(1) + self.communicatorInst = None self.onionrCore = core.Core() #self.deleteRunFiles() self.onionrUtils = onionrutils.OnionrUtils(self.onionrCore) @@ -749,7 +750,13 @@ class Onionr: time.sleep(1) # TODO: make runable on windows - communicatorProc = subprocess.Popen([communicatorDaemon, 'run', str(net.socksPort)]) + #communicatorProc = subprocess.Popen([communicatorDaemon, 'run', str(net.socksPort)]) + + communicatorThread = Thread(target=communicator2.startCommunicator, args=(self, str(net.socksPort))) + communicatorThread.start() + + while self.communicatorInst is None: + time.sleep(0.1) # print nice header thing :) if config.get('general.display_header', True): @@ -769,7 +776,7 @@ class Onionr: #proc = psutil.Process() #print('api-files:',proc.open_files(), len(psutil.net_connections())) # Break if communicator process ends, so we don't have left over processes - if communicatorProc.poll() is not None: + if self.communicatorInst.shutdown: break if self.killed: break # Break out if sigterm for clean exit diff --git a/onionr/onionrusers.py b/onionr/onionrusers.py index 9671c5db..c5bf41d1 100644 --- a/onionr/onionrusers.py +++ b/onionr/onionrusers.py @@ -83,7 +83,7 @@ class OnionrUser: if self._core._utils.validatePubKey(forwardKey): retData = self._core._crypto.pubKeyEncrypt(data, forwardKey, encodedData=True, anonymous=True) else: - raise onionrexceptions.InvalidPubkey("No valid forward key available for this user") + raise onionrexceptions.InvalidPubkey("No valid forward secrecy key available for this user") #self.generateForwardKey() return (retData, forwardKey) diff --git a/onionr/serializeddata.py b/onionr/serializeddata.py index a7ff2e80..79143374 100644 --- a/onionr/serializeddata.py +++ b/onionr/serializeddata.py @@ -36,7 +36,7 @@ class SerializedData: def getStats(self): '''Return statistics about our node''' stats = {} - stats['uptime'] = self._core._utils.localCommand('getuptime') + stats['uptime'] = int(self._core.daemonQueueSimple('localCommand', 'getuptime')) stats['connectedNodes'] = self._core.daemonQueueSimple('connectedPeers') stats['blockCount'] = len(self._core.getBlockList()) return json.dumps(stats)