2020-08-26 08:25:43 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-07-02 06:32:26 +00:00
|
|
|
|
2020-08-26 08:25:43 +00:00
|
|
|
Misc public API endpoints too small to need their own file
|
|
|
|
and that need access to the public api inst
|
|
|
|
"""
|
|
|
|
from flask import Response, Blueprint, request, send_from_directory, abort, g
|
|
|
|
from . import getblocks, upload, announce
|
|
|
|
from coredb import keydb
|
|
|
|
import config
|
|
|
|
"""
|
2019-07-02 06:32:26 +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-08-26 08:25:43 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2019-07-02 06:32:26 +00:00
|
|
|
class PublicEndpoints:
|
|
|
|
def __init__(self, public_api):
|
|
|
|
|
|
|
|
public_endpoints_bp = Blueprint('publicendpoints', __name__)
|
|
|
|
self.public_endpoints_bp = public_endpoints_bp
|
2019-07-03 02:50:22 +00:00
|
|
|
|
2019-07-02 06:32:26 +00:00
|
|
|
@public_endpoints_bp.route('/')
|
|
|
|
def banner():
|
2020-09-07 03:18:23 +00:00
|
|
|
# Display info to people who visit a node address in their browser
|
2019-07-02 06:32:26 +00:00
|
|
|
try:
|
2019-09-23 22:56:05 +00:00
|
|
|
with open('../static-data/index.html', 'r') as html:
|
2019-07-02 06:32:26 +00:00
|
|
|
resp = Response(html.read(), mimetype='text/html')
|
|
|
|
except FileNotFoundError:
|
|
|
|
resp = Response("")
|
|
|
|
return resp
|
|
|
|
|
|
|
|
@public_endpoints_bp.route('/getblocklist')
|
|
|
|
def get_block_list():
|
2020-08-26 08:25:43 +00:00
|
|
|
"""Get a list of blocks, optionally filtered by epoch time stamp,
|
|
|
|
excluding those hidden"""
|
2019-08-02 23:00:04 +00:00
|
|
|
return getblocks.get_public_block_list(public_api, request)
|
2019-07-02 06:32:26 +00:00
|
|
|
|
|
|
|
@public_endpoints_bp.route('/getdata/<name>')
|
|
|
|
def get_block_data(name):
|
2019-07-03 02:50:22 +00:00
|
|
|
# Share data for a block if we have it and it isn't hidden
|
2019-08-02 23:00:04 +00:00
|
|
|
return getblocks.get_block_data(public_api, name)
|
2019-07-02 06:32:26 +00:00
|
|
|
|
|
|
|
@public_endpoints_bp.route('/www/<path:path>')
|
|
|
|
def www_public(path):
|
|
|
|
# A way to share files directly over your .onion
|
|
|
|
if not config.get("www.public.run", True):
|
|
|
|
abort(403)
|
2020-09-07 03:18:23 +00:00
|
|
|
return send_from_directory(
|
|
|
|
config.get('www.public.path', 'static-data/www/public/'), path)
|
2019-07-02 06:32:26 +00:00
|
|
|
|
|
|
|
@public_endpoints_bp.route('/ping')
|
|
|
|
def ping():
|
|
|
|
# Endpoint to test if nodes are up
|
|
|
|
return Response("pong!")
|
|
|
|
|
|
|
|
@public_endpoints_bp.route('/pex')
|
|
|
|
def peer_exchange():
|
2019-07-19 04:59:44 +00:00
|
|
|
response = ','.join(keydb.listkeys.list_adders(recent=3600))
|
2019-07-02 06:32:26 +00:00
|
|
|
if len(response) == 0:
|
|
|
|
response = ''
|
|
|
|
return Response(response)
|
|
|
|
|
|
|
|
@public_endpoints_bp.route('/announce', methods=['post'])
|
|
|
|
def accept_announce():
|
2020-08-26 08:25:43 +00:00
|
|
|
"""Accept announcements with pow token to prevent spam"""
|
2019-08-05 17:57:25 +00:00
|
|
|
g.shared_state = public_api._too_many
|
2019-08-02 23:00:04 +00:00
|
|
|
resp = announce.handle_announce(request)
|
2019-07-02 06:32:26 +00:00
|
|
|
return resp
|
|
|
|
|
|
|
|
@public_endpoints_bp.route('/upload', methods=['post'])
|
2019-07-03 00:43:18 +00:00
|
|
|
def upload_endpoint():
|
2020-08-26 08:25:43 +00:00
|
|
|
"""Accept file uploads.
|
|
|
|
In the future this will be done more often than on creation
|
2019-07-02 06:32:26 +00:00
|
|
|
to speed up block sync
|
2020-08-26 08:25:43 +00:00
|
|
|
"""
|
2019-08-02 23:00:04 +00:00
|
|
|
return upload.accept_upload(request)
|