forked from mtusi2026/mtusi2026lab4
274 lines
9.8 KiB
Python
274 lines
9.8 KiB
Python
import base64
|
|
import hashlib
|
|
import http.client
|
|
import json
|
|
import secrets
|
|
import time
|
|
from urllib.parse import urlsplit
|
|
|
|
|
|
class FlowError(Exception):
|
|
def __init__(self, code):
|
|
self.code = code
|
|
|
|
|
|
class Client:
|
|
def __init__(self, base, protocol, stop):
|
|
self.base, self.protocol, self.stop = base, protocol, stop
|
|
|
|
def request(
|
|
self,
|
|
method,
|
|
url,
|
|
body=None,
|
|
token=None,
|
|
headers=None,
|
|
stage="api",
|
|
records=None,
|
|
deadline=None,
|
|
):
|
|
if self.stop.is_set():
|
|
raise FlowError("interrupted")
|
|
parsed = urlsplit(url)
|
|
if parsed.scheme != "http" or parsed.hostname not in ("localhost", "127.0.0.1"):
|
|
raise FlowError("unexpected_endpoint")
|
|
expected_port = self.protocol["ports"]["s3" if stage == "s3_post" else "api"]
|
|
if parsed.port != expected_port:
|
|
raise FlowError("unexpected_endpoint_port")
|
|
data = json.dumps(body).encode("utf-8") if isinstance(body, dict) else body
|
|
h = dict(headers or {})
|
|
if isinstance(body, dict):
|
|
h["Content-Type"] = "application/json"
|
|
if token:
|
|
h["Authorization"] = "Bearer " + token
|
|
remaining = (
|
|
deadline - time.monotonic()
|
|
if deadline
|
|
else self.protocol["request_timeout_seconds"]
|
|
)
|
|
if remaining <= 0:
|
|
raise FlowError("flow_timeout")
|
|
connection = http.client.HTTPConnection(
|
|
parsed.hostname,
|
|
parsed.port,
|
|
timeout=min(self.protocol["request_timeout_seconds"], remaining),
|
|
)
|
|
started = time.monotonic_ns()
|
|
record = {
|
|
"stage": stage,
|
|
"method": method,
|
|
"started_ns": started,
|
|
"request_body_bytes": len(data or b""),
|
|
}
|
|
if records is not None:
|
|
records.append(record)
|
|
try:
|
|
connection.request(
|
|
method,
|
|
parsed.path + (("?" + parsed.query) if parsed.query else ""),
|
|
data,
|
|
h,
|
|
)
|
|
response = connection.getresponse()
|
|
payload = response.read(65537)
|
|
record.update(
|
|
status=response.status,
|
|
response_body_bytes=len(payload),
|
|
instance=response.getheader("X-Instance-Id"),
|
|
request_id=response.getheader("X-Request-Id"),
|
|
)
|
|
if len(payload) > 65536:
|
|
raise FlowError("unexpected_response_size")
|
|
try:
|
|
decoded = json.loads(payload) if payload else None
|
|
except ValueError:
|
|
if response.status < 400:
|
|
raise FlowError("unexpected_response_format") from None
|
|
decoded = None
|
|
if response.status >= 400:
|
|
code = (
|
|
decoded.get("error", {}).get("code", "http_error")
|
|
if isinstance(decoded, dict)
|
|
else "http_" + str(response.status)
|
|
)
|
|
record["error"] = code
|
|
raise FlowError(code)
|
|
return decoded, response.status, record
|
|
except FlowError:
|
|
raise
|
|
except Exception as error:
|
|
record["error"] = type(error).__name__
|
|
raise FlowError(type(error).__name__) from None
|
|
finally:
|
|
record["finished_ns"] = time.monotonic_ns()
|
|
connection.close()
|
|
|
|
def api(self, method, path, **kwargs):
|
|
return self.request(method, self.base + path, **kwargs)
|
|
|
|
def bootstrap(self):
|
|
actors = []
|
|
for label in ("alice", "bob"):
|
|
username = "exp_" + label + "_" + secrets.token_hex(6)
|
|
password = secrets.token_hex(24)
|
|
user, status, _ = self.api(
|
|
"POST",
|
|
"/api/v1/users",
|
|
body={
|
|
"username": username,
|
|
"display_name": label,
|
|
"password": password,
|
|
},
|
|
)
|
|
if status != 201:
|
|
raise FlowError("registration_status")
|
|
basic = base64.b64encode((username + ":" + password).encode()).decode()
|
|
session, status, _ = self.api(
|
|
"POST",
|
|
"/api/v1/auth/sessions",
|
|
headers={"Authorization": "Basic " + basic},
|
|
)
|
|
if status != 201:
|
|
raise FlowError("session_status")
|
|
actors.append({"id": user["id"], "token": session["token"]})
|
|
chat, status, _ = self.api(
|
|
"POST",
|
|
"/api/v1/chats",
|
|
token=actors[0]["token"],
|
|
body={
|
|
"title": "Upload experiment",
|
|
"member_ids": [a["id"] for a in actors],
|
|
},
|
|
)
|
|
if status != 201:
|
|
raise FlowError("chat_status")
|
|
return actors, chat["id"]
|
|
|
|
def upload(self, mode, actor, chat, data, expected_digest, records, deadline):
|
|
path = "/api/v1/chats/" + chat
|
|
if mode == "proxy":
|
|
attachment, status, record = self.api(
|
|
"POST",
|
|
path + "/attachments?filename=experiment.png",
|
|
body=data,
|
|
token=actor["token"],
|
|
headers={"Content-Type": "image/png"},
|
|
stage="proxy",
|
|
records=records,
|
|
deadline=deadline,
|
|
)
|
|
else:
|
|
# Matches the browser: hash inside the measured logical direct flow.
|
|
start_hash = time.monotonic_ns()
|
|
checksum = hashlib.sha256(data).hexdigest()
|
|
records.append(
|
|
{
|
|
"stage": "client_sha256",
|
|
"started_ns": start_hash,
|
|
"finished_ns": time.monotonic_ns(),
|
|
}
|
|
)
|
|
instruction, status, _ = self.api(
|
|
"POST",
|
|
path + "/attachment-uploads",
|
|
token=actor["token"],
|
|
body={
|
|
"filename": "experiment.png",
|
|
"content_type": "image/png",
|
|
"size_bytes": len(data),
|
|
"sha256": checksum,
|
|
},
|
|
stage="init",
|
|
records=records,
|
|
deadline=deadline,
|
|
)
|
|
if status != 201:
|
|
raise FlowError("init_status")
|
|
fields = instruction["upload"]["fields"]
|
|
boundary = "experiment-" + secrets.token_hex(12)
|
|
pieces = [
|
|
f'--{boundary}\r\nContent-Disposition: form-data; name="{k}"\r\n\r\n{v}\r\n'.encode()
|
|
for k, v in fields.items()
|
|
]
|
|
pieces += [
|
|
f'--{boundary}\r\nContent-Disposition: form-data; name="file"; filename="experiment.png"\r\nContent-Type: image/png\r\n\r\n'.encode(),
|
|
data,
|
|
f"\r\n--{boundary}--\r\n".encode(),
|
|
]
|
|
multipart = b"".join(pieces)
|
|
# This is the only S3 request; no Bearer token or application headers.
|
|
_, post_status, _ = self.request(
|
|
"POST",
|
|
instruction["upload"]["url"],
|
|
body=multipart,
|
|
headers={"Content-Type": "multipart/form-data; boundary=" + boundary},
|
|
stage="s3_post",
|
|
records=records,
|
|
deadline=deadline,
|
|
)
|
|
if post_status != 204:
|
|
raise FlowError("s3_post_status")
|
|
attachment, status, record = self.api(
|
|
"POST",
|
|
path + "/attachment-uploads/" + instruction["upload_id"] + "/complete",
|
|
token=actor["token"],
|
|
stage="complete",
|
|
records=records,
|
|
deadline=deadline,
|
|
)
|
|
if (
|
|
status != 201
|
|
or attachment.get("status") != "ready"
|
|
or attachment.get("size_bytes") != len(data)
|
|
or attachment.get("sha256") != expected_digest
|
|
):
|
|
raise FlowError("incorrect_ready_metadata")
|
|
return {
|
|
"id": attachment["id"],
|
|
"sha256": attachment["sha256"],
|
|
"size_bytes": attachment["size_bytes"],
|
|
"instance": record["instance"],
|
|
}
|
|
|
|
def verify(self, actor, attachment, size, expected_digest):
|
|
if self.stop.is_set():
|
|
raise FlowError("interrupted")
|
|
parsed = urlsplit(self.base)
|
|
connection = http.client.HTTPConnection(
|
|
parsed.hostname, parsed.port, timeout=35
|
|
)
|
|
checksum, received = hashlib.sha256(), 0
|
|
try:
|
|
connection.request(
|
|
"GET",
|
|
"/api/v1/attachments/" + attachment["id"] + "/content",
|
|
headers={"Authorization": "Bearer " + actor["token"]},
|
|
)
|
|
response = connection.getresponse()
|
|
if (
|
|
response.status != 200
|
|
or response.getheader("Content-Length") != str(size)
|
|
or response.getheader("Content-Type") != "image/png"
|
|
):
|
|
raise FlowError("download_headers")
|
|
while True:
|
|
if self.stop.is_set():
|
|
raise FlowError("interrupted")
|
|
block = response.read(32768)
|
|
if not block:
|
|
break
|
|
received += len(block)
|
|
if received > size:
|
|
raise FlowError("download_size")
|
|
checksum.update(block)
|
|
if received != size or checksum.hexdigest() != expected_digest:
|
|
raise FlowError("download_checksum")
|
|
return {
|
|
"id": attachment["id"],
|
|
"verified": True,
|
|
"size_bytes": received,
|
|
"sha256": checksum.hexdigest(),
|
|
}
|
|
finally:
|
|
connection.close()
|