Every worker bot now works on a computer as its own Linux account (cb-<bot id>, no sudo), owning its folder; a workspace's folder belongs to a group its bots share. Only the coordinator works as gem, with sudo. gem, and so Chrome, can't open worker folders: downloads, uploads and files saved by name are copied by the worker's own account, and workers open no file: URLs. The computer's own services (agent, DevTools, Playwright MCP, VNC, ttyd) are kept from worker accounts by an nftables rule, so containers get NET_ADMIN; without the rule the agent refuses worker commands. The X display needs gem's cookie. File reads for a worker are checked on the open descriptor, and writes are made as the worker. Workers ask the coordinator to install system packages with request_install: a coordinator task in the super admin's conversation weighs it and answers, and the answer wakes the worker. Stop ends a worker's leftover processes on its computers. Computer image 1.2.0; the layout bump recreates every computer once.
145 lines
5.7 KiB
TypeScript
145 lines
5.7 KiB
TypeScript
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
|
|
import { AgentState } from "./agent-state";
|
|
import { AgentConfig } from "./config";
|
|
import handleExec from "./handle-exec";
|
|
import handleFileDownload from "./handle-file-download";
|
|
import handleFileStat from "./handle-file-stat";
|
|
import handleFileWrite from "./handle-file-write";
|
|
import isAuthorized from "./is-authorized";
|
|
import isPortAnswering from "./is-port-answering";
|
|
import log from "./log";
|
|
import bridgeLoopbackSocket from "./bridge-loopback-socket";
|
|
import bridgeVncSocket from "./bridge-vnc-socket";
|
|
import type { BrowserParking } from "./create-browser-parking";
|
|
import proxyLoopback from "./proxy-loopback";
|
|
import sendJson from "./send-json";
|
|
import serveStaticFile from "./serve-static-file";
|
|
|
|
/**
|
|
* The agent's one port:
|
|
*
|
|
* - `GET /health` (open): 200 once the browser and its tools answer.
|
|
* - `GET /api/info`, `POST /api/exec`, `GET /api/files/stat|download`,
|
|
* `POST /api/files/write`: the app's shell and file access, as gem or
|
|
* as a worker bot's own account (`run_as`).
|
|
* - `/mcp`: Playwright MCP, driving the computer's browser.
|
|
* - `/desktop/` and its `/desktop/websockify` websocket: the screen
|
|
* (noVNC over Xvnc), Chrome's whole window.
|
|
* - `/terminal/` and its websocket: a shell in the browser (ttyd).
|
|
*
|
|
* Everything but the health check needs the computer's token. Browser
|
|
* tool calls and screen views wake a parked browser first (`parking`).
|
|
*/
|
|
export default function createAgentServer({ parking }: { parking: BrowserParking }) {
|
|
const server = createServer((req, res) => {
|
|
route({ req, res, parking }).catch((error) => {
|
|
log({ name: "agent", message: `${req.method} ${req.url} failed: ${error?.message || error}` });
|
|
|
|
if (!res.headersSent) sendJson({ res, status: 500, body: { error: String(error?.message || error) } });
|
|
else res.destroy();
|
|
});
|
|
});
|
|
|
|
server.on("upgrade", (req, socket, head) => {
|
|
const pathname = new URL(req.url || "/", "http://agent").pathname;
|
|
|
|
const authorized = isAuthorized({ headers: req.headers, token: AgentConfig.Token });
|
|
|
|
if (authorized && pathname == "/desktop/websockify") {
|
|
const watching = parking.watch();
|
|
|
|
socket.once("close", watching.done);
|
|
bridgeVncSocket({ req, socket, head, port: AgentConfig.VncPort });
|
|
return;
|
|
}
|
|
|
|
if (authorized && pathname.startsWith("/terminal/")) {
|
|
bridgeLoopbackSocket({ req, socket, head, port: AgentConfig.TerminalPort });
|
|
return;
|
|
}
|
|
|
|
socket.end("HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\n");
|
|
});
|
|
|
|
/**
|
|
* Commands and event streams can run long; the app keeps its own
|
|
* timeouts.
|
|
*/
|
|
server.requestTimeout = 0;
|
|
server.headersTimeout = 60_000;
|
|
|
|
return server;
|
|
}
|
|
|
|
async function route({ req, res, parking }: { req: IncomingMessage; res: ServerResponse; parking: BrowserParking }) {
|
|
const { pathname } = new URL(req.url || "/", "http://agent");
|
|
|
|
if (pathname == "/health") {
|
|
const [browser, mcp] = await Promise.all([
|
|
isPortAnswering({ url: `http://127.0.0.1:${AgentConfig.CdpPort}/json/version` }),
|
|
isPortAnswering({ url: `http://127.0.0.1:${AgentConfig.McpPort}/mcp` }),
|
|
]);
|
|
|
|
return sendJson({ res, status: browser && mcp ? 200 : 503, body: { ok: browser && mcp, browser, mcp } });
|
|
}
|
|
|
|
if (!isAuthorized({ headers: req.headers, token: AgentConfig.Token })) {
|
|
return sendJson({ res, status: 401, body: { error: "Unauthorized" } });
|
|
}
|
|
|
|
if (pathname == "/mcp" || pathname.startsWith("/mcp/")) {
|
|
// A GET is the session's event stream, open for as long as the
|
|
// session: only calls hold parking off while they run.
|
|
const holding = req.method == "GET" ? undefined : parking.hold();
|
|
|
|
if (holding) res.once("close", holding.done);
|
|
|
|
await parking.wake();
|
|
|
|
return proxyLoopback({ req, res, port: AgentConfig.McpPort, unavailable: "The browser tools aren't answering yet." });
|
|
}
|
|
|
|
if (pathname == "/terminal") {
|
|
res.writeHead(302, { location: "terminal/" }).end();
|
|
return;
|
|
}
|
|
|
|
if (pathname.startsWith("/terminal/")) {
|
|
return proxyLoopback({ req, res, port: AgentConfig.TerminalPort, unavailable: "The shell isn't answering yet." });
|
|
}
|
|
|
|
if (pathname == "/" || pathname == "/desktop") {
|
|
res.writeHead(302, { location: "desktop/" }).end();
|
|
return;
|
|
}
|
|
|
|
if (pathname == "/desktop/") return serveStaticFile({ res, root: AgentConfig.NoVncDir, name: "vnc.html" });
|
|
if (pathname.startsWith("/desktop/")) {
|
|
return serveStaticFile({ res, root: AgentConfig.NoVncDir, name: pathname.slice("/desktop/".length) });
|
|
}
|
|
|
|
if (pathname == "/api/info" && req.method == "GET") {
|
|
return sendJson({
|
|
res,
|
|
body: {
|
|
home_dir: AgentConfig.Home,
|
|
workspace_dir: AgentConfig.WorkspaceDir,
|
|
user: AgentConfig.User,
|
|
workers_guarded: AgentState.workers_guarded,
|
|
browser: {
|
|
binary: AgentState.browser_binary,
|
|
version: AgentState.browser_version,
|
|
sandbox: AgentState.browser_sandbox,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
if (pathname == "/api/exec" && req.method == "POST") return handleExec({ req, res });
|
|
if (pathname == "/api/files/stat" && req.method == "GET") return handleFileStat({ req, res });
|
|
if (pathname == "/api/files/download" && req.method == "GET") return handleFileDownload({ req, res });
|
|
if (pathname == "/api/files/write" && req.method == "POST") return handleFileWrite({ req, res });
|
|
|
|
sendJson({ res, status: 404, body: { error: "Not Found" } });
|
|
}
|