From ca707761fc272a7367b705a5a6fbe923e0174a4d Mon Sep 17 00:00:00 2001 From: k2safe Date: Tue, 22 Sep 2026 15:15:32 +0800 Subject: [PATCH] fix: stabilize proxy verification readiness --- scripts/verifyProxy.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/scripts/verifyProxy.py b/scripts/verifyProxy.py index 4212b2b..8b3e449 100644 --- a/scripts/verifyProxy.py +++ b/scripts/verifyProxy.py @@ -103,14 +103,21 @@ def request(connection: http.client.HTTPSConnection, path: str) -> tuple[int, di return response.status, dict(response.headers), body +def header(headers: dict[str, str], name: str) -> str: + for key, value in headers.items(): + if key.lower() == name.lower(): + return value + return "" + + def verify_keep_alive(edge_port: int) -> None: context = ssl._create_unverified_context() connection = http.client.HTTPSConnection("127.0.0.1", edge_port, context=context, timeout=3) - expected = b'{"code":"OK","message":"success","data":{"status":"ok"},"requestId":"proxyCheck"}' + expected = b'{"code":"OK","message":"success","data":{"status":"ok"},"requestId":"proxyCheck"}\n' try: for attempt in range(5): status, headers, body = request(connection, "/health") - if status != 200 or headers.get("X-Request-ID") != "proxyCheck" or body != expected: + if status != 200 or header(headers, "X-Request-ID") != "proxyCheck" or body != expected: raise AssertionError(f"unexpected proxied response: status={status} body={body!r}") if attempt < 4 and getattr(connection, "sock", None) is None: raise AssertionError("Nginx did not keep the TLS client connection alive") @@ -118,6 +125,27 @@ def verify_keep_alive(edge_port: int) -> None: connection.close() +def wait_for_proxy(edge_port: int, timeout: float) -> None: + deadline = time.monotonic() + timeout + last_error = "proxy did not return a healthy response" + while time.monotonic() < deadline: + connection = None + try: + context = ssl._create_unverified_context() + connection = http.client.HTTPSConnection("127.0.0.1", edge_port, context=context, timeout=2) + status, headers, body = request(connection, "/health") + if status == 200 and header(headers, "X-Request-ID") == "proxyCheck" and body.endswith(b"\n"): + return + last_error = f"status={status} body={body!r}" + except (OSError, http.client.HTTPException) as error: + last_error = str(error) + finally: + if connection is not None: + connection.close() + time.sleep(0.2) + raise RuntimeError(f"Nginx proxy did not become healthy: {last_error}") + + def verify_timeout(edge_port: int) -> None: context = ssl._create_unverified_context() connection = http.client.HTTPSConnection("127.0.0.1", edge_port, context=context, timeout=3) @@ -190,6 +218,7 @@ def main() -> None: else: raise RuntimeError("Nginx did not start listening") + wait_for_proxy(edge_port, 30) verify_keep_alive(edge_port) verify_timeout(edge_port) print("PASS Nginx TLS proxy, Keep-Alive and upstream timeout", flush=True)