Browser Farm: grid of 8 isolated Neko browsers with per-browser identity, single login, and sync control

This commit is contained in:
Muse committed 2026-12-01 21:20:41 +07:00
commit 32ac658c70
11 files changed
+2564

No files matched your search

+119
View File
@@ -0,0 +1,119 @@
#!/usr/bin/env node
// generate.js — single source of truth for per-browser Neko files.
// Usage: node generate.js <browserId> [uaKey] [proxyJson]
// browserId: b1..b8 (required)
// uaKey: one of the keys in manager/config.json uaPresets (optional,
// defaults to the browser's configured uaKey)
// proxyJson: JSON string, e.g. '{"type":"direct"}' or
// '{"type":"http","host":"1.2.3.4","port":8080,"user":"","pass":""}'
// (optional, defaults to {"type":"direct"})
// Writes: <FARM_ROOT>/<id>/chromium.conf and <FARM_ROOT>/<id>/meta.json
// FARM_ROOT env var overrides the output root (default /opt/farm).
// Only ever called with validated arguments (browser id ^b[1-8]$, ua key from
// the preset list, proxy validated by the caller). Never passes user input
// to a shell.
'use strict';
const fs = require('fs');
const path = require('path');
const FARM_ROOT = process.env.FARM_ROOT || '/opt/farm';
const BASE = path.join(__dirname, '..'); // repo root: browser-farm/
const CONFIG_PATH = path.join(BASE, 'manager', 'config.json');
const TEMPLATE_PATH = path.join(BASE, 'templates', 'chromium.conf.template');
function fail(msg) {
console.error('generate.js: ' + msg);
process.exit(1);
}
function main() {
const id = process.argv[2];
const uaKeyArg = process.argv[3] || '';
const proxyJson = process.argv[4] || '';
if (!/^b[1-8]$/.test(id || '')) fail('invalid browser id (want b1..b8)');
let cfg;
try {
cfg = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf8'));
} catch (e) {
fail('cannot read config.json: ' + e.message);
}
const browser = (cfg.browsers || []).find((b) => b.id === id);
if (!browser) fail('browser ' + id + ' not in config.json');
const uaKey = uaKeyArg || browser.uaKey;
const preset = (cfg.uaPresets || {})[uaKey];
if (!preset) fail('invalid ua preset: ' + uaKey);
let proxy = { type: 'direct' };
if (proxyJson) {
try {
proxy = JSON.parse(proxyJson);
} catch (e) {
fail('invalid proxy JSON');
}
}
if (!proxy || !['direct', 'http', 'socks5'].includes(proxy.type)) {
fail('invalid proxy type (want direct|http|socks5)');
}
// Build the --proxy-server flag. Direct = no flag at all.
// With user/pass the credentials are embedded in the URL (Chromium uses
// them for proxy auth); they are visible in the container's process list,
// so prefer proxies without auth when possible.
let proxyFlag = '';
let proxyMeta = { type: 'direct' };
if (proxy.type !== 'direct') {
const host = typeof proxy.host === 'string' ? proxy.host.trim() : '';
const port = Number(proxy.port);
if (!host || !Number.isInteger(port) || port < 1 || port > 65535) {
fail('invalid proxy host/port');
}
const user = typeof proxy.user === 'string' ? proxy.user : '';
const pass = typeof proxy.pass === 'string' ? proxy.pass : '';
const auth =
user && pass
? encodeURIComponent(user) + ':' + encodeURIComponent(pass) + '@'
: '';
proxyFlag = ' --proxy-server="' + proxy.type + '://' + auth + host + ':' + port + '"';
proxyMeta = { type: proxy.type, host: host, port: port, user: user, pass: pass };
}
let tpl;
try {
tpl = fs.readFileSync(TEMPLATE_PATH, 'utf8');
} catch (e) {
fail('cannot read chromium.conf.template: ' + e.message);
}
// Function replacers: replacement strings are used literally (no $ handling).
const out = tpl
.replace('__UA__', () => preset.ua)
.replace('__PROXY__', () => proxyFlag);
const dir = path.join(FARM_ROOT, id);
fs.mkdirSync(dir, { recursive: true });
const confPath = path.join(dir, 'chromium.conf');
fs.writeFileSync(confPath, out);
fs.chmodSync(confPath, 0o600);
const meta = {
id: browser.id,
name: browser.name,
uaKey: uaKey,
uaLabel: preset.label,
proxy: proxyMeta,
httpPort: browser.httpPort,
udpStart: browser.udpStart,
udpEnd: browser.udpEnd,
};
const metaPath = path.join(dir, 'meta.json');
fs.writeFileSync(metaPath, JSON.stringify(meta, null, 2) + '\n');
fs.chmodSync(metaPath, 0o600);
console.log('wrote ' + confPath + ' and ' + metaPath);
}
main();