#!/bin/bash set -euo pipefail DOMAIN="${1:-eifeldc.local}" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" INFRA_DIR="$(dirname "$SCRIPT_DIR")" echo "=== EifelDC Infrastructure Setup ===" echo "Domain: ${DOMAIN}" echo "" read -p "Use Docker? (y/N): " USE_DOCKER if [[ "${USE_DOCKER,,}" == "y" ]]; then echo "" echo "[Docker Mode]" echo "[1/5] Generating secrets..." TURN_SECRET=$(openssl rand -hex 16) MACAROON_SECRET=$(openssl rand -hex 32) FORM_SECRET=$(openssl rand -hex 32) POSTGRES_PASSWORD=$(openssl rand -hex 16) echo "[2/5] Writing .env file..." cat > "${INFRA_DIR}/.env" </dev/null || true sed -i "s/eifeldc.local/${DOMAIN}/g" "${INFRA_DIR}/coturn/turnserver.conf" 2>/dev/null || true sed -i "s/eifeldc.local/${DOMAIN}/g" "${INFRA_DIR}/nginx/eifeldc.conf" 2>/dev/null || true echo "[5/5] Generating self-signed certs and starting containers..." "${SCRIPT_DIR}/generate-certs.sh" "${DOMAIN}" cd "${INFRA_DIR}" docker compose up -d echo "" echo "Waiting for Synapse..." for i in $(seq 1 30); do if curl -sf "http://localhost:8008/_matrix/client/versions" > /dev/null 2>&1; then echo "Synapse is ready!" break fi sleep 2 done echo "" echo "=== Docker Setup Complete! ===" else echo "" echo "[Native Mode]" SYNAPSE_DIR="/opt/eifeldc/synapse-data" NGINX_CONF="/etc/nginx/sites-available/eifeldc" COTURN_CONF="/etc/turnserver.conf" echo "[1/6] Generating secrets..." TURN_SECRET=$(openssl rand -hex 16) MACAROON_SECRET=$(openssl rand -hex 32) FORM_SECRET=$(openssl rand -hex 32) POSTGRES_PASSWORD=$(openssl rand -hex 16) echo "[2/6] Writing .env file..." cat > "${INFRA_DIR}/.env" </dev/null || true sudo -u postgres psql -c "CREATE DATABASE synapse ENCODING 'UTF8' LC_COLLATE='C' LC_CTYPE='C' TEMPLATE template0 OWNER synapse;" 2>/dev/null || true sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE synapse TO synapse;" 2>/dev/null || true echo "[5/6] Installing and configuring Synapse..." mkdir -p "${SYNAPSE_DIR}" if [ ! -d "${SYNAPSE_DIR}/venv" ]; then python3 -m venv "${SYNAPSE_DIR}/venv" "${SYNAPSE_DIR}/venv/bin/pip" install --upgrade pip "${SYNAPSE_DIR}/venv/bin/pip" install matrix-synapse fi if [ ! -f "${SYNAPSE_DIR}/homeserver.yaml" ]; then "${SYNAPSE_DIR}/venv/bin/python" -m synapse.app.homeserver \ --server-name "${DOMAIN}" \ --config-path "${SYNAPSE_DIR}/homeserver.yaml" \ --generate-config \ --report-stats=no fi sed -i "s/synapse_password/${POSTGRES_PASSWORD}/g" "${SYNAPSE_DIR}/homeserver.yaml" 2>/dev/null || true sed -i "s/CHANGE_ME_GENERATE_A_RANDOM_STRING/${MACAROON_SECRET}/g" "${SYNAPSE_DIR}/homeserver.yaml" 2>/dev/null || true sed -i "s/CHANGE_ME_GENERATE_ANOTHER_RANDOM_STRING/${FORM_SECRET}/g" "${SYNAPSE_DIR}/homeserver.yaml" 2>/dev/null || true sed -i "s/CHANGE_ME_MATCH_COTURN_SECRET/${TURN_SECRET}/g" "${SYNAPSE_DIR}/homeserver.yaml" 2>/dev/null || true echo "[6/6] Configuring services..." sudo tee "${COTURN_CONF}" > /dev/null </dev/null || true sudo tee "${NGINX_CONF}" > /dev/null <<'NGINXEOF' server { listen 80; server_name DOMAIN_PLACEHOLDER; return 301 https://$host$request_uri; } server { listen 443 ssl http2; server_name DOMAIN_PLACEHOLDER; ssl_certificate /etc/letsencrypt/live/DOMAIN_PLACEHOLDER/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/DOMAIN_PLACEHOLDER/privkey.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; client_max_body_size 50M; add_header X-Frame-Options SAMEORIGIN; add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection "1; mode=block"; location /.well-known/matrix/server { default_type application/json; return 200 '{"m.server": "DOMAIN_PLACEHOLDER:443"}'; } location /.well-known/matrix/client { default_type application/json; return 200 '{"m.homeserver": {"base_url": "https://DOMAIN_PLACEHOLDER"}}'; } location ~ ^/_matrix/(client|media|federation|key|v2) { proxy_pass http://127.0.0.1:8008; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 600s; proxy_send_timeout 600s; } location /_synapse { proxy_pass http://127.0.0.1:8008; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; } location /api/ { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } location / { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } NGINXEOF sudo sed -i "s/DOMAIN_PLACEHOLDER/${DOMAIN}/g" "${NGINX_CONF}" sudo ln -sf "${NGINX_CONF}" /etc/nginx/sites-enabled/eifeldc 2>/dev/null || true sudo nginx -t && sudo systemctl reload nginx || true sudo tee /etc/systemd/system/eifeldc-synapse.service > /dev/null < /dev/null <