#!/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="1722" 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 \ -e NEKO_IMPLICIT_CONTROL=true \ "$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 < 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 <> /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 <