#!/bin/bash
# One-shot Apache updater for 7rnx.net
# Adds /relay location to Apache config
set -e
APACHE_CONF="/etc/apache2/sites-available/000-default-le-ssl.conf"
BACKUP_SUFFIX=".backup-before-relay-$(date +%Y%m%d-%H%M%S)"
echo "🔧 Updating Apache config for wirecore-relay..."
echo ""
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "❌ This script must be run as root"
echo "Run: sudo $0"
exit 1
fi
# Check if config file exists
if [ ! -f "$APACHE_CONF" ]; then
echo "❌ Apache config not found: $APACHE_CONF"
exit 1
fi
# Check if already has /relay location
if grep -q "<Location /relay>" "$APACHE_CONF"; then
echo "⚠️ Apache config already has <Location /relay>"
echo "Config appears to be updated already."
echo ""
echo "To re-apply, manually edit: $APACHE_CONF"
exit 0
fi
# Backup
echo "📦 Backing up current config..."
cp "$APACHE_CONF" "${APACHE_CONF}${BACKUP_SUFFIX}"
echo " Backup: ${APACHE_CONF}${BACKUP_SUFFIX}"
echo ""
# Add the config
echo "✏️ Adding /relay location..."
# Insert before </VirtualHost>
sed -i '/<\/VirtualHost>/i \
\ # Wirecore Relay (APDS-compatible) at /relay\
<Location /relay>\
ProxyPass http://10.22.0.1:9000\
ProxyPassReverse http://10.22.0.1:9000\
ProxyPreserveHost Off\
RequestHeader set X-Forwarded-Proto "https"\
RequestHeader set X-Forwarded-Port "443"\
RequestHeader set X-Forwarded-Prefix "/relay"\
</Location>\
\
# WebSocket proxy for /relay\
RewriteCond %{HTTP:Upgrade} =websocket [NC]\
RewriteRule ^/relay(.*)$ ws://10.22.0.1:9000$1 [P,L]\
' "$APACHE_CONF"
echo " Added <Location /relay> block"
echo ""
# Test config
echo "🧪 Testing Apache configuration..."
if apache2ctl configtest 2>&1 | grep -q "Syntax OK"; then
echo " ✅ Syntax OK"
echo ""
else
echo " ❌ Syntax error!"
echo ""
echo "Rolling back..."
cp "${APACHE_CONF}${BACKUP_SUFFIX}" "$APACHE_CONF"
echo "Config restored to backup."
echo ""
echo "Manual edit required. See: wirecore-relay/DIRECT_DEPLOY.md"
exit 1
fi
# Reload Apache
echo "🔄 Reloading Apache..."
systemctl reload apache2
if systemctl is-active --quiet apache2; then
echo " ✅ Apache reloaded successfully"
echo ""
else
echo " ❌ Apache failed to reload"
echo ""
echo "Check logs: sudo journalctl -u apache2 -n 20"
exit 1
fi
# Test the endpoint
echo "🧪 Testing /relay endpoint..."
sleep 1
if curl -s -f -m 5 https://7rnx.net/relay/stats > /dev/null 2>&1; then
echo " ✅ https://7rnx.net/relay/stats is responding"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✅ SUCCESS! Wirecore relay is live!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "Public URL: https://7rnx.net/relay/"
echo "Stats: https://7rnx.net/relay/stats"
echo ""
echo "Test it:"
echo " curl https://7rnx.net/relay/stats"
echo ""
else
echo " ⚠️ Endpoint not responding yet"
echo ""
echo "Possible issues:"
echo " - Relay not running on tabertron:9000"
echo " - VPN connection issue"
echo ""
echo "Check:"
echo " - On tabertron: ./wirecore-relay/status-relay.sh"
echo " - VPN: ping 10.22.0.1"
echo ""
fi
echo "Backup saved at:"
echo " ${APACHE_CONF}${BACKUP_SUFFIX}"
echo ""