# Apache equivalent of deploy/nginx-tunnel.conf.
# Serves ONLY the router config scripts, and ONLY on the WireGuard tunnel address (10.99.0.1:8081 by default).
# Routers fetch http://10.99.0.1:8081/provision/<token>.rsc through the encrypted tunnel; public_url is not involved.
# The app additionally refuses to hand a config script to any address other than the requesting router's own tunnel IP.
#
# Install (Debian/Ubuntu):
#   a2enmod rewrite headers            # headers is optional; rewrite is required
#   # PHP: either mod_php (libapache2-mod-php) or php-fpm via:  a2enmod proxy_fcgi setenvif && a2enconf php8.3-fpm
#   cp deploy/apache-tunnel.conf /etc/apache2/sites-available/billing-tunnel.conf
#   a2ensite billing-tunnel && apachectl configtest && systemctl reload apache2
# Adjust DocumentRoot and the php-fpm socket to your server. The tunnel IP must already exist (wg-quick up wg0 first),
# otherwise Apache fails to bind: start Apache after wg-quick@wg0 (systemctl edit apache2 -> [Unit] After=wg-quick@wg0.service).

Listen 10.99.0.1:8081

<VirtualHost 10.99.0.1:8081>
    ServerName tunnel.invalid
    DocumentRoot /var/www/billing/public

    # RouterOS /tool fetch fails with "failed closing the connection (4)" on keep-alive / chunked replies.
    # Answer in plain HTTP/1.0 style and close the connection after each script.
    KeepAlive Off
    SetEnv force-response-1.0 1
    SetEnv downgrade-1.0 1
    SetEnv no-gzip 1

    # The token is part of the URL, so it must never reach a log.
    # - Defining our own CustomLog stops this vhost inheriting Debian's global "other_vhosts_access.log" (which would log every URL).
    # - LogLevel crit: Apache's error-level messages (e.g. permission denied) quote the full request URL.
    CustomLog /dev/null combined
    ErrorLog ${APACHE_LOG_DIR}/billing-tunnel-error.log
    LogLevel crit

    <Directory /var/www/billing/public>
        AllowOverride None
        Options -Indexes -FollowSymLinks +SymLinksIfOwnerMatch
        Require all granted
    </Directory>

    # Only the provisioning script may run on this listener.
    <Files "rsc.php">
        # --- php-fpm (comment out if you use mod_php) ---
        <IfModule proxy_fcgi_module>
            SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost"
        </IfModule>
    </Files>

    RewriteEngine On
    RewriteRule ^/provision/([a-f0-9]{48})\.rsc$ /provision/rsc.php?t=$1 [L,QSA]
    # Everything else, including a direct request for /provision/rsc.php, is a plain 404.
    RewriteRule ^ - [R=404,L]
</VirtualHost>

# ---------------------------------------------------------------------------------------------------------------
# In your MAIN (public, HTTPS) vhost nothing special is needed for the first-contact bootstrap script:
# the bundled public/.htaccess already rewrites /provision/<token>.rsc  (needs  AllowOverride All  and mod_rewrite).
# BUT keep the token out of that vhost's access log. Give the vhost its OWN CustomLog (otherwise Debian's global
# other_vhosts_access.log also records every URL) and exclude /provision/:
#
#   SetEnvIf Request_URI "^/provision/" nolog
#   CustomLog ${APACHE_LOG_DIR}/access.log combined env=!nolog
#
# Bootstrap tokens are short-lived and single-use, and a config token only works from the router's own tunnel IP,
# but treat logs as sensitive anyway.
