work on sockets

This commit is contained in:
Kevin Froman 2018-09-22 00:01:17 -05:00
parent 4e8f7e2761
commit 759da55094
3 changed files with 51 additions and 10 deletions

View File

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

View File

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

View File

@ -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.)
retData = ''
try:
retData = self.connPool[peer]
except KeyError:
pass
return
def sendData(self, peer, data):
self.sendData[peer] = data