-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cjs
More file actions
395 lines (365 loc) · 16.2 KB
/
Copy pathmain.cjs
File metadata and controls
395 lines (365 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"use strict";
const { app, autoUpdater, BrowserWindow, dialog, shell, session, systemPreferences } = require("electron");
const { createServer } = require("node:net");
const { pathToFileURL } = require("node:url");
const path = require("node:path");
const crypto = require("node:crypto");
const fs = require("node:fs");
const { spawnSync } = require("node:child_process");
const { createNativeUpdateService } = require("./updater.cjs");
const { allowedRemoteUrl, desktopGatewayAction, isHostedWorkspaceOrigin, normalizeRemoteOrigin } = require("./workspace-target.cjs");
const LOOPBACK = "127.0.0.1";
// The OCI architecture is an intentional clean start. Keep the retired
// installation's Application Support/AppData tree untouched and never import
// it implicitly into this runtime generation.
const DATA_NAMESPACE = "1Helm-OCI-v1";
const SERVER_READY_TIMEOUT_MS = process.platform === "win32" ? 3 * 60_000 : 30_000;
app.setPath("userData", path.join(app.getPath("appData"), DATA_NAMESPACE));
let mainWindow = null;
let authWindow = null;
let localOrigin = "";
let quitting = false;
let hostUpdateService = null;
const remoteWorkspacePath = () => path.join(app.getPath("userData"), "remote-workspace");
const desktopModePath = () => path.join(app.getPath("userData"), "desktop-mode");
const localDatabasePath = () => path.join(app.getPath("userData"), "ctrl-pane.db");
function desktopMode() {
try {
const mode = fs.readFileSync(desktopModePath(), "utf8").trim();
if (["client", "server"].includes(mode)) return mode;
} catch { /* older installations have no explicit mode */ }
if (fs.existsSync(remoteWorkspacePath())) return "client";
if (fs.existsSync(localDatabasePath())) return "server";
return "choose";
}
function rememberDesktopMode(mode) {
if (!["client", "server"].includes(mode)) return;
fs.writeFileSync(desktopModePath(), `${mode}\n`, { mode: 0o600 });
}
// Windows packages ship application code inside app.asar; assets consumed by
// external processes (Python, PowerShell, WSL, plain-Node sidecars) are
// unpacked beside the archive. Translate paths for those consumers. Loose
// packages (macOS, Linux, development) pass through unchanged.
function unpackedPath(target) {
return String(target).replace(/app\.asar(?=[\\/]|$)/, "app.asar.unpacked");
}
function handleSquirrelEvent() {
if (process.platform !== "win32") return false;
const event = process.argv[1];
if (!["--squirrel-install", "--squirrel-updated", "--squirrel-uninstall", "--squirrel-obsolete"].includes(event)) return false;
const appFolder = path.resolve(process.execPath, "..");
const updateExe = path.resolve(appFolder, "..", "Update.exe");
const exe = path.basename(process.execPath);
if (event === "--squirrel-install" || event === "--squirrel-updated") {
spawnSync(updateExe, ["--createShortcut", exe], { stdio: "ignore", windowsHide: true });
} else if (event === "--squirrel-uninstall") {
const dataRoot = app.getPath("userData");
const wslRoot = path.join(String(process.env.LOCALAPPDATA || ""), "1Helm-Runtime");
const cleanup = unpackedPath(path.resolve(__dirname, "..", "scripts", "windows-removal.cjs"));
spawnSync(process.execPath, [cleanup, dataRoot, wslRoot], { env: { ...process.env, ELECTRON_RUN_AS_NODE: "1" }, stdio: "ignore", windowsHide: true, timeout: 10 * 60_000 });
spawnSync(updateExe, ["--removeShortcut", exe], { stdio: "ignore", windowsHide: true });
}
setTimeout(() => app.quit(), 1000);
return true;
}
function preferredWorkspaceOrigin() {
if (desktopMode() !== "client") return localOrigin;
try {
const value = fs.readFileSync(remoteWorkspacePath(), "utf8").trim();
return normalizeRemoteOrigin(value);
} catch {
return "";
}
}
function rememberTeamUrl(raw) {
const origin = normalizeRemoteOrigin(raw);
if (!origin) return;
fs.writeFileSync(remoteWorkspacePath(), origin + "\n", { mode: 0o600 });
rememberDesktopMode("client");
}
process.on("1helm-removal-prepared", () => {
// Prevent a cleaned installation from relaunching at login and recreating
// its channel VMs before the user moves the app to Trash.
app.setLoginItemSettings({ openAtLogin: false, type: "mainAppService" });
});
function freePort() {
return new Promise((resolve, reject) => {
const probe = createServer();
probe.once("error", reject);
probe.listen(0, LOOPBACK, () => {
const address = probe.address();
const port = typeof address === "object" && address ? address.port : 0;
probe.close((error) => error ? reject(error) : resolve(port));
});
});
}
async function waitForServer(origin, timeoutMs = SERVER_READY_TIMEOUT_MS) {
const deadline = Date.now() + timeoutMs;
let lastError = null;
while (Date.now() < deadline) {
try {
const response = await fetch(`${origin}/api/setup/status`);
if (response.ok) return;
lastError = new Error(`1Helm returned HTTP ${response.status}`);
} catch (error) {
lastError = error;
}
await new Promise((resolve) => setTimeout(resolve, 100));
}
throw lastError || new Error("1Helm did not become ready in time.");
}
async function startLocalRuntime() {
const appRoot = app.getAppPath();
// Server code is imported from inside app.asar so dependency resolution
// stays within the archive, but HELM_APP_ROOT and the working directory
// must be the real on-disk root: scripts, container assets, deploy config,
// and public files are read by external processes that cannot open asar.
const assetRoot = unpackedPath(appRoot);
const port = await freePort();
process.env.HELM_DESKTOP = "1";
process.env.HELM_APP_ROOT = assetRoot;
process.env.HELM_RESOURCES_PATH = process.resourcesPath;
process.env.HELM_HOST = LOOPBACK;
process.env.PORT = String(port);
process.env.CTRL_DATA_DIR = app.getPath("userData");
if (process.platform !== "win32") process.env.SHELL ||= "/bin/zsh";
process.env.HELM_INTERNAL_WAKE_TOKEN ||= crypto.randomBytes(32).toString("hex");
process.chdir(assetRoot);
localOrigin = `http://${LOOPBACK}:${port}`;
await import(pathToFileURL(path.join(appRoot, "src", "server", "index.ts")).href);
await waitForServer(localOrigin);
}
function removeLegacyWakeLaunchAgent() {
if (process.platform !== "darwin") return;
const plistPath = path.join(app.getPath("home"), "Library", "LaunchAgents", "com.gitcommit90.1helm.wake.plist");
const domain = `gui/${process.getuid()}`;
spawnSync("/bin/launchctl", ["bootout", `${domain}/com.gitcommit90.1helm.wake`], { stdio: "ignore" });
try { fs.unlinkSync(plistPath); } catch { /* absent */ }
}
function keepSkipperAvailable() {
// The signed main app appears under its 1Helm app identity and icon in Login
// Items. The separately registered legacy LaunchAgent was the component
// macOS exposed as software from the certificate publisher.
app.setLoginItemSettings({ openAtLogin: true, type: "mainAppService" });
}
function stopAutomaticServerStartup() {
app.setLoginItemSettings({ openAtLogin: false, type: "mainAppService" });
}
function prepareWindowsWslDataRoot() {
if (process.platform !== "win32") return;
// One installation-scoped virtual disk stays outside the replaceable app.
fs.mkdirSync(path.join(String(process.env.LOCALAPPDATA || ""), "1Helm-Runtime"), { recursive: true });
}
function allowedLocalUrl(raw) {
try {
const url = new URL(raw);
return url.origin === localOrigin && ["http:", "ws:"].includes(url.protocol);
} catch {
return false;
}
}
function allowedTeamUrl(raw) {
return allowedRemoteUrl(raw, preferredWorkspaceOrigin() === localOrigin ? "" : preferredWorkspaceOrigin());
}
const allowedAppUrl = (raw) => allowedLocalUrl(raw) || allowedTeamUrl(raw);
async function connectRemoteWorkspace(window, origin) {
try {
const response = await fetch(`${origin}/api/mobile/compatibility`, { signal: AbortSignal.timeout(15_000) });
const result = await response.json().catch(() => ({}));
if (!response.ok || result.product !== "1Helm" || result.mobile_api !== 1) throw new Error("That address is not a compatible 1Helm instance.");
if (!result.has_users || !result.setup_complete) throw new Error("Finish setting up this 1Helm instance before connecting the app.");
rememberTeamUrl(origin);
await window.loadURL(origin);
} catch (error) {
await loadDesktopGateway(window, { origin, error: error instanceof Error ? error.message : "Could not connect to this instance." });
}
}
function loadDesktopGateway(window, state = {}) {
return window.loadFile(path.join(__dirname, "gateway.html"), { query: {
...(state.origin ? { origin: state.origin, custom: isHostedWorkspaceOrigin(state.origin) ? "0" : "1" } : {}),
...(state.error ? { error: state.error } : {}),
} });
}
async function loadInitialWorkspace(window) {
const mode = desktopMode();
if (mode === "server" || process.platform === "linux") { await window.loadURL(localOrigin); return; }
if (mode === "client") {
const preferred = preferredWorkspaceOrigin();
if (preferred) { await window.loadURL(preferred); return; }
}
await loadDesktopGateway(window);
}
async function startServerMode(window) {
try {
keepSkipperAvailable();
prepareWindowsWslDataRoot();
if (!localOrigin) await startLocalRuntime();
rememberDesktopMode("server");
hostUpdateService?.schedule();
await window.loadURL(localOrigin);
} catch (error) {
stopAutomaticServerStartup();
await dialog.showMessageBox({
type: "error",
title: "1Helm could not start",
message: `The local 1Helm runtime could not start on this ${process.platform === "win32" ? "Windows PC" : "Mac"}.`,
detail: error instanceof Error ? error.stack || error.message : String(error),
});
await loadDesktopGateway(window, { error: "This PC could not start its local 1Helm server." });
}
}
function microphonePermissionAllowed(webContents, permission, details = {}) {
const pageUrl = webContents?.getURL?.() || "";
if (permission !== "media" || !allowedAppUrl(pageUrl)) return false;
const mediaTypes = Array.isArray(details.mediaTypes) ? details.mediaTypes : [];
return mediaTypes.length === 0 || (mediaTypes.includes("audio") && !mediaTypes.includes("video"));
}
function openAuthWindow(url) {
if (authWindow && !authWindow.isDestroyed()) authWindow.close();
const window = new BrowserWindow({
width: 1040,
height: 780,
parent: mainWindow || undefined,
title: "Connect to 1Helm",
webPreferences: { contextIsolation: true, nodeIntegration: false, sandbox: true },
});
const returnToApp = (event, nextUrl) => {
if (!allowedAppUrl(nextUrl)) return;
event.preventDefault();
void mainWindow?.loadURL(nextUrl);
window.close();
mainWindow?.show();
mainWindow?.focus();
};
window.webContents.on("will-navigate", returnToApp);
window.webContents.on("will-redirect", returnToApp);
window.webContents.setWindowOpenHandler(({ url: nextUrl }) => {
if (/^https?:/i.test(nextUrl) || /^mailto:build@1helm\.com$/i.test(nextUrl)) void shell.openExternal(nextUrl);
return { action: "deny" };
});
window.on("closed", () => { if (authWindow === window) authWindow = null; });
void window.loadURL(url);
authWindow = window;
}
function createWindow(showWhenReady = true) {
const window = new BrowserWindow({
width: 1440,
height: 940,
minWidth: 820,
minHeight: 600,
show: false,
backgroundColor: "#08090c",
title: "1Helm",
webPreferences: {
contextIsolation: true,
nodeIntegration: false,
sandbox: true,
spellcheck: true,
},
});
window.webContents.setWindowOpenHandler(({ url }) => {
if (allowedAppUrl(url)) return { action: "allow" };
if (/^https?:/i.test(url) || /^mailto:build@1helm\.com$/i.test(url)) void shell.openExternal(url);
return { action: "deny" };
});
window.webContents.on("will-navigate", (event, url) => {
const gatewayAction = desktopGatewayAction(url);
if (gatewayAction) {
event.preventDefault();
if (gatewayAction.type === "setup") void startServerMode(window);
else void connectRemoteWorkspace(window, gatewayAction.origin);
return;
}
if (allowedTeamUrl(url)) { rememberTeamUrl(url); return; }
if (allowedLocalUrl(url)) return;
event.preventDefault();
// The legacy OpenRouter PKCE flow performs a full-page navigation and
// needs the same Electron session on return so its verifier survives.
if (/^https?:/i.test(url)) openAuthWindow(url);
});
if (showWhenReady) window.once("ready-to-show", () => window.show());
window.on("closed", () => { if (mainWindow === window) mainWindow = null; });
void loadInitialWorkspace(window);
mainWindow = window;
}
if (handleSquirrelEvent()) {
// Squirrel install/update/uninstall work must exit before the application
// acquires its normal single-instance lock or starts the local server.
} else if (!app.requestSingleInstanceLock()) {
app.quit();
} else {
app.on("second-instance", (_event, argv) => {
if (argv.includes("--1helm-background")) return;
if (!mainWindow) createWindow();
if (mainWindow?.isMinimized()) mainWindow.restore();
mainWindow?.show();
mainWindow?.focus();
});
app.whenReady().then(async () => {
if (process.platform === "win32") app.setAppUserModelId("com.squirrel.1Helm.1Helm");
session.defaultSession.setPermissionCheckHandler((webContents, permission, _origin, details) => microphonePermissionAllowed(webContents, permission, details));
session.defaultSession.setPermissionRequestHandler(async (webContents, permission, callback, details) => {
if (!microphonePermissionAllowed(webContents, permission, details)) { callback(false); return; }
if (process.platform !== "darwin") { callback(true); return; }
try { callback(await systemPreferences.askForMediaAccess("microphone")); }
catch { callback(false); }
});
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
if (!allowedLocalUrl(details.url)) {
callback({ responseHeaders: details.responseHeaders });
return;
}
callback({
responseHeaders: {
...details.responseHeaders,
"Content-Security-Policy": [
"default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com data:; img-src 'self' data: blob:; media-src 'self' blob:; connect-src 'self' ws: wss: https:; frame-src 'self' blob:; object-src 'none'; base-uri 'self'; form-action 'self'",
],
},
});
});
try {
removeLegacyWakeLaunchAgent();
hostUpdateService = createNativeUpdateService({ app, autoUpdater });
hostUpdateService.initialize();
globalThis[Symbol.for("1helm.nativeUpdater")] = {
state: hostUpdateService.state,
check: hostUpdateService.check,
install: hostUpdateService.install,
};
process.on("1helm-native-update-ready", () => { hostUpdateService?.commitInstall(); });
const mode = desktopMode();
if (mode === "server" || process.platform === "linux") {
keepSkipperAvailable();
prepareWindowsWslDataRoot();
await startLocalRuntime();
hostUpdateService.schedule();
} else {
stopAutomaticServerStartup();
}
const login = app.getLoginItemSettings({ type: "mainAppService" });
createWindow(!login.wasOpenedAtLogin && !process.argv.includes("--1helm-background"));
} catch (error) {
await dialog.showMessageBox({
type: "error",
title: "1Helm could not start",
message: `The local 1Helm runtime could not start on this ${process.platform === "win32" ? "Windows PC" : "Mac"}.`,
detail: error instanceof Error ? error.stack || error.message : String(error),
});
app.quit();
}
});
app.on("activate", () => { if (!mainWindow) createWindow(); });
app.on("window-all-closed", () => {
// On macOS 1Helm remains the native scheduler/fleet manager until Cmd-Q.
if (process.platform !== "darwin") app.quit();
});
app.on("before-quit", () => {
hostUpdateService?.stop();
if (quitting) return;
quitting = true;
// Explicit Quit is respected; the signed main-app login service starts the
// local control plane hidden again at the next user login.
if (localOrigin) process.emit("SIGTERM", "SIGTERM");
});
}