mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-04 16:34:52 +03:00
fix(ssl): preserve live certificates and renewal state across failed deployments
This commit is contained in:
+384
-79
@@ -223,89 +223,394 @@ ip_ssl_shell_quote() {
|
||||
printf "'%s'" "$value"
|
||||
}
|
||||
|
||||
# The public paths always resolve through one generation pointer after migration.
|
||||
ip_ssl_link() {
|
||||
local state="$1" target="$2" link="$3"
|
||||
rm -f -- "$state/pending/link" &&
|
||||
ln -s -- "$target" "$state/pending/link" &&
|
||||
mv -Tf -- "$state/pending/link" "$link"
|
||||
}
|
||||
|
||||
ip_ssl_lock() {
|
||||
local dir="$1" state="$1/.xui-ip-ssl"
|
||||
command -v flock >/dev/null || { printf 'IP certificates require flock. Install it and retry.\n' >&2; return 1; }
|
||||
umask 077
|
||||
mkdir -p -- "$dir" || return 1
|
||||
[[ -d "$dir" && ! -L "$dir" && -O "$dir" ]] || return 1
|
||||
chmod go-w -- "$dir" || return 1
|
||||
[[ ! -L "$state" ]] && mkdir -p -- "$state" && [[ -O "$state" ]] && chmod 700 -- "$state" || return 1
|
||||
exec 9>>"$state/lock" || return 1
|
||||
flock -x 9
|
||||
}
|
||||
|
||||
# An old cron can have sourced a legacy config already. Check on both sides
|
||||
# of retiring its entries. Managed renewals always take the destination lock.
|
||||
ip_ssl_legacy_busy() {
|
||||
local acme="$1" cmdline arg found action
|
||||
for cmdline in /proc/[0-9]*/cmdline; do
|
||||
[[ -r "$cmdline" ]] || continue
|
||||
found=0 action=0
|
||||
{
|
||||
while IFS= read -r -d '' arg; do
|
||||
if [[ "$arg" == "$acme" ]]; then
|
||||
found=1
|
||||
elif ((found)); then
|
||||
case "$arg" in
|
||||
--cron|--renew|--renew-all|--renewAll|--renewall|--issue|--install-cert|--installcert|--deploy) action=1 ;;
|
||||
esac
|
||||
fi
|
||||
done
|
||||
} < "$cmdline" 2>/dev/null || continue
|
||||
if ((found && action)); then return 0; fi
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
ip_ssl_retire_legacy() {
|
||||
local state="$1" acme="$2" ipv4="$3" dir="$4" gen="$5" home="$6"
|
||||
local entry name conf value destination destinations
|
||||
local -a retire=()
|
||||
for entry in "$home"/*; do
|
||||
[[ -d "$entry" && ! -L "$entry" ]] || continue
|
||||
name="${entry##*/}"
|
||||
ip_ssl_ipv4 "${name%_ecc}" || continue
|
||||
conf="$entry/${name%_ecc}.conf"
|
||||
[[ -f "$conf" ]] || continue
|
||||
# Retire every legacy IP entry sharing this destination, including an older primary IP.
|
||||
destinations=$(sed -n "s/^Le_Real\(FullChain\|Cert\|Key\|CACert\)Path='\(.*\)'$/\2/p" "$conf") || return 1
|
||||
while IFS= read -r value; do
|
||||
case "${value##*/}" in fullchain.pem|privkey.pem) ;; *) continue ;; esac
|
||||
destination=$(cd -P -- "${value%/*}" 2>/dev/null && pwd -P) || continue
|
||||
if [[ "$destination" == "$dir" ]]; then
|
||||
retire+=("$name")
|
||||
break
|
||||
fi
|
||||
done <<< "$destinations"
|
||||
done
|
||||
# The requested entries can also be unfinished orders with no destinations.
|
||||
for name in "$ipv4" "${ipv4}_ecc"; do
|
||||
entry="$home/$name"
|
||||
conf="$entry/$ipv4.conf"
|
||||
[[ -d "$entry" && ! -L "$entry" ]] || continue
|
||||
if [[ ! -f "$conf" ]] || ! grep -Eq "^Le_(Real(Cert|Key|CACert|FullChain)Path|ReloadCmd|DeployHook)='[^']+" "$conf"; then
|
||||
retire+=("$name")
|
||||
fi
|
||||
done
|
||||
((${#retire[@]})) || return 0
|
||||
if ip_ssl_legacy_busy "$acme"; then
|
||||
printf 'Wait for the existing acme.sh command to finish, then retry IP certificate setup.\n' >&2
|
||||
return 1
|
||||
fi
|
||||
for name in "${retire[@]}"; do
|
||||
[[ -d "$home/$name" ]] || continue
|
||||
# Journal before the same-directory rename so a crash can restore it.
|
||||
printf '%s\n' "$name" >> "$state/pending/legacy" &&
|
||||
mv -T -- "$home/$name" "$home/.xui-retired-$gen-$name" || return 1
|
||||
done
|
||||
if ip_ssl_legacy_busy "$acme"; then
|
||||
printf 'An existing acme.sh command started during migration. Retry IP certificate setup after it exits.\n' >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
ip_ssl_recover() {
|
||||
local dir="$1" state="$1/.xui-ip-ssl" pending="$1/.xui-ip-ssl/pending"
|
||||
local gen='' home='' current='' entry held file name failed=0 previous=''
|
||||
[[ -d "$pending" ]] || return 0
|
||||
if [[ -f "$pending/generation" ]]; then
|
||||
if ! IFS= read -r gen < "$pending/generation" || [[ ! "$gen" =~ ^gen\.[a-zA-Z0-9]+$ ]]; then
|
||||
printf 'Invalid IP certificate generation journal. Keep recovery files at %s.\n' "$pending" >&2
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
current=$(readlink -- "$state/current") || current=''
|
||||
if [[ -f "$pending/home" ]] && ! IFS= read -r home < "$pending/home"; then
|
||||
printf 'Invalid ACME home in the IP certificate journal. Keep recovery files at %s.\n' "$pending" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [[ -n "$gen" && "$current" == "$gen" ]]; then
|
||||
# Commit is the pointer swap. Never roll back a committed generation.
|
||||
if [[ -f "$pending/legacy" ]]; then
|
||||
while IFS= read -r entry; do
|
||||
ip_ssl_ipv4 "${entry%_ecc}" && [[ -n "$home" ]] &&
|
||||
rm -rf -- "$home/.xui-retired-$gen-$entry" || failed=1
|
||||
done < "$pending/legacy"
|
||||
fi
|
||||
if [[ -f "$state/$gen/previous" ]]; then IFS= read -r previous < "$state/$gen/previous" || failed=1; fi
|
||||
for entry in "$state"/gen.*; do
|
||||
name="${entry##*/}"
|
||||
[[ "$name" != "$gen" && "$name" != "$previous" ]] || continue
|
||||
rm -rf -- "$entry" || failed=1
|
||||
done
|
||||
else
|
||||
if [[ -f "$pending/live-changing" ]]; then
|
||||
for file in fullchain.pem privkey.pem; do
|
||||
if [[ -e "$pending/live/$file" || -L "$pending/live/$file" ]]; then
|
||||
rm -f -- "$pending/restore" &&
|
||||
cp -a -- "$pending/live/$file" "$pending/restore" &&
|
||||
mv -Tf -- "$pending/restore" "$dir/$file" || failed=1
|
||||
else
|
||||
rm -f -- "$dir/$file" || failed=1
|
||||
fi
|
||||
done
|
||||
fi
|
||||
# Keep stock cron away from the public paths until the original file
|
||||
# types are restored. A failed restore may still leave a live alias.
|
||||
if ((!failed)) && [[ -f "$pending/legacy" ]]; then
|
||||
while IFS= read -r entry; do
|
||||
if ! ip_ssl_ipv4 "${entry%_ecc}" || [[ -z "$home" || -z "$gen" ]]; then failed=1; continue; fi
|
||||
held="$home/.xui-retired-$gen-$entry"
|
||||
if [[ -e "$held" ]]; then
|
||||
if [[ -e "$home/$entry" || -L "$home/$entry" ]] ||
|
||||
! mv -T -- "$held" "$home/$entry"; then
|
||||
printf 'ACME recovery copy remains at %s\n' "$held" >&2
|
||||
failed=1
|
||||
fi
|
||||
elif [[ ! -d "$home/$entry" ]]; then
|
||||
failed=1
|
||||
fi
|
||||
done < "$pending/legacy"
|
||||
fi
|
||||
if ((!failed)) && [[ -n "$gen" ]]; then rm -rf -- "${state:?}/${gen:?}" || failed=1; fi
|
||||
fi
|
||||
if ((failed)); then
|
||||
printf 'IP certificate recovery could not complete. Keep recovery files at %s and retry after resolving the error.\n' "$pending" >&2
|
||||
return 1
|
||||
fi
|
||||
if ! rm -f -- "$state/staging/fullchain.pem" "$state/staging/privkey.pem" || ! rm -rf -- "$pending"; then
|
||||
printf 'Could not remove the IP certificate journal at %s\n' "$pending" >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
ip_ssl_aliases() {
|
||||
local dir="$1" state="$1/.xui-ip-ssl" pending="$1/.xui-ip-ssl/pending"
|
||||
local file seed
|
||||
if [[ -f "$state/current/config" ]]; then
|
||||
for file in fullchain.pem privkey.pem; do
|
||||
[[ "$(readlink -- "$dir/$file")" == ".xui-ip-ssl/current/$file" ]] || return 1
|
||||
done
|
||||
return 0
|
||||
fi
|
||||
# The seed contains the original bytes, so converting either alias first
|
||||
# still exposes the same pair. The journal keeps the original file types.
|
||||
seed=$(mktemp -d "$state/gen.XXXXXX") || return 1
|
||||
for file in fullchain.pem privkey.pem; do
|
||||
if [[ -e "$pending/live/$file" || -L "$pending/live/$file" ]]; then
|
||||
# Follow the original live symlink relative to its own directory.
|
||||
cp -pL -- "$dir/$file" "$seed/$file" || return 1
|
||||
fi
|
||||
done
|
||||
ip_ssl_link "$state" "${seed##*/}" "$state/current" &&
|
||||
touch "$pending/live-changing" || return 1
|
||||
for file in fullchain.pem privkey.pem; do
|
||||
ip_ssl_link "$state" ".xui-ip-ssl/current/$file" "$dir/$file" || return 1
|
||||
done
|
||||
}
|
||||
|
||||
ip_ssl_validate_pair() {
|
||||
local chain="$1" key="$2" ipv4="$3" ipv6="$4" cert_pub key_pub
|
||||
[[ -s "$chain" && -s "$key" ]] &&
|
||||
openssl crl2pkcs7 -nocrl -certfile "$chain" -out /dev/null &&
|
||||
openssl verify -purpose sslserver -untrusted "$chain" -verify_ip "$ipv4" "$chain" >/dev/null || return 1
|
||||
[[ -z "$ipv6" ]] || openssl verify -purpose sslserver -untrusted "$chain" -verify_ip "$ipv6" "$chain" >/dev/null || return 1
|
||||
cert_pub=$(openssl x509 -in "$chain" -pubkey -noout) &&
|
||||
key_pub=$(openssl pkey -in "$key" -passin pass: -pubout) &&
|
||||
[[ -n "$cert_pub" && "$cert_pub" == "$key_pub" ]]
|
||||
}
|
||||
|
||||
ip_ssl_reload() {
|
||||
local gen="$1" reload="$2"
|
||||
if sh -c "$reload" 9>&-; then
|
||||
rm -f -- "$gen/reload-pending" ||
|
||||
printf 'Could not clear the IP certificate reload marker.\n' >&2
|
||||
else
|
||||
printf 'A valid certificate is installed. Panel reload failed and will be retried by the IP certificate job.\n' >&2
|
||||
fi
|
||||
}
|
||||
|
||||
ip_ssl_schedule() (
|
||||
local state="$1" home="$2" command line existing
|
||||
[[ "$state" != *[$'\n\r%']* ]] || return 1
|
||||
# Different destinations share the user's crontab, but can order and stage in parallel.
|
||||
exec 8>>"$home/.xui-ip-crontab.lock" && flock -x 8 || return 1
|
||||
command=$(ip_ssl_shell_quote "$state/renew-v1")
|
||||
line="19 * * * * $command # x-ui managed IP certificate"
|
||||
if ! existing=$(LC_ALL=C crontab -l 2>&1); then
|
||||
case "$existing" in
|
||||
*"no crontab for "*|*"No such file or directory"*) existing='' ;;
|
||||
*) printf 'Could not read crontab: %s\n' "$existing" >&2; return 1 ;;
|
||||
esac
|
||||
fi
|
||||
if grep -Fxq -- "$line" <<< "$existing"; then return 0; fi
|
||||
{ printf '%s\n' "$existing"; printf '%s\n' "$line"; } | crontab -
|
||||
)
|
||||
|
||||
ip_ssl_account_config() (
|
||||
local home="$1" cert_home="$2" ACCOUNT_CONF_PATH=''
|
||||
# acme.sh sources account.conf after parsing --cert-home, so a saved
|
||||
# CERT_HOME overrides the command line. Keep this override private.
|
||||
if [[ -f "$home/account.conf" ]]; then
|
||||
# shellcheck disable=SC1091
|
||||
source "$home/account.conf" || return 1
|
||||
fi
|
||||
local original="${ACCOUNT_CONF_PATH:-$home/account.conf}"
|
||||
if [[ -f "$original" ]]; then cat -- "$original" || return 1; fi
|
||||
printf '\nCERT_HOME=%s\n' "$(ip_ssl_shell_quote "$cert_home")"
|
||||
)
|
||||
|
||||
ip_ssl_legacy_home() (
|
||||
local home="$1" ACCOUNT_CONF_PATH='' CERT_HOME=''
|
||||
if [[ -f "$home/account.conf" ]]; then
|
||||
# shellcheck disable=SC1091
|
||||
source "$home/account.conf" || return 1
|
||||
fi
|
||||
if [[ -n "$ACCOUNT_CONF_PATH" && -f "$ACCOUNT_CONF_PATH" ]]; then
|
||||
# shellcheck disable=SC1090
|
||||
source "$ACCOUNT_CONF_PATH" || return 1
|
||||
fi
|
||||
printf '%s\n' "${CERT_HOME:-$home}"
|
||||
)
|
||||
|
||||
ip_ssl_transaction() (
|
||||
local action="$1" acme="$2" ipv4="$3" ipv6="$4" port="$5" dir="$6" reload="$7"
|
||||
local state="$dir/.xui-ip-ssl" pending="$dir/.xui-ip-ssl/pending" home="${acme%/*}"
|
||||
local work gen file previous rc legacy_home stage="$dir/.xui-ip-ssl/staging"
|
||||
local -a scope domains=(-d "$ipv4")
|
||||
mkdir -- "$pending" || return 1
|
||||
# Recovery runs under the caller's flock, including on normal exit.
|
||||
trap 'rc=$?; ip_ssl_recover "$dir" || rc=1; exit "$rc"' EXIT
|
||||
trap 'exit 130' INT
|
||||
trap 'exit 143' TERM
|
||||
trap 'exit 129' HUP
|
||||
|
||||
work=$(mktemp -d "$state/gen.XXXXXX") || return 1
|
||||
gen="${work##*/}"
|
||||
printf '%s\n' "$gen" > "$pending/generation" && mkdir -- "$work/acme" "$pending/live" || return 1
|
||||
if [[ "$action" == issue ]]; then
|
||||
legacy_home=$(ip_ssl_legacy_home "$home") || return 1
|
||||
[[ "$legacy_home" == /* && "$legacy_home" != *[$'\n\r']* ]] || return 1
|
||||
printf '%s\n' "$legacy_home" > "$pending/home" &&
|
||||
ip_ssl_retire_legacy "$state" "$acme" "$ipv4" "$dir" "$gen" "$legacy_home" || return 1
|
||||
fi
|
||||
for file in fullchain.pem privkey.pem; do
|
||||
[[ ! -e "$dir/$file" && ! -L "$dir/$file" ]] ||
|
||||
cp -a -- "$dir/$file" "$pending/live/$file" || return 1
|
||||
done
|
||||
if [[ -f "$state/current/config" ]]; then
|
||||
previous=$(readlink -- "$state/current") &&
|
||||
printf '%s\n' "$previous" > "$work/previous" || return 1
|
||||
fi
|
||||
if [[ "$action" != issue ]]; then
|
||||
cp -a -- "$state/current/acme/." "$work/acme/" || return 1
|
||||
fi
|
||||
[[ ! -L "$stage" ]] && mkdir -p -- "$stage" && chmod 700 -- "$stage" &&
|
||||
rm -f -- "$stage/fullchain.pem" "$stage/privkey.pem" || return 1
|
||||
ip_ssl_account_config "$home" "$work/acme" > "$pending/account.conf" || return 1
|
||||
scope=(--home "$home" --cert-home "$work/acme" --accountconf "$pending/account.conf")
|
||||
# acme.sh saves destinations in single quotes. A fixed private working
|
||||
# directory makes relative install paths safe even with a quoted parent.
|
||||
cd -- "$state" || return 1
|
||||
if [[ "$action" == issue ]]; then
|
||||
[[ -z "$ipv6" ]] || domains+=(-d "$ipv6")
|
||||
if ! "$acme" --issue "${scope[@]}" "${domains[@]}" --keylength ec-256 --standalone "${IP_SSL_LISTEN_ARGS[@]}" \
|
||||
--server letsencrypt --certificate-profile shortlived --days 6 --httpport "$port" --force; then
|
||||
printf 'IP certificate issuance failed. Check external port 80 on each selected address.\n' >&2
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
rc=0
|
||||
local -a force=()
|
||||
[[ "$action" != renew-force ]] || force=(--force)
|
||||
"$acme" --renew "${scope[@]}" -d "$ipv4" --ecc "${force[@]}" || rc=$?
|
||||
# acme.sh uses 2 when a certificate is not due for renewal.
|
||||
if ((rc == 2)); then return 0; fi
|
||||
((rc == 0)) || return "$rc"
|
||||
fi
|
||||
# A renewal may already have copied to staging. Require both outputs from
|
||||
# this explicit install so an incomplete copy cannot reuse an older file.
|
||||
rm -f -- "$stage/fullchain.pem" "$stage/privkey.pem" || return 1
|
||||
if ! "$acme" --installcert "${scope[@]}" --ecc -d "$ipv4" --key-file staging/privkey.pem \
|
||||
--fullchain-file staging/fullchain.pem --reloadcmd ""; then
|
||||
printf 'Could not stage the IP certificate and key.\n' >&2
|
||||
return 1
|
||||
fi
|
||||
if ! ip_ssl_validate_pair "$stage/fullchain.pem" "$stage/privkey.pem" "$ipv4" "$ipv6"; then
|
||||
printf 'Staged IP certificate failed SAN, validity or private-key checks.\n' >&2
|
||||
return 1
|
||||
fi
|
||||
cp -- "$stage/privkey.pem" "$work/privkey.pem" &&
|
||||
cp -- "$stage/fullchain.pem" "$work/fullchain.pem" &&
|
||||
chmod 600 -- "$work/privkey.pem" && chmod 644 -- "$work/fullchain.pem" &&
|
||||
printf 'managed_acme=%q\nmanaged_ipv4=%q\nmanaged_ipv6=%q\nmanaged_reload=%q\n' "$acme" "$ipv4" "$ipv6" "$reload" > "$work/config" &&
|
||||
touch "$work/reload-pending" || return 1
|
||||
if [[ "$action" == issue ]]; then ip_ssl_schedule "$state" "$home" || return 1; fi
|
||||
ip_ssl_aliases "$dir" &&
|
||||
ip_ssl_link "$state" "$gen" "$state/current" || return 1
|
||||
ip_ssl_reload "$work" "$reload"
|
||||
)
|
||||
|
||||
ip_ssl_scheduled() (
|
||||
local dir="$1" state="$1/.xui-ip-ssl" action="${2:-renew}"
|
||||
local managed_acme managed_ipv4 managed_ipv6 managed_reload
|
||||
ip_ssl_lock "$dir" && ip_ssl_recover "$dir" || return 1
|
||||
[[ -f "$state/current/config" ]] || return 0
|
||||
# This file is generated with Bash quoting under the private state directory.
|
||||
# shellcheck disable=SC1091
|
||||
source "$state/current/config" || return 1
|
||||
if [[ -f "$state/current/reload-pending" ]]; then ip_ssl_reload "$state/current" "$managed_reload"; fi
|
||||
ip_ssl_transaction "$action" "$managed_acme" "$managed_ipv4" "$managed_ipv6" '' "$dir" "$managed_reload"
|
||||
)
|
||||
|
||||
ip_ssl_revoke_managed() (
|
||||
local dir="$1" state="$1/.xui-ip-ssl" managed_acme managed_ipv4 gen account
|
||||
ip_ssl_lock "$dir" && ip_ssl_recover "$dir" || return 1
|
||||
[[ -f "$state/current/config" ]] || return 1
|
||||
# shellcheck disable=SC1091
|
||||
source "$state/current/config" || return 1
|
||||
account=$(mktemp "$state/.account.XXXXXX") || return 1
|
||||
trap 'rm -f -- "$account"' EXIT
|
||||
ip_ssl_account_config "${managed_acme%/*}" "$state/current/acme" > "$account" || return 1
|
||||
"$managed_acme" --revoke --home "${managed_acme%/*}" --cert-home "$state/current/acme" --accountconf "$account" -d "$managed_ipv4" --ecc ||
|
||||
printf 'CA revocation failed. Removing local renewal state and files.\n' >&2
|
||||
# Keep the lock inode and inert scheduled runner for later issuance.
|
||||
rm -f -- "$state/current" "$dir/fullchain.pem" "$dir/privkey.pem" || return 1
|
||||
for gen in "$state"/gen.*; do rm -rf -- "$gen" || return 1; done
|
||||
)
|
||||
|
||||
ip_ssl_write_runner() {
|
||||
local state="$1" temp
|
||||
temp=$(mktemp "$state/.runner.XXXXXX") || return 1
|
||||
if {
|
||||
printf '#!/bin/bash\n# x-ui managed IP certificate deployment v1\n'
|
||||
# The OpenRC service commands live in /sbin on Alpine.
|
||||
# shellcheck disable=SC2016
|
||||
printf 'export PATH="${PATH:+$PATH:}/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"\n'
|
||||
declare -f ip_ssl_ipv4 ip_ssl_shell_quote ip_ssl_link ip_ssl_lock ip_ssl_legacy_busy \
|
||||
ip_ssl_retire_legacy ip_ssl_recover ip_ssl_aliases ip_ssl_validate_pair ip_ssl_reload \
|
||||
ip_ssl_schedule ip_ssl_account_config ip_ssl_legacy_home ip_ssl_transaction ip_ssl_scheduled
|
||||
# shellcheck disable=SC2016
|
||||
printf '\nip_ssl_scheduled "$(cd -- "$(dirname -- "$0")/.." && pwd -P)"\n'
|
||||
} > "$temp" && chmod 700 -- "$temp" && mv -Tf -- "$temp" "$state/renew-v1"; then
|
||||
return 0
|
||||
fi
|
||||
rm -f -- "$temp" || printf 'Could not remove incomplete renewal helper %s\n' "$temp" >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
ip_ssl_issue_and_install() (
|
||||
local acme="$1" ipv4="$2" ipv6="$3" port="$4" cert_dir="$5" reload="$6"
|
||||
local stage='' attempted=0 installed=0 code=0 file restored
|
||||
local acme_home="${acme%/*}" renewal_backup entry
|
||||
local quoted_cert quoted_key validation check_key reload_guard
|
||||
local -a domain_args=(-d "$ipv4")
|
||||
local acme="$1" ipv4="$2" ipv6="$3" port="$4" dir="$5" reload="$6"
|
||||
if ! ip_ssl_ipv4 "$ipv4" || { [[ -n "$ipv6" ]] && ! ip_ssl_public_ipv6 "$ipv6"; }; then
|
||||
printf 'Invalid IP certificate identifier; no ACME request was sent.\n' >&2
|
||||
return 1
|
||||
fi
|
||||
[[ "$port" =~ ^[0-9]{1,5}$ ]] && ((10#$port >= 1 && 10#$port <= 65535)) || return 1
|
||||
[[ -z "$ipv6" ]] || domain_args+=(-d "$ipv6")
|
||||
ip_ssl_prepare_listener "$acme" "$port" "$ipv6" || return 1
|
||||
renewal_backup=$(mktemp -d "$acme_home/.xui-acme.XXXXXX") || return 1
|
||||
for entry in "$ipv4" "${ipv4}_ecc"; do
|
||||
[[ ! -d "$acme_home/$entry" ]] || cp -a -- "$acme_home/$entry" "$renewal_backup/$entry" || {
|
||||
rm -rf -- "$renewal_backup"
|
||||
return 1
|
||||
}
|
||||
done
|
||||
if ! "$acme" --issue "${domain_args[@]}" --standalone "${IP_SSL_LISTEN_ARGS[@]}" \
|
||||
--server letsencrypt --certificate-profile shortlived --days 6 --httpport "$port" --force; then
|
||||
# acme.sh saves the requested names before it places the order, so a failed one would
|
||||
# make cron renew the working certificate with the names that just failed.
|
||||
for entry in "$ipv4" "${ipv4}_ecc"; do
|
||||
rm -rf -- "${acme_home:?}/$entry"
|
||||
[[ ! -d "$renewal_backup/$entry" ]] || mv -- "$renewal_backup/$entry" "$acme_home/$entry"
|
||||
done
|
||||
rm -rf -- "$renewal_backup"
|
||||
printf 'IP certificate issuance failed. Existing certificates and ACME data were preserved. Check external port 80 on every selected address.\n' >&2
|
||||
return 1
|
||||
fi
|
||||
rm -rf -- "$renewal_backup"
|
||||
|
||||
umask 077
|
||||
mkdir -p "$cert_dir" || return 1
|
||||
stage=$(mktemp -d "$cert_dir/.xui-install.XXXXXX") || return 1
|
||||
# Invoked indirectly by the EXIT trap; older ShellCheck reports SC2317.
|
||||
# shellcheck disable=SC2317,SC2329
|
||||
ip_ssl_install_cleanup() {
|
||||
restored=1
|
||||
if ((attempted && !installed)); then
|
||||
for file in fullchain.pem privkey.pem; do
|
||||
if [[ -f "$stage/$file" ]]; then
|
||||
cp -p -- "$stage/$file" "$cert_dir/$file" || restored=0
|
||||
else
|
||||
rm -f -- "$cert_dir/$file" || restored=0
|
||||
fi
|
||||
done
|
||||
fi
|
||||
if ((restored)); then
|
||||
rm -rf -- "$stage"
|
||||
else
|
||||
printf 'Could not restore certificate files; recovery copies remain in %s\n' "$stage" >&2
|
||||
fi
|
||||
}
|
||||
trap ip_ssl_install_cleanup EXIT
|
||||
for file in fullchain.pem privkey.pem; do
|
||||
[[ ! -e "$cert_dir/$file" ]] || cp -p -- "$cert_dir/$file" "$stage/$file" || return 1
|
||||
done
|
||||
quoted_cert=$(ip_ssl_shell_quote "$cert_dir/fullchain.pem")
|
||||
quoted_key=$(ip_ssl_shell_quote "$cert_dir/privkey.pem")
|
||||
# Trust the leaf only for local identity checks, not as a new system CA.
|
||||
validation="openssl verify -trusted $quoted_cert -partial_chain -verify_ip '$ipv4' $quoted_cert"
|
||||
[[ -z "$ipv6" ]] || validation+=" && openssl verify -trusted $quoted_cert -partial_chain -verify_ip '$ipv6' $quoted_cert"
|
||||
# Expansion belongs to the later reload shell, including cron renewals.
|
||||
# shellcheck disable=SC2016
|
||||
printf -v check_key 'xui_cert_pub=$(openssl x509 -in %s -pubkey -noout) && xui_key_pub=$(openssl pkey -in %s -passin pass: -pubout) && [ -n "$xui_cert_pub" ] && [ "$xui_cert_pub" = "$xui_key_pub" ]' "$quoted_cert" "$quoted_key"
|
||||
validation+=" && $check_key"
|
||||
# acme.sh persists this self-contained guard for later cron renewals too.
|
||||
reload_guard="$validation && { $reload; }"
|
||||
attempted=1
|
||||
"$acme" --installcert --force -d "$ipv4" --key-file "$cert_dir/privkey.pem" \
|
||||
--fullchain-file "$cert_dir/fullchain.pem" --reloadcmd "$reload_guard" || code=$?
|
||||
if ! sh -c "$validation" > /dev/null 2>&1; then
|
||||
printf 'Installed certificate failed IP SAN, validity or private-key checks; restoring previous files.\n' >&2
|
||||
return 1
|
||||
fi
|
||||
if ((code != 0)) && [[ -f "$stage/fullchain.pem" ]] && [[ "$(cat "$stage/fullchain.pem")" == "$(cat "$cert_dir/fullchain.pem")" ]]; then
|
||||
printf 'Certificate installation failed without replacing the previous certificate.\n' >&2
|
||||
return 1
|
||||
fi
|
||||
chmod 600 "$cert_dir/privkey.pem" && chmod 644 "$cert_dir/fullchain.pem" || return 1
|
||||
installed=1
|
||||
if ((code != 0)); then printf 'Certificate files are valid, but the reload command reported an error.\n' >&2; fi
|
||||
[[ "$acme" == /* && "$acme" != *[$'\n\r']* && "$dir" == /* && "$dir" != *[$'\n\r%']* ]] || return 1
|
||||
command -v crontab >/dev/null || { printf 'IP certificate renewal requires crontab.\n' >&2; return 1; }
|
||||
ip_ssl_lock "$dir" && dir=$(cd -- "$dir" && pwd -P) && acme=$(readlink -f -- "$acme") &&
|
||||
ip_ssl_recover "$dir" || return 1
|
||||
ip_ssl_prepare_listener "$acme" "$port" "$ipv6" &&
|
||||
ip_ssl_write_runner "$dir/.xui-ip-ssl" &&
|
||||
ip_ssl_transaction issue "$acme" "$ipv4" "$ipv6" "$port" "$dir" "$reload"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user