2020-06-30 23:34:13 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
|
|
|
|
|
|
|
Accept block uploads to the public API server
|
|
|
|
"""
|
|
|
|
import sys
|
2019-05-07 17:56:20 +00:00
|
|
|
|
2020-01-06 12:06:27 +00:00
|
|
|
from gevent import spawn
|
2019-11-27 00:01:32 +00:00
|
|
|
from flask import Response
|
|
|
|
from flask import abort
|
2020-06-30 23:34:13 +00:00
|
|
|
from flask import g
|
2019-11-27 00:01:32 +00:00
|
|
|
|
2019-12-22 19:42:10 +00:00
|
|
|
from onionrutils import localcommand
|
2019-11-27 00:01:32 +00:00
|
|
|
from onionrblocks import blockimporter
|
|
|
|
import onionrexceptions
|
|
|
|
import logger
|
|
|
|
|
2020-06-30 23:34:13 +00:00
|
|
|
"""
|
2019-05-07 17:56:20 +00:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-06-30 23:34:13 +00:00
|
|
|
"""
|
2019-09-21 23:49:24 +00:00
|
|
|
|
|
|
|
|
2019-08-02 23:00:04 +00:00
|
|
|
def accept_upload(request):
|
2019-11-27 00:01:32 +00:00
|
|
|
"""Accept uploaded blocks to our public Onionr protocol API server"""
|
2019-05-07 17:56:20 +00:00
|
|
|
resp = 'failure'
|
2019-09-12 15:54:36 +00:00
|
|
|
data = request.get_data()
|
2019-12-22 19:42:10 +00:00
|
|
|
b_hash = ''
|
2019-09-12 15:54:36 +00:00
|
|
|
if sys.getsizeof(data) < 100000000:
|
|
|
|
try:
|
2019-12-22 19:42:10 +00:00
|
|
|
b_hash = blockimporter.import_block_from_data(data)
|
|
|
|
if b_hash:
|
2020-07-26 03:28:32 +00:00
|
|
|
if g.too_many.get_by_string("DeadSimpleKV").get('onlinePeers'):
|
2020-06-30 23:34:13 +00:00
|
|
|
spawn(
|
|
|
|
localcommand.local_command,
|
2020-08-26 08:25:43 +00:00
|
|
|
'/daemon-event/upload_event',
|
2020-06-30 23:34:13 +00:00
|
|
|
post=True,
|
|
|
|
is_json=True,
|
2020-07-17 18:49:18 +00:00
|
|
|
post_data={'block': b_hash}
|
2020-06-30 23:34:13 +00:00
|
|
|
).get(timeout=10)
|
2019-09-12 15:54:36 +00:00
|
|
|
resp = 'success'
|
|
|
|
else:
|
2019-08-12 04:00:08 +00:00
|
|
|
resp = 'failure'
|
2020-06-30 23:34:13 +00:00
|
|
|
logger.warn(
|
|
|
|
f'Error encountered importing uploaded block {b_hash}')
|
2019-09-12 15:54:36 +00:00
|
|
|
except onionrexceptions.BlacklistedBlock:
|
|
|
|
logger.debug('uploaded block is blacklisted')
|
|
|
|
resp = 'failure'
|
|
|
|
except onionrexceptions.InvalidProof:
|
|
|
|
resp = 'proof'
|
|
|
|
except onionrexceptions.DataExists:
|
|
|
|
resp = 'exists'
|
2019-05-07 17:56:20 +00:00
|
|
|
if resp == 'failure':
|
|
|
|
abort(400)
|
2019-09-12 15:54:36 +00:00
|
|
|
elif resp == 'proof':
|
|
|
|
resp = Response(resp, 400)
|
2020-06-30 23:34:13 +00:00
|
|
|
logger.warn(
|
|
|
|
f'Error importing uploaded block, invalid proof {b_hash}')
|
2019-09-12 15:54:36 +00:00
|
|
|
else:
|
|
|
|
resp = Response(resp)
|
2019-11-16 04:18:38 +00:00
|
|
|
return resp
|