#!/usr/bin/env bash # setup.sh — One-time server setup for TRI Bootstrap Server # Run as root or with sudo set -euo pipefail echo "=== TRI Bootstrap Server Setup ===" echo "" # Check if running as root if [ "$EUID" -ne 0 ]; then echo "Please run as root: sudo $0" exit 1 fi # Detect OS if [ -f /etc/debian_version ]; then OS="debian" elif [ -f /etc/redhat-release ]; then OS="rhel" else echo "This setup script supports Debian/Ubuntu and RHEL/Fedora." echo "For other OSes, please follow the manual steps in docs/setup.md" OS="unknown" fi # Install dependencies echo "[1/6] Installing dependencies..." if [ "$OS" = "debian" ]; then apt-get update -qq apt-get install -y -qq tor caddy wget curl jq > /dev/null 2>&1 elif [ "$OS" = "rhel" ]; then yum install -y tor caddy wget curl jq > /dev/null 2>&1 fi # Create directories echo "[2/6] Creating directories..." mkdir -p /var/www/triangles-bootstrap mkdir -p /etc/tor/tri-bootstrap mkdir -p /var/lib/tor/tri-bootstrap mkdir -p /var/log/tri-bootstrap # Generate Tor private key (if not exists) if [ ! -f /var/lib/tor/tri-bootstrap/private_key ]; then echo "[3/6] Generating Tor hidden service key..." tor -f /etc/tor/tri-bootstrap/torrc --quiet sleep 2 pkill -HUP tor || true else echo "[3/6] Tor key already exists, skipping..." fi # Get the onion address if [ -f /var/lib/tor/tri-bootstrap/hostname ]; then ONION=$(cat /var/lib/tor/tri-bootstrap/hostname) echo " Onion address: $ONION" else echo " Warning: onion address not yet available (Tor may still be starting)" fi # Configure Caddy echo "[4/6] Configuring Caddy..." if [ ! -f /etc/caddy/tri-bootstrap-Caddyfile ]; then cp "$(dirname "$0")/../caddy/Caddyfile.template" /etc/caddy/tri-bootstrap-Caddyfile # Replace template variables read -p " Enter your domain (e.g. bootstrap.cryptographic-triangles.org): " DOMAIN read -p " Enter your email for Let's Encrypt: " EMAIL read -p " Enter bootstrap directory (default: /var/www/triangles-bootstrap): " BOOTSTRAP_DIR BOOTSTRAP_DIR="${BOOTSTRAP_DIR:-/var/www/triangles-bootstrap}" sed -i "s/{{BOOTSTRAP_DOMAIN}}/$DOMAIN/g" /etc/caddy/tri-bootstrap-Caddyfile sed -i "s/{{EMAIL}}/$EMAIL/g" /etc/caddy/tri-bootstrap-Caddyfile sed -i "s|{{BOOTSTRAP_DIR}}|$BOOTSTRAP_DIR|g" /etc/caddy/tri-bootstrap-Caddyfile else echo " Caddyfile already exists, skipping..." fi # Copy Tor config echo "[5/6] Configuring Tor..." if [ ! -f /etc/tor/tri-bootstrap/torrc ]; then cp "$(dirname "$0")/../tor/torrc.template" /etc/tor/tri-bootstrap/torrc chown -R debian-tor:debian-tor /etc/tor/tri-bootstrap chown -R debian-tor:debian-tor /var/lib/tor/tri-bootstrap else echo " Tor config already exists, skipping..." fi # Enable and start services echo "[6/6] Starting services..." systemctl enable tor@tri-bootstrap 2>/dev/null || true systemctl enable caddy 2>/dev/null || true systemctl restart tor@tri-bootstrap || true systemctl restart caddy || true echo "" echo "=== Setup Complete ===" echo "" echo "Next steps:" echo " 1. Upload a bootstrap archive to /var/www/triangles-bootstrap/" echo " (e.g. triangles-bootstrap.tar.gz)" echo "" echo " 2. Test:" echo " curl -I https://YOUR_DOMAIN/triangles-bootstrap.tar.gz" echo "" echo " 3. Share your onion address:" if [ -f /var/lib/tor/tri-bootstrap/hostname ]; then cat /var/lib/tor/tri-bootstrap/hostname fi echo "" echo " 4. Update your DNS A record to point to this server's IP"