2019-05-07 17:56:20 +00:00
|
|
|
'''
|
2019-06-12 20:12:56 +00:00
|
|
|
Onionr - Private P2P Communication
|
2019-05-07 17:56:20 +00:00
|
|
|
|
|
|
|
Public endpoints to get block data and lists
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
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/>.
|
|
|
|
'''
|
|
|
|
from flask import Response, abort
|
2019-06-25 23:07:35 +00:00
|
|
|
import config
|
|
|
|
from onionrutils import bytesconverter, stringvalidators
|
2019-07-17 16:25:29 +00:00
|
|
|
from coredb import blockmetadb
|
2019-07-27 21:56:06 +00:00
|
|
|
from utils import reconstructhash
|
2019-08-02 23:00:04 +00:00
|
|
|
from .. import apiutils
|
|
|
|
def get_public_block_list(publicAPI, request):
|
2019-05-07 17:56:20 +00:00
|
|
|
# Provide a list of our blocks, with a date offset
|
|
|
|
dateAdjust = request.args.get('date')
|
2019-07-17 16:25:29 +00:00
|
|
|
bList = blockmetadb.get_block_list(dateRec=dateAdjust)
|
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):
|
2019-05-07 17:56:20 +00:00
|
|
|
for b in publicAPI.hideBlocks:
|
|
|
|
if b in bList:
|
|
|
|
# Don't share blocks we created if they haven't been *uploaded* yet, makes it harder to find who created a block
|
|
|
|
bList.remove(b)
|
2019-07-27 21:56:06 +00:00
|
|
|
for b in bList:
|
|
|
|
share_list += '%s\n' % (reconstructhash.deconstruct_hash(b),)
|
|
|
|
return Response(share_list)
|
2019-05-07 17:56:20 +00:00
|
|
|
|
2019-08-02 23:00:04 +00:00
|
|
|
def get_block_data(publicAPI, data):
|
2019-05-07 17:56:20 +00:00
|
|
|
'''data is the block hash in hex'''
|
|
|
|
resp = ''
|
2019-06-25 23:07:35 +00:00
|
|
|
if stringvalidators.validate_hash(data):
|
2019-08-02 23:00:04 +00:00
|
|
|
if not config.get('general.hide_created_blocks', True) or data not in publicAPI.hideBlocks:
|
2019-07-17 16:25:29 +00:00
|
|
|
if data in blockmetadb.get_block_list():
|
2019-08-02 23:00:04 +00:00
|
|
|
block = apiutils.GetBlockData().get_block_data(data, raw=True)
|
2019-05-07 17:56:20 +00:00
|
|
|
try:
|
|
|
|
block = block.encode() # Encode in case data is binary
|
|
|
|
except AttributeError:
|
2019-09-11 21:50:09 +00:00
|
|
|
if len(block) == 0:
|
|
|
|
abort(404)
|
2019-06-25 23:07:35 +00:00
|
|
|
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')
|