changed communicator to be in the same process

master
Kevin Froman 2019-01-20 12:09:53 -06:00
parent 6b25a9301c
commit d2e7ced776
5 changed files with 27 additions and 31 deletions

View File

@ -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():

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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)