#!/bin/bash

set -x

# To meet uploading requirements.
apt install rsync

d=forward-proxy
mkdir -p /srv/www/$d
chown user:user /srv/www/$d

cat <<EOF >/etc/apache2/sites-available/$d.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

<VirtualHost $d.defun.work:*>
    ProxyRequests On
    ProxyVia On

    Require ip 46.118

    LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error_forward_proxy.log
    CustomLog ${APACHE_LOG_DIR}/access_forward_proxy.log
</VirtualHost>

<IfModule mod_ssl.c>
  <VirtualHost $d.defun.work:443>
    ProxyRequests On
    ProxyVia On

    Require ip 46.118

    LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error_forward_proxy.log
    CustomLog ${APACHE_LOG_DIR}/access_forward_proxy.log

    SSLCertificateFile /etc/letsencrypt/live/defun.work/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/defun.work/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
  </VirtualHost>
</IfModule>
EOF

a2ensite $d
service apache2 reload
