Files

131 lines
6.7 KiB
JavaScript

#!/usr/bin/env node
/* Edge Kit deploy check — run me first: node setup.mjs
* Auto-detects the environment (Windows / Linux / macOS) for all supported
* browsers, verifies everything the kit needs, and prints exact next steps.
* The kit's proxy is fully built in: nothing here — and nothing the kit does —
* ever depends on a system/v2ray proxy. Node fetch ignores proxy env vars, so
* the egress check below is always direct.
* Exit code 0 = ready. */
import fs from 'node:fs/promises';
import os from 'node:os';
import net from 'node:net';
import path from 'node:path';
import { execSync } from 'node:child_process';
const P = process.platform;
const HOME = os.homedir();
let failures = 0;
const ok = (s) => console.log(' [ok] ' + s);
const warn = (s) => console.log(' [warn] ' + s);
const fail = (s) => { console.log(' [FAIL] ' + s); failures++; };
console.log('Edge Kit environment check — ' + P + ' ' + os.arch() + ', node ' + process.version + '\n');
/* 1. Node version (kit uses fetch/AbortSignal/WS built-ins) */
const maj = Number(process.versions.node.split('.')[0]);
if (maj >= 18) ok(`node ${process.version}`);
else fail(`node ${process.version} — Edge Kit needs >= 18 (fetch/AbortSignal)`);
/* 2. Browsers (same discovery table as the kit itself) */
const W = P === 'win32';
const CAND = {
edge: W
? ['C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe', 'C:\\Program Files\\Microsoft\\Edge\\Application\\msedge.exe']
: P === 'darwin'
? ['/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge']
: ['/opt/microsoft/msedge/msedge', '/usr/bin/microsoft-edge', '/usr/bin/microsoft-edge-stable', '/opt/edge/msedge'],
chrome: W
? ['C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe', 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe']
: P === 'darwin'
? ['/Applications/Google Chrome.app/Contents/MacOS/Google Chrome']
: ['/opt/google/chrome/chrome', '/usr/bin/google-chrome', '/usr/bin/google-chrome-stable', '/usr/bin/chromium', '/usr/bin/chromium-browser', '/snap/bin/chromium'],
firefox: W
? ['C:\\Program Files\\Mozilla Firefox\\firefox.exe', 'C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe']
: P === 'darwin'
? ['/Applications/Firefox.app/Contents/MacOS/firefox']
: ['/usr/lib/firefox/firefox', '/usr/bin/firefox', '/snap/bin/firefox'],
};
const found = {};
for (const [kind, list] of Object.entries(CAND)) {
for (const p of list) {
try { await fs.access(p); (found[kind] ||= []).push(p); break; } catch { /* next */ }
}
}
for (const kind of ['edge', 'chrome', 'firefox']) {
if (found[kind]) ok(`${kind}: ${found[kind][0]}`);
else warn(`${kind}: not found — install it (or pass executablePath per call)`);
}
if (!Object.keys(found).length) fail('no supported browser found — the kit needs at least one of edge/chrome/firefox');
/* 3. Kit profile dir writable (fresh launch profiles live here) */
const kitDir = path.join(HOME, '.dsh', 'browser-profiles', 'edge-kit');
try {
await fs.mkdir(kitDir, { recursive: true });
await fs.writeFile(path.join(kitDir, '.setup-probe'), 'x');
await fs.rm(path.join(kitDir, '.setup-probe'));
ok('profile dir writable: ' + kitDir);
} catch (e) { fail(`cannot write ${kitDir}: ${e.message}`); }
/* 4. DevTools ports */
for (const port of [9444, 9445, 9446]) {
const free = await new Promise((r) => {
const s = net.createServer();
s.once('error', () => r(false));
s.listen(port, '127.0.0.1', () => s.close(() => r(true)));
});
if (free) ok(`debug port ${port} free`);
else warn(`debug port ${port} busy — a browser already holds it; the kit will attach to it (relaunch:true takes over)`);
}
/* 4b. Edge startup-boost survivor background processes re-spawn with stale
* command lines and silently swallow the kit's proxy flag on relaunch.
* The enterprise policy kills them for good. */
if (P === 'win32') {
try {
execSync('reg query "HKCU\\Software\\Policies\\Microsoft\\Edge" /v StartupBoostEnabled', { stdio: 'pipe' });
ok('Edge startup-boost already governed by policy');
} catch {
try {
execSync('reg add "HKCU\\Software\\Policies\\Microsoft\\Edge" /v StartupBoostEnabled /t REG_DWORD /d 0 /f', { stdio: 'pipe' });
ok('disabled Edge startup boost (policy) — survivor processes can no longer drop proxy flags');
} catch { warn('could not write Edge StartupBoostEnabled policy — restarts may race a background Edge'); }
}
}
/* 5. Direct egress — proves there is an internet path WITHOUT any system/v2ray
* proxy (node fetch never reads HTTP_PROXY). Geo-restricted hosts (Iran)
* often have no open HTTP echo without VPN but can reach proxies directly
* over raw TCP — which is all the kit ever needs — so fall back to that. */
let egress = null;
for (const url of ['http://checkip.amazonaws.com/', 'https://api.ipify.org']) {
try { const r = await fetch(url, { signal: AbortSignal.timeout(7000) }); egress = (await r.text()).trim().split(/\s/)[0]; break; } catch { /* next echo */ }
}
if (egress) ok(`direct egress: ${egress} (no system proxy involved)`);
else {
let tcpOk = false;
for (const [host, port] of [['34.43.46.91', 80], ['1.1.1.1', 443], ['8.8.8.8', 53]]) {
tcpOk = await new Promise((r) => {
const s = net.connect({ host, port });
const t = setTimeout(() => { s.destroy(); r(false); }, 6000);
s.once('connect', () => { clearTimeout(t); s.destroy(); r(true); });
s.once('error', () => { clearTimeout(t); r(false); });
});
if (tcpOk) { warn(`no open HTTP echo (geo-blocked without VPN?) — raw TCP to ${host}:${port} works, which is all the kit needs: it routes through verified proxies`); break; }
}
if (!tcpOk) fail('no usable internet path at all (no HTTP echo, no raw TCP) — fix the network before installing');
}
const sysProxy = process.env.HTTP_PROXY || process.env.http_proxy || process.env.HTTPS_PROXY || process.env.https_proxy;
if (sysProxy) warn(`shell proxy var present (${sysProxy}) — ignored by design: the kit pins browser proxy state itself`);
/* 6. Integration summary */
console.log(`
${failures ? `${failures} blocking issue(s) above — fix and re-run.` : 'Ready to integrate.'}
1. Install the kit into your agent host: point its plugin manager at this folder
(e.g. {action:"install_bundle", target:"${process.cwd()}"}), or add to the host's
composition: "plugin: { kit: { path: '${process.cwd()}' } }".
2. The web seam (web_fetch/web_search) is pinned to the kit via cordis.patch.yml —
the host applies it automatically; the kit lazily launches a browser on first use.
3. First call: {action:"status"}; browse: {action:"goto", url:"https://…"}, {action:"see"}.
4. Optional verified routing: {action:"proxy", op:"auto", proxyChain:"ip:port,…"}.`);
process.exitCode = failures ? 1 : 0;