fixed uploads with bytes

This commit is contained in:
Kevin Froman 2019-09-12 10:54:36 -05:00
parent 742ef564e1
commit 48f111021b
4 changed files with 33 additions and 26 deletions

View File

@ -26,16 +26,16 @@ def importBlockFromData(content):
blacklist = onionrblacklist.OnionrBlackList() blacklist = onionrblacklist.OnionrBlackList()
retData = False retData = False
dataHash = crypto.hashers.sha3_hash(content)
if blacklist.inBlacklist(dataHash):
raise onionrexceptions.BlacklistedBlock('%s is a blacklisted block' % (dataHash,))
try: try:
content = content.encode() content = content.encode()
except AttributeError: except AttributeError:
pass 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 metas = blockmetadata.get_block_metadata_from_data(content) # returns tuple(metadata, meta), meta is also in metadata
metadata = metas[0] metadata = metas[0]
if validatemetadata.validate_metadata(metadata, metas[2]): # check if metadata is valid 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) blockmetadb.add_to_block_DB(blockHash, dataSaved=True)
blockmetadata.process_block_metadata(blockHash) # caches block metadata values to block database blockmetadata.process_block_metadata(blockHash) # caches block metadata values to block database
retData = True retData = True
else:
raise onionrexceptions.InvalidProof
return retData return retData

View File

@ -44,19 +44,22 @@ def upload_blocks_from_communicator(comm_inst):
triedPeers.append(peer) triedPeers.append(peer)
url = 'http://' + peer + '/upload' url = 'http://' + peer + '/upload'
try: try:
data = {'block': block.Block(bl).getRaw()} #data = {'block': block.Block(bl).getRaw()}
data = block.Block(bl).getRaw()
except onionrexceptions.NoDataAvailable: except onionrexceptions.NoDataAvailable:
finishedUploads.append(bl) finishedUploads.append(bl)
break break
proxyType = proxypicker.pick_proxy(peer) proxyType = proxypicker.pick_proxy(peer)
logger.info("Uploading block %s to %s" % (bl[:8], peer), terminal=True) 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 not resp == False:
if resp == 'success': if resp == 'success':
localcommand.local_command('waitforshare/' + bl, post=True) localcommand.local_command('waitforshare/' + bl, post=True)
finishedUploads.append(bl) finishedUploads.append(bl)
elif resp == 'exists': elif resp == 'exists':
finishedUploads.append(bl) finishedUploads.append(bl)
else:
logger.warn('Failed to upload %s, reason: %s' % (bl[:8], resp[:150]), terminal=True)
for x in finishedUploads: for x in finishedUploads:
try: try:
comm_inst.blocksToUpload.remove(x) comm_inst.blocksToUpload.remove(x)

View File

@ -22,12 +22,7 @@ from flask import Response, abort
import blockimporter, onionrexceptions, logger import blockimporter, onionrexceptions, logger
def accept_upload(request): def accept_upload(request):
resp = 'failure' resp = 'failure'
try: data = request.get_data()
data = request.form['block']
except KeyError:
logger.warn('No block specified for upload')
pass
else:
if sys.getsizeof(data) < 100000000: if sys.getsizeof(data) < 100000000:
try: try:
if blockimporter.importBlockFromData(data): if blockimporter.importBlockFromData(data):
@ -38,9 +33,14 @@ def accept_upload(request):
except onionrexceptions.BlacklistedBlock: except onionrexceptions.BlacklistedBlock:
logger.debug('uploaded block is blacklisted') logger.debug('uploaded block is blacklisted')
resp = 'failure' resp = 'failure'
except onionrexceptions.InvalidProof:
resp = 'proof'
except onionrexceptions.DataExists: except onionrexceptions.DataExists:
resp = 'exists' resp = 'exists'
if resp == 'failure': if resp == 'failure':
abort(400) abort(400)
elif resp == 'proof':
resp = Response(resp, 400)
else:
resp = Response(resp) resp = Response(resp)
return resp return resp

View File

@ -21,7 +21,7 @@ import requests, streamedrequests
import logger, onionrexceptions import logger, onionrexceptions
from etc import onionrvalues from etc import onionrvalues
from . import localcommand 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 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: else:
return return
headers = {'User-Agent': 'PyOnionr', 'Connection':'close'} headers = {'User-Agent': 'PyOnionr', 'Connection':'close'}
if len(content_type) > 0:
headers['Content-Type'] = content_type
try: try:
proxies = {'http': 'socks4a://127.0.0.1:' + str(port), 'https': 'socks4a://127.0.0.1:' + str(port)} 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)) #r = requests.post(url, data=data, headers=headers, proxies=proxies, allow_redirects=False, timeout=(15, 30))