diff options
Diffstat (limited to 'packages/meshbay-client/src/main.js')
| -rw-r--r-- | packages/meshbay-client/src/main.js | 106 |
1 files changed, 105 insertions, 1 deletions
diff --git a/packages/meshbay-client/src/main.js b/packages/meshbay-client/src/main.js index 9c27cfe..d341619 100644 --- a/packages/meshbay-client/src/main.js +++ b/packages/meshbay-client/src/main.js @@ -804,7 +804,59 @@ function registerBridge() { }); } + // ── Windows: the Startup-folder launcher that stands in for the systemd unit ── + // A logon-triggered Task Scheduler task needs elevation to create, which an + // ordinary user does not have, so autostart is a `.vbs` in the per-user + // Startup folder instead: wscript runs it hidden at every sign-in — no admin, + // no console window. Kept in step with meshbay_node.platform._startup_vbs(). + const WIN_STARTUP_VBS = path.join( + app.getPath('appData'), 'Microsoft', 'Windows', 'Start Menu', 'Programs', + 'Startup', 'MeshBay Node.vbs'); + + function winAutostartInstalled() { + try { return fs.existsSync(WIN_STARTUP_VBS); } catch { return false; } + } + + function winAutostartInstall(bin) { + fs.mkdirSync(path.dirname(WIN_STARTUP_VBS), { recursive: true }); + // Chr(34) is a literal " — wraps the path so a space in it doesn't split + // the command. 0 = hidden window, False = don't wait. Kept in step with + // meshbay_node.platform.autostart_install(). + fs.writeFileSync(WIN_STARTUP_VBS, + `CreateObject("WScript.Shell").Run Chr(34) & "${bin}" & Chr(34), 0, False\r\n`); + } + + function winAutostartRemove() { + try { fs.rmSync(WIN_STARTUP_VBS, { force: true }); } catch { /* not there */ } + } + + function killNodeProcesses() { + return new Promise((resolve) => { + execFile('taskkill', ['/IM', 'meshbay-node.exe', '/F'], () => resolve()); + }); + } + + async function spawnNodeDetached() { + const bin = await findNodeBinary(); + if (!bin) throw new Error('meshbay-node not found on PATH'); + const child = spawn(bin, [], { detached: true, stdio: 'ignore', windowsHide: true }); + child.unref(); + } + + async function waitForNode(deadline) { + while (Date.now() < deadline) { + const p = await probeNode(); + if (p) return p; + await new Promise((r) => setTimeout(r, 800)); + } + return null; + } + ipcMain.handle('node:installed', async () => { + if (process.platform === 'win32') { + const bin = await findNodeBinary(); + return { installed: Boolean(bin), autostart: winAutostartInstalled() }; + } if (process.platform !== 'linux') return { installed: false }; const unit = await new Promise((resolve) => { execFile('systemctl', ['--user', 'show', 'meshbay-node.service', @@ -823,6 +875,16 @@ function registerBridge() { // own HTTP API, which cannot answer while the daemon is stopped or crash- // looping — exactly the states this panel exists to show and act on. ipcMain.handle('node:service-status', async () => { + if (process.platform === 'win32') { + // No Task Scheduler to ask "is it running" — probe the daemon itself. + const p = await probeNode(); + return { + supported: true, + installed: winAutostartInstalled(), + activeState: p ? 'active' : 'inactive', + subState: p ? 'running' : '', + }; + } if (process.platform !== 'linux') return { supported: false }; return new Promise((resolve) => { execFile('systemctl', ['--user', 'show', 'meshbay-node.service', @@ -848,6 +910,10 @@ function registerBridge() { }); ipcMain.handle('node:service-stop', async () => { + if (process.platform === 'win32') { + await killNodeProcesses(); // hard kill — no CTRL_CLOSE handler yet + return { stopped: true }; + } if (process.platform !== 'linux') { throw new Error('Service control is only supported on Linux'); } @@ -862,6 +928,13 @@ function registerBridge() { }); ipcMain.handle('node:service-restart', async () => { + if (process.platform === 'win32') { + await killNodeProcesses(); + await spawnNodeDetached(); + const p = await waitForNode(Date.now() + 30000); + if (!p) throw new Error('node did not come back up within 30s'); + return { restarted: true, ...p }; + } if (process.platform !== 'linux') { throw new Error('Service control is only supported on Linux'); } @@ -875,6 +948,22 @@ function registerBridge() { return { restarted: true }; }); + // Install / remove the Windows Startup-folder launcher, and query it. + ipcMain.handle('node:autostart', async (_e, action) => { + if (process.platform !== 'win32') return { supported: false }; + if (action === 'install') { + const bin = await findNodeBinary(); + if (!bin) throw new Error('meshbay-node not found on PATH'); + winAutostartInstall(bin); + return { supported: true, installed: true }; + } + if (action === 'remove') { + winAutostartRemove(); + return { supported: true, installed: false }; + } + return { supported: true, installed: winAutostartInstalled() }; + }); + async function probeNode() { const nc = readNodeConfig(); const dataDir = nc ? nc.dataDir : meshbayDataDir(); @@ -895,6 +984,11 @@ function registerBridge() { } catch { return null; } } + // A path inside a TOML basic string: forward slashes only. A raw Windows + // path there (`C:\Users\...`) is a parse error — `\U`, `\a`, ... are escape + // sequences. pathlib on the node reads the `/` form fine. + const tomlPath = (p) => p.split(path.sep).join('/'); + function provisionNode(hubUrl, username) { const configDir = meshbayConfigDir(); const dataDir = meshbayDataDir(); @@ -920,7 +1014,7 @@ function registerBridge() { 'ui_port = 18000', '', '[keystore]', - `unlock_file = "${path.join(configDir, 'unlock.key')}"`, + `unlock_file = "${tomlPath(path.join(configDir, 'unlock.key'))}"`, '', ].join('\n'); fs.writeFileSync(configFile, toml, { mode: 0o600 }); @@ -939,6 +1033,16 @@ function registerBridge() { return { started: true, ...already }; } + if (process.platform === 'win32') { + if (opts && opts.hubUrl && opts.username) provisionNode(opts.hubUrl, opts.username); + await killNodeProcesses(); // clear a crash-looping one + await spawnNodeDetached(); + const p = await waitForNode(Date.now() + 60000); + if (!p) throw new Error('the node did not start within 60s — run it from a ' + + 'terminal (`meshbay-node`) to see why'); + return { started: true, ...p }; + } + if (process.platform !== 'linux') { throw new Error('Automatic node start is only supported on Linux'); } |