Files

45 lines
2.3 KiB
Python

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# QC phase 4: slow-client probes against a TLS listener, by hand-written bytes (no HTTP library).
# slow.py <host> <port> idle <limit s> [sni] a TCP connection that never sends a ClientHello
# slow.py <host> <port> h2 <limit s> [sni] an h2 HEADERS block continued by one CONTINUATION frame every 0.5 s
# Prints "closed after Ns" or "STILL OPEN after Ns". Used by crates/edge/tests/listeners.rs and scripts/conformance.sh.
import socket, ssl, sys, time
host, port, mode, limit = sys.argv[1], int(sys.argv[2]), sys.argv[3], float(sys.argv[4])
sni = sys.argv[5] if len(sys.argv) > 5 else "app.test"
s = socket.create_connection((host, port))
start = time.time()
def el(): return f"{time.time()-start:.1f}s"
if mode == "idle":
# TCP open, no TLS ClientHello ever sent
s.settimeout(limit)
try:
d = s.recv(1)
print(f"idle {port}: closed after {el()} ({d!r})")
except socket.timeout:
print(f"idle {port}: STILL OPEN after {limit:.0f}s")
except OSError as e:
print(f"idle {port}: closed after {el()} ({e})")
elif mode == "h2":
ctx = ssl.create_default_context(); ctx.check_hostname = False; ctx.verify_mode = ssl.CERT_NONE
ctx.set_alpn_protocols(["h2"])
t = ctx.wrap_socket(s, server_hostname=sni)
def frame(ty, flags, sid, payload): return len(payload).to_bytes(3, "big") + bytes([ty, flags]) + sid.to_bytes(4, "big") + payload
# preface, SETTINGS, HEADERS (END_STREAM, no END_HEADERS), then a CONTINUATION byte every 0.5 s
t.sendall(b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" + frame(4, 0, 0, b"") + frame(1, 1, 1, b"\x82"))
start = time.time(); t.settimeout(0.5)
while time.time() - start < limit:
try:
t.sendall(frame(9, 0, 1, b"\x82"))
d = t.recv(4096)
if not d:
print(f"h2 {port} alpn={t.selected_alpn_protocol()}: closed after {el()}"); break
except socket.timeout:
pass
except OSError as e:
print(f"h2 {port} alpn={t.selected_alpn_protocol()}: closed after {el()} ({e})"); break
else:
print(f"h2 {port} alpn={t.selected_alpn_protocol()}: STILL OPEN after {limit:.0f}s")