Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 75 additions & 57 deletions src/agent_env/env/envs/computer/utils/site_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import urllib.request
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional
from typing import Awaitable, Optional
from urllib.parse import urlsplit, urlunsplit

HOSTS_BEGIN = "# agentenv sites"
Expand All @@ -64,8 +64,8 @@
# log the failure shows.
LISTEN_PROBE_S, LISTEN_POLL_S, LOG_TAIL_CHARS = 1.0, 0.2, 2000
CHECK_TIMEOUT_S = 15.0
#: The checks after each site's: Python through ``SSL_CERT_FILE``, Node and curl.
RUNTIME_CHECKS = 3
#: The checks after each site's: Python through ``SSL_CERT_FILE``, Node and curl, and the forward port's.
RUNTIME_CHECKS = 4
#: What a JDK's store is opened with unless it says otherwise, how deep a JDK's store is looked for under its folders,
#: the folders not looked in, and how long a ``keytool`` call may take.
JAVA_STORE_PASSWORD = "changeit"
Expand Down Expand Up @@ -371,61 +371,62 @@ def __init__(self, config: Config, tls: Optional[ssl.SSLContext] = None):
self.upstream_tls = ssl.create_default_context()

async def handle(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, scheme: str) -> None:
await self._serving(reader, writer, scheme, self._requests)
await self._serving(writer, scheme, self._requests(reader, writer, scheme, _peer(writer), forward=False))

