Revert changes to ping code, catch raised errs accordingly
This commit is contained in:
parent
ce7e47c472
commit
0b451c9516
2 changed files with 22 additions and 25 deletions
41
src/ping.py
41
src/ping.py
|
|
@ -1,39 +1,33 @@
|
||||||
import struct
|
|
||||||
import socket
|
|
||||||
import base64
|
import base64
|
||||||
import json
|
import json
|
||||||
|
import socket
|
||||||
|
import struct
|
||||||
|
|
||||||
|
|
||||||
class ServerPingResponse:
|
class Server:
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self.description = data.get("description")
|
self.description = data.get("description")
|
||||||
if isinstance(self.description, dict):
|
if isinstance(self.description, dict):
|
||||||
self.description = self.description["text"]
|
self.description = self.description["text"]
|
||||||
|
|
||||||
self.icon = base64.b64decode(data.get("favicon", "")[22:])
|
self.icon = base64.b64decode(data.get("favicon", "")[22:])
|
||||||
self.players: ServerPingResponsePlayers = ServerPingResponsePlayers(data["players"])
|
self.players = Players(data["players"])
|
||||||
self.version = data["version"]["name"]
|
self.version = data["version"]["name"]
|
||||||
self.protocol = data["version"]["protocol"]
|
self.protocol = data["version"]["protocol"]
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Server(description={!r}, icon={!r}, version={!r}, protocol={!r}, players={})".format(
|
return "Server(description={!r}, icon={!r}, version={!r}, protocol={!r}, players={})".format(self.description, bool(self.icon), self.version, self.protocol, self.players)
|
||||||
self.description,
|
|
||||||
bool(self.icon),
|
|
||||||
self.version,
|
|
||||||
self.protocol,
|
|
||||||
self.players,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class ServerPingResponsePlayers(list):
|
class Players(list):
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
super().__init__(ServerPingResponsePlayer(x) for x in data.get("sample", []))
|
super().__init__(Player(x) for x in data.get("sample", []))
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "[{}]".format(", ".join(str(x) for x in self))
|
return "[{}]".format(", ".join(str(x) for x in self))
|
||||||
|
|
||||||
|
|
||||||
class ServerPingResponsePlayer:
|
class Player:
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self.id = data["id"]
|
self.id = data["id"]
|
||||||
self.name = data["name"]
|
self.name = data["name"]
|
||||||
|
|
@ -55,16 +49,12 @@ def ping(ip, port=25565):
|
||||||
i |= (k & 0x7F) << (j * 7)
|
i |= (k & 0x7F) << (j * 7)
|
||||||
j += 1
|
j += 1
|
||||||
if j > 5:
|
if j > 5:
|
||||||
return -1
|
raise ValueError("var_int too big")
|
||||||
if not (k & 0x80):
|
if not (k & 0x80):
|
||||||
return i
|
return i
|
||||||
|
|
||||||
try:
|
sock = socket.socket()
|
||||||
sock = socket.socket()
|
sock.connect((ip, port))
|
||||||
sock.connect((ip, port))
|
|
||||||
except ConnectionRefusedError:
|
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
host = ip.encode("utf-8")
|
host = ip.encode("utf-8")
|
||||||
data = b"" # wiki.vg/Server_List_Ping
|
data = b"" # wiki.vg/Server_List_Ping
|
||||||
|
|
@ -77,7 +67,10 @@ def ping(ip, port=25565):
|
||||||
sock.sendall(data + b"\x01\x00") # handshake + status ping
|
sock.sendall(data + b"\x01\x00") # handshake + status ping
|
||||||
length = read_var_int() # full packet length
|
length = read_var_int() # full packet length
|
||||||
if length < 10:
|
if length < 10:
|
||||||
return None
|
if length < 0:
|
||||||
|
raise ValueError("negative length read")
|
||||||
|
else:
|
||||||
|
raise ValueError(f"invalid response {sock.read(length)}") # type: ignore
|
||||||
|
|
||||||
sock.recv(1) # packet type, 0 for pings
|
sock.recv(1) # packet type, 0 for pings
|
||||||
length = read_var_int() # string length
|
length = read_var_int() # string length
|
||||||
|
|
@ -85,11 +78,11 @@ def ping(ip, port=25565):
|
||||||
while len(data) != length:
|
while len(data) != length:
|
||||||
chunk = sock.recv(length - len(data))
|
chunk = sock.recv(length - len(data))
|
||||||
if not chunk:
|
if not chunk:
|
||||||
return None
|
raise ValueError("connection abborted")
|
||||||
|
|
||||||
data += chunk
|
data += chunk
|
||||||
try:
|
try:
|
||||||
return ServerPingResponse(json.loads(data))
|
return Server(json.loads(data))
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
return None
|
return None
|
||||||
finally:
|
finally:
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,11 @@ class Server:
|
||||||
if protocol != "tcp":
|
if protocol != "tcp":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
server_ping_info = ping_server("localhost", port=int(port))
|
try:
|
||||||
|
server_ping_info = ping_server("localhost", port=int(port))
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
|
||||||
if server_ping_info is not None:
|
if server_ping_info is not None:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue