/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import type { ConnectionTransport } from "../../types/ConnectionTransport.ts"; import { drainStream } from "../internal/stream-drain.ts"; import { dirname, join } from "node:path"; import { resolveBxcConfig } from "../config/resolve.ts"; import { resolveNativeExecutable } from "../native/loader.ts"; import { DEFAULT_ACCEPT_LANGUAGE } from "../internal/browser-headers.ts"; /** * Builds a User-Agent for a locally installed Chromium, derived from the * binary's own version. * * Under `--headless=new` Chrome advertises `HeadlessChrome/`, which is the * cheapest bot signal there is — a WAF matches the literal token and serves a * challenge no amount of stealth patching will clear. Overriding it with a * *hardcoded* UA trades one tell for another, because `Sec-CH-UA` still carries * the real major version and the mismatch is itself a signal. So read the * version off the binary and only drop the `Headless` marker. * * Returns `null` when the version cannot be read: no override beats an * inconsistent one. */ function chromeUaFromBinary(chromePath: string, isWin: boolean): string | null { try { const proc = Bun.spawnSync([chromePath, "--version"], { env: Bun.env }); if (!proc.success) return null; const raw = proc.stdout.toString().trim(); const major = /(\d+)\.\d+\.\d+/.exec(raw)?.[1]; if (!major) return null; const platform = isWin ? "Windows NT 10.0; Win64; x64" : process.platform === "darwin" ? "Macintosh; Intel Mac OS X 10_15_7" : "X11; Linux x86_64"; return ( `Mozilla/5.0 (${platform}) AppleWebKit/537.36 (KHTML, like Gecko) ` + `Chrome/${major}.0.0.0 Safari/537.36` ); } catch { return null; } } export interface WebSocketTransportOptions { /** URL to the chromium websocket endpoint (if already running) */ browserWSEndpoint?: string; /** Extra environment variables to pass to the sub-process. */ env?: Record; /** Optional logger for stderr messages from the sub-process. */ stderrLogger?: (chunk: string) => void; /** Force headless mode. */ headless?: boolean; // --- Legacy Lightpanda Options (SocketPairTransport compatibility) --- binaryPath?: string; logLevel?: "debug" | "info" | "warn" | "error" | "fatal" | string; /** User-Agent for the launched browser. Defaults to the binary's own. */ userAgent?: string; readyTimeoutMs?: number; insecure?: boolean; proxy?: string; proxyAuth?: string; } async function findFreePort(host: string): Promise { for (let attempt = 0; attempt < 32; attempt++) { const port = 49152 + Math.floor(Math.random() * (65535 - 49152)); const free = await new Promise((resolve) => { try { const server = Bun.serve({ hostname: host, port, fetch: () => new Response(null), }); server.stop(true); resolve(true); } catch { resolve(false); } }); if (free) return port; } throw new Error("WebSocketTransport: could not find a free TCP port"); } export class WebSocketTransport implements ConnectionTransport { onmessage?: (message: string) => void; onclose?: () => void; readonly #wsUrl: string; #proc?: ReturnType; #ws: WebSocket; #closed = false; #sendQueue: string[] = []; private constructor( ws: WebSocket, wsUrl: string, proc?: ReturnType, ) { this.#ws = ws; this.#wsUrl = wsUrl; this.#proc = proc; this.#wireSocket(ws); } static async create( opts: WebSocketTransportOptions = {}, ): Promise { let wsUrl = opts.browserWSEndpoint; let proc: ReturnType | undefined; if (!wsUrl) { const repoRoot = join(import.meta.dir, "..", ".."); const engine = resolveNativeExecutable("bxc-engine", { envVar: "BXC_ENGINE_BIN", candidates: [ dirname(process.execPath), Bun.env.BXC_BIN_DIR ?? resolveBxcConfig().settings.binDir, join(repoRoot, "rust-bridge", "target", "release"), join(repoRoot, "dist"), ], }); const bin = engine.path; let chromePath = opts.binaryPath ?? Bun.env["BXC_CHROME_BIN"] ?? Bun.env["CHROME_PATH"]; let userDataDir = Bun.env["BXC_USER_DATA_DIR"]; const isWin = process.platform === "win32"; if (isWin) { // Windows 'Native Power' Discovery if (!chromePath) { const winPaths = [ "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", join( Bun.env["LOCALAPPDATA"] || "", "Google\\Chrome\\Application\\chrome.exe", ), ]; for (const p of winPaths) { if (await Bun.file(p).exists()) { chromePath = p; break; } } } if (!userDataDir) { // Isolate user-data-dir per profile or session to avoid ProcessSingleton lock conflicts // when the user already has Google Chrome running natively on Windows. userDataDir = join( Bun.env["LOCALAPPDATA"] || "", "Google\\Chrome\\User Data\\BxcProfile", ); } } if (!chromePath) { if (!bin) { // Never build from source at runtime: compiling the engine takes // minutes, needs a toolchain and makes startup non-deterministic. throw new Error( "bxc-engine is required to resolve a Chromium binary but was not found " + `(tried: ${engine.tried.join(", ")}). Install it with \`bxc install --full\`, ` + "set BXC_ENGINE_BIN, or pass an explicit browser path via BXC_CHROME_BIN / CHROME_PATH.", ); } const pathArgs = [bin, "chrome-path"]; const pathProc = Bun.spawnSync(pathArgs, { env: Bun.env, }); if (!pathProc.success) { throw new Error( "Failed to resolve chrome path: " + pathProc.stderr.toString(), ); } chromePath = pathProc.stdout .toString() .trim() .split("\n") .pop() ?.trim(); if (!chromePath) { throw new Error("Failed to parse resolved chromium path"); } } const isLightpanda = chromePath.includes("lightpanda"); const headless = opts.headless ?? (isWin ? false : true); const port = await findFreePort("127.0.0.1"); let args: string[]; if (isLightpanda) { args = [chromePath, "serve", "--port", port.toString()]; if (opts.headless !== false) { // Lightpanda is headless by default, but we can pass stealth } if (opts.logLevel) { args.push("--log-level", opts.logLevel); } if (opts.insecure) { args.push("--insecure-disable-tls-host-verification"); } if (opts.proxy) { args.push("--proxy", opts.proxy); } } else { const launchArgs = [ `--remote-debugging-port=${port}`, "--remote-allow-origins=*", // Required for CDP connections // Removes `navigator.webdriver`, which every commercial WAF reads // before it looks at anything harder to fake. "--disable-blink-features=AutomationControlled", `--accept-lang=${DEFAULT_ACCEPT_LANGUAGE}`, ]; // Drop the `HeadlessChrome/` token when we can name the real version. const nativeUa = opts.userAgent ?? chromeUaFromBinary(chromePath, isWin); if (nativeUa) { launchArgs.push(`--user-agent=${nativeUa}`); } const rawProxy = opts.proxy ?? Bun.env["ALL_PROXY"] ?? Bun.env["all_proxy"] ?? Bun.env["HTTPS_PROXY"] ?? Bun.env["https_proxy"] ?? Bun.env["HTTP_PROXY"] ?? Bun.env["http_proxy"]; if (rawProxy) { const normalizedProxy = rawProxy.replace(/^socks5h:\/\//i, "socks5://"); launchArgs.push(`--proxy-server=${normalizedProxy}`); } if (isWin) { // Windows Optimized Flags launchArgs.push("--enable-gpu"); launchArgs.push("--no-sandbox"); if (headless) { launchArgs.push("--headless=new"); } else { launchArgs.push("--start-maximized"); } if (userDataDir) { launchArgs.push("--user-data-dir=" + userDataDir); launchArgs.push("--profile-directory=Default"); } } else { // Linux Server Optimized Flags (Headless Dominance) if (headless) { launchArgs.push("--headless=new"); } launchArgs.push("--disable-gpu"); launchArgs.push("--disable-dev-shm-usage"); launchArgs.push("--no-sandbox"); } args = [chromePath, ...launchArgs]; } // Inherit advanced VPS variables: BXC_USER_DATA_DIR, BXC_PROFILE proc = Bun.spawn(args, { stdin: "ignore", stdout: "pipe", stderr: "pipe", env: { ...Bun.env, ...opts.env, }, }); drainStream( proc.stdout as ReadableStream | undefined, opts.stderrLogger, ); if (isLightpanda) { wsUrl = `ws://127.0.0.1:${port}`; // Lightpanda starts extremely fast, but wait a small fraction just in case await Bun.sleep(50); } else { wsUrl = await new Promise((resolve, reject) => { let output = ""; // Chromium typically logs the WebSocket URL to stderr const reader = ( proc!.stderr as ReadableStream ).getReader(); const readLoop = async () => { try { const { done, value } = await reader.read(); if (value) { const text = new TextDecoder().decode(value); output += text; if (opts.stderrLogger) opts.stderrLogger(text); const lines = output.split("\n"); for (const line of lines) { const match = line.match(/ws:\/\/[^\s]+/); if (match) { resolve(match[0].trim()); return; } } } if (!done) { readLoop(); } else { reject( new Error( "Process exited before emitting ws url. Output: " + output, ), ); } } catch (e) { reject(e); } }; readLoop(); }); } } let ws: WebSocket; let attempts = 0; const maxAttempts = 5; while (true) { try { ws = new WebSocket(wsUrl); await new Promise((resolve, reject) => { const timer = setTimeout(() => { ws.close(); reject(new Error("Connection timeout")); }, 5000); const onOpen = () => { clearTimeout(timer); ws.removeEventListener("error", onErr); resolve(); }; const onErr = (ev: Event) => { clearTimeout(timer); ws.removeEventListener("open", onOpen); reject(ev); }; ws.addEventListener("open", onOpen, { once: true }); ws.addEventListener("error", onErr, { once: true }); }); // Connected! break; } catch (err) { attempts++; if (attempts >= maxAttempts) { throw new Error( `WebSocketTransport: WebSocket failed to open after ${maxAttempts} attempts to ${wsUrl} (${(err as ErrorEvent).message ?? "Connection Refused"})`, ); } // Exponential backoff await Bun.sleep(200 * Math.pow(2, attempts - 1)); } } return new WebSocketTransport(ws, wsUrl, proc); } send(message: string): void { if (this.#closed) return; if (this.#ws.readyState === WebSocket.OPEN) { this.#ws.send(message); } else { this.#sendQueue.push(message); } } close(): void { if (this.#closed) return; this.#closed = true; try { if ( this.#ws.readyState === WebSocket.OPEN || this.#ws.readyState === WebSocket.CONNECTING ) { this.#ws.close(); } } catch { // best effort } try { this.#proc?.kill(); } catch { // best effort } queueMicrotask(() => { this.onclose?.(); }); } async closeProcess(): Promise { this.close(); try { await this.#proc?.exited; } catch { // ignore } } get webSocketDebuggerUrl(): string { return this.#wsUrl; } get closed(): boolean { return this.#closed; } #wireSocket(ws: WebSocket): void { ws.addEventListener("message", (ev: MessageEvent) => { if (this.#closed) return; const data = typeof ev.data === "string" ? ev.data : String(ev.data); this.onmessage?.(data); }); ws.addEventListener("close", () => { if (this.#closed) return; this.#closed = true; queueMicrotask(() => { this.onclose?.(); }); }); if (ws.readyState === WebSocket.OPEN) { this.#flushQueue(); } else { ws.addEventListener("open", () => this.#flushQueue(), { once: true }); } } #flushQueue(): void { while (this.#sendQueue.length > 0 && !this.#closed) { const msg = this.#sendQueue.shift(); if (msg !== undefined) this.#ws.send(msg); } } }