Onionr/src/httpapi/miscpublicapi/getblocks.py

72 lines
2.8 KiB
Python
Raw Normal View History

2020-04-15 03:40:31 +00:00
"""Onionr - Private P2P Communication.
2019-05-07 17:56:20 +00:00
2020-04-15 03:40:31 +00:00
Public endpoints to get block data and lists
"""
from flask import Response, abort
import config
from onionrutils import bytesconverter, stringvalidators
from coredb import blockmetadb
from utils import reconstructhash
from onionrblocks import BlockList
from onionrblocks.onionrblockapi import Block
2020-04-15 03:40:31 +00:00
from .. import apiutils
"""
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-04-15 03:40:31 +00:00
"""
def get_public_block_list(public_API, request):
2019-05-07 17:56:20 +00:00
# Provide a list of our blocks, with a date offset
date_adjust = request.args.get('date')
type_filter = request.args.get('type')
b_list = blockmetadb.get_block_list(date_rec=date_adjust)
2019-07-27 21:56:06 +00:00
share_list = ''
2019-08-02 23:00:04 +00:00
if config.get('general.hide_created_blocks', True):
for b in public_API.hideBlocks:
if b in b_list:
2019-05-07 17:56:20 +00:00
# Don't share blocks we created if they haven't been *uploaded* yet, makes it harder to find who created a block
b_list.remove(b)
for b in b_list:
if type_filter:
if Block(b, decrypt=False).getType() != type_filter:
continue
2019-07-27 21:56:06 +00:00
share_list += '%s\n' % (reconstructhash.deconstruct_hash(b),)
return Response(share_list)
2019-05-07 17:56:20 +00:00
2020-04-15 03:40:31 +00:00
def get_block_data(public_API, b_hash):
"""return block data by hash unless we are hiding it"""
2019-05-07 17:56:20 +00:00
resp = ''
b_hash = reconstructhash.reconstruct_hash(b_hash)
if stringvalidators.validate_hash(b_hash):
2020-08-26 08:25:43 +00:00
if not config.get('general.hide_created_blocks', True) \
or b_hash not in public_API.hideBlocks:
if b_hash in public_API._too_many.get(BlockList).get():
block = apiutils.GetBlockData().get_block_b_hash(
b_hash, raw=True, decrypt=False)
2019-05-07 17:56:20 +00:00
try:
2020-08-26 08:25:43 +00:00
# Encode in case data is binary
block = block.encode('utf-8')
2019-05-07 17:56:20 +00:00
except AttributeError:
if len(block) == 0:
abort(404)
block = bytesconverter.str_to_bytes(block)
2019-05-07 17:56:20 +00:00
resp = block
if len(resp) == 0:
abort(404)
resp = ""
# Has to be octet stream, otherwise binary data fails hash check
return Response(resp, mimetype='application/octet-stream')