work on sockets

This commit is contained in:
Kevin Froman 2018-09-14 20:05:25 -05:00
parent ee2a74380b
commit e0fbe2033e
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
4 changed files with 44 additions and 2 deletions

View File

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

View File

@ -66,3 +66,8 @@ class InvalidAddress(Exception):
class DiskAllocationReached(Exception):
pass
# onionrsocket exceptions
class MissingAddress(Exception):
pass

View File

@ -17,3 +17,28 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
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')

View File

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