remove subdomain

Signed-off-by: Jean-Yves <7360784+docjyJ@users.noreply.github.com>
This commit is contained in:
Jean-Yves 2024-07-30 19:21:25 +02:00
parent 357a998d4d
commit b2479ddb36
No known key found for this signature in database
GPG key ID: 644C8B9C4CABAEF7
6 changed files with 65 additions and 122 deletions

View file

@ -9,9 +9,60 @@ function loop {
}
function template_nextcloud_route() {
function template_loop_route {
IFS=',' read -ra array <<< "$1"
TARGET="${array[0]}"
ROUTE="${array[1]}"
if [ "${array[2]}" == "1" ]; then
URI_STRIP_PREFIX="uri strip_prefix $ROUTE"
fi
cat << CADDY
route $ROUTE/* {
$URI_STRIP_PREFIX
reverse_proxy $TARGET
}
CADDY
}
if [ -n "$APACHE_PORT" ] && [ "$APACHE_PORT" != "443" ]; then
TRUSTED_PROXIES="trusted_proxies static private_ranges"
AUTO_HTTPS="auto_https off"
TARGET="http://:$APACHE_PORT"
else
IPv4_ADDRESS="$(dig "$APACHE_HOST" A +short +search | head -1 | sed 's|[0-9]\+$|0/16|')"
TRUSTED_PROXIES="trusted_proxies static $IPv4_ADDRESS"
AUTO_HTTPS="auto_https disable_redirects"
TARGET="https://$NC_DOMAIN:443"
fi
if [ -n "$ADDITIONAL_TRUSTED_DOMAIN" ]; then
ADDITIONAL_TARGET="https://$ADDITIONAL_TRUSTED_DOMAIN:443"
fi
cat << CADDY
{
$AUTO_HTTPS
storage file_system {
root /mnt/data/caddy
}
servers {
$TRUSTED_PROXIES
}
log {
level ERROR
}
}
$ADDITIONAL_TARGET
$TARGET {
header -Server
header -X-Powered-By
$(loop route ';' "$CADDY_ROUTES")
route {
header Strict-Transport-Security max-age=31536000;
reverse_proxy localhost:8000
@ -24,79 +75,6 @@ function template_nextcloud_route() {
disable_http_challenge
}
}
CADDY
}
function template_loop_route {
IFS=',' read -ra array <<< "$1"
ROUTE="${array[0]}"
URI_STRIP_PREFIX="${array[1]}"
TARGET_HOST="${array[2]}"
TARGET_PORT="${array[3]}"
cat << CADDY
route $(test -z "$ROUTE" || echo "$ROUTE/* "){
$([ "$URI_STRIP_PREFIX" == "1" ] && echo "uri strip_prefix $ROUTE")
reverse_proxy $TARGET_HOST:$TARGET_PORT
}
CADDY
}
function template_loop_subdomain {
IFS='|' read -ra array <<< "$1"
SUBDOMAIN="${array[0]}"
ROUTES="${array[1]}"
if [ -z "$TRUSTED_DOMAINS" ] && [ -n "$SUBDOMAIN" ]; then
# Ignore subdomains if in proxy mode
return 0
fi
cat << CADDY
$(echo "$TRUSTED_DOMAINS" | tr ',' '\n' | sed "s/.*/$PROTOCOL:\/\/$SUBDOMAIN&:$APACHE_PORT/" | sed '$ ! s/$/,/') {
header -Server
header -X-Powered-By
$(loop route ';' "$ROUTES")
$(test -z "$SUBDOMAIN" && template_nextcloud_route)
}
CADDY
}
function template_caddyfile {
if [ -z "$TRUSTED_DOMAINS" ]; then
IPv4_ADDRESS="private_ranges"
PROTOCOL="http"
else
IPv4_ADDRESS="$(dig "$APACHE_HOST" A +short +search | head -1 | sed 's|[0-9]\+$|0/16|')"
PROTOCOL="https"
fi
cat << CADDY
{
auto_https $(test -z "$TRUSTED_DOMAINS" && echo "off" || echo "disable_redirects")
storage file_system {
root /mnt/data/caddy
}
servers {
trusted_proxies static $IPv4_ADDRESS
}
log {
level ERROR
}
}
$(loop subdomain '@' "$CADDY_ROUTES")
CADDY
}
template_caddyfile

View file

