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()
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

View File

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

View File

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

View File

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