9router/cli/hooks/trayRuntime.js

108 lines
4.2 KiB
JavaScript
Raw Permalink Normal View History

fix(tray): switch macOS/Linux tray to systray2 fork (#1080) The legacy `systray@1.0.5` package (last published 2018) bundles a 2017 x86_64 Go binary whose Mach-O headers are rejected by modern dyld (macOS 14+ / Apple Silicon). The result on affected systems was that `9router --tray` (and "Hide to Tray" from the interactive menu) printed "Router is now running in system tray" but no menubar icon appeared — the failure was silently swallowed by `catch (err) { return null }`. This swaps the runtime tray library for `systray2@2.1.4`, which embeds the maintained getlantern/systray-portable binaries that work on macOS 14+ under Rosetta. Changes: - hooks/trayRuntime.js: install `systray2@2.1.4` (not `systray@1.0.5`) into ~/.9router/runtime/node_modules. Always purge the legacy systray package on every run — its binary is broken on macOS and an AV false positive on Windows. chmod +x the bundled Go binary in case the npm tarball drops the executable bit (observed on macOS). - src/cli/tray/tray.js: resolveSystray() now prefers systray2 with a fallback to legacy systray for safety. initUnixTray() uses the new .ready() promise API, surfaces failures to stderr instead of silently returning null, and sets isTemplateIcon:false so the full-color icon.png renders correctly (template mode would show a solid white square because only the alpha channel is used). killTray() passes false to systray2's kill so it doesn't call process.exit(0) before the rest of cleanup (server SIGKILL, MITM/tunnel) runs. - package.json: update the `comment_systray` field to describe the new package choice. Fixes #1079
2026-05-13 04:30:33 -04:00
// Lazy install systray2 for macOS/Linux into USER_DATA_DIR/runtime/node_modules.
2026-05-12 09:26:08 -04:00
// Windows uses PowerShell NotifyIcon (no binary) → no systray needed.
// This keeps the published npm tarball free of unsigned Go binaries that
// trigger antivirus false positives (e.g. Kaspersky flagging tray_windows.exe).
fix(tray): switch macOS/Linux tray to systray2 fork (#1080) The legacy `systray@1.0.5` package (last published 2018) bundles a 2017 x86_64 Go binary whose Mach-O headers are rejected by modern dyld (macOS 14+ / Apple Silicon). The result on affected systems was that `9router --tray` (and "Hide to Tray" from the interactive menu) printed "Router is now running in system tray" but no menubar icon appeared — the failure was silently swallowed by `catch (err) { return null }`. This swaps the runtime tray library for `systray2@2.1.4`, which embeds the maintained getlantern/systray-portable binaries that work on macOS 14+ under Rosetta. Changes: - hooks/trayRuntime.js: install `systray2@2.1.4` (not `systray@1.0.5`) into ~/.9router/runtime/node_modules. Always purge the legacy systray package on every run — its binary is broken on macOS and an AV false positive on Windows. chmod +x the bundled Go binary in case the npm tarball drops the executable bit (observed on macOS). - src/cli/tray/tray.js: resolveSystray() now prefers systray2 with a fallback to legacy systray for safety. initUnixTray() uses the new .ready() promise API, surfaces failures to stderr instead of silently returning null, and sets isTemplateIcon:false so the full-color icon.png renders correctly (template mode would show a solid white square because only the alpha channel is used). killTray() passes false to systray2's kill so it doesn't call process.exit(0) before the rest of cleanup (server SIGKILL, MITM/tunnel) runs. - package.json: update the `comment_systray` field to describe the new package choice. Fixes #1079
2026-05-13 04:30:33 -04:00
//
// We use the maintained `systray2` fork. The original `systray@1.0.5` package
// bundles a 2017 x86_64 Go binary whose Mach-O headers are rejected by modern
// dyld (macOS 14+), so the tray silently fails to register on Apple Silicon.
2026-05-12 09:26:08 -04:00
const { spawnSync } = require("child_process");
const fs = require("fs");
const path = require("path");
2026-05-16 00:39:39 -04:00
const { getRuntimeDir, getRuntimeNodeModules, runNpmInstall, summarizeNpmError } = require("./sqliteRuntime");
2026-05-12 09:26:08 -04:00
fix(tray): switch macOS/Linux tray to systray2 fork (#1080) The legacy `systray@1.0.5` package (last published 2018) bundles a 2017 x86_64 Go binary whose Mach-O headers are rejected by modern dyld (macOS 14+ / Apple Silicon). The result on affected systems was that `9router --tray` (and "Hide to Tray" from the interactive menu) printed "Router is now running in system tray" but no menubar icon appeared — the failure was silently swallowed by `catch (err) { return null }`. This swaps the runtime tray library for `systray2@2.1.4`, which embeds the maintained getlantern/systray-portable binaries that work on macOS 14+ under Rosetta. Changes: - hooks/trayRuntime.js: install `systray2@2.1.4` (not `systray@1.0.5`) into ~/.9router/runtime/node_modules. Always purge the legacy systray package on every run — its binary is broken on macOS and an AV false positive on Windows. chmod +x the bundled Go binary in case the npm tarball drops the executable bit (observed on macOS). - src/cli/tray/tray.js: resolveSystray() now prefers systray2 with a fallback to legacy systray for safety. initUnixTray() uses the new .ready() promise API, surfaces failures to stderr instead of silently returning null, and sets isTemplateIcon:false so the full-color icon.png renders correctly (template mode would show a solid white square because only the alpha channel is used). killTray() passes false to systray2's kill so it doesn't call process.exit(0) before the rest of cleanup (server SIGKILL, MITM/tunnel) runs. - package.json: update the `comment_systray` field to describe the new package choice. Fixes #1079
2026-05-13 04:30:33 -04:00
const SYSTRAY_PKG = "systray2";
const SYSTRAY_VERSION = "2.1.4";
const LEGACY_SYSTRAY_PKG = "systray";
2026-05-12 09:26:08 -04:00
function hasSystray() {
fix(tray): switch macOS/Linux tray to systray2 fork (#1080) The legacy `systray@1.0.5` package (last published 2018) bundles a 2017 x86_64 Go binary whose Mach-O headers are rejected by modern dyld (macOS 14+ / Apple Silicon). The result on affected systems was that `9router --tray` (and "Hide to Tray" from the interactive menu) printed "Router is now running in system tray" but no menubar icon appeared — the failure was silently swallowed by `catch (err) { return null }`. This swaps the runtime tray library for `systray2@2.1.4`, which embeds the maintained getlantern/systray-portable binaries that work on macOS 14+ under Rosetta. Changes: - hooks/trayRuntime.js: install `systray2@2.1.4` (not `systray@1.0.5`) into ~/.9router/runtime/node_modules. Always purge the legacy systray package on every run — its binary is broken on macOS and an AV false positive on Windows. chmod +x the bundled Go binary in case the npm tarball drops the executable bit (observed on macOS). - src/cli/tray/tray.js: resolveSystray() now prefers systray2 with a fallback to legacy systray for safety. initUnixTray() uses the new .ready() promise API, surfaces failures to stderr instead of silently returning null, and sets isTemplateIcon:false so the full-color icon.png renders correctly (template mode would show a solid white square because only the alpha channel is used). killTray() passes false to systray2's kill so it doesn't call process.exit(0) before the rest of cleanup (server SIGKILL, MITM/tunnel) runs. - package.json: update the `comment_systray` field to describe the new package choice. Fixes #1079
2026-05-13 04:30:33 -04:00
return fs.existsSync(path.join(getRuntimeNodeModules(), SYSTRAY_PKG, "package.json"));
2026-05-12 09:26:08 -04:00
}
fix(tray): switch macOS/Linux tray to systray2 fork (#1080) The legacy `systray@1.0.5` package (last published 2018) bundles a 2017 x86_64 Go binary whose Mach-O headers are rejected by modern dyld (macOS 14+ / Apple Silicon). The result on affected systems was that `9router --tray` (and "Hide to Tray" from the interactive menu) printed "Router is now running in system tray" but no menubar icon appeared — the failure was silently swallowed by `catch (err) { return null }`. This swaps the runtime tray library for `systray2@2.1.4`, which embeds the maintained getlantern/systray-portable binaries that work on macOS 14+ under Rosetta. Changes: - hooks/trayRuntime.js: install `systray2@2.1.4` (not `systray@1.0.5`) into ~/.9router/runtime/node_modules. Always purge the legacy systray package on every run — its binary is broken on macOS and an AV false positive on Windows. chmod +x the bundled Go binary in case the npm tarball drops the executable bit (observed on macOS). - src/cli/tray/tray.js: resolveSystray() now prefers systray2 with a fallback to legacy systray for safety. initUnixTray() uses the new .ready() promise API, surfaces failures to stderr instead of silently returning null, and sets isTemplateIcon:false so the full-color icon.png renders correctly (template mode would show a solid white square because only the alpha channel is used). killTray() passes false to systray2's kill so it doesn't call process.exit(0) before the rest of cleanup (server SIGKILL, MITM/tunnel) runs. - package.json: update the `comment_systray` field to describe the new package choice. Fixes #1079
2026-05-13 04:30:33 -04:00
// Remove the legacy `systray` package from all known locations.
// On Windows it was an AV false-positive risk; on macOS/Linux its bundled
// binary is broken on modern OS versions.
function cleanupLegacySystray({ silent = false } = {}) {
// 1) Runtime dir: ~/.9router/runtime/node_modules/systray (or %APPDATA% on Win)
// 2) npm global nested: <npm_prefix>/node_modules/9router/node_modules/systray
// __dirname here = <pkg root>/hooks → up 1 = pkg root
2026-05-12 09:26:08 -04:00
const targets = [
fix(tray): switch macOS/Linux tray to systray2 fork (#1080) The legacy `systray@1.0.5` package (last published 2018) bundles a 2017 x86_64 Go binary whose Mach-O headers are rejected by modern dyld (macOS 14+ / Apple Silicon). The result on affected systems was that `9router --tray` (and "Hide to Tray" from the interactive menu) printed "Router is now running in system tray" but no menubar icon appeared — the failure was silently swallowed by `catch (err) { return null }`. This swaps the runtime tray library for `systray2@2.1.4`, which embeds the maintained getlantern/systray-portable binaries that work on macOS 14+ under Rosetta. Changes: - hooks/trayRuntime.js: install `systray2@2.1.4` (not `systray@1.0.5`) into ~/.9router/runtime/node_modules. Always purge the legacy systray package on every run — its binary is broken on macOS and an AV false positive on Windows. chmod +x the bundled Go binary in case the npm tarball drops the executable bit (observed on macOS). - src/cli/tray/tray.js: resolveSystray() now prefers systray2 with a fallback to legacy systray for safety. initUnixTray() uses the new .ready() promise API, surfaces failures to stderr instead of silently returning null, and sets isTemplateIcon:false so the full-color icon.png renders correctly (template mode would show a solid white square because only the alpha channel is used). killTray() passes false to systray2's kill so it doesn't call process.exit(0) before the rest of cleanup (server SIGKILL, MITM/tunnel) runs. - package.json: update the `comment_systray` field to describe the new package choice. Fixes #1079
2026-05-13 04:30:33 -04:00
path.join(getRuntimeNodeModules(), LEGACY_SYSTRAY_PKG),
path.join(__dirname, "..", "node_modules", LEGACY_SYSTRAY_PKG)
2026-05-12 09:26:08 -04:00
];
for (const dir of targets) {
if (fs.existsSync(dir)) {
try {
fs.rmSync(dir, { recursive: true, force: true });
if (!silent) console.log(`[9router][runtime] removed legacy systray: ${dir}`);
} catch (e) {
if (!silent) console.warn(`[9router][runtime] failed to remove ${dir}: ${e.message}`);
}
}
}
}
fix(tray): switch macOS/Linux tray to systray2 fork (#1080) The legacy `systray@1.0.5` package (last published 2018) bundles a 2017 x86_64 Go binary whose Mach-O headers are rejected by modern dyld (macOS 14+ / Apple Silicon). The result on affected systems was that `9router --tray` (and "Hide to Tray" from the interactive menu) printed "Router is now running in system tray" but no menubar icon appeared — the failure was silently swallowed by `catch (err) { return null }`. This swaps the runtime tray library for `systray2@2.1.4`, which embeds the maintained getlantern/systray-portable binaries that work on macOS 14+ under Rosetta. Changes: - hooks/trayRuntime.js: install `systray2@2.1.4` (not `systray@1.0.5`) into ~/.9router/runtime/node_modules. Always purge the legacy systray package on every run — its binary is broken on macOS and an AV false positive on Windows. chmod +x the bundled Go binary in case the npm tarball drops the executable bit (observed on macOS). - src/cli/tray/tray.js: resolveSystray() now prefers systray2 with a fallback to legacy systray for safety. initUnixTray() uses the new .ready() promise API, surfaces failures to stderr instead of silently returning null, and sets isTemplateIcon:false so the full-color icon.png renders correctly (template mode would show a solid white square because only the alpha channel is used). killTray() passes false to systray2's kill so it doesn't call process.exit(0) before the rest of cleanup (server SIGKILL, MITM/tunnel) runs. - package.json: update the `comment_systray` field to describe the new package choice. Fixes #1079
2026-05-13 04:30:33 -04:00
// systray2's npm tarball sometimes ships the bundled Go binary without the
// executable bit set on macOS, causing spawn() to fail with EACCES. Set +x
// best-effort so the tray actually starts.
function chmodSystrayBin({ silent = false } = {}) {
if (process.platform === "win32") return;
const binName = process.platform === "darwin" ? "tray_darwin_release" : "tray_linux_release";
const binPath = path.join(getRuntimeNodeModules(), SYSTRAY_PKG, "traybin", binName);
if (!fs.existsSync(binPath)) return;
try {
fs.chmodSync(binPath, 0o755);
} catch (e) {
if (!silent) console.warn(`[9router][runtime] chmod tray bin failed: ${e.message}`);
}
}
2026-05-12 09:26:08 -04:00
function ensureRuntimeDir() {
const dir = getRuntimeDir();
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
const pkgPath = path.join(dir, "package.json");
if (!fs.existsSync(pkgPath)) {
fs.writeFileSync(pkgPath, JSON.stringify({
name: "9router-runtime",
version: "1.0.0",
private: true
}, null, 2));
}
return dir;
}
function npmInstall(pkgs, { silent = false } = {}) {
const cwd = ensureRuntimeDir();
2026-05-16 00:39:39 -04:00
if (!silent) console.log("⏳ Installing system tray (first run)...");
const res = runNpmInstall({ cwd, pkgs, extraArgs: ["--no-save"], timeout: 120000 });
if (!res.ok && !silent) {
const reason = summarizeNpmError(res.stderr);
console.warn("⚠️ System tray install failed — tray disabled");
console.warn(` Reason: ${reason}`);
console.warn(` Retry: cd "${cwd}" && npm install ${pkgs.join(" ")}`);
}
return res.ok;
2026-05-12 09:26:08 -04:00
}
fix(tray): switch macOS/Linux tray to systray2 fork (#1080) The legacy `systray@1.0.5` package (last published 2018) bundles a 2017 x86_64 Go binary whose Mach-O headers are rejected by modern dyld (macOS 14+ / Apple Silicon). The result on affected systems was that `9router --tray` (and "Hide to Tray" from the interactive menu) printed "Router is now running in system tray" but no menubar icon appeared — the failure was silently swallowed by `catch (err) { return null }`. This swaps the runtime tray library for `systray2@2.1.4`, which embeds the maintained getlantern/systray-portable binaries that work on macOS 14+ under Rosetta. Changes: - hooks/trayRuntime.js: install `systray2@2.1.4` (not `systray@1.0.5`) into ~/.9router/runtime/node_modules. Always purge the legacy systray package on every run — its binary is broken on macOS and an AV false positive on Windows. chmod +x the bundled Go binary in case the npm tarball drops the executable bit (observed on macOS). - src/cli/tray/tray.js: resolveSystray() now prefers systray2 with a fallback to legacy systray for safety. initUnixTray() uses the new .ready() promise API, surfaces failures to stderr instead of silently returning null, and sets isTemplateIcon:false so the full-color icon.png renders correctly (template mode would show a solid white square because only the alpha channel is used). killTray() passes false to systray2's kill so it doesn't call process.exit(0) before the rest of cleanup (server SIGKILL, MITM/tunnel) runs. - package.json: update the `comment_systray` field to describe the new package choice. Fixes #1079
2026-05-13 04:30:33 -04:00
// Public: ensure systray2 is installed on macOS/Linux only.
2026-05-12 09:26:08 -04:00
// Windows skips entirely (uses PowerShell tray).
function ensureTrayRuntime({ silent = false } = {}) {
fix(tray): switch macOS/Linux tray to systray2 fork (#1080) The legacy `systray@1.0.5` package (last published 2018) bundles a 2017 x86_64 Go binary whose Mach-O headers are rejected by modern dyld (macOS 14+ / Apple Silicon). The result on affected systems was that `9router --tray` (and "Hide to Tray" from the interactive menu) printed "Router is now running in system tray" but no menubar icon appeared — the failure was silently swallowed by `catch (err) { return null }`. This swaps the runtime tray library for `systray2@2.1.4`, which embeds the maintained getlantern/systray-portable binaries that work on macOS 14+ under Rosetta. Changes: - hooks/trayRuntime.js: install `systray2@2.1.4` (not `systray@1.0.5`) into ~/.9router/runtime/node_modules. Always purge the legacy systray package on every run — its binary is broken on macOS and an AV false positive on Windows. chmod +x the bundled Go binary in case the npm tarball drops the executable bit (observed on macOS). - src/cli/tray/tray.js: resolveSystray() now prefers systray2 with a fallback to legacy systray for safety. initUnixTray() uses the new .ready() promise API, surfaces failures to stderr instead of silently returning null, and sets isTemplateIcon:false so the full-color icon.png renders correctly (template mode would show a solid white square because only the alpha channel is used). killTray() passes false to systray2's kill so it doesn't call process.exit(0) before the rest of cleanup (server SIGKILL, MITM/tunnel) runs. - package.json: update the `comment_systray` field to describe the new package choice. Fixes #1079
2026-05-13 04:30:33 -04:00
// Always evict the legacy `systray` package — its binary is broken on
// modern macOS and an AV false-positive on Windows.
cleanupLegacySystray({ silent });
2026-05-12 09:26:08 -04:00
if (process.platform === "win32") {
return { systray: false, skipped: true };
}
if (hasSystray()) {
fix(tray): switch macOS/Linux tray to systray2 fork (#1080) The legacy `systray@1.0.5` package (last published 2018) bundles a 2017 x86_64 Go binary whose Mach-O headers are rejected by modern dyld (macOS 14+ / Apple Silicon). The result on affected systems was that `9router --tray` (and "Hide to Tray" from the interactive menu) printed "Router is now running in system tray" but no menubar icon appeared — the failure was silently swallowed by `catch (err) { return null }`. This swaps the runtime tray library for `systray2@2.1.4`, which embeds the maintained getlantern/systray-portable binaries that work on macOS 14+ under Rosetta. Changes: - hooks/trayRuntime.js: install `systray2@2.1.4` (not `systray@1.0.5`) into ~/.9router/runtime/node_modules. Always purge the legacy systray package on every run — its binary is broken on macOS and an AV false positive on Windows. chmod +x the bundled Go binary in case the npm tarball drops the executable bit (observed on macOS). - src/cli/tray/tray.js: resolveSystray() now prefers systray2 with a fallback to legacy systray for safety. initUnixTray() uses the new .ready() promise API, surfaces failures to stderr instead of silently returning null, and sets isTemplateIcon:false so the full-color icon.png renders correctly (template mode would show a solid white square because only the alpha channel is used). killTray() passes false to systray2's kill so it doesn't call process.exit(0) before the rest of cleanup (server SIGKILL, MITM/tunnel) runs. - package.json: update the `comment_systray` field to describe the new package choice. Fixes #1079
2026-05-13 04:30:33 -04:00
chmodSystrayBin({ silent });
2026-05-16 00:39:39 -04:00
if (!silent) console.log("✅ System tray ready");
2026-05-12 09:26:08 -04:00
return { systray: true };
}
fix(tray): switch macOS/Linux tray to systray2 fork (#1080) The legacy `systray@1.0.5` package (last published 2018) bundles a 2017 x86_64 Go binary whose Mach-O headers are rejected by modern dyld (macOS 14+ / Apple Silicon). The result on affected systems was that `9router --tray` (and "Hide to Tray" from the interactive menu) printed "Router is now running in system tray" but no menubar icon appeared — the failure was silently swallowed by `catch (err) { return null }`. This swaps the runtime tray library for `systray2@2.1.4`, which embeds the maintained getlantern/systray-portable binaries that work on macOS 14+ under Rosetta. Changes: - hooks/trayRuntime.js: install `systray2@2.1.4` (not `systray@1.0.5`) into ~/.9router/runtime/node_modules. Always purge the legacy systray package on every run — its binary is broken on macOS and an AV false positive on Windows. chmod +x the bundled Go binary in case the npm tarball drops the executable bit (observed on macOS). - src/cli/tray/tray.js: resolveSystray() now prefers systray2 with a fallback to legacy systray for safety. initUnixTray() uses the new .ready() promise API, surfaces failures to stderr instead of silently returning null, and sets isTemplateIcon:false so the full-color icon.png renders correctly (template mode would show a solid white square because only the alpha channel is used). killTray() passes false to systray2's kill so it doesn't call process.exit(0) before the rest of cleanup (server SIGKILL, MITM/tunnel) runs. - package.json: update the `comment_systray` field to describe the new package choice. Fixes #1079
2026-05-13 04:30:33 -04:00
const ok = npmInstall([`${SYSTRAY_PKG}@${SYSTRAY_VERSION}`], { silent });
if (ok) chmodSystrayBin({ silent });
2026-05-12 09:26:08 -04:00
return { systray: ok && hasSystray() };
}
module.exports = { ensureTrayRuntime };