From 48f111021b2bf365e61c3ec9eb073a1af2da7ae2 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Thu, 12 Sep 2019 10:54:36 -0500 Subject: [PATCH] fixed uploads with bytes --- onionr/blockimporter.py | 12 ++++---- onionr/communicatorutils/uploadblocks.py | 7 +++-- onionr/httpapi/miscpublicapi/upload.py | 36 ++++++++++++------------ onionr/onionrutils/basicrequests.py | 4 ++- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/onionr/blockimporter.py b/onionr/blockimporter.py index 758633d9..a0de48c6 100755 --- a/onionr/blockimporter.py +++ b/onionr/blockimporter.py @@ -26,16 +26,16 @@ def importBlockFromData(content): blacklist = onionrblacklist.OnionrBlackList() retData = False - dataHash = crypto.hashers.sha3_hash(content) - - if blacklist.inBlacklist(dataHash): - raise onionrexceptions.BlacklistedBlock('%s is a blacklisted block' % (dataHash,)) - try: content = content.encode() except AttributeError: pass + dataHash = crypto.hashers.sha3_hash(content) + + if blacklist.inBlacklist(dataHash): + raise onionrexceptions.BlacklistedBlock('%s is a blacklisted block' % (dataHash,)) + metas = blockmetadata.get_block_metadata_from_data(content) # returns tuple(metadata, meta), meta is also in metadata metadata = metas[0] if validatemetadata.validate_metadata(metadata, metas[2]): # check if metadata is valid @@ -49,4 +49,6 @@ def importBlockFromData(content): blockmetadb.add_to_block_DB(blockHash, dataSaved=True) blockmetadata.process_block_metadata(blockHash) # caches block metadata values to block database retData = True + else: + raise onionrexceptions.InvalidProof return retData \ No newline at end of file diff --git a/onionr/communicatorutils/uploadblocks.py b/onionr/communicatorutils/uploadblocks.py index 221089e2..647b4943 100755 --- a/onionr/communicatorutils/uploadblocks.py +++ b/onionr/communicatorutils/uploadblocks.py @@ -44,19 +44,22 @@ def upload_blocks_from_communicator(comm_inst): triedPeers.append(peer) url = 'http://' + peer + '/upload' try: - data = {'block': block.Block(bl).getRaw()} + #data = {'block': block.Block(bl).getRaw()} + data = block.Block(bl).getRaw() except onionrexceptions.NoDataAvailable: finishedUploads.append(bl) break proxyType = proxypicker.pick_proxy(peer) logger.info("Uploading block %s to %s" % (bl[:8], peer), terminal=True) - resp = basicrequests.do_post_request(url, data=data, proxyType=proxyType) + resp = basicrequests.do_post_request(url, data=data, proxyType=proxyType, content_type='application/octet-stream') if not resp == False: if resp == 'success': localcommand.local_command('waitforshare/' + bl, post=True) finishedUploads.append(bl) elif resp == 'exists': finishedUploads.append(bl) + else: + logger.warn('Failed to upload %s, reason: %s' % (bl[:8], resp[:150]), terminal=True) for x in finishedUploads: try: comm_inst.blocksToUpload.remove(x) diff --git a/onionr/httpapi/miscpublicapi/upload.py b/onionr/httpapi/miscpublicapi/upload.py index 9328f270..2c8f5224 100755 --- a/onionr/httpapi/miscpublicapi/upload.py +++ b/onionr/httpapi/miscpublicapi/upload.py @@ -22,25 +22,25 @@ from flask import Response, abort import blockimporter, onionrexceptions, logger def accept_upload(request): resp = 'failure' - try: - data = request.form['block'] - except KeyError: - logger.warn('No block specified for upload') - pass - else: - if sys.getsizeof(data) < 100000000: - try: - 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') + data = request.get_data() + if sys.getsizeof(data) < 100000000: + try: + if blockimporter.importBlockFromData(data): + resp = 'success' + else: resp = 'failure' - except onionrexceptions.DataExists: - resp = 'exists' + logger.warn('Error encountered importing uploaded block') + except onionrexceptions.BlacklistedBlock: + logger.debug('uploaded block is blacklisted') + resp = 'failure' + except onionrexceptions.InvalidProof: + resp = 'proof' + except onionrexceptions.DataExists: + resp = 'exists' if resp == 'failure': abort(400) - resp = Response(resp) + elif resp == 'proof': + resp = Response(resp, 400) + else: + resp = Response(resp) return resp \ No newline at end of file diff --git a/onionr/onionrutils/basicrequests.py b/onionr/onionrutils/basicrequests.py index 22913b6b..13d65769 100644 --- a/onionr/onionrutils/basicrequests.py +++ b/onionr/onionrutils/basicrequests.py @@ -21,7 +21,7 @@ import requests, streamedrequests import logger, onionrexceptions from etc import onionrvalues from . import localcommand -def do_post_request(url, data={}, port=0, proxyType='tor', max_size=10000): +def do_post_request(url, data={}, port=0, proxyType='tor', max_size=10000, content_type: str = ''): ''' Do a POST request through a local tor or i2p instance ''' @@ -34,6 +34,8 @@ def do_post_request(url, data={}, port=0, proxyType='tor', max_size=10000): else: return headers = {'User-Agent': 'PyOnionr', 'Connection':'close'} + if len(content_type) > 0: + headers['Content-Type'] = content_type try: proxies = {'http': 'socks4a://127.0.0.1:' + str(port), 'https': 'socks4a://127.0.0.1:' + str(port)} #r = requests.post(url, data=data, headers=headers, proxies=proxies, allow_redirects=False, timeout=(15, 30))