From cad7e8d8c1e9aa6d8852a06428460a92cad124f5 Mon Sep 17 00:00:00 2001 From: kiriharu Date: Tue, 5 Jan 2021 14:56:37 +0300 Subject: [PATCH] added minecraft checker --- apps/api/api/checkers/minecraft.py | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 apps/api/api/checkers/minecraft.py diff --git a/apps/api/api/checkers/minecraft.py b/apps/api/api/checkers/minecraft.py new file mode 100644 index 0000000..7d2c02d --- /dev/null +++ b/apps/api/api/checkers/minecraft.py @@ -0,0 +1,37 @@ +from core.coretypes import Response, PortResponse, ErrorPayload, ErrorCodes, MinecraftResponse, ResponseStatus +from mcstatus import MinecraftServer +from .base import BaseChecker +import socket + + +class MinecraftChecker(BaseChecker): + + def __init__(self, target: str, port: int): + self.port = port + super().__init__(target) + + def check(self) -> Response: + try: + server = MinecraftServer.lookup(f"{self.target}:{self.port}") + status = server.status() + except socket.gaierror: + return Response( + status=ResponseStatus.ERROR, + payload=ErrorPayload( + message="Server does not respond!", + code=ErrorCodes.ConnectError + ), + node=self.node_info + ) + return Response( + status=ResponseStatus.OK, + payload=MinecraftResponse( + latency=status.latency, + max_players=status.players.max, + online=status.players.online + ), + node=self.node_info + ) + + +