From fb452da28f533c8b2eec8706a73b0dc022cb5d7a Mon Sep 17 00:00:00 2001 From: sqozz Date: Thu, 6 May 2021 18:46:52 +0200 Subject: [PATCH] Replace rcon with "server list ping" for basic information See https://wiki.vg/Server_List_Ping for details. It should allow to run the landing page more stable as less parsing is involved. Also it does not need to wait for the version reply anymore. This should fix several 500 errors if the version response is to slow. --- homepage/html/index.html | 3 +-- homepage/main.py | 50 +++++++++++++-------------------------- homepage/requirements.txt | 1 + 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/homepage/html/index.html b/homepage/html/index.html index 0fa203e..55cc891 100644 --- a/homepage/html/index.html +++ b/homepage/html/index.html @@ -38,8 +38,7 @@ Hardware: Versions: • Minecraft: {{server_details["versions"]["minecraft"]}} -• Paper: {{server_details["versions"]["paper"]}} -• API: {{server_details["versions"]["api"]}} +• Server: {{server_details["versions"]["paper"]}} Datapacks: % for datapack in server_details["datapacks"]: diff --git a/homepage/main.py b/homepage/main.py index 3b102b6..a1b8204 100644 --- a/homepage/main.py +++ b/homepage/main.py @@ -1,9 +1,10 @@ #!/usr/bin/env python3 -from bottle import route, run, static_file, template -from os import listdir, sysconf +from mcstatus import MinecraftServer from os.path import join as pathjoin +from bottle import route, run, static_file, template from random import random, choice from mcrcon import MCRcon +from os import listdir, sysconf import configparser import platform import requests @@ -48,29 +49,14 @@ def callback(path): return static_file(path, root="static") def currentPlayerData(): + status = server_status() player = {} - with MCRcon(CONFIG["mcrcon"]["host"], CONFIG["mcrcon"]["password"], port=int(CONFIG["mcrcon"]["port"])) as mcr: - resp = mcr.command("list uuids") - resp = resp.split(":") - player_string = resp[1].strip() - if player_string != "": - player_list = player_string.split(",") - else: - player_list = [] - player_list = list(map(lambda x: x.strip(), player_list)) - count_regex = re.match("There are ([0-9]+) of a max of ([0-9]+) players online", resp[0]) - num_players = count_regex.group(1) - max_players = count_regex.group(2) - player["count"] = {} - player["count"]["current"] = num_players - player["count"]["max"] = max_players + player["count"]["current"] = status.players.online + player["count"]["max"] = status.players.max player["players"] = [] - for p in player_list: - data = p.split(" ") - name = data[0].strip() - uuid = data[1].strip().replace("(", "").replace(")", "") - player["players"].append({"name": name, "uuid": uuid}) + for p in status.players.sample: + player["players"].append({"name": p.name, "uuid": p.id}) return player def fetchSkin(uuid): @@ -107,17 +93,10 @@ def hardware_info(): return {"cpu": processor, "ram": mem_pretty} def paper_version(): - with MCRcon(CONFIG["mcrcon"]["host"], CONFIG["mcrcon"]["password"], port=int(CONFIG["mcrcon"]["port"])) as mcr: - for i in range(0, 10): - resp = mcr.command("version") - if not "please wait" in resp: - break - match = re.match(".*(This server is running Paper version )(.*) \((.*)\) \(Implementing API version (.*)\).*", resp) - paper_version = match.group(2) - mc_version = match.group(3) - mc_version = mc_version.split(":")[1].strip() - api_version = match.group(4) - return {"paper": paper_version, "minecraft": mc_version, "api": api_version} + status = server_status() + paper_version = status.version.name + mc_version = status.version.protocol + return {"paper": paper_version, "minecraft": mc_version} def datapack_info(): with MCRcon(CONFIG["mcrcon"]["host"], CONFIG["mcrcon"]["password"], port=int(CONFIG["mcrcon"]["port"])) as mcr: @@ -141,6 +120,11 @@ def server_details(): datapacks = datapack_info() return {"hardware": hardware, "versions": versions, "datapacks": datapacks} +def server_status(): + server = MinecraftServer.lookup(CONFIG["mcrcon"]["host"]) + status = server.status() + return status + CONFIG = parseConfig() run(host="localhost", port=8080) diff --git a/homepage/requirements.txt b/homepage/requirements.txt index f3b4441..76b95f1 100644 --- a/homepage/requirements.txt +++ b/homepage/requirements.txt @@ -1,2 +1,3 @@ bottle==0.12.19 mcrcon==0.6.0 +mcstatus==5.2.0