53 lines
2.6 KiB
Python
53 lines
2.6 KiB
Python
import base64, hashlib, json, struct, subprocess, sys, time, urllib.request, uuid, zlib
|
|
|
|
BASE = "http://localhost:8080"; PWD = "correct-horse-battery-staple"
|
|
|
|
|
|
def call(method, path, body=None, headers=None):
|
|
req = urllib.request.Request(BASE + path, data=body, method=method, headers=headers or {})
|
|
with urllib.request.urlopen(req, timeout=30) as r:
|
|
return r.status, r.headers, r.read() # r.headers нечувствителен к регистру
|
|
|
|
|
|
def chunk(t, d):
|
|
return struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d) & 0xFFFFFFFF)
|
|
|
|
|
|
png = (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", 16, 16, 8, 2, 0, 0, 0))
|
|
+ chunk(b"IDAT", zlib.compress(b"".join(b"\x00" + bytes([i, 80, 200]) * 16 for i in range(16))))
|
|
+ chunk(b"IEND", b""))
|
|
|
|
j = {"Content-Type": "application/json"}
|
|
name = "af_" + uuid.uuid4().hex[:8]
|
|
_, _, raw = call("POST", "/api/v1/users", json.dumps({"username": name, "display_name": "a", "password": PWD}).encode(), j)
|
|
user = json.loads(raw)
|
|
_, _, raw = call("POST", "/api/v1/auth/sessions", None,
|
|
{"Authorization": "Basic " + base64.b64encode(f"{name}:{PWD}".encode()).decode()})
|
|
auth = {"Authorization": "Bearer " + json.loads(raw)["token"]}
|
|
_, _, raw = call("POST", "/api/v1/chats", json.dumps({"title": "x", "member_ids": [user["id"]]}).encode(), {**j, **auth})
|
|
chat = json.loads(raw)
|
|
|
|
_, hdr, raw = call("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=f.png", png,
|
|
{"Content-Type": "image/png", **auth})
|
|
att = json.loads(raw)
|
|
inst = hdr.get("X-Instance-Id")
|
|
print("uploaded via instance", inst, "sha256", att["sha256"])
|
|
|
|
ids = subprocess.run(["docker", "compose", "ps", "-q", "api"], capture_output=True, text=True).stdout.split()
|
|
target = next((i for i in ids if subprocess.run(["docker", "inspect", "-f", "{{.Config.Hostname}}", i],
|
|
capture_output=True, text=True).stdout.strip() == inst), None)
|
|
if not target:
|
|
sys.exit(f"container for instance {inst} not found")
|
|
subprocess.run(["docker", "stop", "-t", "8", target], capture_output=True)
|
|
print("stopped", inst)
|
|
time.sleep(4)
|
|
|
|
seen = set()
|
|
for _ in range(10):
|
|
_, h, data = call("GET", f"/api/v1/attachments/{att['id']}/content", None, auth)
|
|
seen.add(h.get("X-Instance-Id"))
|
|
assert hashlib.sha256(data).hexdigest() == att["sha256"], "SHA-256 mismatch"
|
|
print("downloaded 10x via", sorted(seen), "- sha256 matches; stopped instance served:", inst in seen)
|
|
|
|
subprocess.run(["docker", "compose", "up", "-d", "--no-recreate", "api"], capture_output=True)
|
|
print("restored") |