async def handle_forward(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
"""The forward-proxy port's connection: a ``CONNECT`` to a site's name tunnels to the site as the listeners
serve it, over TLS with its certificate for a name served over HTTPS; a request naming a site's URL is served
as the HTTP listener serves it; any other host, loopback among them, is refused at once and logged."""
await self._serving(reader, writer, "proxy", self._proxied)

async def _requests(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, scheme: str,
peer: str) -> None:
while await self.exchange(reader, writer, scheme, peer):
serve it, over TLS with its certificate for a name served over HTTPS, and any other request is served as the
HTTP listener serves it; ``exchange`` refuses one naming any other host, loopback among them, at once and logs
it."""
await self._serving(writer, "proxy", self._proxied(reader, writer, _peer(writer)))

async def _requests(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, scheme: str, peer: str, *,
forward: bool) -> None:
while await self.exchange(reader, writer, scheme, peer, forward=forward):
pass

async def _proxied(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, _: str, peer: str) -> None:
async def _proxied(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, peer: str) -> None:
while (head := await read_head(reader)) is not None:
start, headers = parse_head(head)
method, _, target = start.partition(" ")
target = target.rpartition(" ")[0]
method, _, rest = parse_head(head)[0].partition(" ")
if method == "CONNECT":
host = split_host(target)
if (site := self.config.site_for(host)) is None:
self._refuse(writer, method, target)
return
writer.write(b"HTTP/1.1 200 Connection established\r\n\r\n")
await writer.drain()
tls = site.tls(host.lower())
if tls:
if self.tls is None:
raise BadRequest(f"{host} is served over HTTPS, and this proxy has no certificate for it")
await writer.start_tls(self.tls)
await self._requests(reader, writer, "https" if tls else "http", peer)
await self._tunnel(reader, writer, peer, rest.rpartition(" ")[0])
return
named = urlsplit(target).netloc if "://" in target else (header(headers, "host") or "")
if self.config.site_for(split_host(named)) is None:
self._refuse(writer, method, named or target)
return
if not await self.exchange(reader, writer, "http", peer, head=head):
if not await self.exchange(reader, writer, "http", peer, head=head, forward=True):
return

async def _tunnel(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, peer: str,
authority: str) -> None:
"""A ``CONNECT`` to ``authority``: the site it names, whose certificate the tunnel's TLS needs before any
request in it, then each request in it, as the port serves them."""
host = split_host(authority)
if (site := self.config.site_for(host)) is None:
self._refuse(writer, "CONNECT", authority)
return
writer.write(b"HTTP/1.1 200 Connection established\r\n\r\n")
await writer.drain()
tls = site.tls(host.lower())
if tls:
if self.tls is None:
raise BadRequest(f"{host} is served over HTTPS, and this proxy has no certificate for it")
await writer.start_tls(self.tls)
await self._requests(reader, writer, "https" if tls else "http", peer, forward=True)

@staticmethod
def _refuse(writer: asyncio.StreamWriter, method: str, host: str) -> None:
print(f"site proxy: refused {method} {host}: no site of this computer", file=sys.stderr, flush=True)
writer.write(simple_response("403 Forbidden", f"{host} is no site of this computer, and the browser reaches "
"its sites alone\n", FAILED))

async def _serving(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, scheme: str, run) -> None:
peer = (writer.get_extra_info("peername") or (LOOPBACK,))[0]
@staticmethod
async def _serving(writer: asyncio.StreamWriter, label: str, serving: Awaitable[None]) -> None:
try:
await run(reader, writer, scheme, peer)
await serving
except BadRequest as e:
writer.write(simple_response("400 Bad Request", f"{e}\n"))
except (ConnectionError, asyncio.IncompleteReadError, ssl.SSLError, OSError):
pass
except Exception as e:
print(f"site proxy: a {scheme} connection failed: {type(e).__name__}: {e}", file=sys.stderr, flush=True)
print(f"site proxy: a {label} connection failed: {type(e).__name__}: {e}", file=sys.stderr, flush=True)
finally:
try:
await writer.drain()
Expand All @@ -435,8 +436,9 @@ async def _serving(self, reader: asyncio.StreamReader, writer: asyncio.StreamWri
pass

async def exchange(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, scheme: str,
peer: str, head: Optional[bytes] = None) -> bool:
"""One request, read unless its ``head`` is given, and its answer; whether the connection takes another."""
peer: str, head: Optional[bytes] = None, *, forward: bool = False) -> bool:
"""One request, read unless its ``head`` is given, and its answer; whether the connection takes another. One
naming no site gets 421, or on the forward port (``forward``) the port's refusal, logged."""
head = head or await read_head(reader)
if head is None:
return False
Expand All @@ -448,8 +450,11 @@ async def exchange(self, reader: asyncio.StreamReader, writer: asyncio.StreamWri
host = (urlsplit(target).netloc if "://" in target else "") or header(headers, "host") or ""
site = self.config.site_for(split_host(host)) if host else None
if site is None:
writer.write(simple_response("421 Misdirected Request", f"{host or 'no host'} is no site of this "
"computer\n", FAILED))
if forward:
self._refuse(writer, method, host or target)
else:
writer.write(simple_response("421 Misdirected Request", f"{host or 'no host'} is no site of this "
"computer\n", FAILED))
return False
if scheme == "http" and site.tls(split_host(host).lower()):
parts = urlsplit(target)
Expand Down Expand Up @@ -629,6 +634,10 @@ def failed_answer(query: bytes, question: tuple[str, int, int]) -> bytes:
return struct.pack("!HHHHHH", ident, 0x8082 | (flags & 0x0100), 1, 0, 0, 0) + query[12:question[2]]


def _peer(writer: asyncio.StreamWriter) -> str:
return (writer.get_extra_info("peername") or (LOOPBACK,))[0]


def tls_context(config: Config) -> Optional[ssl.SSLContext]:
"""The HTTPS side's context: each site's certificate, picked by the name the browser asks for."""
contexts = {}
Expand Down Expand Up @@ -998,7 +1007,8 @@ def install(config_path: str, paths: Paths = Paths()) -> dict:
serve_path.write_text(json.dumps(serving))
start(str(serve_path), given.get("log"))
checks = checks_as(user, str(serve_path))
return {"trust": trust, "names": names, "checks": checks["sites"], "runtimes": checks["runtimes"]}
return {"trust": trust, "names": names, "checks": checks["sites"], "runtimes": checks["runtimes"],
**({"forward_port": checks["forward_port"]} if "forward_port" in checks else {})}


def start(serve_path: str, log: Optional[str]) -> None:
Expand Down Expand Up @@ -1051,28 +1061,36 @@ def checks_as(user: Optional[str], serve_path: str) -> dict:

def check(config: Config) -> dict:
"""``sites``: each site's first name loaded, over HTTPS but for a ``.localhost`` name, through the system's
resolver and trust store: its status, or why it didn't load, which an answer the proxy made itself (it marks them
``FAILED``), such as its 502 for a gateway it couldn't reach, is too. ``runtimes``: the first of them over HTTPS
loaded through each other runtime's own trust (``check_runtimes``)."""
resolver and trust store (``_loaded``). ``runtimes``: the first of them over HTTPS loaded through each other
runtime's own trust (``check_runtimes``). ``forward_port``, with one: the first of them over HTTPS, else the first,
loaded through it as a browser alone loads it."""
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
results = {}
for site in config.sites:
name = next((n for n in site.domains if not n.startswith("*.")), None) \
or "agentenv-check." + site.domains[0].removeprefix("*.")
url = f"{'https' if site.tls(name) else 'http'}://{name}/"
try:
with opener.open(url, timeout=CHECK_TIMEOUT_S) as response:
results[name] = {"url": url, "status": response.status}
except urllib.error.HTTPError as e:
if e.headers is not None and e.headers.get(FAILED[0][0]) == FAILED[0][1]:
body = e.read().decode(errors="replace").strip()
results[name] = {"url": url, "error": f"{e.code} {e.reason}: {body}"}
else:
results[name] = {"url": url, "status": e.code}
except (urllib.error.URLError, OSError, ssl.SSLError) as e:
results[name] = {"url": url, "error": str(getattr(e, "reason", e))}
results[name] = _loaded(opener, f"{'https' if site.tls(name) else 'http'}://{name}/")
secure = next((result["url"] for result in results.values() if result["url"].startswith("https:")), None)
return {"sites": results, "runtimes": check_runtimes(secure) if secure else {}}
report = {"sites": results, "runtimes": check_runtimes(secure) if secure else {}}
if config.proxy_port and results:
proxy = f"http://{config.listen}:{config.proxy_port}"
forward = urllib.request.build_opener(urllib.request.ProxyHandler({"http": proxy, "https": proxy}))
report["forward_port"] = {"proxy": proxy, **_loaded(forward, secure or next(iter(results.values()))["url"])}
return report


def _loaded(opener, url: str) -> dict:
"""``url`` loaded by ``opener``: its status, or why it didn't load, which an answer the proxy made itself (it marks
them ``FAILED``), such as its 502 for a gateway it couldn't reach, is too."""
try:
with opener.open(url, timeout=CHECK_TIMEOUT_S) as response:
return {"url": url, "status": response.status}
except urllib.error.HTTPError as e:
if e.headers is not None and e.headers.get(FAILED[0][0]) == FAILED[0][1]:
return {"url": url, "error": f"{e.code} {e.reason}: {e.read().decode(errors='replace').strip()}"}
return {"url": url, "status": e.code}
except (urllib.error.URLError, OSError, ssl.SSLError) as e:
return {"url": url, "error": str(getattr(e, "reason", e))}


def check_runtimes(url: str) -> dict:
Expand Down
12 changes: 8 additions & 4 deletions src/agent_env/env/envs/computer/utils/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,18 +447,22 @@ async def route(sandbox: Sandbox, sites: list[Site], *, user: Optional[str], net
if failed := routing_problems(report):
raise RuntimeError("site routing: " + "; ".join(failed))
return {"names": table.to_dict(), "ca": issued.ca_pem, "ca_path": trust_files(os_type)["ca"],
"trust": report["trust"], "checks": report["checks"], "runtimes": report.get("runtimes", {})}
"trust": report["trust"], "checks": report["checks"], "runtimes": report.get("runtimes", {}),
**({"forward_port": report["forward_port"]} if "forward_port" in report else {})}


def routing_problems(report: dict) -> list[str]:
"""What the install's ``report`` says failed: a site that didn't load, a runtime that didn't trust the CA, and a
JDK's store that didn't take it."""
"""What the install's ``report`` says failed: a site that didn't load, a runtime that didn't trust the CA, a JDK's
store that didn't take it, and the forward port of a browser alone that didn't load a site."""
forward = report.get("forward_port") or {}
return ([f"{result.get('url', name)} didn't load in the computer: {result['error']}"
for name, result in report["checks"].items() if "error" in result]
+ [f"{runtime} in the computer didn't trust the deploy's CA loading {result.get('url')}: {result['error']}"
for runtime, result in report.get("runtimes", {}).items() if "error" in result]
+ [f"the JDK store {store} didn't take the deploy's CA: {result['error']}"
for store, result in report["trust"].get("java", {}).items() if "error" in result])
for store, result in report["trust"].get("java", {}).items() if "error" in result]
+ ([f"the browser's forward port {forward.get('proxy')} didn't load {forward.get('url')}: "
f"{forward['error']}"] if "error" in forward else []))


