mirror of
https://github.com/hasso5703/alzeo.git
synced 2026-12-06 21:04:21 +01:00
opencode gives its terminal only to a page of its own host or of loopback: the connect ticket in every pinned version, the WebSocket too from 1.19.0. The relay forwarded the browser's tailnet Origin with Host set to loopback, so the terminal panel opened with no shell (ticket 403 twice, no WebSocket; measured in Chrome on 1.18.35 and 1.19.0). The relay, which already holds the Origin to its own host, now presents opencode's own; a request with none still carries none. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
637 lines
29 KiB
Python
637 lines
29 KiB
Python
#!/usr/bin/env python3
|
|
"""Agent relay: opencode's web interface behind the cockpit's own login.
|
|
|
|
`opencode serve` listens on loopback with a Basic password that never leaves the
|
|
box. This relay is the only thing that reaches it. It binds one address (the
|
|
tailnet address by default), accepts a request only with a valid cockpit session
|
|
cookie, presents the Basic credentials upstream, and streams the answer back:
|
|
plain responses, server-sent events, and the WebSocket upgrade the terminal
|
|
panel uses. The browser sees one host for the cockpit and the relay (cookies
|
|
ignore ports), so the Agent tab frames the interface with no second login and
|
|
no Basic-auth prompt, which Chrome would block inside a cross-origin frame.
|
|
|
|
Stdlib only, like the cockpit. The pure helpers (header hygiene, origin check,
|
|
address resolution) are kept apart from the socket code so the tests can pin
|
|
them without a browser.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import http.client
|
|
import http.server
|
|
import json
|
|
import re
|
|
import select
|
|
import socket
|
|
import socketserver
|
|
import subprocess
|
|
import threading
|
|
import urllib.parse
|
|
from collections.abc import Callable
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
RELAY_VERSION = "1.1"
|
|
|
|
HOP_BY_HOP = {"connection", "keep-alive", "proxy-authenticate", "proxy-authorization",
|
|
"te", "trailer", "transfer-encoding", "upgrade"}
|
|
# Request headers the relay never forwards: the cockpit session must not reach
|
|
# opencode, the client's own Authorization is replaced by ours, Host and Origin name
|
|
# the upstream, lengths are re-set, and the referrer says nothing about the cockpit.
|
|
REQUEST_DROP = HOP_BY_HOP | {"host", "origin", "cookie", "authorization", "content-length", "referer"}
|
|
# Response headers the relay owns (Date and Server come from this server).
|
|
RESPONSE_DROP = HOP_BY_HOP | {"content-length", "date", "server"}
|
|
SESSION_COOKIE = "cockpit"
|
|
# Fetched by browsers with no cookie (a manifest link omits credentials): the app's
|
|
# name and icons, nothing else, so it may pass without a session (GET only).
|
|
PUBLIC_PATHS = {"/site.webmanifest"}
|
|
MAX_BODY = 64 * 1024 * 1024 # request body ceiling (attachments included)
|
|
CHUNK = 64 * 1024
|
|
CONNECT_TIMEOUT = 5.0
|
|
HANDSHAKE_LIMIT = 64 * 1024
|
|
INJECT_LIMIT = 256 * 1024 # an HTML document we rewrite must stay this small (the SPA index is ~3 KB)
|
|
# A client that sends nothing for this long, mid-request or between two, is let go. With
|
|
# no limit a request sent in part held its thread, and a descriptor, for good: 100 of
|
|
# them held 100 threads, in a process whose 1,024 descriptors its collectors and memory
|
|
# floor need too (found in review, 2026-09-24). Only waits on the client count: a stream
|
|
# this side is writing, or a tunnel both ends are quiet on, never times out here.
|
|
CLIENT_TIMEOUT = 30
|
|
|
|
# The cockpit's mobile comfort layer for opencode's interface (phone typography,
|
|
# touch targets, safe areas, theme default). opencode's own files are never
|
|
# touched: these two are the relay's, served same-origin (so its CSP 'self'
|
|
# accepts them) and linked into the served index by inject_mobile below.
|
|
INJECT_DIR = Path(__file__).resolve().parent / "static"
|
|
INJECTED_FILES = {
|
|
"/__cockpit/agent-mobile.css": ("agent-mobile.css", "text/css; charset=utf-8"),
|
|
"/__cockpit/agent-mobile.js": ("agent-mobile.js", "application/javascript; charset=utf-8"),
|
|
}
|
|
MOBILE_TAGS = (b'<link rel="stylesheet" href="/__cockpit/agent-mobile.css">'
|
|
b'<script src="/__cockpit/agent-mobile.js"></script>')
|
|
|
|
|
|
# ── pure helpers ─────────────────────────────────────────────────────────────
|
|
def host_name(host_header: str | None) -> str:
|
|
"""The host part of a Host header: `a.b:30090` -> `a.b`, `[::1]:1` -> `[::1]`."""
|
|
h = (host_header or "").strip()
|
|
if not h:
|
|
return ""
|
|
if h.startswith("["):
|
|
return h.split("]", 1)[0] + "]"
|
|
if h.count(":") == 1:
|
|
return h.rsplit(":", 1)[0]
|
|
if ":" in h:
|
|
# A bare IPv6 literal: no port can follow without brackets, and the
|
|
# callers build http://{name}:{port} URLs, so return the bracketed
|
|
# canonical form instead of a string that reads as host:port:port.
|
|
return f"[{h}]"
|
|
return h
|
|
|
|
|
|
def origin_allowed(origin: str | None, host_header: str | None) -> bool:
|
|
"""A browser names its origin on every POST and every WebSocket handshake. The
|
|
interface is served by this relay, so the only legitimate origin is the relay's
|
|
own host as the browser typed it. No header (curl, the cockpit's own probes) is
|
|
fine: those requests still need the session cookie. `null` is never fine."""
|
|
if origin is None:
|
|
return True
|
|
o = origin.strip().lower()
|
|
host = (host_header or "").strip().lower()
|
|
return bool(host) and o in (f"http://{host}", f"https://{host}")
|
|
|
|
|
|
def cookie_values(header: str | None, name: str) -> list[str]:
|
|
"""Every value the Cookie header gives `name`, read the way browsers send it (`a=b;
|
|
c=d`) and nothing more. http.cookies.SimpleCookie gives up at the first pair it has no
|
|
grammar for, and raises on a name such as `a/b`: another app's cookie on this host (a
|
|
JSON value, a space in one) hid the session, or ended the connection with no answer
|
|
(found in review, 2026-09-24)."""
|
|
out = []
|
|
for part in (header or "").split(";"):
|
|
key, sep, value = part.partition("=")
|
|
if sep and key.strip() == name:
|
|
value = value.strip()
|
|
if len(value) >= 2 and value[0] == value[-1] == '"':
|
|
value = value[1:-1]
|
|
out.append(value)
|
|
return out
|
|
|
|
|
|
def basic_auth(user: str, password: str) -> str:
|
|
return "Basic " + base64.b64encode(f"{user}:{password}".encode()).decode("ascii")
|
|
|
|
|
|
def read_credentials(path: Path) -> tuple[str, str] | None:
|
|
"""The env file the opencode unit reads (KEY=VALUE lines, optional quotes)."""
|
|
try:
|
|
text = Path(path).read_text()
|
|
except OSError:
|
|
return None
|
|
kv = {}
|
|
for line in text.splitlines():
|
|
line = line.strip()
|
|
if not line or line.startswith("#") or "=" not in line:
|
|
continue
|
|
k, _, v = line.partition("=")
|
|
kv[k.strip()] = v.strip().strip('"').strip("'")
|
|
pw = kv.get("OPENCODE_SERVER_PASSWORD", "")
|
|
if not pw:
|
|
return None
|
|
return kv.get("OPENCODE_SERVER_USERNAME") or "opencode", pw
|
|
|
|
|
|
def forward_request_headers(items, upstream_host: str, auth: str,
|
|
websocket: bool = False) -> list[tuple[str, str]]:
|
|
items = list(items)
|
|
out = [(k, v) for k, v in items if k.lower() not in REQUEST_DROP]
|
|
out.append(("Host", upstream_host))
|
|
if any(k.lower() == "origin" for k, _ in items):
|
|
# The browser's origin was held to this relay's own host before anything was
|
|
# forwarded (origin_allowed), so opencode is shown the origin of the page it
|
|
# serves itself. It refuses its terminal to any origin but its own host and
|
|
# loopback: the connect ticket in every version this repo has pinned, and the
|
|
# WebSocket itself from 1.19.0 on, so the Agent tab opened by the tailnet
|
|
# address had a terminal with no shell (in Chrome, 2026-10-11: the ticket
|
|
# refused with 403 on 1.18.35 and 1.19.0, and no WebSocket opened).
|
|
out.append(("Origin", f"http://{upstream_host}"))
|
|
out.append(("Authorization", auth))
|
|
if websocket:
|
|
out.append(("Connection", "Upgrade"))
|
|
out.append(("Upgrade", "websocket"))
|
|
else:
|
|
out.append(("Connection", "close"))
|
|
return out
|
|
|
|
|
|
def forward_response_headers(items, drop=()) -> list[tuple[str, str]]:
|
|
extra = {d.lower() for d in drop}
|
|
out = []
|
|
for k, v in items:
|
|
kl = k.lower()
|
|
if kl in RESPONSE_DROP or kl in extra:
|
|
continue
|
|
# a cookie named like the cockpit session would shadow the real one on this host
|
|
if kl == "set-cookie" and v.strip().lower().startswith(SESSION_COOKIE + "="):
|
|
continue
|
|
out.append((k, v))
|
|
return out
|
|
|
|
|
|
def frame_policy(host_header: str | None, cockpit_port: int) -> str:
|
|
"""Only the cockpit on the same host may frame the interface (clickjacking
|
|
guard). The upstream policy has no frame-ancestors, so this one governs."""
|
|
name = host_name(host_header) or "127.0.0.1"
|
|
return f"frame-ancestors 'self' http://{name}:{cockpit_port}"
|
|
|
|
|
|
def split_upstream(url: str) -> tuple[str, int]:
|
|
u = urllib.parse.urlsplit(url if "://" in url else "http://" + url)
|
|
return u.hostname or "127.0.0.1", u.port or 80
|
|
|
|
|
|
def injected_asset(path: str) -> tuple[str, str] | None:
|
|
"""(filename, content-type) when the request asks for one of the cockpit's
|
|
own injected assets, else None."""
|
|
return INJECTED_FILES.get(path.split("?", 1)[0])
|
|
|
|
|
|
def inject_mobile(html: bytes) -> bytes:
|
|
"""Link the mobile comfort layer into a served document, just before
|
|
</head> (the script stays before opencode's module, so its theme default
|
|
lands before the app paints). Idempotent; appends when no </head> exists."""
|
|
if b"/__cockpit/agent-mobile." in html:
|
|
return html
|
|
out, n = re.subn(rb"(?i)</head\s*>", lambda m: MOBILE_TAGS + m.group(0), html, count=1)
|
|
return out if n else html + MOBILE_TAGS
|
|
|
|
|
|
def wants_html_rewrite(command: str, status: int, content_type: str | None,
|
|
content_encoding: str | None, chunked: bool,
|
|
length: int | None) -> bool:
|
|
"""Rewrite only a small, known-length, uncompressed 200 HTML document we
|
|
were asked to GET; everything else (SSE, images, gzip, redirects) streams
|
|
byte-exact as before."""
|
|
return (command == "GET" and status == 200
|
|
and "text/html" in (content_type or "").lower()
|
|
and (content_encoding or "").strip().lower() in ("", "identity")
|
|
and not chunked and length is not None and 0 < length <= INJECT_LIMIT)
|
|
|
|
|
|
def tailscale_ipv4(run=subprocess.run) -> str | None:
|
|
"""The tailnet IPv4 of this box: the interface first (kernel state, no daemon
|
|
socket needed), the CLI as a fallback."""
|
|
for argv in (["ip", "-4", "-o", "addr", "show", "dev", "tailscale0"], ["tailscale", "ip", "-4"]):
|
|
try:
|
|
out = run(argv, capture_output=True, text=True, timeout=5).stdout
|
|
except (OSError, subprocess.SubprocessError):
|
|
continue
|
|
m = re.search(r"\b(100\.(?:6[4-9]|[7-9]\d|1[01]\d|12[0-7])\.\d{1,3}\.\d{1,3})\b", out or "")
|
|
if m:
|
|
return m.group(1)
|
|
return None
|
|
|
|
|
|
def resolve_bind(spec: str, run=subprocess.run) -> str | None:
|
|
"""`tailscale` resolves to the tailnet address (None while it is not up);
|
|
anything else is taken as an address literal."""
|
|
spec = (spec or "").strip()
|
|
if spec.lower() == "tailscale":
|
|
return tailscale_ipv4(run)
|
|
return spec or None
|
|
|
|
|
|
# ── configuration ────────────────────────────────────────────────────────────
|
|
@dataclass
|
|
class RelayConfig:
|
|
upstream: tuple[str, int] # opencode serve, loopback
|
|
credentials: Callable[[], tuple[str, str] | None] # (user, password) or None
|
|
is_authed: Callable[[str | None], bool] # Cookie header -> valid session?
|
|
cockpit_port: int # for frame-ancestors and the sign-in hint
|
|
|
|
@property
|
|
def upstream_host(self) -> str:
|
|
return f"{self.upstream[0]}:{self.upstream[1]}"
|
|
|
|
|
|
def health(cfg: RelayConfig, timeout: float = 3.0) -> dict:
|
|
"""GET /global/health on the upstream with the credentials: what the Agent
|
|
panel shows as the server state."""
|
|
creds = cfg.credentials()
|
|
if not creds:
|
|
return {"healthy": False, "error": "no credentials file"}
|
|
conn = http.client.HTTPConnection(*cfg.upstream, timeout=timeout)
|
|
try:
|
|
conn.request("GET", "/global/health", headers={"Authorization": basic_auth(*creds)})
|
|
resp = conn.getresponse()
|
|
raw = resp.read(4096)
|
|
if resp.status == 401:
|
|
return {"healthy": False, "error": "credentials refused (restart opencode-web.service after editing the env file)"}
|
|
if resp.status != 200:
|
|
return {"healthy": False, "error": f"HTTP {resp.status}"}
|
|
data = json.loads(raw or b"{}")
|
|
return {"healthy": bool(data.get("healthy")), "version": data.get("version")}
|
|
except (OSError, http.client.HTTPException, ValueError) as e:
|
|
return {"healthy": False, "error": f"{type(e).__name__}: {str(e)[:80]}"}
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def upstream_json(cfg: RelayConfig, path: str, timeout: float = 3.0):
|
|
"""One authenticated GET on the upstream, parsed; None when it cannot be had."""
|
|
creds = cfg.credentials()
|
|
if not creds:
|
|
return None
|
|
conn = http.client.HTTPConnection(*cfg.upstream, timeout=timeout)
|
|
try:
|
|
conn.request("GET", path, headers={"Authorization": basic_auth(*creds)})
|
|
resp = conn.getresponse()
|
|
raw = resp.read(1024 * 1024)
|
|
if resp.status != 200:
|
|
return None
|
|
return json.loads(raw or b"null")
|
|
except (OSError, http.client.HTTPException, ValueError):
|
|
return None
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def permission_is_auto(permission) -> bool:
|
|
"""opencode's permission config, as served: "allow" or {"*": "allow"} means every
|
|
tool call runs without a prompt (deny rules aside)."""
|
|
if permission == "allow":
|
|
return True
|
|
return isinstance(permission, dict) and permission.get("*") == "allow"
|
|
|
|
|
|
# ── the relay ────────────────────────────────────────────────────────────────
|
|
SIGN_IN_PAGE = """<!doctype html><meta charset="utf-8"><title>Spark Cockpit</title>
|
|
<style>body{{font:15px/1.5 system-ui,sans-serif;background:#0e1014;color:#ece9e1;display:grid;place-items:center;min-height:100vh;margin:0}}
|
|
main{{max-width:44ch;padding:32px;text-align:center}}a{{color:#d9b45e}}</style>
|
|
<main><h1 style="font-size:18px">Cockpit session required</h1>
|
|
<p>This is the agent relay of the Spark Cockpit. Sign in to the cockpit at
|
|
<a href="{cockpit}">{cockpit}</a>, then come back to the Agent view.</p></main>"""
|
|
|
|
|
|
def make_handler(cfg: RelayConfig) -> type[http.server.BaseHTTPRequestHandler]:
|
|
class Relay(http.server.BaseHTTPRequestHandler):
|
|
protocol_version = "HTTP/1.1"
|
|
server_version = "SparkCockpitRelay/" + RELAY_VERSION
|
|
sys_version = ""
|
|
timeout = CLIENT_TIMEOUT
|
|
|
|
def log_message(self, fmt, *args): # quiet: a chatty relay hides the real journal lines
|
|
pass
|
|
|
|
# every method goes through the same gate and the same forwarder
|
|
def do_GET(self):
|
|
self.relay()
|
|
|
|
do_POST = do_PUT = do_PATCH = do_DELETE = do_OPTIONS = do_HEAD = do_GET
|
|
|
|
# ---- gate ----
|
|
def relay(self):
|
|
host = self.headers.get("Host")
|
|
if not origin_allowed(self.headers.get("Origin"), host):
|
|
return self.refuse(403, "origin not allowed")
|
|
# a manifest fetch carries no body: one that does is not that fetch, and went to
|
|
# opencode with the Basic credentials and no session, up to MAX_BODY of it
|
|
public = (self.command == "GET" and self.path.split("?", 1)[0] in PUBLIC_PATHS
|
|
and not self.headers.get("Transfer-Encoding")
|
|
and (self.headers.get("Content-Length") or "0").strip() == "0")
|
|
if not public and not cfg.is_authed(self.headers.get("Cookie")):
|
|
return self.refuse(401, "cockpit session required")
|
|
asset = injected_asset(self.path) if self.command in ("GET", "HEAD") else None
|
|
if asset:
|
|
return self.serve_asset(asset)
|
|
creds = cfg.credentials()
|
|
if not creds:
|
|
return self.refuse(503, "agent credentials missing on the box (run dashboard/install-agent.sh)")
|
|
auth = basic_auth(*creds)
|
|
if self.headers.get("Upgrade", "").lower() == "websocket":
|
|
return self.tunnel(auth)
|
|
if "chunked" in self.headers.get("Transfer-Encoding", "").lower():
|
|
return self.refuse(411, "length required")
|
|
try:
|
|
length = int(self.headers.get("Content-Length") or 0)
|
|
except ValueError:
|
|
return self.refuse(400, "bad content length")
|
|
if length < 0 or length > MAX_BODY:
|
|
return self.refuse(413, "body too large")
|
|
body = self.rfile.read(length) if length else b""
|
|
self.forward(auth, body)
|
|
|
|
def refuse(self, code: int, why: str):
|
|
# never try to keep a connection whose body was not consumed
|
|
self.close_connection = True
|
|
wants_page = self.command == "GET" and "text/html" in (self.headers.get("Accept") or "")
|
|
if wants_page and code == 401:
|
|
cockpit = f"http://{host_name(self.headers.get('Host')) or '127.0.0.1'}:{cfg.cockpit_port}/"
|
|
payload = SIGN_IN_PAGE.format(cockpit=cockpit).encode()
|
|
ctype = "text/html; charset=utf-8"
|
|
else:
|
|
payload = json.dumps({"error": why}).encode()
|
|
ctype = "application/json"
|
|
self.send_response(code)
|
|
self.send_header("Content-Type", ctype)
|
|
self.send_header("Content-Length", str(len(payload)))
|
|
self.send_header("Cache-Control", "no-store")
|
|
self.send_header("Connection", "close")
|
|
self.own_headers()
|
|
self.end_headers()
|
|
if self.command != "HEAD":
|
|
self.wfile.write(payload)
|
|
|
|
def serve_asset(self, asset: tuple[str, str]):
|
|
"""The cockpit's own injected asset, read fresh from disk each time
|
|
(iterating on it should never need a relay restart)."""
|
|
try:
|
|
payload = (INJECT_DIR / asset[0]).read_bytes()
|
|
except OSError:
|
|
return self.refuse(404, "injected asset missing on the box: " + asset[0])
|
|
self.send_response(200)
|
|
self.send_header("Content-Type", asset[1])
|
|
if self.command != "HEAD":
|
|
self.send_header("Content-Length", str(len(payload)))
|
|
self.send_header("Cache-Control", "no-cache")
|
|
self.own_headers()
|
|
self.end_headers()
|
|
if self.command != "HEAD":
|
|
self.wfile.write(payload)
|
|
|
|
def own_headers(self):
|
|
self.send_header("Content-Security-Policy", frame_policy(self.headers.get("Host"), cfg.cockpit_port))
|
|
self.send_header("X-Content-Type-Options", "nosniff")
|
|
self.send_header("Referrer-Policy", "no-referrer")
|
|
|
|
# ---- plain HTTP, streamed ----
|
|
def forward(self, auth: str, body: bytes):
|
|
conn = http.client.HTTPConnection(*cfg.upstream, timeout=CONNECT_TIMEOUT)
|
|
try:
|
|
conn.connect()
|
|
# a long generation or an idle event stream must not time out here;
|
|
# a vanished client shows up as a write error on our side instead
|
|
conn.sock.settimeout(None)
|
|
conn.putrequest(self.command, self.path, skip_host=True, skip_accept_encoding=True)
|
|
for k, v in forward_request_headers(self.headers.items(), cfg.upstream_host, auth):
|
|
conn.putheader(k, v)
|
|
if body or self.command in ("POST", "PUT", "PATCH"):
|
|
conn.putheader("Content-Length", str(len(body)))
|
|
conn.endheaders(body if body else None)
|
|
resp = conn.getresponse()
|
|
except (OSError, http.client.HTTPException) as e:
|
|
conn.close()
|
|
return self.refuse(502, f"agent server unreachable ({type(e).__name__}); is opencode-web.service running?")
|
|
rewrite = wants_html_rewrite(self.command, resp.status,
|
|
resp.getheader("Content-Type"),
|
|
resp.getheader("Content-Encoding"),
|
|
resp.chunked, resp.length)
|
|
try:
|
|
self.send_response(resp.status, resp.reason)
|
|
for k, v in forward_response_headers(resp.getheaders(),
|
|
drop=("cache-control",) if rewrite else ()):
|
|
self.send_header(k, v)
|
|
self.own_headers()
|
|
bodyless = self.command == "HEAD" or resp.status in (204, 304) or resp.status < 200
|
|
if bodyless:
|
|
if self.command == "HEAD" and resp.getheader("Content-Length") is not None:
|
|
self.send_header("Content-Length", resp.getheader("Content-Length"))
|
|
self.end_headers()
|
|
return
|
|
if rewrite:
|
|
want = resp.length
|
|
raw = resp.read(want)
|
|
if len(raw) < want:
|
|
# opencode ended the document early (a restart mid-answer), and
|
|
# once injected it would read as whole: nothing has been sent
|
|
# yet, so the browser gets a 502 it can reload from instead
|
|
self._headers_buffer = []
|
|
return self.refuse(502, "the agent server cut its answer short; reload the tab")
|
|
payload = inject_mobile(raw)
|
|
self.send_header("Content-Length", str(len(payload)))
|
|
# opencode sends no Cache-Control (Safari then serves the
|
|
# document from its heuristic cache and the injection - and
|
|
# with it the phone layer - never reaches the browser)
|
|
self.send_header("Cache-Control", "no-cache")
|
|
self.end_headers()
|
|
self.wfile.write(payload)
|
|
return
|
|
if resp.length is not None and not resp.chunked:
|
|
self.send_header("Content-Length", str(resp.length))
|
|
self.end_headers()
|
|
remaining = resp.length
|
|
while remaining > 0:
|
|
data = resp.read(min(CHUNK, remaining))
|
|
if not data:
|
|
break
|
|
self.wfile.write(data)
|
|
remaining -= len(data)
|
|
self.wfile.flush()
|
|
if remaining > 0:
|
|
# opencode ended early (a restart mid-answer). The browser was
|
|
# promised the whole length, and kept this keep-alive connection,
|
|
# one of its six to this origin, waiting for the rest: closing it
|
|
# is how it learns the answer was cut (found in review, 2026-09-24)
|
|
self.close_connection = True
|
|
try:
|
|
self.connection.shutdown(socket.SHUT_RDWR)
|
|
except OSError:
|
|
pass
|
|
return
|
|
# unknown length (server-sent events, chunked answers): re-chunk for a
|
|
# 1.1 client, close-delimit for a 1.0 one; flush every piece so an
|
|
# event reaches the browser the moment opencode emits it
|
|
chunked = self.request_version >= "HTTP/1.1"
|
|
if chunked:
|
|
self.send_header("Transfer-Encoding", "chunked")
|
|
else:
|
|
self.close_connection = True
|
|
self.end_headers()
|
|
while True:
|
|
data = resp.read1(CHUNK)
|
|
if not data:
|
|
break
|
|
if chunked:
|
|
self.wfile.write(b"%x\r\n" % len(data) + data + b"\r\n")
|
|
else:
|
|
self.wfile.write(data)
|
|
self.wfile.flush()
|
|
if chunked:
|
|
self.wfile.write(b"0\r\n\r\n")
|
|
self.wfile.flush()
|
|
except (OSError, http.client.HTTPException):
|
|
# the browser went away (tab closed, stream cancelled) or opencode
|
|
# dropped the connection: nothing to answer to anymore
|
|
self.close_connection = True
|
|
finally:
|
|
conn.close()
|
|
|
|
# ---- WebSocket (the terminal panel) ----
|
|
def tunnel(self, auth: str):
|
|
try:
|
|
up = socket.create_connection(cfg.upstream, timeout=CONNECT_TIMEOUT)
|
|
except OSError as e:
|
|
return self.refuse(502, f"agent server unreachable ({type(e).__name__})")
|
|
self.close_connection = True
|
|
try:
|
|
up.settimeout(CONNECT_TIMEOUT)
|
|
lines = [f"{self.command} {self.path} HTTP/1.1"]
|
|
lines += [f"{k}: {v}" for k, v in forward_request_headers(
|
|
self.headers.items(), cfg.upstream_host, auth, websocket=True)]
|
|
up.sendall(("\r\n".join(lines) + "\r\n\r\n").encode("latin-1"))
|
|
head = b""
|
|
while b"\r\n\r\n" not in head:
|
|
piece = up.recv(4096)
|
|
if not piece:
|
|
raise OSError("upstream closed during the handshake")
|
|
head += piece
|
|
if len(head) > HANDSHAKE_LIMIT:
|
|
raise OSError("handshake too large")
|
|
head, rest = head.split(b"\r\n\r\n", 1)
|
|
# the handshake answer goes back verbatim (101, or the refusal)
|
|
self.wfile.write(head + b"\r\n\r\n")
|
|
self.wfile.flush()
|
|
if not head.startswith(b"HTTP/1.1 101"):
|
|
if rest:
|
|
self.wfile.write(rest)
|
|
self.wfile.flush()
|
|
return
|
|
up.settimeout(None)
|
|
client = self.connection
|
|
if rest:
|
|
client.sendall(rest)
|
|
pump(client, up)
|
|
except OSError:
|
|
pass
|
|
finally:
|
|
up.close()
|
|
|
|
return Relay
|
|
|
|
|
|
def pump(a: socket.socket, b: socket.socket):
|
|
"""Bytes both ways until either side closes."""
|
|
peer = {a: b, b: a}
|
|
while True:
|
|
try:
|
|
ready, _, broken = select.select([a, b], [], [a, b])
|
|
except (OSError, ValueError):
|
|
return
|
|
if broken:
|
|
return
|
|
for s in ready:
|
|
try:
|
|
data = s.recv(CHUNK)
|
|
except OSError:
|
|
return
|
|
if not data:
|
|
return
|
|
try:
|
|
peer[s].sendall(data)
|
|
except OSError:
|
|
return
|
|
|
|
|
|
class BoundedThreadingMixIn(socketserver.ThreadingMixIn):
|
|
"""A thread per connection, as ThreadingMixIn, and a bounded number of them: past
|
|
`max_connections` in all, or `max_per_address` from one address, a new connection is
|
|
closed before any thread starts for it. A browser holds six per origin at most."""
|
|
daemon_threads = True
|
|
max_connections = 96
|
|
max_per_address = 24
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self._cap_lock = threading.Lock()
|
|
self._cap_open: dict[str, int] = {}
|
|
super().__init__(*args, **kwargs)
|
|
|
|
def _cap_release(self, addr: str) -> None:
|
|
with self._cap_lock:
|
|
n = self._cap_open.get(addr, 0) - 1
|
|
if n > 0:
|
|
self._cap_open[addr] = n
|
|
else:
|
|
self._cap_open.pop(addr, None)
|
|
|
|
def process_request(self, request, client_address):
|
|
addr = str(client_address[0]) if client_address else ""
|
|
with self._cap_lock:
|
|
full = (sum(self._cap_open.values()) >= self.max_connections
|
|
or self._cap_open.get(addr, 0) >= self.max_per_address)
|
|
if not full:
|
|
self._cap_open[addr] = self._cap_open.get(addr, 0) + 1
|
|
if full:
|
|
self.shutdown_request(request)
|
|
return
|
|
try:
|
|
super().process_request(request, client_address)
|
|
except BaseException:
|
|
self._cap_release(addr)
|
|
raise
|
|
|
|
def process_request_thread(self, request, client_address):
|
|
try:
|
|
super().process_request_thread(request, client_address)
|
|
finally:
|
|
self._cap_release(str(client_address[0]) if client_address else "")
|
|
|
|
|
|
class RelayServer(BoundedThreadingMixIn, http.server.HTTPServer):
|
|
allow_reuse_address = True
|
|
request_queue_size = 128
|
|
|
|
|
|
def serve(cfg: RelayConfig, bind: str, port: int) -> RelayServer:
|
|
"""Bind now (raises OSError if the address is not ours yet); the caller runs
|
|
serve_forever in its own thread."""
|
|
return RelayServer((bind, port), make_handler(cfg))
|
|
|
|
|
|
if __name__ == "__main__": # dev: python3 agent_relay.py 127.0.0.1 30091 (no session check)
|
|
import sys
|
|
cfg = RelayConfig(upstream=split_upstream(sys.argv[3] if len(sys.argv) > 3 else "http://127.0.0.1:4096"),
|
|
credentials=lambda: read_credentials(Path.home() / ".config/qwen38/opencode-web.env"),
|
|
is_authed=lambda cookie: True, cockpit_port=30090)
|
|
srv = serve(cfg, sys.argv[1] if len(sys.argv) > 1 else "127.0.0.1", int(sys.argv[2]) if len(sys.argv) > 2 else 30091)
|
|
print(f"relay (UNAUTHENTICATED dev mode) on {srv.server_address} -> {cfg.upstream_host}", flush=True)
|
|
srv.serve_forever()
|