From 759da5509427d60e4dfa0b1b9cba80f5b79b0c33 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 22 Sep 2018 00:01:17 -0500 Subject: [PATCH] work on sockets --- onionr/communicator2.py | 10 ++++++--- onionr/onionr.py | 5 ++--- onionr/onionrsockets.py | 46 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/onionr/communicator2.py b/onionr/communicator2.py index 56a8e8e6..108ecf0d 100755 --- a/onionr/communicator2.py +++ b/onionr/communicator2.py @@ -109,6 +109,7 @@ class OnionrCommunicatorDaemon: announceTimer.count = (cleanupTimer.frequency - 60) self.socketServer = onionrsockets.OnionrSocketServer(self._core) + self.socketClient = onionrsockets.OnionrSocketClient(self._core) # Main daemon loop, mainly for calling timers, don't do any complex operations here to avoid locking try: @@ -469,10 +470,13 @@ class OnionrCommunicatorDaemon: elif cmd[0] == 'uploadBlock': self.blockToUpload = cmd[1] threading.Thread(target=self.uploadBlock).start() - elif cmd[0] == 'addSocket': + elif cmd[0] == 'startSocket': socketInfo = json.loads(cmd[1]) - if socketInfo['reason'] in ('chat'): - onionrsockets.OnionrSocketClient(self._core, socketInfo['peer']) + peer = socketInfo['peer'] + reason = socketInfo['reason'] + self.socketServer.addSocket(peer, reason) + elif cmd[0] == 'connectSocket': + pass else: logger.info('Recieved daemonQueue command:' + cmd[0]) diff --git a/onionr/onionr.py b/onionr/onionr.py index 12e9b2fd..c1c07628 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -271,9 +271,8 @@ class Onionr: ''' def startChat(self): - peer = sys.argv[2] - socketInfo = json.dumps({'peer': '', 'address': peer, 'port': 1337, 'create': True, 'reason': 'chat'}) - self.onionrCore.daemonQueueAdd('startSocket', socketInfo) + data = json.dumps({'peer': sys.argv[2], 'reason': 'chat'}) + self.onionrCore.daemonQueueAdd('startSocket', data) def getCommands(self): return self.cmds diff --git a/onionr/onionrsockets.py b/onionr/onionrsockets.py index bb505c8c..73920f27 100644 --- a/onionr/onionrsockets.py +++ b/onionr/onionrsockets.py @@ -19,7 +19,7 @@ ''' import stem.control import socks, config, uuid -import onionrexceptions, time, requests +import onionrexceptions, time, requests, onionrblockapi from dependencies import secrets from flask import request, Response, abort @@ -27,10 +27,12 @@ class OnionrSocketServer: def __init__(self, coreInst): self.sockets = {} # pubkey: tor address self.connPool = {} + self.bindPort = 1337 self._core = coreInst self.responseData = {} self.killSocket = False + app = flask.Flask(__name__) http_server = WSGIServer((socket.service_id, bindPort), app) @@ -55,7 +57,7 @@ class OnionrSocketServer: def setResponseData(self, host, data): self.responseData[host] = data - def addSocket(self, peer): + def addSocket(self, peer, reason=''): bindPort = 1337 with stem.control.Controller.from_port(port=config.get('tor.controlPort')) as controller: controller.authenticate(config.get('tor.controlpassword')) @@ -65,7 +67,7 @@ class OnionrSocketServer: self.responseData[socket.service_id] = '' - self._core.insertBlock(uuid.uuid4(), header='startSocket', sign=True, encryptType='asym', asymPeer=peer, meta={}) + self._core.insertBlock(uuid.uuid4(), header='startSocket', sign=True, encryptType='asym', asymPeer=peer, meta={'reason': reason}) while not self.killSocket: time.sleep(3) @@ -75,11 +77,47 @@ class OnionrSocketClient: def __init__(self, coreInst): self.sockets = {} # pubkey: tor address self.connPool = {} + self.sendData = {} self.bindPort = 1337 self._core = coreInst self.response = '' self.request = '' self.connected = False + self.killSocket = False + def startSocket(self, peer): + address = '' + # Find the newest open socket for a given peer + for block in self._core.getBlocksByType('openSocket'): + block = onionrblockapi.Block(block, core=self._myCore) + if block.decrypt(): + if block.verifySig() and block.signer == peer: + address = block.getMetadata('address') + if self._core._utils.validateID(address): + # If we got their address, it is valid, and verified, we can break out + break + else: + address = '' + if address != '': + self.sockets[peer] = address + data = '' + while not self.killSocket: + try: + data = self.sendData[peer] + except KeyError: + pass + else: + self.sendData[peer] = '' + postData = {'data': data} + self.connPool[peer] = self._core._utils.doPostRequest('http://' + address + '/dc/', data=postData) + def getResponse(self, peer): - self._core._utils.doPostRequest(self.) \ No newline at end of file + retData = '' + try: + retData = self.connPool[peer] + except KeyError: + pass + return + + def sendData(self, peer, data): + self.sendData[peer] = data \ No newline at end of file