From e6bf64a41febc3a7a0e393465b74d2b2c7c5db94 Mon Sep 17 00:00:00 2001 From: kevdagoat Date: Sun, 6 Sep 2020 12:58:17 +0800 Subject: [PATCH] working statistics api route --- app/classes/shared/models.py | 7 +++++-- app/classes/web/api_handler.py | 30 ++++++++++++++++++++++++------ app/classes/web/tornado.py | 3 +++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/app/classes/shared/models.py b/app/classes/shared/models.py index 130998fb..12bbfd11 100644 --- a/app/classes/shared/models.py +++ b/app/classes/shared/models.py @@ -171,14 +171,17 @@ class db_builder: username = default_data.get("username", 'admin') password = default_data.get("password", 'crafty') - + api_token = helper.random_string_generator(32) + Users.insert({ Users.username: username, Users.password: helper.encode_pass(password), - Users.api_token: helper.random_string_generator(32), + Users.api_token: api_token, Users.enabled: True }).execute() + console.info("API token is {}".format(api_token)) + @staticmethod def is_fresh_install(): if helper.check_file_exists(helper.db_path): diff --git a/app/classes/web/api_handler.py b/app/classes/web/api_handler.py index 45442c4a..cb87a8ea 100644 --- a/app/classes/web/api_handler.py +++ b/app/classes/web/api_handler.py @@ -6,6 +6,7 @@ import tornado.escape import logging from app.classes.shared.models import Users +from app.classes.minecraft.stats import stats log = logging.getLogger(__name__) @@ -21,20 +22,37 @@ class BaseHandler(tornado.web.RequestHandler): self.set_status(403) self.finish(self.return_response(403, {'error':'ACCESS_DENIED', 'info':'You were denied access to the requested resource'})) - def authenticate_user(self, token): + def authenticate_user(self): try: log.debug("Searching for specified token") - user_data = Users.get(api_token=token) + user_data = Users.get(api_token=self.get_argument('token')) log.debug("Checking results") if user_data: - # Login successful! Return the username + # Login successful! Check perms log.info("User {} has authenticated to API".format(user_data.username)) - return user_data.username + # TODO: Role check else: logging.debug("Auth unsuccessful") - return None + return self.access_denied("unknown") except: log.warning("Traceback occurred when authenticating user to API. Most likely wrong token") - return None + return self.access_denied("unknown") pass + +class ServersStats(BaseHandler): + def get(self): + """Get details about all servers""" + self.authenticate_user() + # Get server stats + self.finish(self.write({"servers": stats.get_servers_stats()})) + + +class NodeStats(BaseHandler): + def get(self): + """Get stats for particular node""" + self.authenticate_user() + # Get node stats + node_stats = stats.get_node_stats() + node_stats.pop("servers") + self.finish(self.write(node_stats)) diff --git a/app/classes/web/tornado.py b/app/classes/web/tornado.py index 6753b4f9..03b0a97f 100644 --- a/app/classes/web/tornado.py +++ b/app/classes/web/tornado.py @@ -23,6 +23,7 @@ try: from app.classes.web.default_handler import DefaultHandler from app.classes.web.server_handler import ServerHandler from app.classes.web.ajax_handler import AjaxHandler + from app.classes.web.api_handler import ServersStats, NodeStats except ModuleNotFoundError as e: logger.critical("Import Error: Unable to load {} module".format(e, e.name)) @@ -122,6 +123,8 @@ class webserver: (r'/panel/(.*)', PanelHandler), (r'/server/(.*)', ServerHandler), (r'/ajax/(.*)', AjaxHandler), + (r'/api/stats/servers', ServersStats), + (r'/api/stats/node', NodeStats), ] app = tornado.web.Application(