From 620897a2ebcf33f93eede43c029975b6af5fdd2e Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 15 Sep 2018 11:13:03 -0500 Subject: [PATCH] work on sockets --- onionr/onionrsockets.py | 27 ++++++++++++++++--- .../default-plugins/metadataprocessor/main.py | 9 +++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/onionr/onionrsockets.py b/onionr/onionrsockets.py index f80b1637..972ef328 100644 --- a/onionr/onionrsockets.py +++ b/onionr/onionrsockets.py @@ -17,7 +17,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import stem +import stem.control +import socket, selectors import onionrexceptions from dependencies import secrets @@ -36,16 +37,34 @@ class OnionrSockets: self.socketInfo = socketInfo # Make sure socketInfo provides all necessary values - for i in ('peer', 'address', 'create'): + for i in ('peer', 'address', 'create', 'port'): try: socketInfo[i] except KeyError: raise ValueError('Must provide peer, address, and create in socketInfo dict argument') - self.isServer = socketInfo['create'] + self.isServer = socketInfo['create'] # if we are the one creating the service - self.serverKey = socketInfo['peer'] + self.remotePeer = socketInfo['peer'] + self.socketPort = socketInfo['port'] self.serverAddress = socketInfo['address'] + + if self.isServer: + self.createServer() def createServer(self): + # Create our HS and advertise it via a block + dataID = uuid.uuid4().hex + ourAddress = '' + ourPort = 1337 + ourInternalPort = 1338 + + # Setup the empheral HS + with stem.control.Controller.from_port() as controller: + controller.authenticate() + socketHS = controller.create_ephemeral_hidden_service({ourPort: ourInternalPort}, await_publication = True) + ourAddress = socketHS.service_id + + meta = {'address': ourAddress, 'port': ourPort} + self._core.insertBlock(dataID, header='openSocket', encryptType='asym', asymPeer=self.remotePeer, sign=True, meta=meta) return \ 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 199c8821..3b0191ae 100644 --- a/onionr/static-data/default-plugins/metadataprocessor/main.py +++ b/onionr/static-data/default-plugins/metadataprocessor/main.py @@ -76,12 +76,17 @@ def on_processBlocks(api): _processForwardKey(api, myBlock) # socket blocks elif blockType == 'openSocket': - if api.data['validSig'] == True: + if api.data['validSig'] == True and myBlock.decrypted: # we check if it is decrypted as a way of seeing if it was for us 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}) + try: + port = api.data['port'] + except KeyError: + raise ValueError("Missing port for new socket") + + socketInfo = json.dumps({'peer': api.data['signer'], 'address': address, 'port': port, create = False}) api.get_core().daemonQueueAdd('startSocket', socketInfo) def on_init(api, data = None):