From e0b0e52bd514883baaec56df814897d548703ee6 Mon Sep 17 00:00:00 2001 From: luukas Date: Tue, 31 May 2022 00:11:17 +0300 Subject: [PATCH] Add stdin route for the API --- app/classes/shared/server.py | 1 + app/classes/web/routes/api/api_handlers.py | 6 +++ .../web/routes/api/servers/server/stdin.py | 48 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 app/classes/web/routes/api/servers/server/stdin.py diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index 4e5db4c3..78afa666 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -630,6 +630,7 @@ class Server: # send it self.process.stdin.write(f"{command}\n".encode("utf-8")) self.process.stdin.flush() + return True def crash_detected(self, name): diff --git a/app/classes/web/routes/api/api_handlers.py b/app/classes/web/routes/api/api_handlers.py index 6c032811..e5f72b48 100644 --- a/app/classes/web/routes/api/api_handlers.py +++ b/app/classes/web/routes/api/api_handlers.py @@ -22,6 +22,7 @@ from app.classes.web.routes.api.servers.server.public import ( ApiServersServerPublicHandler, ) from app.classes.web.routes.api.servers.server.stats import ApiServersServerStatsHandler +from app.classes.web.routes.api.servers.server.stdin import ApiServersServerStdinHandler from app.classes.web.routes.api.servers.server.users import ApiServersServerUsersHandler from app.classes.web.routes.api.users.index import ApiUsersIndexHandler from app.classes.web.routes.api.users.user.index import ApiUsersUserIndexHandler @@ -127,6 +128,11 @@ def api_handlers(handler_args): ApiServersServerPublicHandler, handler_args, ), + ( + r"/api/v2/servers/([0-9]+)/stdin/?", + ApiServersServerStdinHandler, + handler_args, + ), ( r"/api/v2/roles/?", ApiRolesIndexHandler, diff --git a/app/classes/web/routes/api/servers/server/stdin.py b/app/classes/web/routes/api/servers/server/stdin.py new file mode 100644 index 00000000..aff8697d --- /dev/null +++ b/app/classes/web/routes/api/servers/server/stdin.py @@ -0,0 +1,48 @@ +import logging + +from peewee import DoesNotExist +from app.classes.models.server_permissions import EnumPermissionsServer +from app.classes.web.base_api_handler import BaseApiHandler + + +logger = logging.getLogger(__name__) + + +class ApiServersServerStdinHandler(BaseApiHandler): + def post(self, server_id: str): + auth_data = self.authenticate_user() + if not auth_data: + return + + if server_id not in [str(x["server_id"]) for x in auth_data[0]]: + # if the user doesn't have access to the server, return an error + return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"}) + + if ( + EnumPermissionsServer.COMMANDS + not in self.controller.server_perms.get_user_id_permissions_list( + auth_data[4]["user_id"], server_id + ) + ): + # if the user doesn't have Commands permission, return an error + return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"}) + + try: + svr = self.controller.get_server_obj(server_id) + except DoesNotExist: + logger.critical( + "Something has gone VERY wrong! " + "Crafty can't access the server object. " + "Please report this to the devs" + ) + return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"}) + + if svr.send_command(self.request.body.decode("utf-8")): + return self.finish_json( + 200, + {"status": "ok"}, + ) + self.finish_json( + 200, + {"status": "error", "error": "SERVER_NOT_RUNNING"}, + )