Files
maspend 26427401db Dashboard VPN: per-browser VPN toggle with country picker
- manager/vpn-pool.js: free proxy pool from ProxyScrape JSON (no key),
  live-checked via curl, cached 30 min, grouped by country
- server.js: GET /api/vpn/pool, POST /api/vpn/refresh,
  POST /api/browsers/:id/vpn; vpn state stored in meta.json
- generate.js: 5th arg vpnJson, stored in meta.json
- index.html: VPN section in Settings (toggle + country + refresh),
  VPN badge on cells, manual proxy disabled while VPN is on
- README: VPN section + file list
Verified live on 49.128.189.254: 24 working proxies in 11 countries,
b1 VPN on (PH 180.232.127.250) showed the proxy IP, off restored direct
2026-12-02 14:38:03 +07:00

139 lines
4.9 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] [vpnJson]
// 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"})
// vpnJson: JSON string describing the dashboard VPN state, e.g.
// '{"on":true,"country":"Netherlands","countryCode":"NL",
// "type":"socks5","host":"1.2.3.4","port":1080}'
// (optional, defaults to {"on":false}). Stored in meta.json
// only — it does not change chromium.conf beyond the proxy.
// 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] || '';
const vpnJson = process.argv[5] || '';
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)');
}
// Dashboard VPN state (stored in meta.json only; the proxy above is what
// actually routes the traffic). Kept as given, default off.
let vpn = { on: false };
if (vpnJson) {
try {
vpn = JSON.parse(vpnJson);
} catch (e) {
fail('invalid vpn JSON');
}
if (!vpn || typeof vpn.on !== 'boolean') fail('invalid vpn state (want {on:boolean,...})');
}
// 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,
vpn: vpn,
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();