From 0e2928db3864bc93ce6eef7679694c089efbe5af Mon Sep 17 00:00:00 2001 From: KF Date: Tue, 2 Jul 2019 18:16:04 -0500 Subject: [PATCH] * refactored client api endpoints to their own file and class --- onionr/api.py | 87 +---------------- onionr/httpapi/miscclientapi/__init__.py | 2 +- onionr/httpapi/miscclientapi/endpoints.py | 112 ++++++++++++++++++++++ 3 files changed, 114 insertions(+), 87 deletions(-) create mode 100644 onionr/httpapi/miscclientapi/endpoints.py diff --git a/onionr/api.py b/onionr/api.py index f13203fc..18036263 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -105,96 +105,11 @@ class API: app.register_blueprint(insertblock.ib) app.register_blueprint(miscclientapi.getblocks.client_get_blocks) app.register_blueprint(miscclientapi.staticfiles.static_files_bp) + app.register_blueprint(miscclientapi.endpoints.PrivateEndpoints(self).private_endpoints_bp) app.register_blueprint(onionrsitesapi.site_api) app.register_blueprint(apiutils.shutdown.shutdown_bp) httpapi.load_plugin_blueprints(app) self.get_block_data = apiutils.GetBlockData(self) - - @app.route('/serviceactive/') - def serviceActive(pubkey): - try: - if pubkey in self._core.onionrInst.communicatorInst.active_services: - return Response('true') - except AttributeError as e: - pass - return Response('false') - - @app.route('/www/', endpoint='www') - def wwwPublic(path): - if not config.get("www.private.run", True): - abort(403) - return send_from_directory(config.get('www.private.path', 'static-data/www/private/'), path) - - @app.route('/hitcount') - def get_hit_count(): - return Response(str(self.publicAPI.hitCount)) - - @app.route('/queueResponseAdd/', methods=['post']) - def queueResponseAdd(name): - # Responses from the daemon. TODO: change to direct var access instead of http endpoint - self.queueResponse[name] = request.form['data'] - return Response('success') - - @app.route('/queueResponse/') - def queueResponse(name): - # Fetch a daemon queue response - resp = 'failure' - try: - resp = self.queueResponse[name] - except KeyError: - pass - else: - del self.queueResponse[name] - if resp == 'failure': - return resp, 404 - else: - return resp - - @app.route('/ping') - def ping(): - # Used to check if client api is working - return Response("pong!") - - @app.route('/lastconnect') - def lastConnect(): - return Response(str(self.publicAPI.lastRequest)) - - @app.route('/waitforshare/', methods=['post']) - def waitforshare(name): - '''Used to prevent the **public** api from sharing blocks we just created''' - assert name.isalnum() - if name in self.publicAPI.hideBlocks: - self.publicAPI.hideBlocks.remove(name) - return Response("removed") - else: - self.publicAPI.hideBlocks.append(name) - return Response("added") - - @app.route('/shutdown') - def shutdown(): - return apiutils.shutdown.shutdown(self) - - @app.route('/getstats') - def getStats(): - # returns node stats - #return Response("disabled") - while True: - try: - return Response(self._core.serializer.getStats()) - except AttributeError: - pass - - @app.route('/getuptime') - def showUptime(): - return Response(str(self.getUptime())) - - @app.route('/getActivePubkey') - def getActivePubkey(): - return Response(self._core._crypto.pubKey) - - @app.route('/getHumanReadable/') - def getHumanReadable(name): - return Response(mnemonickeys.get_human_readable_ID(name)) self.httpServer = WSGIServer((self.host, bindPort), app, log=None, handler_class=fdsafehandler.FDSafeHandler) self.httpServer.serve_forever() diff --git a/onionr/httpapi/miscclientapi/__init__.py b/onionr/httpapi/miscclientapi/__init__.py index 161fe6d5..6ea8ea0f 100644 --- a/onionr/httpapi/miscclientapi/__init__.py +++ b/onionr/httpapi/miscclientapi/__init__.py @@ -1 +1 @@ -from . import getblocks, staticfiles \ No newline at end of file +from . import getblocks, staticfiles, endpoints \ No newline at end of file diff --git a/onionr/httpapi/miscclientapi/endpoints.py b/onionr/httpapi/miscclientapi/endpoints.py new file mode 100644 index 00000000..79037232 --- /dev/null +++ b/onionr/httpapi/miscclientapi/endpoints.py @@ -0,0 +1,112 @@ +''' + Onionr - Private P2P Communication + + Misc client API endpoints too small to need their own file and that need access to the client 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 . +''' +from flask import Response, Blueprint, request, send_from_directory, abort +from httpapi import apiutils +class PrivateEndpoints: + def __init__(self, client_api): + private_endpoints_bp = Blueprint('privateendpoints', __name__) + self.private_endpoints_bp = private_endpoints_bp + config = client_api._core.config + + @private_endpoints_bp.route('/serviceactive/') + def serviceActive(pubkey): + try: + if pubkey in client_api._core.onionrInst.communicatorInst.active_services: + return Response('true') + except AttributeError as e: + pass + return Response('false') + + @private_endpoints_bp.route('/www/', endpoint='www') + def wwwPublic(path): + if not config.get("www.private.run", True): + abort(403) + return send_from_directory(config.get('www.private.path', 'static-data/www/private/'), path) + + @private_endpoints_bp.route('/hitcount') + def get_hit_count(): + return Response(str(client_api.publicAPI.hitCount)) + + @private_endpoints_bp.route('/queueResponseAdd/', methods=['post']) + def queueResponseAdd(name): + # Responses from the daemon. TODO: change to direct var access instead of http endpoint + client_api.queueResponse[name] = request.form['data'] + return Response('success') + + @private_endpoints_bp.route('/queueResponse/') + def queueResponse(name): + # Fetch a daemon queue response + resp = 'failure' + try: + resp = client_api.queueResponse[name] + except KeyError: + pass + else: + del client_api.queueResponse[name] + if resp == 'failure': + return resp, 404 + else: + return resp + + @private_endpoints_bp.route('/ping') + def ping(): + # Used to check if client api is working + return Response("pong!") + + @private_endpoints_bp.route('/lastconnect') + def lastConnect(): + return Response(str(client_api.publicAPI.lastRequest)) + + @private_endpoints_bp.route('/waitforshare/', methods=['post']) + def waitforshare(name): + '''Used to prevent the **public** api from sharing blocks we just created''' + assert name.isalnum() + if name in client_api.publicAPI.hideBlocks: + client_api.publicAPI.hideBlocks.remove(name) + return Response("removed") + else: + client_api.publicAPI.hideBlocks.private_endpoints_bpend(name) + return Response("added") + + @private_endpoints_bp.route('/shutdown') + def shutdown(): + return apiutils.shutdown.shutdown(client_api) + + @private_endpoints_bp.route('/getstats') + def getStats(): + # returns node stats + #return Response("disabled") + while True: + try: + return Response(client_api._core.serializer.getStats()) + except AttributeError: + pass + + @private_endpoints_bp.route('/getuptime') + def showUptime(): + return Response(str(client_api.getUptime())) + + @private_endpoints_bp.route('/getActivePubkey') + def getActivePubkey(): + return Response(client_api._core._crypto.pubKey) + + @private_endpoints_bp.route('/getHumanReadable/') + def getHumanReadable(name): + return Response(mnemonickeys.get_human_readable_ID(name)) \ No newline at end of file