Onionr/onionr/httpapi/miscpublicapi/endpoints.py

81 lines
3.3 KiB
Python
Raw Permalink Normal View History

'''
Onionr - Private P2P Communication
Misc public API endpoints too small to need their own file and that need access to the public api inst
'''
'''
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/>.
'''
2019-08-05 17:57:25 +00:00
from flask import Response, Blueprint, request, send_from_directory, abort, g
from . import getblocks, upload, announce
2019-07-19 04:59:44 +00:00
from coredb import keydb
2019-08-02 23:00:04 +00:00
import config
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
@public_endpoints_bp.route('/')
def banner():
# Display a bit of information to people who visit a node address in their browser
try:
with open('static-data/index.html', 'r') as html:
resp = Response(html.read(), mimetype='text/html')
except FileNotFoundError:
resp = Response("")
return resp
@public_endpoints_bp.route('/getblocklist')
def get_block_list():
2019-07-03 02:50:22 +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)
@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)
@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)
return send_from_directory(config.get('www.public.path', 'static-data/www/public/'), path)
@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))
if len(response) == 0:
response = ''
return Response(response)
@public_endpoints_bp.route('/announce', methods=['post'])
def accept_announce():
'''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)
return resp
@public_endpoints_bp.route('/upload', methods=['post'])
2019-07-03 00:43:18 +00:00
def upload_endpoint():
'''Accept file uploads. In the future this will be done more often than on creation
to speed up block sync
'''
2019-08-02 23:00:04 +00:00
return upload.accept_upload(request)