#!/usr/bin/env bash # Verify Tor, nginx loopback vhosts, and Next.js are responding (local checks). # Usage: bash scripts/health-check-stack.sh # Exit 0 if stack looks healthy; non-zero if something is wrong. set -euo pipefail REPO="$(cd "$(dirname "$0")/.." && pwd)" RANGE_FILE="${REPO}/scripts/generated/onion-port-range.txt" FAIL=0 if ! command -v curl >/dev/null 2>&1; then echo "[!] curl is required for HTTP checks. Install: apt install curl" exit 2 fi http_code() { local url="$1" local out # stderr off — connection refused is expected when Next is down out=$(curl -g -sS -o /dev/null -w "%{http_code}" --connect-timeout 3 --max-time 8 "$url" 2>/dev/null) || out="000" [[ -z "${out}" ]] && out="000" echo "${out:0:3}" } echo "[*] CyberLux stack health" echo "" if command -v systemctl >/dev/null 2>&1; then if systemctl is-active --quiet tor@default.service 2>/dev/null || systemctl is-active --quiet tor 2>/dev/null; then echo " tor: OK (active)" else echo " tor: FAIL (not active)" FAIL=1 fi if systemctl is-active --quiet nginx 2>/dev/null; then echo " nginx: OK (active)" else echo " nginx: FAIL (not active)" FAIL=1 fi else echo " tor/nginx: SKIP (no systemctl — not Linux systemd?)" fi code="$(http_code "http://127.0.0.1:3000/")" if [[ "${code}" =~ ^(200|304|302|301)$ ]]; then echo " Next.js: OK (127.0.0.1:3000 → HTTP ${code})" else echo " Next.js: FAIL (127.0.0.1:3000 → HTTP ${code})" FAIL=1 fi if [[ -f "${RANGE_FILE}" ]]; then read -r pmin pmax < "${RANGE_FILE}" || true hub_code="$(http_code "http://127.0.0.1:${pmin:-8080}/")" if [[ "${hub_code}" =~ ^(200|304|302|301)$ ]]; then echo " hub vhost: OK (127.0.0.1:${pmin:-8080} → HTTP ${hub_code})" else echo " hub vhost: FAIL (127.0.0.1:${pmin:-8080} → HTTP ${hub_code})" FAIL=1 fi else echo " hub vhost: SKIP (no ${RANGE_FILE} — run npm run build or ./start.sh once)" fi if [[ "${FAIL}" -ne 0 ]]; then echo "" echo " ▶ Next must be RUNNING while you run this check (second terminal)." echo " Terminal A: cd ${REPO} && npm run start:onion ← leave it open" echo " Terminal B: npm run health:stack" echo "" echo " Hint: HTTP 000 on :3000 = nothing listening (Next stopped or never started)." echo " nginx 502 on :8080 = nginx is up but upstream :3000 is dead." echo " Start Next (pick one):" echo " cd ${REPO} && npm run start:onion" echo " ./start.sh" echo " sudo systemctl start cyberlux.service # if you installed systemd" if command -v systemctl >/dev/null 2>&1; then if systemctl list-unit-files cyberlux.service &>/dev/null; then st="$(systemctl is-active cyberlux.service 2>/dev/null || echo unknown)" echo " cyberlux.service status: ${st}" fi fi fi echo "" if [[ "${FAIL}" -eq 0 ]]; then echo "✓ health-check-stack: OK" exit 0 fi echo "✗ health-check-stack: one or more checks failed" exit 1