Implement authenticated public relay and Rust device daemons
This commit is contained in:
1 parent
f96c1aa109
commit
e1c152a657
29 files changed
+3244
-39
No files matched your search
+196
@@ -0,0 +1,196 @@
|
||||
"""Exercise the actual Go relay and Rust daemons using a simulated phone TCP endpoint.
|
||||
|
||||
Build first with scripts/build.ps1 or scripts/build.sh. No external Python packages.
|
||||
"""
|
||||
import argparse
|
||||
import concurrent.futures
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
import socket
|
||||
import socketserver
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def free_port():
|
||||
with socket.socket() as sock:
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
return sock.getsockname()[1]
|
||||
|
||||
|
||||
def wait_until(predicate, description, seconds=15):
|
||||
deadline = time.monotonic() + seconds
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
if predicate():
|
||||
return
|
||||
except (OSError, urllib.error.URLError):
|
||||
pass
|
||||
time.sleep(0.1)
|
||||
raise AssertionError(f"Timed out: {description}")
|
||||
|
||||
|
||||
class Echo(socketserver.BaseRequestHandler):
|
||||
def handle(self):
|
||||
try:
|
||||
while data := self.request.recv(65536):
|
||||
self.request.sendall(data)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
class Phone(socketserver.ThreadingTCPServer):
|
||||
daemon_threads = True
|
||||
allow_reuse_address = True
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--bin-dir", type=Path, default=ROOT / "bin")
|
||||
parser.add_argument("--rust-dir", type=Path)
|
||||
args = parser.parse_args()
|
||||
rust_dir = args.rust_dir or args.bin_dir
|
||||
suffix = ".exe" if os.name == "nt" else ""
|
||||
server_bin = (args.bin_dir / f"ads-server{suffix}").resolve()
|
||||
agent_bin = (rust_dir / f"ads-agent{suffix}").resolve()
|
||||
client_bin = (rust_dir / f"ads-client{suffix}").resolve()
|
||||
for path in (server_bin, agent_bin, client_bin):
|
||||
if not path.exists():
|
||||
raise SystemExit(f"Build missing binary first: {path}")
|
||||
|
||||
logs = ROOT / "work" / "e2e"
|
||||
logs.mkdir(parents=True, exist_ok=True)
|
||||
processes, handles = [], []
|
||||
agent_token, client_token = secrets.token_hex(32), secrets.token_hex(32)
|
||||
relay_port, local_port, second_port = free_port(), free_port(), free_port()
|
||||
relay = f"ws://127.0.0.1:{relay_port}"
|
||||
api = f"http://127.0.0.1:{relay_port}"
|
||||
|
||||
def launch(name, command, tokens):
|
||||
env = os.environ.copy()
|
||||
env.pop("ADS_AGENT_TOKEN", None)
|
||||
env.pop("ADS_CLIENT_TOKEN", None)
|
||||
env.update(tokens)
|
||||
handle = open(logs / f"{name}.log", "wb")
|
||||
handles.append(handle)
|
||||
process = subprocess.Popen([str(item) for item in command], env=env,
|
||||
stdout=handle, stderr=subprocess.STDOUT,
|
||||
creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0)
|
||||
processes.append(process)
|
||||
return process
|
||||
|
||||
def stop(process):
|
||||
if process.poll() is None:
|
||||
process.terminate()
|
||||
try:
|
||||
process.wait(timeout=8)
|
||||
except subprocess.TimeoutExpired:
|
||||
process.kill()
|
||||
process.wait(timeout=5)
|
||||
|
||||
def devices(token=client_token):
|
||||
req = urllib.request.Request(api + "/v1/devices", headers={"Authorization": "Bearer " + token})
|
||||
with urllib.request.urlopen(req, timeout=2) as response:
|
||||
return json.load(response)
|
||||
|
||||
def busy():
|
||||
return any(device["id"] == "test-phone" and device["busy"] for device in devices())
|
||||
|
||||
def transfer(size):
|
||||
payload = secrets.token_bytes(size)
|
||||
with socket.create_connection(("127.0.0.1", local_port), timeout=8) as sock:
|
||||
sock.settimeout(10)
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as writer:
|
||||
sent = writer.submit(sock.sendall, payload)
|
||||
received = bytearray()
|
||||
while len(received) < size:
|
||||
chunk = sock.recv(min(65536, size - len(received)))
|
||||
if not chunk:
|
||||
raise AssertionError("Tunnel closed before complete response")
|
||||
received.extend(chunk)
|
||||
sent.result(timeout=5)
|
||||
assert received == payload, "Binary stream corruption"
|
||||
|
||||
with Phone(("127.0.0.1", 0), Echo) as phone:
|
||||
threading.Thread(target=phone.serve_forever, daemon=True).start()
|
||||
phone_port = phone.server_address[1]
|
||||
server_command = [server_bin, "--listen", f"127.0.0.1:{relay_port}"]
|
||||
agent_command = [agent_bin, "--server", relay, "--allow-insecure", "--device", "test-phone",
|
||||
"--target", f"127.0.0.1:{phone_port}"]
|
||||
client_command = [client_bin, "--server", relay, "--allow-insecure", "--device", "test-phone",
|
||||
"--listen", f"127.0.0.1:{local_port}", "--no-auto-connect"]
|
||||
try:
|
||||
server = launch("server", server_command, {"ADS_AGENT_TOKEN": agent_token, "ADS_CLIENT_TOKEN": client_token})
|
||||
wait_until(lambda: devices() == [], "relay startup")
|
||||
try:
|
||||
devices(agent_token)
|
||||
raise AssertionError("Agent token granted client access")
|
||||
except urllib.error.HTTPError as error:
|
||||
assert error.code == 401
|
||||
agent = launch("agent", agent_command, {"ADS_AGENT_TOKEN": agent_token})
|
||||
wait_until(lambda: len(devices()) == 1, "agent registration")
|
||||
client = launch("client", client_command, {"ADS_CLIENT_TOKEN": client_token})
|
||||
wait_until(busy, "client exclusive lease")
|
||||
transfer(2 * 1024 * 1024)
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as pool:
|
||||
list(pool.map(transfer, [256 * 1024] * 8))
|
||||
print("PASS: 2 MiB binary stream and eight concurrent streams")
|
||||
|
||||
# Keep a data stream idle across the server's 20-second control ping.
|
||||
with socket.create_connection(("127.0.0.1", local_port), timeout=8) as idle:
|
||||
idle.settimeout(5)
|
||||
time.sleep(22)
|
||||
idle.sendall(b"after-heartbeat")
|
||||
received = bytearray()
|
||||
while len(received) < 15:
|
||||
chunk = idle.recv(15 - len(received))
|
||||
assert chunk, "Idle tunnel was closed by heartbeat processing"
|
||||
received.extend(chunk)
|
||||
assert received == b"after-heartbeat"
|
||||
print("PASS: idle tunnel survives control ping/pong heartbeat")
|
||||
|
||||
competitor_command = client_command.copy()
|
||||
competitor_command[competitor_command.index("--listen") + 1] = f"127.0.0.1:{second_port}"
|
||||
competitor = launch("competitor", competitor_command, {"ADS_CLIENT_TOKEN": client_token})
|
||||
time.sleep(1)
|
||||
with socket.create_connection(("127.0.0.1", second_port), timeout=3) as sock:
|
||||
sock.settimeout(3)
|
||||
try:
|
||||
sock.sendall(b"denied")
|
||||
assert sock.recv(16) == b"", "Second client stole exclusive device"
|
||||
except (ConnectionResetError, ConnectionAbortedError):
|
||||
pass
|
||||
stop(competitor)
|
||||
print("PASS: competing client cannot acquire occupied device")
|
||||
|
||||
stop(agent)
|
||||
wait_until(lambda: devices() == [], "offline device removal")
|
||||
agent = launch("agent-restarted", agent_command, {"ADS_AGENT_TOKEN": agent_token})
|
||||
wait_until(busy, "client lease recovery after agent restart", seconds=40)
|
||||
transfer(65536)
|
||||
print("PASS: agent restart and client reconnection")
|
||||
|
||||
stop(server)
|
||||
server = launch("server-restarted", server_command, {"ADS_AGENT_TOKEN": agent_token, "ADS_CLIENT_TOKEN": client_token})
|
||||
wait_until(busy, "both daemons reconnect after relay restart", seconds=45)
|
||||
transfer(65536)
|
||||
print("PASS: relay restart and both-daemon recovery")
|
||||
assert agent.poll() is None and client.poll() is None
|
||||
finally:
|
||||
for process in reversed(processes):
|
||||
stop(process)
|
||||
for handle in handles:
|
||||
handle.close()
|
||||
phone.shutdown()
|
||||
print("PASS: all three-program integration checks")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,98 @@
|
||||
"""Opt-in, read-only smoke test through a remote ADB smart socket and all three programs."""
|
||||
import argparse
|
||||
import os
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
from e2e import ROOT, free_port, wait_until
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--adb-server", required=True)
|
||||
parser.add_argument("--serial", required=True)
|
||||
parser.add_argument("--adb", default="adb")
|
||||
parser.add_argument("--bin-dir", type=Path, default=ROOT / "bin")
|
||||
parser.add_argument("--rust-dir", type=Path)
|
||||
args = parser.parse_args()
|
||||
suffix = ".exe" if os.name == "nt" else ""
|
||||
rust_dir = args.rust_dir or args.bin_dir
|
||||
relay_port, local_port = free_port(), free_port()
|
||||
server_url = f"ws://127.0.0.1:{relay_port}"
|
||||
endpoint = f"127.0.0.1:{local_port}"
|
||||
env = os.environ.copy()
|
||||
env["ADS_AGENT_TOKEN"] = secrets.token_hex(32)
|
||||
env["ADS_CLIENT_TOKEN"] = secrets.token_hex(32)
|
||||
for key in ("ADB_SERVER_SOCKET", "ANDROID_ADB_SERVER_ADDRESS", "ANDROID_ADB_SERVER_PORT"):
|
||||
env.pop(key, None)
|
||||
folder = ROOT / "work" / "real-device"
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
processes, files = [], []
|
||||
|
||||
def start(name, command):
|
||||
handle = open(folder / f"{name}.log", "wb")
|
||||
files.append(handle)
|
||||
process = subprocess.Popen([str(value) for value in command], env=env,
|
||||
stdout=handle, stderr=subprocess.STDOUT,
|
||||
creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0)
|
||||
processes.append(process)
|
||||
|
||||
def adb(*command):
|
||||
return subprocess.run([args.adb, *command], env=env, capture_output=True,
|
||||
text=True, timeout=10)
|
||||
|
||||
def leased():
|
||||
request = urllib.request.Request(f"http://127.0.0.1:{relay_port}/v1/devices",
|
||||
headers={"Authorization": "Bearer " + env["ADS_CLIENT_TOKEN"]})
|
||||
with urllib.request.urlopen(request, timeout=2) as response:
|
||||
return b'"busy":true' in response.read()
|
||||
|
||||
try:
|
||||
start("server", [(args.bin_dir / f"ads-server{suffix}").resolve(), "--listen", f"127.0.0.1:{relay_port}"])
|
||||
start("agent", [(rust_dir / f"ads-agent{suffix}").resolve(), "--server", server_url,
|
||||
"--allow-insecure", "--device", "smoke-phone", "--adb-server", args.adb_server,
|
||||
"--adb-serial", args.serial])
|
||||
start("client", [(rust_dir / f"ads-client{suffix}").resolve(), "--server", server_url,
|
||||
"--allow-insecure", "--device", "smoke-phone", "--listen", endpoint,
|
||||
"--no-auto-connect"])
|
||||
wait_until(leased, "remote phone lease", seconds=20)
|
||||
print(adb("connect", endpoint).stdout.strip(), flush=True)
|
||||
print("If requested, approve this computer's debugging key on the phone.", flush=True)
|
||||
deadline = time.monotonic() + 20
|
||||
while True:
|
||||
state = adb("-s", endpoint, "get-state")
|
||||
if state.returncode == 0 and state.stdout.strip() == "device":
|
||||
break
|
||||
if time.monotonic() >= deadline:
|
||||
raise RuntimeError("Phone did not authorize local ADB: " + state.stderr.strip())
|
||||
time.sleep(1)
|
||||
listing = adb("devices", "-l")
|
||||
assert listing.returncode == 0 and endpoint in listing.stdout
|
||||
print(listing.stdout.strip())
|
||||
model = adb("-s", endpoint, "shell", "getprop", "ro.product.model")
|
||||
assert model.returncode == 0, model.stderr
|
||||
marker = adb("-s", endpoint, "shell", "echo", "ads-read-only-smoke-ok")
|
||||
assert marker.returncode == 0 and marker.stdout.strip() == "ads-read-only-smoke-ok", marker.stderr
|
||||
print("PASS: ordinary local adb devices and remote shell via Go server + both Rust daemons")
|
||||
print("Remote model:", model.stdout.strip())
|
||||
finally:
|
||||
try:
|
||||
adb("disconnect", endpoint)
|
||||
finally:
|
||||
for process in reversed(processes):
|
||||
if process.poll() is None:
|
||||
process.terminate()
|
||||
try:
|
||||
process.wait(timeout=5)
|
||||
except subprocess.TimeoutExpired:
|
||||
process.kill()
|
||||
process.wait(timeout=5)
|
||||
for handle in files:
|
||||
handle.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user