diff --git a/scripts/setup-domain.sh b/scripts/setup-domain.sh index c383984..8f97d31 100755 --- a/scripts/setup-domain.sh +++ b/scripts/setup-domain.sh @@ -123,6 +123,24 @@ domain_from_origin() { printf '%s\n' "${authority}" } +proxy_from_systemd_environment() { + local environment="$1" + if [[ " ${environment} " =~ [[:space:]]HTTPS_PROXY=([^[:space:]]+) ]]; then + printf '%s\n' "${BASH_REMATCH[1]}" + elif [[ " ${environment} " =~ [[:space:]]HTTP_PROXY=([^[:space:]]+) ]]; then + printf '%s\n' "${BASH_REMATCH[1]}" + else + return 1 + fi +} + +detect_docker_proxy() { + command -v systemctl >/dev/null 2>&1 || return 1 + local environment + environment="$(systemctl show docker.service --property=Environment --value 2>/dev/null || true)" + proxy_from_systemd_environment "${environment}" +} + initialize_privilege() { if ((EUID == 0)); then ROOT_COMMAND=() @@ -308,6 +326,9 @@ main() { default_domain="$(domain_from_origin "${origin}" 2>/dev/null || true)" default_email="${EASY_STREAM_ADMIN_EMAIL:-$(read_env_value ADMIN_BOOTSTRAP_EMAIL || true)}" default_proxy="${EASY_STREAM_OUTBOUND_PROXY:-$(read_env_value BUILD_HTTP_PROXY || true)}" + if [[ -z "${default_proxy}" ]]; then + default_proxy="$(detect_docker_proxy || true)" + fi info 'Public domain and automatic HTTPS' prompt_value DOMAIN 'Public domain' "${default_domain}" validate_domain @@ -325,8 +346,10 @@ main() { write_proxy_override install_configuration configure_firewall - run_root systemctl enable --now caddy.service - run_root systemctl reload caddy.service + run_root systemctl enable caddy.service + # A reload applies the Caddyfile but retains the running process environment. + # Restart so a newly installed systemd proxy override is actually inherited. + run_root systemctl restart caddy.service success "Caddy is active. Open https://${DOMAIN}/ and https://${DOMAIN}/admin." printf 'Certificate status: journalctl -u caddy --since "10 minutes ago" --no-pager\n' } diff --git a/scripts/test-setup-domain.sh b/scripts/test-setup-domain.sh index a11fc57..f9eeb73 100755 --- a/scripts/test-setup-domain.sh +++ b/scripts/test-setup-domain.sh @@ -22,6 +22,11 @@ expect_valid validate_optional_proxy 'http://127.0.0.1:8118' expect_invalid validate_optional_proxy 'socks5://127.0.0.1:1080' [[ "$(domain_from_origin 'https://es.mvphub.ir')" == 'es.mvphub.ir' ]] || fail 'Origin parsing failed' if domain_from_origin 'https://es.mvphub.ir:8443' >/dev/null 2>&1; then fail 'Custom origin port was accepted'; fi +[[ "$(proxy_from_systemd_environment 'HTTP_PROXY=http://127.0.0.1:8118 HTTPS_PROXY=http://127.0.0.1:8118 NO_PROXY=localhost')" == 'http://127.0.0.1:8118' ]] \ + || fail 'Docker systemd proxy detection failed' +if proxy_from_systemd_environment 'NO_PROXY=localhost,127.0.0.1' >/dev/null 2>&1; then + fail 'A missing Docker proxy was accepted' +fi fixture="$(mktemp -d)" trap 'rm -rf -- "${fixture}"' EXIT @@ -33,5 +38,7 @@ grep -q $'^\treverse_proxy 127\.0\.0\.1:8080$' "${fixture}/Caddyfile" || fail 'U grep -q $'^\temail admin@mvphub\.ir$' "${fixture}/Caddyfile" || fail 'Email missing from Caddyfile' grep -q 'install -m 0644 "${keyring_tmp}" "${keyring_file}"' "${TEST_DIR}/setup-domain.sh" \ || fail 'APT keyring readability protection is missing' +grep -q 'systemctl restart caddy.service' "${TEST_DIR}/setup-domain.sh" \ + || fail 'Caddy restart required for proxy inheritance is missing' printf 'setup-domain helper tests: ok\n'