Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Mac: use the system HTTPS proxy when the environment names none
AdityaUmaleeaf37d41ce2b
diff --git a/electron/main.mjs b/electron/main.mjsindex 28013eb606..add03bb7b0 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 { createWindowStateFile } from "./window-state-file.mjs"; import { systemProxyEnvironment } 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";@@ -1109,7 +1109,7 @@ async function startServerOn(port, dataDir = runningDataDir ?? startupEnvironmenuserData: app.getPath("userData"),});const childEnv = managedComposioChildEnvironment(composioBrokerUrl(), secureCredentials, {- ...process.env,+ ...process.env, ...(await systemProxyEnvironment()), // A Mac app opened from Finder never gets the shell's proxy.// The parent owns the data-dir lease; a child only validates it, so fallback-port children never replace it....desktopDataDirLease.utilityServerLeaseEnvironment(),OMB_DATA_DIR: dataDir,diff --git a/electron/system-proxy.mjs b/electron/system-proxy.mjsnew file mode 100644index 0000000000..c498540638--- /dev/null+++ b/electron/system-proxy.mjs@@ -0,0 +1,54 @@+// A Mac app opened from Finder or the Dock never gets the shell's proxy variables, so on a network that only reaches the+// internet through a proxy set in System Settings, the server's HTTPS calls (ChatGPT sign-in, model APIs) fail.+import { execFile } from "node:child_process";++const PROXY_ENV = ["HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy"];+/** Node's env proxy, unlike curl, does not exempt loopback, and matches an IPv6 host only bracketed ("[::1]"). */+const LOOPBACK = ["localhost", "127.0.0.1", "::1", "[::1]"];++/** The HTTPS proxy at the top level of `scutil --proxy`, with its exceptions, or null when it is off or half set.+ * Only HTTPS: on Node 24 a plain http:// request through an env proxy hangs, so http:// stays direct. No PAC or SOCKS. */+export function macProxyFromScutil(output) {+ const top = {};+ const exceptions = [];+ let depth = 0;+ let inExceptions = false;+ for (const raw of String(output ?? "").split("\n")) {+ const line = raw.trim();+ if (line === "}") {+ depth -= 1;+ if (depth === 1) inExceptions = false;+ continue;+ }+ const match = /^(\S+) : (.*)$/.exec(line);+ if (line.endsWith("{")) {+ if (depth === 1 && match?.[1] === "ExceptionsList") inExceptions = true;+ depth += 1;+ continue;+ }+ if (!match) continue;+ if (depth === 1) top[match[1]] = match[2].trim();+ else if (depth === 2 && inExceptions && match[2].trim()) exceptions.push(match[2].trim());+ }+ const host = top.HTTPSProxy;+ const port = Number(top.HTTPSPort);+ if (top.HTTPSEnable !== "1" || !host || !Number.isInteger(port) || port <= 0 || port > 65535) return null;+ return { https: `http://${host.includes(":") ? `[${host}]` : host}:${port}`, exceptions };+}++function scutilProxy() {+ return new Promise((resolve) => {+ execFile("/usr/sbin/scutil", ["--proxy"], { timeout: 2_000, encoding: "utf8" }, (error, stdout) => resolve(error ? "" : String(stdout)));+ });+}++/** The env the server child needs to use the Mac's system HTTPS proxy: nothing off a Mac, when the environment already+ * names a proxy (it always wins), without a system proxy, or when scutil fails. A NO_PROXY already set is kept. */+export async function systemProxyEnvironment({ platform = process.platform, env = process.env, read = scutilProxy } = {}) {+ if (platform !== "darwin" || PROXY_ENV.some((name) => env[name])) return {};+ const proxy = macProxyFromScutil(await read());+ if (!proxy) return {};+ const own = [env.NO_PROXY, env.no_proxy].flatMap((list) => (list ?? "").split(",")).map((entry) => entry.trim()).filter(Boolean);+ const bypass = [...new Set([...own, ...proxy.exceptions, ...LOOPBACK])].join(",");+ return { HTTPS_PROXY: proxy.https, https_proxy: proxy.https, NO_PROXY: bypass, no_proxy: bypass, NODE_USE_ENV_PROXY: "1" };+}diff --git a/electron/system-proxy.test.mjs b/electron/system-proxy.test.mjsnew file mode 100644index 0000000000..9effcdeb2e--- /dev/null+++ b/electron/system-proxy.test.mjs@@ -0,0 +1,72 @@+// A Mac whose internet goes through a proxy set in System Settings: the server reaches HTTPS sites through it too.+import { describe, expect, it } from "vitest";+import { macProxyFromScutil, systemProxyEnvironment } from "./system-proxy.mjs";++const SCUTIL = `<dictionary> {+ ExceptionsList : <array> {+ 0 : *.local+ 1 : 169.254/16+ }+ FTPPassive : 1+ HTTPEnable : 1+ HTTPPort : 1082+ HTTPProxy : 127.0.0.1+ HTTPSEnable : 1+ HTTPSPort : 1082+ HTTPSProxy : 127.0.0.1+ __SCOPED__ : <dictionary> {+ en0 : <dictionary> {+ HTTPSEnable : 1+ HTTPSPort : 9999+ HTTPSProxy : 10.0.0.9+ }+ }+}+`;++const read = (output) => async () => output;++describe("the Mac's system proxy", () => {+ it("reads the HTTPS proxy and its exceptions from the top level only", () => {+ expect(macProxyFromScutil(SCUTIL)).toEqual({ https: "http://127.0.0.1:1082", exceptions: ["*.local", "169.254/16"] });+ });++ it("finds none when the HTTPS proxy is off, half set or the output is empty", () => {+ expect(macProxyFromScutil(SCUTIL.replace("HTTPSEnable : 1\n HTTPSPort", "HTTPSEnable : 0\n HTTPSPort"))).toBeNull();+ expect(macProxyFromScutil("<dictionary> {\n HTTPSEnable : 1\n HTTPSPort : 8080\n}\n")).toBeNull();+ expect(macProxyFromScutil("")).toBeNull();+ });++ it("brackets an IPv6 proxy host", () => {+ expect(macProxyFromScutil("<dictionary> {\n HTTPSEnable : 1\n HTTPSPort : 8080\n HTTPSProxy : ::1\n}\n")?.https).toBe("http://[::1]:8080");+ });++ it("gives the server the proxy, its exceptions and loopback on a Mac with none set in the environment", async () => {+ expect(await systemProxyEnvironment({ platform: "darwin", env: {}, read: read(SCUTIL) })).toEqual({+ HTTPS_PROXY: "http://127.0.0.1:1082",+ https_proxy: "http://127.0.0.1:1082",+ NO_PROXY: "*.local,169.254/16,localhost,127.0.0.1,::1,[::1]",+ no_proxy: "*.local,169.254/16,localhost,127.0.0.1,::1,[::1]",+ NODE_USE_ENV_PROXY: "1",+ });+ });++ it("keeps the hosts a NO_PROXY in the environment already skips", async () => {+ const env = await systemProxyEnvironment({ platform: "darwin", env: { no_proxy: "intranet.example, localhost" }, read: read(SCUTIL) });+ expect(env.NO_PROXY).toBe("intranet.example,localhost,*.local,169.254/16,127.0.0.1,::1,[::1]");+ expect(env.no_proxy).toBe(env.NO_PROXY);+ });++ it("never touches a proxy already set in the environment, in any case", async () => {+ for (const name of ["HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy"]) {+ expect(await systemProxyEnvironment({ platform: "darwin", env: { [name]: "http://corp:3128" }, read: read(SCUTIL) })).toEqual({});+ }+ });++ it("changes nothing off a Mac, without a system proxy or when scutil fails", async () => {+ expect(await systemProxyEnvironment({ platform: "win32", env: {}, read: read(SCUTIL) })).toEqual({});+ expect(await systemProxyEnvironment({ platform: "linux", env: {}, read: read(SCUTIL) })).toEqual({});+ expect(await systemProxyEnvironment({ platform: "darwin", env: {}, read: read("<dictionary> {\n HTTPSEnable : 0\n}\n") })).toEqual({});+ expect(await systemProxyEnvironment({ platform: "darwin", env: {}, read: read("") })).toEqual({});+ });+});