From 26b0a05d663f4beaa11efd4de5f44f7cd3bb562d Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sun, 11 Aug 2019 23:00:08 -0500 Subject: [PATCH] * uploads now only remove from upload queue on 'exists' for efficiency-security trade off fixes #26 --- onionr/blockimporter.py | 2 +- onionr/communicator/uploadqueue/__init__.py | 10 ++++++---- onionr/communicatorutils/uploadblocks.py | 12 ++++++++---- onionr/httpapi/miscpublicapi/upload.py | 5 ++++- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/onionr/blockimporter.py b/onionr/blockimporter.py index 64c29eb2..758633d9 100755 --- a/onionr/blockimporter.py +++ b/onionr/blockimporter.py @@ -44,7 +44,7 @@ def importBlockFromData(content): try: blockHash = onionrstorage.set_data(content) except onionrexceptions.DiskAllocationReached: - pass + logger.warn('Failed to save block due to full disk allocation') else: blockmetadb.add_to_block_DB(blockHash, dataSaved=True) blockmetadata.process_block_metadata(blockHash) # caches block metadata values to block database diff --git a/onionr/communicator/uploadqueue/__init__.py b/onionr/communicator/uploadqueue/__init__.py index 84f725f8..0143425e 100644 --- a/onionr/communicator/uploadqueue/__init__.py +++ b/onionr/communicator/uploadqueue/__init__.py @@ -38,11 +38,13 @@ class UploadQueue: ''' def __init__(self, communicator: 'OnionrCommunicatorDaemon'): + '''Start the UploadQueue object, loading left over uploads into queue + and registering save shutdown function + ''' self.communicator = communicator cache = deadsimplekv.DeadSimpleKV(UPLOAD_MEMORY_FILE) self.store_obj = cache cache: list = cache.get('uploads') - if cache == None: cache = [] @@ -52,7 +54,7 @@ class UploadQueue: atexit.register(self.save) def save(self): + '''Saves to disk on shutdown or if called manually''' bl: list = self.communicator.blocksToUpload - if len(bl) > 0: - self.store_obj.put('uploads', bl) - self.store_obj.flush() + self.store_obj.put('uploads', bl) + self.store_obj.flush() diff --git a/onionr/communicatorutils/uploadblocks.py b/onionr/communicatorutils/uploadblocks.py index 9e8d48d1..56afb7b3 100755 --- a/onionr/communicatorutils/uploadblocks.py +++ b/onionr/communicatorutils/uploadblocks.py @@ -45,12 +45,16 @@ def upload_blocks_from_communicator(comm_inst): data = {'block': block.Block(bl).getRaw()} proxyType = proxypicker.pick_proxy(peer) logger.info("Uploading block to " + peer, terminal=True) - if not basicrequests.do_post_request(url, data=data, proxyType=proxyType) == False: - localcommand.local_command('waitforshare/' + bl, post=True) - finishedUploads.append(bl) + resp = basicrequests.do_post_request(url, data=data, proxyType=proxyType) + if not resp == False: + if resp == 'success': + localcommand.local_command('waitforshare/' + bl, post=True) + finishedUploads.append(bl) + elif resp == 'exists': + finishedUploads.append(bl) for x in finishedUploads: try: comm_inst.blocksToUpload.remove(x) except ValueError: pass - comm_inst.decrementThreadCount(TIMER_NAME) \ No newline at end of file + comm_inst.decrementThreadCount(TIMER_NAME) diff --git a/onionr/httpapi/miscpublicapi/upload.py b/onionr/httpapi/miscpublicapi/upload.py index 87811fbe..9328f270 100755 --- a/onionr/httpapi/miscpublicapi/upload.py +++ b/onionr/httpapi/miscpublicapi/upload.py @@ -33,10 +33,13 @@ def accept_upload(request): if blockimporter.importBlockFromData(data): resp = 'success' else: + resp = 'failure' logger.warn('Error encountered importing uploaded block') except onionrexceptions.BlacklistedBlock: logger.debug('uploaded block is blacklisted') - pass + resp = 'failure' + except onionrexceptions.DataExists: + resp = 'exists' if resp == 'failure': abort(400) resp = Response(resp)