mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-04 16:34:52 +03:00
fix(ssl): recover interrupted IP certificate migrations
Register recovery before retiring legacy renewal state and detach completed journals before deleting backups.
This commit is contained in:
+33
-16
@@ -308,6 +308,8 @@ ip_ssl_retire_legacy() {
|
||||
fi
|
||||
done
|
||||
((${#retire[@]})) || return 0
|
||||
# A scheduled runner must be able to restore retired entries after SIGKILL.
|
||||
ip_ssl_schedule "$state" "${acme%/*}" || return 1
|
||||
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
|
||||
@@ -326,13 +328,17 @@ ip_ssl_retire_legacy() {
|
||||
|
||||
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
|
||||
local gen home='' current='' entry held file name failed=0 previous=''
|
||||
# These directories are unpublished work or journals already detached from recovery.
|
||||
if ! rm -rf -- "$state/pending-init" "$state/pending-cleanup"; then
|
||||
printf 'Could not remove disposable IP certificate files at %s.\n' "$state" >&2
|
||||
return 1
|
||||
fi
|
||||
[[ -e "$pending" || -L "$pending" ]] || return 0
|
||||
if [[ ! -d "$pending" || -L "$pending" || ! -f "$pending/generation" ]] ||
|
||||
! 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
|
||||
current=$(readlink -- "$state/current") || current=''
|
||||
if [[ -f "$pending/home" ]] && ! IFS= read -r home < "$pending/home"; then
|
||||
@@ -340,7 +346,7 @@ ip_ssl_recover() {
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [[ -n "$gen" && "$current" == "$gen" ]]; then
|
||||
if [[ "$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
|
||||
@@ -370,7 +376,7 @@ ip_ssl_recover() {
|
||||
# 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
|
||||
if ! ip_ssl_ipv4 "${entry%_ecc}" || [[ -z "$home" ]]; then failed=1; continue; fi
|
||||
held="$home/.xui-retired-$gen-$entry"
|
||||
if [[ -e "$held" ]]; then
|
||||
if [[ -e "$home/$entry" || -L "$home/$entry" ]] ||
|
||||
@@ -383,14 +389,20 @@ ip_ssl_recover() {
|
||||
fi
|
||||
done < "$pending/legacy"
|
||||
fi
|
||||
if ((!failed)) && [[ -n "$gen" ]]; then rm -rf -- "${state:?}/${gen:?}" || failed=1; fi
|
||||
if ((!failed)); 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
|
||||
# Remove the journal from the recovery path before deleting any of its files.
|
||||
if ! rm -f -- "$state/staging/fullchain.pem" "$state/staging/privkey.pem" ||
|
||||
! mv -T -- "$pending" "$state/pending-cleanup"; then
|
||||
printf 'Could not detach the IP certificate journal at %s\n' "$pending" >&2
|
||||
return 1
|
||||
fi
|
||||
if ! rm -rf -- "$state/pending-cleanup"; then
|
||||
printf 'Could not remove the disposable IP certificate journal at %s\n' "$state/pending-cleanup" >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
@@ -487,18 +499,23 @@ ip_ssl_legacy_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 work gen file previous rc legacy_home stage="$state/staging" pending_init="$state/pending-init"
|
||||
local -a scope domains=(-d "$ipv4")
|
||||
mkdir -- "$pending" || return 1
|
||||
mkdir -- "$pending_init" || 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
|
||||
# Publish only a journal with a complete generation record.
|
||||
work=$(mktemp -d "$pending_init/gen.XXXXXX") || return 1
|
||||
gen="${work##*/}"
|
||||
printf '%s\n' "$gen" > "$pending/generation" && mkdir -- "$work/acme" "$pending/live" || return 1
|
||||
printf '%s\n' "$gen" > "$pending_init/generation" &&
|
||||
mkdir -- "$work/acme" "$pending_init/live" &&
|
||||
mv -T -- "$pending_init" "$pending" &&
|
||||
mv -T -- "$pending/$gen" "$state/$gen" || return 1
|
||||
work="$state/$gen"
|
||||
if [[ "$action" == issue ]]; then
|
||||
legacy_home=$(ip_ssl_legacy_home "$home") || return 1
|
||||
[[ "$legacy_home" == /* && "$legacy_home" != *[$'\n\r']* ]] || return 1
|
||||
|
||||
Reference in New Issue
Block a user