Skip to content

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

Mac proxy: read it outside startServerOn so launches never overlap

AdityaUmale8cd6110838bd
diff --git a/electron/main.mjs b/electron/main.mjs
index faed082adb..b79a840280 100644
--- a/electron/main.mjs
+++ b/electron/main.mjs
@@ -110,7 +110,7 @@ import { askForMicrophone, openPrivacySettings } from "./privacy-settings.mjs";
import { createHomeWindow, installHomeWindowIpc } from "./home-window.mjs";
import { createTeamProfileSeeds } from "./team-profile-seed.mjs";
import { createHomePlaces } from "./home-places.mjs"; import { openStartPlace } from "./start-place.mjs"; import { createDesktopRing, installAccounts } from "./accounts-main.mjs"; import { createAddedCloudNotice, installCloudRail, railPlaces } from "./cloud-rail.mjs"; import { createPlaceInboxWatch } from "./place-inbox-watch.mjs"; import { homeLook, otherOwnClouds, withoutOldCloudHome } from "./cloud-list.mjs"; import { installPlaceLook } from "./place-look.mjs"; import { CHROME_RAIL_PATH, createChromeRail } from "./chrome-rail.mjs"; import { openComputerSettings } from "./open-computer-settings.mjs";
-import { createWindowStateFile } from "./window-state-file.mjs"; import { systemProxyEnvironment } from "./system-proxy.mjs";
+import { createWindowStateFile } from "./window-state-file.mjs"; import { createSystemProxyCache } from "./system-proxy.mjs";
import { createEnvironmentsFile, ENVIRONMENTS_SAVE_FAILED } from "./environments-file.mjs";
import { createOrgLibrary } from "./org-library.mjs";
import { createCompanyBackups } from "./company-backups.mjs";
@@ -289,7 +289,7 @@ let companyBackupConfigurationRevision = 0;
const managedDesktopRelay = createManagedDesktopRelay();
const includedX = createDesktopIncludedX({ account: () => cloudAccount, server: () => serverProc, log: slog }), includedPages = createDesktopIncludedPages({ account: () => cloudAccount, server: () => serverProc, log: slog }), phoneLinkFact = createPhoneLinkFact({ retired: () => companionTunnelRetired(secureCredentialState?.read() ?? secureCredentials), server: () => (serverReady ? serverProc : null), log: slog }), phonePushAccess = createPhonePushAccessSync({ server: () => (serverReady ? serverProc : null), credentials: () => secureCredentialState?.read() ?? secureCredentials, controlPlane: () => resolveCompanionControlPlaneURL({ isPackaged: app.isPackaged, environment: process.env }), log: slog });
const cloudPower = createCloudPowerGate({ power: () => cloudAccount?.power() ?? null, wanted: () => Boolean(cloudSync?.running() || computerSharing?.lendingToCloud()) });
-const utilityServerExits = new WeakMap();
+const utilityServerExits = new WeakMap(), systemProxy = createSystemProxyCache(); // A Mac app opened from Finder never gets the shell's proxy.
const UTILITY_SERVER_STOP_TIMEOUT_MS = 6_500;
const trustedApprovalMode = createTrustedApprovalModeCoordinator({ randomId: randomUUID, t: (...args) => nativeT(...args) });
const desktopMutationToken = randomBytes(32).toString("base64url");
@@ -304,7 +304,7 @@ const serverSupervisor = createServerSupervisor({
serverStartConflictOnly = false;
slog(`server ready pid=${proc.pid} port=${SERVER_PORT}`);
void readCloudOfferBrand();
- syncManagedComposioCredentials(); includedX.serverStarted(); includedPages.serverStarted(); phoneLinkFact.serverStarted(); phonePushAccess.serverStarted(); railChanged();
+ syncManagedComposioCredentials(); includedX.serverStarted(); includedPages.serverStarted(); phoneLinkFact.serverStarted(); phonePushAccess.serverStarted(); railChanged(); void systemProxy.refresh();
reportOwnListeners(proc);
orgLibrary?.runtimeReady();
if (managedDesktop) void managedDesktop.refresh().catch(() => {});
@@ -1103,15 +1103,14 @@ function receivePhoneSecretSave(proc, rawMessage) {
}
async function startServerOn(port, dataDir = runningDataDir ?? startupEnvironmentDir(environmentsState, desktopDataDir())) {
- const systemProxy = await systemProxyEnvironment(); // A Mac app opened from Finder never gets the shell's proxy.
- if (desktopShutdownStarted) return { proc: null, abort: true }; // After the lookup: a quit during it forks nothing.
+ if (desktopShutdownStarted) return { proc: null, abort: true };
const { entry, compileCacheDir } = serverChildLaunch({
resourcesPath: process.resourcesPath,
userData: app.getPath("userData"),
});
const childEnv = managedComposioChildEnvironment(composioBrokerUrl(), secureCredentials, {
// The parent owns the data-dir lease; a child only validates it, so fallback-port children never replace it.
- ...process.env, ...systemProxy, ...desktopDataDirLease.utilityServerLeaseEnvironment(),
+ ...process.env, ...systemProxy.latest(), ...desktopDataDirLease.utilityServerLeaseEnvironment(),
OMB_DATA_DIR: dataDir,
// Fail-closed until this process sends the private connection; never a descriptor inherited from the shell.
OMB_DESKTOP_PARENT: "1",
@@ -1191,6 +1190,7 @@ async function startServerOn(port, dataDir = runningDataDir ?? startupEnvironmen
async function startServerPackaged() {
let everyPortForeignOwned = true;
+ await systemProxy.refresh();
for (let attempt = 0; attempt < 2; attempt++) {
for (const port of DESKTOP_PORTS) {
if (desktopShutdownStarted) return false;
diff --git a/electron/system-proxy.mjs b/electron/system-proxy.mjs
index 9f231eb725..658d7544bd 100644
--- a/electron/system-proxy.mjs
+++ b/electron/system-proxy.mjs
@@ -68,3 +68,10 @@ export async function systemProxyEnvironment({ platform = process.platform, env
const bypass = [...new Set([...own, ...proxy.exceptions.map(cidrAsRange), ...LOOPBACK])].join(",");
return { HTTPS_PROXY: proxy.https, https_proxy: proxy.https, NO_PROXY: bypass, no_proxy: bypass, NODE_USE_ENV_PROXY: "1" };
}
+
+/** The last lookup, read without waiting when a server child launches: an await between startServerOn's shutdown check
+ * and its fork would let a recovery restart and an environment switch both fork a child. */
+export function createSystemProxyCache(lookup = systemProxyEnvironment) {
+ let latest = {};
+ return { latest: () => latest, async refresh() { latest = await lookup(); } };
+}
diff --git a/electron/system-proxy.test.mjs b/electron/system-proxy.test.mjs
index 7b9ecec720..c9431c045a 100644
--- a/electron/system-proxy.test.mjs
+++ b/electron/system-proxy.test.mjs
@@ -1,8 +1,9 @@
// A Mac whose internet goes through a proxy set in System Settings: the server reaches HTTPS sites through it too.
import { execFile } from "node:child_process";
+import { readFileSync } from "node:fs";
import { createServer } from "node:net";
import { describe, expect, it } from "vitest";
-import { cidrAsRange, macProxyFromScutil, systemProxyEnvironment } from "./system-proxy.mjs";
+import { cidrAsRange, createSystemProxyCache, macProxyFromScutil, systemProxyEnvironment } from "./system-proxy.mjs";
const SCUTIL = `<dictionary> {
ExceptionsList : <array> {
@@ -118,3 +119,25 @@ describe.runIf(envProxyWorks)("a server started with that env", () => {
expect(await requestGoesThroughProxy("169.254.1.2")).toBe(false);
}, 15_000);
});
+
+describe("main.mjs's use of the system proxy", () => {
+ it("never awaits between the shutdown check and the fork, so a recovery restart and a switch cannot both fork", () => {
+ const source = readFileSync(new URL("./main.mjs", import.meta.url), "utf8").replace(/\r\n/g, "\n");
+ const start = source.indexOf("async function startServerOn(");
+ const launch = source.slice(start, source.indexOf("utilityProcess.fork(", start));
+ expect(start).toBeGreaterThan(-1);
+ expect(launch).toContain("if (desktopShutdownStarted)");
+ expect(launch).not.toMatch(/\bawait\b/);
+ expect(launch).toContain("...systemProxy.latest()");
+ });
+
+ it("keeps the last lookup until the next refresh, and starts empty", async () => {
+ const results = [{ HTTPS_PROXY: "http://a:1" }, {}];
+ const cache = createSystemProxyCache(async () => results.shift());
+ expect(cache.latest()).toEqual({});
+ await cache.refresh();
+ expect(cache.latest()).toEqual({ HTTPS_PROXY: "http://a:1" });
+ await cache.refresh();
+ expect(cache.latest()).toEqual({});
+ });
+});