fix2 motd unicode parse error

https://github.com/Dinnerbone/mcstatus/pull/192#discussion_r800116287
This commit is contained in:
b23r0
2022-02-06 15:43:55 +08:00
parent 6991979f8f
commit 0ff09624bc

View File

@@ -1,5 +1,6 @@
import random import random
import struct import struct
import re
from typing import List from typing import List
from mcstatus.protocol.connection import Connection from mcstatus.protocol.connection import Connection
@@ -128,29 +129,20 @@ class QueryResponse:
data = {} data = {}
players = [] 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: while True:
key = response.read_ascii() 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) response.read(1)
break break
value = response.read_ascii() else:
data[key] = value value = response.read_ascii()
data[key] = value
response.read(len("player_") + 1 + 1) response.read(len("player_") + 1 + 1)