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.
This commit is contained in:
sqozz 2021-05-06 18:46:52 +02:00
parent 095131bf08
commit fb452da28f
3 changed files with 19 additions and 35 deletions

View file

@ -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"]:

View file

@ -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)

View file

@ -1,2 +1,3 @@
bottle==0.12.19
mcrcon==0.6.0
mcstatus==5.2.0