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();
+227
View File
@@ -0,0 +1,227 @@
#!/usr/bin/env bash
# Browser Farm installer — run as root on the target VPS (Ubuntu 24.04).
# Usage: copy the whole browser-farm folder to the VPS, then:
# bash scripts/install.sh
# Idempotent: safe to run twice — it stops/removes the old farm-b* containers,
# regenerates every per-browser file, reinstalls the manager + nginx vhost,
# and recreates the containers.
set -euo pipefail
# ---- settings (change FARM_PASSWORD before first run if you want) ----
FARM_ROOT="/opt/farm"
APP_DIR="/opt/browser-farm"
FARM_PASSWORD="FarmBrowser2026"
NEKO_IMAGE="m1k1o/neko:chromium"
VPS_IP="49.128.191.144"
# ----------------------------------------------------------------------
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# Guard: the installer deletes $APP_DIR and copies the repo into it.
# If the repo IS $APP_DIR, that would delete the source first.
# In that case, copy the repo to a temp dir and re-run from there.
if [ "$REPO_DIR" = "$APP_DIR" ]; then
TMP_SRC="$(mktemp -d)/browser-farm"
cp -r "$REPO_DIR" "$TMP_SRC"
echo "==> Repo is inside $APP_DIR — re-running from $TMP_SRC ..."
exec bash "$TMP_SRC/scripts/install.sh"
fi
if [ "$(id -u)" -ne 0 ]; then
echo "ERROR: jalankan sebagai root (sudo bash scripts/install.sh)" >&2
exit 1
fi
echo "==> Browser Farm installer (repo: $REPO_DIR)"
# ---- dependencies: docker, node, npm, nginx ----
NEED_APT=0
command -v docker >/dev/null 2>&1 || NEED_APT=1
command -v node >/dev/null 2>&1 || NEED_APT=1
command -v npm >/dev/null 2>&1 || NEED_APT=1
command -v nginx >/dev/null 2>&1 || NEED_APT=1
if [ "$NEED_APT" -eq 1 ]; then
echo "==> Installing missing packages (docker/node/nginx)..."
apt-get update -qq
command -v docker >/dev/null 2>&1 || apt-get install -y -qq docker.io
command -v node >/dev/null 2>&1 || apt-get install -y -qq nodejs npm
command -v nginx >/dev/null 2>&1 || apt-get install -y -qq nginx
fi
systemctl enable --now docker >/dev/null 2>&1 || true
NODE_BIN="$(command -v node)"
echo "==> node: $NODE_BIN ($("$NODE_BIN" --version)), docker: $(docker --version)"
# ---- stop / remove any existing farm containers (idempotency) ----
echo "==> Removing old farm-b* containers (if any)..."
for n in 1 2 3 4 5 6 7 8; do
docker rm -f "farm-b$n" >/dev/null 2>&1 || true
done
# ---- per-browser files: profile dir, policies.json, chromium.conf, meta.json ----
echo "==> Generating per-browser files under $FARM_ROOT ..."
mkdir -p "$FARM_ROOT"
for n in 1 2 3 4 5 6 7 8; do
id="b$n"
mkdir -p "$FARM_ROOT/$id/profile"
cp "$REPO_DIR/templates/policies.json" "$FARM_ROOT/$id/policies.json"
# generate.js defaults: uaKey from config.json, proxy direct.
FARM_ROOT="$FARM_ROOT" "$NODE_BIN" "$REPO_DIR/scripts/generate.js" "$id"
done
# ---- pull Neko image ----
echo "==> Pulling $NEKO_IMAGE ..."
docker pull "$NEKO_IMAGE" >/dev/null
# ---- create the 8 browser containers ----
echo "==> Creating farm-b1..farm-b8 ..."
for n in 1 2 3 4 5 6 7 8; do
id="b$n"
http_port=$((18080 + n))
udp_start=$((52000 + (n - 1) * 100))
udp_end=$((udp_start + 99))
docker run -d --name "farm-$id" \
--restart unless-stopped --stop-timeout 60 --shm-size=2gb \
-p "$http_port:8080" \
-p "$udp_start-$udp_end:$udp_start-$udp_end/udp" \
-v "$FARM_ROOT/$id/profile:/home/neko/.config/chromium" \
-v "$FARM_ROOT/$id/policies.json:/etc/chromium/policies/managed/policies.json:ro" \
-v "$FARM_ROOT/$id/chromium.conf:/etc/neko/supervisord/chromium.conf:ro" \
-e NEKO_SCREEN=1280x720@30 \
-e "NEKO_PASSWORD=$FARM_PASSWORD" \
-e "NEKO_PASSWORD_ADMIN=$FARM_PASSWORD" \
-e "NEKO_NAT1TO1=$VPS_IP" \
-e "NEKO_EPR=$udp_start-$udp_end" \
-e NEKO_BIND=:8080 \
-e NEKO_SERVER_BIND=:8080 \
"$NEKO_IMAGE" >/dev/null
echo " farm-$id -> http :$http_port, udp $udp_start-$udp_end"
done
# ---- install the manager (server + UI + generator + templates) ----
echo "==> Installing manager to $APP_DIR ..."
systemctl stop farm-manager >/dev/null 2>&1 || true
rm -rf "$APP_DIR"
mkdir -p "$APP_DIR"
cp -r "$REPO_DIR/manager" "$REPO_DIR/scripts" "$REPO_DIR/templates" "$APP_DIR/"
rm -rf "$APP_DIR/manager/node_modules"
cd "$APP_DIR/manager"
npm install --production --no-audit --no-fund 2>&1 | tail -2
cat > /etc/systemd/system/farm-manager.service <<EOF
[Unit]
Description=Browser Farm manager
After=network.target docker.service
Requires=docker.service
[Service]
Type=simple
WorkingDirectory=$APP_DIR/manager
Environment=FARM_ROOT=$FARM_ROOT
Environment=PORT=18080
Environment=HOST=127.0.0.1
Environment=FARM_PASSWORD=$FARM_PASSWORD
ExecStart=$NODE_BIN $APP_DIR/manager/server.js
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now farm-manager
# ---- nginx vhost (grid on port 80) ----
# The grid (manager) is at /. Each Neko instance is ALSO reachable
# same-origin under /b1/ .. /b8/ so the grid page can script into the
# Neko iframes (Sync control needs same-origin access). The trailing
# slash on proxy_pass strips the /bN/ prefix, so /b1/api/ws reaches the
# Neko server as /api/ws. Host is forwarded unchanged so the Neko
# server's same-origin WebSocket check (Origin host == Host) passes.
# Direct ports 18081..18088 keep working as a fallback.
echo "==> Installing nginx vhost ..."
cat > /etc/nginx/sites-available/farm.conf <<'EOF'
server {
listen 80 default_server;
server_name _;
location / {
proxy_pass http://127.0.0.1:18080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
}
EOF
for n in 1 2 3 4 5 6 7 8; do
http_port=$((18080 + n))
cat >> /etc/nginx/sites-available/farm.conf <<EOF
location /b$n/ {
proxy_pass http://127.0.0.1:$http_port/;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
}
EOF
done
cat >> /etc/nginx/sites-available/farm.conf <<'EOF'
}
EOF
ln -sf /etc/nginx/sites-available/farm.conf /etc/nginx/sites-enabled/farm.conf
# The stock nginx default site would win over farm.conf for unmatched
# hosts (it is the default_server); remove it so the grid is served.
rm -f /etc/nginx/sites-enabled/default
nginx -t
systemctl reload nginx
# ---- firewall (only if UFW is active) ----
if command -v ufw >/dev/null 2>&1 && ufw status 2>/dev/null | grep -q "Status: active"; then
echo "==> UFW aktif — membuka port farm..."
ufw allow 80/tcp >/dev/null
ufw allow 18081:18088/tcp >/dev/null
ufw allow 52000:52799/udp >/dev/null
fi
# ---- verify ----
echo "==> Verifying..."
sleep 8
echo "--- containers ---"
docker ps --filter "name=farm-b" --format "{{.Names}} {{.Status}}"
echo "--- manager ---"
systemctl is-active farm-manager
curl -s --max-time 10 http://127.0.0.1:18080/health; echo
echo "--- neko http ports ---"
for n in 1 2 3 4 5 6 7 8; do
http_port=$((18080 + n))
code="$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "http://127.0.0.1:$http_port/" || echo FAIL)"
echo " farm-b$n :$http_port -> $code"
done
cat <<EOF
===============================================================
Browser Farm LIVE
Grid URL : http://$VPS_IP/
Manager : http://127.0.0.1:18080/health (di VPS)
Browsers : http://$VPS_IP:18081/ .. :18088/
Password : password farm yang di FARM_PASSWORD (login sekali
per cell, Neko mengingatnya di browser kamu)
===============================================================
Checklist verifikasi:
[ ] Buka grid URL — 8 cell tampil, status Running
[ ] Login tiap cell sekali dengan password farm
[ ] Buka https://www.whatismybrowser.com di tiap cell —
User-Agent harus berbeda-beda sesuai label identitas
[ ] Coba Start/Stop/Restart/Wipe/Settings di satu cell
[ ] Kalau pakai proxy, isi di Settings cell lalu cek IP-nya
===============================================================
EOF