Each host build pins one engine version in src-tauri/browser-engine.json. The new engine manager downloads that ZIP over HTTPS, makes sure that its size and SHA-256 match the pin, extracts it safely to the local app data folder, and marks the installation last. It can also install a local ZIP, cancel, report progress, and remove the engine. The host refuses an engine that sends another protocol with its ready event.
65 lines
3.4 KiB
JavaScript
65 lines
3.4 KiB
JavaScript
import { app } from 'electron';
|
|
import path from 'node:path';
|
|
import net from 'node:net';
|
|
import { createEngine } from './engine.mjs';
|
|
for (const flag of ['no-sandbox', 'disable-web-security', 'ignore-certificate-errors', 'remote-debugging-port', 'remote-debugging-pipe']) {
|
|
if (app.commandLine.hasSwitch(flag)) throw new Error('Unsafe browser launch option.');
|
|
}
|
|
app.enableSandbox();
|
|
app.commandLine.appendSwitch('disable-quic');
|
|
app.commandLine.appendSwitch('disable-background-networking');
|
|
app.commandLine.appendSwitch('host-resolver-rules', 'MAP * ~NOTFOUND, EXCLUDE 127.0.0.1');
|
|
app.commandLine.appendSwitch('force-webrtc-ip-handling-policy', 'disable_non_proxied_udp');
|
|
app.on('window-all-closed', () => {});
|
|
app.on('certificate-error', (event, _wc, _url, _error, _cert, done) => { event.preventDefault(); done(false); });
|
|
app.on('select-client-certificate', (event, _wc, _url, _certs, done) => { event.preventDefault(); done(); });
|
|
const root = app.commandLine.getSwitchValue('rs-browser-root');
|
|
const port = Number(app.commandLine.getSwitchValue('rs-connect'));
|
|
const token = process.env.RUNESTONE_BROWSER_TOKEN || '';
|
|
delete process.env.RUNESTONE_BROWSER_TOKEN;
|
|
if (!path.isAbsolute(root) || !Number.isInteger(port) || port < 1024 || port > 65535 || token.length < 32) app.exit(1);
|
|
else {
|
|
app.setPath('userData', root);
|
|
app.whenReady().then(async () => {
|
|
let writable = true;
|
|
const socket = net.createConnection({ host: '127.0.0.1', port });
|
|
socket.setEncoding('utf8'); socket.setNoDelay(true);
|
|
socket.on('error', () => app.exit(1));
|
|
const connected = new Promise(resolve => socket.once('connect', resolve));
|
|
const emit = value => {
|
|
if (socket.destroyed || (value.event === 'frame' && !writable)) return;
|
|
writable = socket.write(JSON.stringify(value) + '\n');
|
|
};
|
|
socket.on('drain', () => { writable = true; });
|
|
await connected;
|
|
socket.write(JSON.stringify({ token }) + '\n');
|
|
const engine = await createEngine({ directory: root, emit, canEmitFrame: () => writable && !socket.destroyed });
|
|
let buffer = '', queued = 0, queue = Promise.resolve();
|
|
socket.on('close', () => { void engine.shutdown().finally(() => app.exit(0)); });
|
|
socket.on('data', chunk => {
|
|
buffer += chunk;
|
|
if (buffer.length > 6_000_000) { socket.destroy(); return; }
|
|
let end;
|
|
while ((end = buffer.indexOf('\n')) >= 0) {
|
|
const line = buffer.slice(0, end); buffer = buffer.slice(end + 1);
|
|
if (++queued > 128) { socket.destroy(); return; }
|
|
queue = queue.then(async () => {
|
|
let message;
|
|
try {
|
|
message = JSON.parse(line);
|
|
if (!Number.isSafeInteger(message.id) || message.id < 1) throw new Error('Invalid request.');
|
|
if (message.command === 'shutdown') {
|
|
await engine.shutdown();
|
|
socket.write(JSON.stringify({ id: message.id, result: null }) + '\n', () => app.exit(0)); return;
|
|
}
|
|
emit({ id: message.id, result: await engine.command(message.command, message.args) });
|
|
} catch (error) { emit({ id: message?.id, error: error.message }); }
|
|
finally { queued--; }
|
|
});
|
|
}
|
|
});
|
|
// The host refuses an engine with another protocol. Keep PROTOCOL in src-tauri/src/browser_engine.rs the same.
|
|
emit({ event: 'ready', protocol: 1, version: app.getVersion() });
|
|
}).catch(error => { console.error(error); app.exit(1); });
|
|
}
|