2021-02-15 11:19:05 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2021-04-08 07:41:55 +02:00
|
|
|
if [[ -z "$EXTERNAL_HTTP_PORT" ]] && [[ -z "$EXTERNAL_HTTPS_PORT" ]]; then
|
|
|
|
echo "EXTERNAL_HTTP_PORT and EXTERNAL_HTTPS_PORT are not set. Aborting."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-02-15 11:19:05 +01:00
|
|
|
if [[ -f "/certs/key.pem" ]] && [[ -f "/certs/cert.pem" ]]; then
|
2021-04-08 07:41:55 +02:00
|
|
|
certs_exists=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n "$EXTERNAL_HTTPS_PORT" ]] && [[ -z "$certs_exists" ]]; then
|
|
|
|
echo "Configured https, but no certificates found. Aborting"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# config: https
|
|
|
|
if [[ -n "$EXTERNAL_HTTPS_PORT" ]] ; then
|
2021-05-27 10:19:22 +02:00
|
|
|
cat <<EOF > /etc/caddy/endpoint
|
2021-04-08 07:41:55 +02:00
|
|
|
https://:8001 {
|
2021-02-15 11:19:05 +01:00
|
|
|
tls /certs/cert.pem /certs/key.pem
|
|
|
|
EOF
|
|
|
|
echo "Configured https"
|
2021-04-08 07:41:55 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# config: http and no https
|
|
|
|
if [[ -n "$EXTERNAL_HTTP_PORT" ]] && [[ -z "$EXTERNAL_HTTPS_PORT" ]] ; then
|
2021-02-15 11:19:05 +01:00
|
|
|
echo "http://:8000 {" > /etc/caddy/endpoint
|
2021-04-08 07:41:55 +02:00
|
|
|
echo "Configured http only"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# config: https and additionally http -> create redirect-file
|
|
|
|
if [[ -n "$EXTERNAL_HTTP_PORT" ]] && [[ -n "$EXTERNAL_HTTPS_PORT" ]] ; then
|
2021-05-27 10:19:22 +02:00
|
|
|
cat <<EOF > /etc/caddy/redirect
|
2021-04-08 07:41:55 +02:00
|
|
|
http://:8000 {
|
|
|
|
redir https://$INSTANCE_DOMAIN{uri}
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
echo "Configured http to https redirect"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Add allowed hosts from $ALLOWED_HOSTS
|
|
|
|
# If the variable is empty, all hosts are allowed.
|
|
|
|
# The hosts are ORed, so the request is valid, if one host matches.
|
|
|
|
# Example: ALLOWED_HOSTS="localhost:8000 127.0.0.1:8000"
|
|
|
|
#
|
|
|
|
# @invalid-host {
|
|
|
|
# not {
|
|
|
|
# header Host localhost:8000
|
|
|
|
# header Host 127.0.0.1:8000
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# respond @invalid-host "Misdirected Request" 421 {
|
|
|
|
# close
|
|
|
|
# }
|
|
|
|
if [[ ! -z "$ALLOWED_HOSTS" ]]; then
|
2021-05-27 10:19:22 +02:00
|
|
|
cat <<EOF > /etc/caddy/invalid_host
|
2021-04-08 07:41:55 +02:00
|
|
|
@invalid-host {
|
|
|
|
not {
|
|
|
|
EOF
|
|
|
|
for host in $ALLOWED_HOSTS; do
|
|
|
|
echo " host $host" >> /etc/caddy/invalid_host
|
|
|
|
done
|
|
|
|
cat <<EOF >> /etc/caddy/invalid_host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
respond @invalid-host "Misdirected Request" 421 {
|
|
|
|
close
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
echo "Configured allowed hosts: $ALLOWED_HOSTS"
|
|
|
|
else
|
|
|
|
echo "All hosts allowed"
|
2021-02-15 11:19:05 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
exec "$@"
|