From cff38cb7c23b0bca82454025b8dbfeadd642507d Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 29 Jun 2019 13:18:31 -0500 Subject: [PATCH] sync and onionrservices fixes --- onionr/api.py | 18 ++++++++++++------ onionr/communicatorutils/downloadblocks.py | 8 +++++--- onionr/communicatorutils/lookupadders.py | 2 +- onionr/communicatorutils/servicecreator.py | 7 +++---- onionr/onionrservices/connectionserver.py | 4 ++-- onionr/onionrstorage/setdata.py | 2 +- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/onionr/api.py b/onionr/api.py index dcd912af..38350e4c 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -33,13 +33,19 @@ from onionrutils import bytesconverter, stringvalidators, epoch, mnemonickeys config.reload() class FDSafeHandler(WSGIHandler): '''Our WSGI handler. Doesn't do much non-default except timeouts''' + def __init__(self, sock, address, server, rfile=None): + self.socket = sock + self.address = address + self.server = server + self.rfile = rfile def handle(self): - timeout = Timeout(60, exception=Exception) - timeout.start() - try: - WSGIHandler.handle(self) - except Timeout as ex: - raise + while True: + timeout = Timeout(120, exception=Exception) + try: + FDSafeHandler.handle(self) + timeout.start() + except Timeout as ex: + raise def setBindIP(filePath=''): '''Set a random localhost IP to a specified file (intended for private or public API localhost IPs)''' diff --git a/onionr/communicatorutils/downloadblocks.py b/onionr/communicatorutils/downloadblocks.py index 1a159c93..76ad178f 100755 --- a/onionr/communicatorutils/downloadblocks.py +++ b/onionr/communicatorutils/downloadblocks.py @@ -80,19 +80,21 @@ def download_blocks_from_communicator(comm_inst): logger.info('Attempting to save block %s...' % blockHash[:12]) try: comm_inst._core.setData(content) + except onionrexceptions.DataExists: + logger.warn('Data is already set for %s ' % (blockHash,)) except onionrexceptions.DiskAllocationReached: - logger.error('Reached disk allocation allowance, cannot save block %s.' % blockHash) + logger.error('Reached disk allocation allowance, cannot save block %s.' % (blockHash,)) removeFromQueue = False else: comm_inst._core.addToBlockDB(blockHash, dataSaved=True) blockmetadata.process_block_metadata(comm_inst._core, blockHash) # caches block metadata values to block database else: - logger.warn('POW failed for block %s.' % blockHash) + logger.warn('POW failed for block %s.' % (blockHash,)) else: if comm_inst._core._blacklist.inBlacklist(realHash): logger.warn('Block %s is blacklisted.' % (realHash,)) else: - logger.warn('Metadata for block %s is invalid.' % blockHash) + logger.warn('Metadata for block %s is invalid.' % (blockHash,)) comm_inst._core._blacklist.addToDB(blockHash) else: # if block didn't meet expected hash diff --git a/onionr/communicatorutils/lookupadders.py b/onionr/communicatorutils/lookupadders.py index fc4527dc..c0394e3d 100755 --- a/onionr/communicatorutils/lookupadders.py +++ b/onionr/communicatorutils/lookupadders.py @@ -21,7 +21,7 @@ import logger from onionrutils import stringvalidators def lookup_new_peer_transports_with_communicator(comm_inst): - logger.info('Looking up new addresses...') + logger.info('Looking up new addresses...', terminal=True) tryAmount = 1 newPeers = [] for i in range(tryAmount): diff --git a/onionr/communicatorutils/servicecreator.py b/onionr/communicatorutils/servicecreator.py index 07d3adfb..769f663b 100755 --- a/onionr/communicatorutils/servicecreator.py +++ b/onionr/communicatorutils/servicecreator.py @@ -18,12 +18,11 @@ along with this program. If not, see . ''' import communicator, onionrblockapi -from onionrutils import stringvalidators +from onionrutils import stringvalidators, bytesconverter def service_creator(daemon): assert isinstance(daemon, communicator.OnionrCommunicatorDaemon) core = daemon._core - utils = core._utils # Find socket connection blocks # TODO cache blocks and only look at recently received ones @@ -31,9 +30,9 @@ def service_creator(daemon): for b in con_blocks: if not b in daemon.active_services: bl = onionrblockapi.Block(b, core=core, decrypt=True) - bs = utils.bytesToStr(bl.bcontent) + '.onion' + bs = bytesconverter.bytes_to_str(bl.bcontent) + '.onion' if stringvalidators.validate_pub_key(bl.signer) and stringvalidators.validate_transport(bs): - signer = utils.bytesToStr(bl.signer) + signer = bytesconverter.bytes_to_str(bl.signer) daemon.active_services.append(b) daemon.active_services.append(signer) daemon.services.create_server(signer, bs) diff --git a/onionr/onionrservices/connectionserver.py b/onionr/onionrservices/connectionserver.py index b9f70e56..413aa6ca 100755 --- a/onionr/onionrservices/connectionserver.py +++ b/onionr/onionrservices/connectionserver.py @@ -24,7 +24,7 @@ import core, logger, httpapi import onionrexceptions from netcontroller import getOpenPort import api -from onionrutils import stringvalidators, basicrequests +from onionrutils import stringvalidators, basicrequests, bytesconverter from . import httpheaders class ConnectionServer: @@ -82,7 +82,7 @@ class ConnectionServer: raise ConnectionError('Could not reach %s bootstrap address %s' % (peer, address)) else: # If no connection error, create the service and save it to local global key store - self.core_inst.keyStore.put('dc-' + response.service_id, self.core_inst._utils.bytesToStr(peer)) + self.core_inst.keyStore.put('dc-' + response.service_id, bytesconverter.bytes_to_str(peer)) logger.info('hosting on %s with %s' % (response.service_id, peer)) http_server.serve_forever() http_server.stop() diff --git a/onionr/onionrstorage/setdata.py b/onionr/onionrstorage/setdata.py index 50721ccf..05cd24d1 100644 --- a/onionr/onionrstorage/setdata.py +++ b/onionr/onionrstorage/setdata.py @@ -31,6 +31,6 @@ def set_data(core_inst, data): else: raise onionrexceptions.DiskAllocationReached else: - raise Exception("Data is already set for " + dataHash) + raise onionrexceptions.DataExists("Data is already set for " + dataHash) return dataHash \ No newline at end of file