From 0ff09624bc78e349a87db9786ec12012f66b1d61 Mon Sep 17 00:00:00 2001 From: b23r0 Date: Sun, 6 Feb 2022 15:43:55 +0800 Subject: [PATCH] fix2 motd unicode parse error https://github.com/Dinnerbone/mcstatus/pull/192#discussion_r800116287 --- mcstatus/querier.py | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/mcstatus/querier.py b/mcstatus/querier.py index cd0fc41..fc0ab76 100644 --- a/mcstatus/querier.py +++ b/mcstatus/querier.py @@ -1,5 +1,6 @@ import random import struct +import re from typing import List from mcstatus.protocol.connection import Connection @@ -128,29 +129,20 @@ class QueryResponse: data = {} players = [] - # If hostname is set to unicode, using other parameters of read_ascii() may be in the wrong order - key = response.received[:8].decode('ISO-8859-1') - if key == 'hostname': - response.read_ascii() - name = bytearray() - while True: - c = response.read(1) - name += c - if c[0] == 0: - if response.received[:8].decode('ISO-8859-1') == 'gametype': - name.pop() - break - # Since the minecraft protocol does not support unicode, the hostname is still not resolved correctly - # However, this will avoid other parameter parsing errors - data[key] = name.decode('ISO-8859-1') - while True: key = response.read_ascii() - if len(key) == 0: + if key == "hostname": + name = re.search(b"(.*)\x00gametype", response.received, flags=re.DOTALL).group(1) + # Since the query protocol does not properly support unicode, the hostname is still not resolved + # correctly; however, this will avoid other parameter parsing errors. + data[key] = response.read(len(name)).decode("ISO-8859-1") + response.read(1) # ignore null byte + elif len(key) == 0: response.read(1) break - value = response.read_ascii() - data[key] = value + else: + value = response.read_ascii() + data[key] = value response.read(len("player_") + 1 + 1)