- config.json: 10 browsers, 10 UA presets, vpsIp 49.128.189.254 - generate.js / server.js: id validation ^b(10|[1-9])$ - install.sh: loops seq 1..10, VPS_IP auto-detected via ifconfig.me, UFW ranges 18081:18090 / 52000:52999 - index.html: grid 5x2, subtitle 10 browser - paste-sync-test: 10 cells, 17/17 pass - README: 10-browser docs, manual add section now covers b11+
120 lines
4.1 KiB
JavaScript
Executable File
120 lines
4.1 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
// generate.js — single source of truth for per-browser Neko files.
|
|
// Usage: node generate.js <browserId> [uaKey] [proxyJson]
|
|
// browserId: b1..b10 (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(10|[1-9])$, 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(10|[1-9])$/.test(id || '')) fail('invalid browser id (want b1..b10)');
|
|
|
|
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();
|