From e0fbe2033e2df596c197effba21cca0e8af6b6e8 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 14 Sep 2018 20:05:25 -0500 Subject: [PATCH] work on sockets --- onionr/communicator2.py | 8 +++++- onionr/onionrexceptions.py | 5 ++++ onionr/onionrsockets.py | 25 +++++++++++++++++++ .../default-plugins/metadataprocessor/main.py | 8 +++++- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/onionr/communicator2.py b/onionr/communicator2.py index f203dd70..e6d49d28 100755 --- a/onionr/communicator2.py +++ b/onionr/communicator2.py @@ -21,7 +21,7 @@ ''' 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 +import onionrdaemontools, onionrsockets from defusedxml import minidom class OnionrCommunicatorDaemon: @@ -79,6 +79,9 @@ class OnionrCommunicatorDaemon: #self.daemonTools = onionrdaemontools.DaemonTools(self) self.daemonTools = onionrdaemontools.DaemonTools(self) + # Active sockets for direct connections + self.sockets = [] + if debug or developmentMode: OnionrCommunicatorTimers(self, self.heartbeat, 10) @@ -462,6 +465,9 @@ class OnionrCommunicatorDaemon: elif cmd[0] == 'uploadBlock': self.blockToUpload = cmd[1] threading.Thread(target=self.uploadBlock).start() + elif cmd[0] == 'createSocket': + # Create a socket + self.onionrsockets.append(onionrsockets.OnionrSockets(self._core, startData)) else: logger.info('Recieved daemonQueue command:' + cmd[0]) diff --git a/onionr/onionrexceptions.py b/onionr/onionrexceptions.py index c32fbbb4..4954550e 100644 --- a/onionr/onionrexceptions.py +++ b/onionr/onionrexceptions.py @@ -65,4 +65,9 @@ class InvalidAddress(Exception): # file exceptions class DiskAllocationReached(Exception): + pass + +# onionrsocket exceptions + +class MissingAddress(Exception): pass \ No newline at end of file diff --git a/onionr/onionrsockets.py b/onionr/onionrsockets.py index df4a1ad7..0f00519f 100644 --- a/onionr/onionrsockets.py +++ b/onionr/onionrsockets.py @@ -17,3 +17,28 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' +import stem +import onionrexceptions +from dependencies import secrets + +class OnionrSockets: + def __init__(self, coreInst, socketInfo): + '''Create a new Socket object. This interface is named a bit misleadingly + and does not actually forward network requests. + + Accepts coreInst, an instance of Onionr core library, and socketInfo, a dict with these values: + 'peer': peer master public key + 'address': string, if we're connecting to a socket, this is the address we connect to. Not applicable if we're creating our own + create: bool + ''' + self.socketID = secrets.token_hex(32) # Generate an ID for this socket + self._core = coreInst + + # Make sure socketInfo provides all necessary values + for i in ('peer', 'address', 'create'): + try: + socketInfo[i] + except KeyError: + raise ValueError('Must provide peer, address, and create in socketInfo dict argument') + + \ No newline at end of file diff --git a/onionr/static-data/default-plugins/metadataprocessor/main.py b/onionr/static-data/default-plugins/metadataprocessor/main.py index 5145d911..4d23fa9a 100644 --- a/onionr/static-data/default-plugins/metadataprocessor/main.py +++ b/onionr/static-data/default-plugins/metadataprocessor/main.py @@ -59,6 +59,7 @@ def _processForwardKey(api, myBlock): raise onionrexceptions.InvalidPubkey("%s is nota valid pubkey key" % (key,)) def on_processBlocks(api): + # Generally fired by utils. myBlock = api.data['block'] blockType = api.data['type'] logger.info('blockType is ' + blockType) @@ -76,7 +77,12 @@ def on_processBlocks(api): # socket blocks elif blockType == 'openSocket': if api.data['validSig']: - pass + try: + address = api.data['address'] + except KeyError: + raise onionrexceptions.MissingAddress("Missing address for new socket") + socketInfo = json.dumps({'peer': api.data['signer'], 'address': address, create = False}) + api.get_core().daemonQueueAdd('createSocket', socketInfo) def on_init(api, data = None):