async def deployed_sites(deployed_envs: list[DeployedEnv], only: Optional[list[str]] = None) -> list[dict]:
Expand Down
12 changes: 8 additions & 4 deletions src/agent_env/env/envs/playwright_mcp/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ set -euo pipefail
# MCP_PORT and MCP_HOST are injected by docker-compose (gateway convention).
# PLAYWRIGHT_MCP_VERSION is baked into the image at build time.

# The recorder serves the browser's card on RECORDER_PORT, restarted whenever it exits; the MCP server starts once
# it listens, so the card is there by the time the server is healthy, or after RECORDER_WAIT_S without it.
# The recorder fronts the MCP port: it serves the browser's card and recording there and passes every other request to
# the MCP server, which listens on loopback PLAYWRIGHT_MCP_PORT behind it. It serves the card on RECORDER_PORT at once,
# opens the MCP port only once the MCP server listens, so the port answering means the server does, and is restarted
# whenever it exits; the MCP server starts once the card is served, or after RECORDER_WAIT_S without it.
export MCP_PORT="${MCP_PORT:-18765}"
export RECORDER_PORT="${RECORDER_PORT:-18766}"
export PLAYWRIGHT_MCP_PORT="${PLAYWRIGHT_MCP_PORT:-18767}"
(while true; do /opt/recorder/bin/python /app/recorder.py || true; sleep 1; done) &
for _ in $(seq 1 "$(( ${RECORDER_WAIT_S:-20} * 10 ))"); do
(exec 3<>"/dev/tcp/127.0.0.1/${RECORDER_PORT}") 2>/dev/null && break
Expand All @@ -18,7 +22,7 @@ exec npx @playwright/mcp@${PLAYWRIGHT_MCP_VERSION} \
--headless \
--isolated \
--no-sandbox \
--host "${MCP_HOST:-0.0.0.0}" \
--port "${MCP_PORT:-18765}" \
--host 127.0.0.1 \
--port "${PLAYWRIGHT_MCP_PORT}" \
--allowed-hosts '*' \
--caps vision
Loading
Loading