Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

browser: look for Xvnc only once a display is wanted, so a server without it does nothing for the desktop view

milind-soni7d773e6aaca0
diff --git a/server/browser/desktop/displays.ts b/server/browser/desktop/displays.ts
index 6e92d85946..6bf0181935 100644
--- a/server/browser/desktop/displays.ts
+++ b/server/browser/desktop/displays.ts
@@ -48,8 +48,8 @@ type Spawn = (file: string, args: string[], options: { env: NodeJS.ProcessEnv })
export interface DisplaySupervisorOptions {
/** Where each display keeps its socket and cookie; a short path, as a unix socket's may not pass about 100 bytes. */
root: string;
- /** The Xvnc binary, or null where there is none. */
- xvnc: string | null;
+ /** The Xvnc binary, or null where there is none; a function is asked once, the first time a display is wanted. */
+ xvnc: string | null | (() => string | null);
/** bubblewrap, which puts the lock-down policy in force for a display's Chrome alone; null where it cannot run. A
* function is asked once, the first time a display is wanted, so a server that never wants one never probes. */
bwrap: string | null | (() => string | null);
@@ -202,6 +202,7 @@ export class DisplaySupervisor {
readonly #claimed = new Set<number>();
#swept = false;
#probed: { bwrap: string | null } | null = null;
+ #found: { xvnc: string | null } | null = null;
constructor(options: DisplaySupervisorOptions) {
this.#options = options;
@@ -209,7 +210,14 @@ export class DisplaySupervisor {
/** Security: with no way to put the policy in force for its Chrome alone, no browser gets a display. */
get available(): boolean {
- return this.#options.xvnc !== null && this.#bwrap() !== null;
+ return this.#xvnc() !== null && this.#bwrap() !== null;
+ }
+
+ #xvnc(): string | null {
+ const xvnc = this.#options.xvnc;
+ if (typeof xvnc !== "function") return xvnc;
+ this.#found ??= { xvnc: xvnc() };
+ return this.#found.xvnc;
}
#bwrap(): string | null {
@@ -288,7 +296,7 @@ export class DisplaySupervisor {
}
async #start(key: string, size: DisplaySize, fileRoots: readonly string[]): Promise<Running> {
- const xvnc = this.#options.xvnc;
+ const xvnc = this.#xvnc();
if (!xvnc) throw new Error("This server has no display for a browser window: Xvnc is not installed.");
const bwrap = this.#bwrap();
if (!bwrap) throw new Error("This server cannot keep a browser window's Chrome locked down: bubblewrap cannot run here.");
diff --git a/server/browser/engines.test.ts b/server/browser/engines.test.ts
index 34969e05f2..82385dbf51 100644
--- a/server/browser/engines.test.ts
+++ b/server/browser/engines.test.ts
@@ -386,7 +386,7 @@ describe("the new engine's desktop view", () => {
const probeBwrap = vi.fn(() => "/usr/bin/bwrap");
const value = new ChromeEngine({ dataDir, host: "container", env: { PATH: bin }, platform: "linux", egressPort: async () => 0, idleMs: () => 180_000, egressNotice: () => null, cloudDisplay: () => on, probeBwrap });
try {
- value.summary();
+ expect(value.summary()).not.toHaveProperty("desktopView");
expect(value.desktopView()).toBe(false);
expect(probeBwrap).not.toHaveBeenCalled();
on = true;
@@ -398,4 +398,19 @@ describe("the new engine's desktop view", () => {
await removeTempDir(dataDir);
}
});
+
+ it("stays inert on a server without Xvnc, the setting on: no lookup at start, no bubblewrap probe, no desktop view", async () => {
+ const dataDir = mkdtempSync(join(tmpdir(), "omb-chrome-engine-"));
+ const probeBwrap = vi.fn(() => "/usr/bin/bwrap");
+ const value = new ChromeEngine({ dataDir, host: "container", env: { PATH: join(dataDir, "no-such-bin") }, platform: "linux", egressPort: async () => 0, idleMs: () => 180_000, egressNotice: () => null, cloudDisplay: () => true, probeBwrap });
+ try {
+ expect(value.desktopView()).toBe(false);
+ expect(value.summary()).not.toHaveProperty("desktopView");
+ expect(probeBwrap).not.toHaveBeenCalled();
+ expect(value.displays.numbers()).toEqual([]);
+ } finally {
+ await value.close();
+ await removeTempDir(dataDir);
+ }
+ });
});
diff --git a/server/browser/engines.ts b/server/browser/engines.ts
index ef50a7aa33..626f1e349d 100644
--- a/server/browser/engines.ts
+++ b/server/browser/engines.ts
@@ -95,7 +95,7 @@ export class ChromeEngine {
const locale = () => browserLanguage(env, options.language?.()) ?? null;
this.displays = new DisplaySupervisor({
root: join(browserRoot(options.dataDir), "displays"),
- xvnc: options.host === "desktop" ? null : findXvnc(env, platform),
+ xvnc: options.host === "desktop" ? null : () => findXvnc(env, platform),
bwrap: options.host === "desktop" ? null : () => (options.probeBwrap ?? findBwrap)(env, platform),
...(options.log ? { log: options.log } : {}),
});