From e826bca19ef974db931e345173f971b451acaf5b Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Thu, 20 Sep 2018 12:04:58 -0500 Subject: [PATCH] work on sockets --- onionr/communicator2.py | 23 ++++++++++++++++++++--- onionr/core.py | 1 - onionr/onionr.py | 7 ++++++- onionr/onionrchat.py | 20 ++++++++++---------- onionr/onionrsockets.py | 15 ++------------- 5 files changed, 38 insertions(+), 28 deletions(-) diff --git a/onionr/communicator2.py b/onionr/communicator2.py index e8f5e7f3..36b5794d 100755 --- a/onionr/communicator2.py +++ b/onionr/communicator2.py @@ -21,7 +21,8 @@ ''' import sys, os, core, config, json, requests, time, logger, threading, base64, onionr import onionrexceptions, onionrpeers, onionrevents as events, onionrplugins as plugins, onionrblockapi as block -import onionrdaemontools, onionrsockets +import onionrdaemontools, onionrsockets, onionrchat +from dependencies import secrets from defusedxml import minidom class OnionrCommunicatorDaemon: @@ -80,7 +81,8 @@ class OnionrCommunicatorDaemon: self.daemonTools = onionrdaemontools.DaemonTools(self) # Active sockets for direct connections - self.sockets = [] + self.sockets = {} + self.socketExchange = {} # Socket ID exchange if debug or developmentMode: OnionrCommunicatorTimers(self, self.heartbeat, 10) @@ -469,12 +471,27 @@ class OnionrCommunicatorDaemon: # Create a socket or connect to one. # The socket handler (such as the plugin or app using it) is specified in startData['reason] startData = json.loads(cmd[1]) - self.onionrsockets.append(onionrsockets.OnionrSockets(self._core, startData)) + threading.Thread(target=self.startSocket, args=(startData,)).start() else: logger.info('Recieved daemonQueue command:' + cmd[0]) self.decrementThreadCount('daemonCommands') + def startSocket(self, startData): + # Start a socket client + mySocket = onionrsockets.OnionrSockets(self._core, startData)) + self.sockets[mySocket.socketID] = mySocket + + sockProgram = '' # Function for socket handler (application) + + if startData['reason'] == 'chat': + sockProgram = onionrchat.OnionrChat + else: + del self.sockets[mySocket.socketID] # Delete socket if we have no handler for it + + threading.Thread(target=sockProgram, args=(self, mySocket)).start() + mySocket.startConn() + def uploadBlock(self): '''Upload our block to a few peers''' # when inserting a block, we try to upload it to a few peers to add some deniability diff --git a/onionr/core.py b/onionr/core.py index 54219390..f6cb88b6 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -79,7 +79,6 @@ class Core: # Initialize the crypto object self._crypto = onionrcrypto.OnionrCrypto(self) self._blacklist = onionrblacklist.OnionrBlackList(self) - self.chatInst = onionrchat.OnionrChat(self) except Exception as error: logger.error('Failed to initialize core Onionr library.', error=error) diff --git a/onionr/onionr.py b/onionr/onionr.py index 1a50783b..26edf57d 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -218,7 +218,7 @@ class Onionr: 'getpasswd': self.printWebPassword, 'get-passwd': self.printWebPassword, - 'chat': self.onionrCore.chatInst.connect() + 'chat': self.startChat(), 'friend': self.friendCmd } @@ -270,6 +270,11 @@ class Onionr: THIS SECTION HANDLES THE COMMANDS ''' + def startChat(self): + self.onionrCore.daemonQueueAdd() + socketInfo = json.dumps({'peer': api.data['signer'], 'address': address, 'port': port, 'create': True, 'reason': reason}) + self.onionrCore.daemonQueueAdd('startSocket', socketInfo) + def getCommands(self): return self.cmds diff --git a/onionr/onionrchat.py b/onionr/onionrchat.py index a4051828..f37f270e 100644 --- a/onionr/onionrchat.py +++ b/onionr/onionrchat.py @@ -17,14 +17,14 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import logger +import logger, time class OnionrChat: - def __init__(self, coreInst): - return - - def recieveMessage(self, socketInst, data): - logger.info('Got %s' % (data,)) - return '' - - def sendMessage(self, socketInst, data): - return "Hello" \ No newline at end of file + def __init__(self, communicatorInst, socketInst): + self.communicator = communicatorInst + self.socket = socketInst + + while True: + time.sleep(2) + logger.info(self.socket.readData()) + self.socket.sendData('rekt') + return \ No newline at end of file diff --git a/onionr/onionrsockets.py b/onionr/onionrsockets.py index 610074c6..740c9054 100644 --- a/onionr/onionrsockets.py +++ b/onionr/onionrsockets.py @@ -23,18 +23,6 @@ import onionrexceptions, time, onionrchat from dependencies import secrets sel = selectors.DefaultSelector() -def getSocketCallbackRecieveHandler(coreInst, reason, create): - '''Return the recieve handler function for a given socket reason''' - retData = '' - if startData == 'chat': - retData = coreInst.chatInst.recieveMessage - -def getSocketCallbackSendHandler(coreInst, reason, create): - '''Return the send handler function for a given socket reason''' - retData = '' - if startData == 'chat': - retData = coreInst.chatInst.sendMessage - class OnionrSockets: def __init__(self, coreInst, socketInfo): '''Create a new Socket object. This interface is named a bit misleadingly @@ -64,8 +52,9 @@ class OnionrSockets: self.connected = False self.readData = [] - self.sendData = 0 + self.sendData = 0 + def startConn(): if self.isServer: self.createServer() else: