Add stdin route for the API

This commit is contained in:
luukas 2022-05-31 00:11:17 +03:00
parent 290c398198
commit e0b0e52bd5
No known key found for this signature in database
GPG Key ID: CC4915E8D71FC044
3 changed files with 55 additions and 0 deletions

View File

@ -630,6 +630,7 @@ class Server:
# send it # send it
self.process.stdin.write(f"{command}\n".encode("utf-8")) self.process.stdin.write(f"{command}\n".encode("utf-8"))
self.process.stdin.flush() self.process.stdin.flush()
return True
def crash_detected(self, name): def crash_detected(self, name):

View File

@ -22,6 +22,7 @@ from app.classes.web.routes.api.servers.server.public import (
ApiServersServerPublicHandler, ApiServersServerPublicHandler,
) )
from app.classes.web.routes.api.servers.server.stats import ApiServersServerStatsHandler 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.servers.server.users import ApiServersServerUsersHandler
from app.classes.web.routes.api.users.index import ApiUsersIndexHandler from app.classes.web.routes.api.users.index import ApiUsersIndexHandler
from app.classes.web.routes.api.users.user.index import ApiUsersUserIndexHandler from app.classes.web.routes.api.users.user.index import ApiUsersUserIndexHandler
@ -127,6 +128,11 @@ def api_handlers(handler_args):
ApiServersServerPublicHandler, ApiServersServerPublicHandler,
handler_args, handler_args,
), ),
(
r"/api/v2/servers/([0-9]+)/stdin/?",
ApiServersServerStdinHandler,
handler_args,
),
( (
r"/api/v2/roles/?", r"/api/v2/roles/?",
ApiRolesIndexHandler, ApiRolesIndexHandler,

View File

@ -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"},
)