Files
AstraPacks/tools/smoke_protocol.py
T

122 lines
3.9 KiB
Python

"""Vanilla protocol 776 probe against an isolated, localhost offline test server.
Does not render resources or claim to be a graphical Minecraft client.
Requires network-compression-threshold=-1, timeoutSeconds=10 and retries=0.
"""
import hashlib
import io
import socket
import struct
import sys
import time
import urllib.request
import uuid
import zipfile
PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 25585
def varint(n):
out = bytearray()
while n > 127:
out.append((n & 127) | 128)
n >>= 7
out.append(n)
return bytes(out)
def readint(read):
n = 0
for i in range(5):
b = read(1)
if not b:
raise EOFError()
n |= (b[0] & 127) << (7 * i)
if b[0] < 128:
return n
raise ValueError("Bad VarInt")
def string(s):
b = s.encode()
return varint(len(b)) + b
class Client:
def __init__(self, name):
self.s = socket.create_connection(("127.0.0.1", PORT), timeout=20)
self.f = self.s.makefile("rb")
self.send(0, varint(776) + string("127.0.0.1") + struct.pack(">H", PORT) + varint(2))
self.send(0, string(name) + uuid.uuid4().bytes)
while True:
kind, data = self.recv()
if kind == 2:
break
if kind == 4: # Login query: vanilla client does not understand mod channels.
query = readint(data.read)
self.send(2, varint(query) + b"\x00")
elif kind == 0:
raise AssertionError("Login disconnected: " + repr(data.read()))
elif kind == 3:
raise AssertionError("Disable compression in the isolated test server")
self.send(3) # login acknowledged
def send(self, kind, data=b""):
payload = varint(kind) + data
self.s.sendall(varint(len(payload)) + payload)
def recv(self):
size = readint(self.f.read)
data = io.BytesIO(self.f.read(size))
return readint(data.read), data
def config(self):
while True:
kind, data = self.recv()
if kind == 14:
self.send(7, varint(0)) # request registry data; no known packs
elif kind == 4:
self.send(4, data.read())
elif kind == 5:
self.send(5, data.read())
elif kind in (2, 3, 9):
return kind, data
def pack(self):
kind, data = self.config()
assert kind == 9, (kind, data.read())
self.id = data.read(16)
self.url = data.read(readint(data.read)).decode()
self.sha1 = data.read(readint(data.read)).decode()
assert data.read(1) == b"\x01", "Pack must be required"
with urllib.request.urlopen(self.url, timeout=10) as response:
payload = response.read()
assert hashlib.sha1(payload).hexdigest() == self.sha1
with zipfile.ZipFile(io.BytesIO(payload)) as archive:
assert "pack.mcmeta" in archive.namelist()
def respond(self, action, identity=None):
self.send(6, (identity or self.id) + varint(action))
def close(self):
self.f.close()
self.s.close()
for mode, action in [("declined", 1), ("failed_download", 2), ("failed_reload", 7), ("loaded", 0), ("accepted_timeout", 3), ("wrong_uuid", None)]:
client = Client("Astra_" + mode[:10])
try:
client.pack()
if action is not None:
client.respond(action)
else:
client.respond(0, uuid.uuid4().bytes)
start = time.monotonic()
kind, data = client.config()
assert kind == (3 if mode == "loaded" else 2), (mode, kind, data.read())
if mode in ("accepted_timeout", "wrong_uuid"):
assert time.monotonic() - start >= 8, "Unexpected early gate completion"
print("PASS", mode, "HTTP SHA-1 verified;", "configuration finished" if kind == 3 else "disconnected before world", flush=True)
finally:
client.close()