@ -17,24 +17,7 @@ while ! nc -z "$NEXTCLOUD_HOST" 9000; do
sleep 5
done
if [ -z "$CADDY_ROUTES" ]; then
export CADDY_ROUTES="|/browser,0,nextcloud-aio-collabora,9980;/hosting,0,nextcloud-aio-collabora,9980;/cool,0,nextcloud-aio-collabora,9980;/push,1,nextcloud-aio-notify-push,7867;/standalone-signaling,1,nextcloud-aio-talk,8081"
fi
if [ -z "$APACHE_PORT" ]; then
export APACHE_PORT="443"
fi
# Set trusted domains if not in reverse proxy mode
if [ "$APACHE_PORT" == '443' ]; then
if [ -z "$ADDITIONAL_TRUSTED_DOMAIN" ]; then
export TRUSTED_DOMAINS="$NC_DOMAIN"
else
export TRUSTED_DOMAINS="$ADDITIONAL_TRUSTED_DOMAIN,$NC_DOMAIN"
fi
fi
./caddyfile.sh > /tmp/Caddyfile
bash caddyfile.sh > /tmp/Caddyfile
# Fix the Caddyfile format
caddy fmt --overwrite /tmp/Caddyfile

View file

@ -29,7 +29,7 @@ services:
- TZ=${TIMEZONE}
- APACHE_MAX_SIZE=${APACHE_MAX_SIZE}
- APACHE_MAX_TIME=${NEXTCLOUD_MAX_TIME}
- CADDY_ROUTE=|/browser,0,nextcloud-aio-collabora,9980;/hosting,0,nextcloud-aio-collabora,9980;/cool,0,nextcloud-aio-collabora,9980;/push,1,nextcloud-aio-notify-push,7867;/standalone-signaling,1,nextcloud-aio-talk,8081
- CADDY_ROUTE=nextcloud-aio-collabora:9980,/browser,0;nextcloud-aio-collabora:9980,/hosting,0;nextcloud-aio-collabora:9980,/cool,0;nextcloud-aio-notify-push:7867,/push,1;nextcloud-aio-talk:8081,/standalone-signaling,1
volumes:
- nextcloud_aio_nextcloud:/var/www/html:ro
- nextcloud_aio_apache:/mnt/data:rw

View file

@ -5,22 +5,20 @@ namespace AIO\Container;
class ContainerCaddyRoute {
public string $route;
public string $subDomain;
public string $target_host;
public string $target_port;
public string $uriStripPrefix;
public string $target;
public bool $uriStripPrefix;
public function __construct(
string $route,
string $subDomain,
string $target_host,
string $target_port,
string $target,
string $uriStripPrefix
) {
$this->route = $route;
$this->subDomain = $subDomain;
$this->target_host = $target_host;
$this->target_port = $target_port;
$this->uriStripPrefix = $uriStripPrefix;
$this->target = $target;
$this->uriStripPrefix = $uriStripPrefix === "true";
}
public function GetFormatedEnv() : string {
return $this->target.",".$this->route.",".$this->uriStripPrefix?"1":"0";
}
}

View file

@ -20,20 +20,6 @@ class ContainerCaddyRoutes {
}
public function GetFormatedEnv() : string {
$caddyRouteBySubDomain = [];
foreach ($this->caddyRoutes as $caddyRoute) {
$subDomain = $caddyRoute->subDomain;
if (!array_key_exists($subDomain, $caddyRouteBySubDomain)) {
$caddyRouteBySubDomain[$subDomain] = [];
}
$caddyRouteBySubDomain[$subDomain][] = $caddyRoute->route.",".$caddyRoute->uriStripPrefix.",".$caddyRoute->target_host.",".$caddyRoute->target_port ;
}
$subDomainGroups = [];
foreach ($caddyRouteBySubDomain as $subDomain => $routes) {
$subDomainGroups[] = $subDomain . "|" . implode(";", $routes);
}
return implode("@", $subDomainGroups);
return implode(";", array_map(fn($caddyRoute) => $caddyRoute->GetFormatedEnv(), $this->caddyRoutes));
}
}

View file

@ -164,9 +164,7 @@ class ContainerDefinitionFetcher
$caddyRoutes->AddCaddyRoute(
new ContainerCaddyRoute(
$value['route'],
"",
$entry['container_name'],
$value['target_port'],
$entry['container_name'].":".$value['target_port'],
$value['uri_strip_prefix']
)
);