mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-04 08:25:26 +03:00
fix(ssl): preserve live certificates and renewal state across failed deployments
This commit is contained in:
@@ -509,6 +509,11 @@ jobs:
|
||||
bash scripts/test/ip-ssl-entrypoint-test.sh
|
||||
IP_SSL_LINUX_TEST=1 bash scripts/test/ip-ssl-listener-test.sh
|
||||
bash scripts/test/ip-ssl-certificate-test.sh
|
||||
- name: IP certificate lifecycle with upstream acme.sh (offline orders)
|
||||
run: |
|
||||
curl -fsSLo "$RUNNER_TEMP/acme-3.1.5.sh" https://raw.githubusercontent.com/acmesh-official/acme.sh/d5fc938d80e266dba3239f54cf4665432f17c00b/acme.sh
|
||||
curl -fsSLo "$RUNNER_TEMP/acme-3.1.6.sh" https://raw.githubusercontent.com/acmesh-official/acme.sh/807da6498377ee5e0cf43a78091f46f12dc59a89/acme.sh
|
||||
bash scripts/test/ip-ssl-acme-lifecycle-test.sh "$RUNNER_TEMP/acme-3.1.5.sh" "$RUNNER_TEMP/acme-3.1.6.sh"
|
||||
- name: Installer panel port regression tests
|
||||
run: bash deploy/test/installer-panel-port.sh
|
||||
|
||||
|
||||
@@ -110,17 +110,38 @@ when using a custom port. In containers, enable IPv6 networking and publish the
|
||||
validation port for both families. Local listener success cannot verify an
|
||||
external firewall, NAT, or the provider's routing.
|
||||
|
||||
The resulting certificate's IP SANs, validity, and matching private key are
|
||||
checked before configuring the panel. Existing live certificate files are backed
|
||||
up during installation and restored after a failed or invalid copy; failed
|
||||
issuance does not remove certificates or ACME account data. Renewal still copies
|
||||
to the usual `/root/cert/ip/` paths, with a persisted SAN/key check before the
|
||||
panel reload command. The private key remains mode `600` and the chain `644`.
|
||||
A failed order also restores the acme.sh renewal entry of the certificate that
|
||||
was already in place, so cron keeps renewing it for its original addresses.
|
||||
If IP setup fails in the installer, the failure is reported and the installation
|
||||
continues: the panel is installed and reachable over plain HTTP, and the
|
||||
certificate can be issued later from the `x-ui` menu.
|
||||
Issuance and renewal work in a private directory under `/root/cert/ip/.xui-ip-ssl/`.
|
||||
The helper checks the chain, validity dates, every requested IP SAN, and the
|
||||
matching key before activating a new generation. One symlink switch activates
|
||||
the certificate, key, and their ACME renewal state together. The panel still
|
||||
uses `/root/cert/ip/fullchain.pem` (mode `644`) and `/root/cert/ip/privkey.pem`
|
||||
(mode `600`). A failed order, copy, validation, or promotion preserves the
|
||||
previous generation and its renewal schedule. If reloading the panel fails
|
||||
after activation, the valid certificate stays installed and reload is retried.
|
||||
|
||||
The root crontab checks the versioned local renewal helper hourly. Manual setup
|
||||
and cron share a file lock for the same panel paths, even when the primary IP
|
||||
changes. The first successful setup retires legacy acme.sh renewal entries for
|
||||
those paths. Existing domain certificates and the ACME account are retained.
|
||||
Renew managed IP certificates from the `x-ui` menu. Standard acme.sh cron
|
||||
continues to handle its other certificates.
|
||||
|
||||
After an interrupted attempt, the next run recovers the pending transaction
|
||||
before making another ACME request. If recovery reports an error, keep the
|
||||
reported journal and any listed `.xui-retired-*` directories. Fix the reported
|
||||
filesystem problem and rerun IP certificate setup from `x-ui` to retry recovery.
|
||||
The installer reports certificate setup failures and continues, so you can issue
|
||||
the certificate from the `x-ui` menu later.
|
||||
|
||||
In acme.sh 3.1.5 and 3.1.6, an explicit issue uses the destinations and hooks
|
||||
passed on its command line. Renewing can replay saved destinations and hooks.
|
||||
The client records the next renewal time before copying files and runs the
|
||||
reload command after the copy. x-ui supplies private staging paths and commits
|
||||
the modified renewal state once the candidate passes validation. The test suite
|
||||
exercises the upstream copy and install-retry behavior with local certificates.
|
||||
Public issuance and renewal require a separate test with port 80 reachable over
|
||||
both address families. [acme.sh issuance and renewal](https://github.com/acmesh-official/acme.sh/blob/3.1.5/acme.sh#L5289-L6742)
|
||||
· [acme.sh install and retry](https://github.com/acmesh-official/acme.sh/blob/3.1.6/acme.sh#L7205-L7380)
|
||||
|
||||
Inspect both IP SAN entries after issuance:
|
||||
|
||||
|
||||
+392
-86
@@ -280,91 +280,396 @@ 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"
|
||||
)
|
||||
# END GENERATED IP SSL HELPERS
|
||||
is_ip() {
|
||||
@@ -493,7 +798,7 @@ install_base() {
|
||||
zypper refresh && zypper -q install -y cron curl tar timezone socat ca-certificates openssl
|
||||
;;
|
||||
alpine)
|
||||
apk update && apk add dcron curl tar tzdata socat ca-certificates openssl
|
||||
apk update && apk add dcron curl tar tzdata socat ca-certificates openssl flock
|
||||
;;
|
||||
*)
|
||||
apt-get update && apt-get install -y -q cron curl tar tzdata socat ca-certificates openssl
|
||||
@@ -896,8 +1201,7 @@ setup_ip_certificate() {
|
||||
fi
|
||||
fi
|
||||
|
||||
# Set reload command for auto-renewal (add || true so it doesn't fail during first install)
|
||||
local reloadCmd="systemctl restart x-ui 2>/dev/null || rc-service x-ui restart 2>/dev/null || true"
|
||||
local reloadCmd="systemctl restart x-ui 2>/dev/null || rc-service x-ui restart 2>/dev/null"
|
||||
|
||||
# Choose port for HTTP-01 listener (default 80, prompt override)
|
||||
local WebPort=""
|
||||
@@ -964,8 +1268,7 @@ setup_ip_certificate() {
|
||||
fi
|
||||
|
||||
echo -e "${green}IP certificate installed and configured successfully!${plain}"
|
||||
echo -e "${green}Certificate valid for ~6 days, auto-renews via acme.sh cron job.${plain}"
|
||||
echo -e "${yellow}acme.sh will automatically renew and reload x-ui before expiry.${plain}"
|
||||
echo -e "${green}Certificate valid for ~6 days, auto-renews via the x-ui IP certificate job.${plain}"
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -1259,7 +1562,7 @@ prompt_and_setup_ssl() {
|
||||
while [[ -z "$server_ip" ]]; do
|
||||
read -rp "Please enter your server's public IPv4 address: " server_ip
|
||||
server_ip="${server_ip// /}"
|
||||
if [[ ! "$server_ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
|
||||
if ! ip_ssl_ipv4 "$server_ip"; then
|
||||
echo -e "${red}Invalid IPv4 address. Please try again.${plain}"
|
||||
server_ip=""
|
||||
fi
|
||||
@@ -1268,7 +1571,10 @@ prompt_and_setup_ssl() {
|
||||
fi
|
||||
|
||||
local ipv6_addr
|
||||
if ! ipv6_addr=$(ip_ssl_select_ipv6 "$((1 - NONINTERACTIVE))" "${XUI_SSL_IPV6:-}"); then
|
||||
if ! ip_ssl_ipv4 "$server_ip"; then
|
||||
echo -e "${red}✗ IP certificate setup failed. Provide a valid public IPv4 address in the x-ui menu.${plain}"
|
||||
SSL_HOST="${server_ip}"
|
||||
elif ! ipv6_addr=$(ip_ssl_select_ipv6 "$((1 - NONINTERACTIVE))" "${XUI_SSL_IPV6:-}"); then
|
||||
# A refused address neither shrinks the requested certificate nor aborts the install.
|
||||
echo -e "${red}✗ IP certificate setup failed. Correct XUI_SSL_IPV6, then issue it from the x-ui menu.${plain}"
|
||||
SSL_HOST="${server_ip}"
|
||||
|
||||
+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"
|
||||
)
|
||||
|
||||
+14
-6
@@ -1,7 +1,7 @@
|
||||
# IP certificate regression tests
|
||||
|
||||
Run from the repository root in Bash. The suite uses temporary files, mocks ACME
|
||||
orders and service commands, and generates local self-signed certificate fixtures.
|
||||
orders and service commands, and generates certificate fixtures from a local CA.
|
||||
It never requests a certificate from a CA or executes the complete installer.
|
||||
|
||||
```bash
|
||||
@@ -14,7 +14,7 @@ IP_SSL_LINUX_TEST=1 bash scripts/test/ip-ssl-listener-test.sh
|
||||
bash scripts/test/ip-ssl-certificate-test.sh
|
||||
```
|
||||
|
||||
Linux integration needs `socat`, `curl`, `openssl` (1.1.1+), and standard coreutils.
|
||||
Linux integration needs `socat`, `curl`, `flock`, `openssl` (1.1.1+), and standard coreutils.
|
||||
The listener tests bind unprivileged random ports on loopback. They need an IPv6
|
||||
loopback interface, but no public IPv6 address, root privileges, or working
|
||||
external echo service. Run the same commands on Debian/Ubuntu and AlmaLinux/RHEL;
|
||||
@@ -22,9 +22,10 @@ install test dependencies in a disposable container if they are missing locally.
|
||||
|
||||
The tests cover real embedded installer/menu functions, discovery and consent,
|
||||
unattended overrides, both socket families, actual IP SAN types and matching keys,
|
||||
failed issuance/installation, rollback, permissions, and the persisted renewal
|
||||
reload guard. Package installation, ACME orders, and panel/service operations are
|
||||
mocked at their command boundaries.
|
||||
failed issuance and installation, failed restoration, interruption, shared-path
|
||||
locking, rejected automatic renewals, and the persisted renewal runner. Package
|
||||
installation, ACME orders, and panel/service operations are mocked at their command
|
||||
boundaries.
|
||||
|
||||
To check unchanged upstream acme.sh listener implementations separately:
|
||||
|
||||
@@ -33,14 +34,21 @@ curl -fsSLo /tmp/acme-3.1.5.sh https://raw.githubusercontent.com/acmesh-official
|
||||
bash scripts/test/ip-ssl-acme-listener-test.sh /tmp/acme-3.1.5.sh 28085
|
||||
curl -fsSLo /tmp/acme-3.1.6.sh https://raw.githubusercontent.com/acmesh-official/acme.sh/3.1.6/acme.sh
|
||||
bash scripts/test/ip-ssl-acme-listener-test.sh /tmp/acme-3.1.6.sh 28085
|
||||
bash scripts/test/ip-ssl-acme-lifecycle-test.sh /tmp/acme-3.1.5.sh /tmp/acme-3.1.6.sh
|
||||
```
|
||||
|
||||
This invokes only the upstream listener dispatcher, sends independent loopback
|
||||
The listener test invokes the upstream dispatcher, sends independent loopback
|
||||
HTTP requests with `curl -4` and `curl -6`, prints the sockets with `ss`, then
|
||||
stops the listeners. It does not issue ACME orders or change an installed client.
|
||||
Run it in a disposable Linux environment with `iproute2`/`iproute` installed.
|
||||
Test both `net.ipv6.bindv6only=0` and `1` container configurations where available.
|
||||
|
||||
The lifecycle test exercises stock `--installcert` and the retry path of stock
|
||||
`--renew`. It reproduces the live-file overwrite on a failed upstream reload,
|
||||
then checks the x-ui runner against the same behavior using isolated ACME state,
|
||||
fixture certificates, and no CA requests. CI fetches the two upstream versions
|
||||
by commit and runs this test alongside the lifecycle and concurrency regressions.
|
||||
|
||||
Loopback success does not establish public reachability. Real CA validation,
|
||||
external port 80 forwarding, provider firewalls, and scheduled cron execution on
|
||||
a deployed server require separate deployment testing.
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
#!/usr/bin/env bash
|
||||
# Exercise the actual upstream install and renewal-retry commands without a CA.
|
||||
set -euo pipefail
|
||||
ROOT=$(cd "$(dirname "$0")/../.." && pwd)
|
||||
# shellcheck disable=SC1091
|
||||
source "$ROOT/scripts/lib/ip-ssl.sh"
|
||||
|
||||
test_acme() (
|
||||
source_file="$1"
|
||||
temp=$(mktemp -d)
|
||||
trap 'rm -rf "$temp"' EXIT
|
||||
ipv4=198.51.100.8
|
||||
ipv6=2001:4860::1
|
||||
export HOME="$temp/home" NO_ARI=1 SSL_CERT_FILE="$temp/ca.pem"
|
||||
export CRON_FILE="$temp/crontab"
|
||||
export PATH="$temp/bin:$PATH"
|
||||
acme_home="$HOME/.acme.sh"
|
||||
global="$temp/global-certs"
|
||||
legacy="$temp/legacy-live"
|
||||
managed="$temp/managed's live"
|
||||
mkdir -p "$acme_home" "$global/${ipv4}_ecc" "$legacy" "$temp/bin"
|
||||
cp "$source_file" "$acme_home/upstream.sh"
|
||||
printf "CERT_HOME='%s'\n" "$global" > "$acme_home/account.conf"
|
||||
cp "$acme_home/account.conf" "$temp/account.conf"
|
||||
touch "$CRON_FILE"
|
||||
cat > "$temp/bin/crontab" <<'CRON'
|
||||
#!/usr/bin/env bash
|
||||
case "$1" in -l) cat "$CRON_FILE";; -) cat > "$CRON_FILE";; *) exit 1;; esac
|
||||
CRON
|
||||
chmod +x "$temp/bin/crontab"
|
||||
|
||||
openssl req -new -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \
|
||||
-days 3 -subj /CN=offline-ACME-CA -keyout "$temp/ca.key" -out "$SSL_CERT_FILE" >/dev/null 2>&1
|
||||
serial=0
|
||||
for name in good invalid renewed; do
|
||||
san="IP:$ipv4,IP:$ipv6"
|
||||
[[ "$name" != invalid ]] || san="IP:$ipv4"
|
||||
printf 'subjectAltName=%s\n' "$san" > "$temp/ext"
|
||||
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \
|
||||
-subj "/CN=$name" -keyout "$temp/$name.key" -out "$temp/$name.csr" >/dev/null 2>&1
|
||||
openssl x509 -req -in "$temp/$name.csr" -CA "$SSL_CERT_FILE" -CAkey "$temp/ca.key" \
|
||||
-set_serial "$((++serial))" -days 2 -extfile "$temp/ext" -out "$temp/$name.cer" >/dev/null 2>&1
|
||||
cat "$temp/$name.cer" "$SSL_CERT_FILE" > "$temp/$name.fullchain"
|
||||
done
|
||||
|
||||
seed_order() {
|
||||
local entry="$1" fixture="$2"
|
||||
mkdir -p "$entry"
|
||||
cp "$temp/$fixture.cer" "$entry/$ipv4.cer"
|
||||
cp "$temp/$fixture.key" "$entry/$ipv4.key"
|
||||
cp "$temp/$fixture.fullchain" "$entry/fullchain.cer"
|
||||
cp "$SSL_CERT_FILE" "$entry/ca.cer"
|
||||
}
|
||||
|
||||
version=$(sh "$acme_home/upstream.sh" --version | tail -n1)
|
||||
entry="$global/${ipv4}_ecc"
|
||||
seed_order "$entry" invalid
|
||||
printf "Le_Domain='%s'\nLe_NextRenewTime='4000000000'\nLe_CertCreateTime='2'\nLe_InstallCertSuccessTime='1'\n" "$ipv4" > "$entry/$ipv4.conf"
|
||||
cp "$temp/good.fullchain" "$legacy/fullchain.pem"
|
||||
cp "$temp/good.key" "$legacy/privkey.pem"
|
||||
# Old design: the guard fails after installcert has copied both files.
|
||||
if sh "$acme_home/upstream.sh" --installcert --home "$acme_home" -d "$ipv4" --ecc \
|
||||
--key-file "$legacy/privkey.pem" --fullchain-file "$legacy/fullchain.pem" --reloadcmd false > "$temp/legacy.log" 2>&1; then
|
||||
printf 'FAIL: upstream accepted a failing reload command (%s)\n' "$version" >&2
|
||||
return 1
|
||||
fi
|
||||
cmp "$temp/invalid.fullchain" "$legacy/fullchain.pem"
|
||||
cmp "$temp/invalid.key" "$legacy/privkey.pem"
|
||||
grep -q "^Le_NextRenewTime='4000000000'" "$entry/$ipv4.conf"
|
||||
grep -q "^Le_InstallCertSuccessTime='1'" "$entry/$ipv4.conf"
|
||||
grep -Fq "Le_RealKeyPath='$legacy/privkey.pem'" "$entry/$ipv4.conf"
|
||||
cp "$temp/good.fullchain" "$legacy/fullchain.pem"
|
||||
if sh "$acme_home/upstream.sh" --renew --home "$acme_home" -d "$ipv4" --ecc > "$temp/legacy-retry.log" 2>&1; then
|
||||
printf 'FAIL: upstream accepted a failing install retry (%s)\n' "$version" >&2
|
||||
return 1
|
||||
fi
|
||||
grep -q 'retrying install cert' "$temp/legacy-retry.log"
|
||||
cmp "$temp/invalid.fullchain" "$legacy/fullchain.pem"
|
||||
printf 'PASS: upstream %s reproduces live overwrite on failed reload, including a saved-path retry\n' "$version"
|
||||
|
||||
# Only the order is replaced by local fixtures. Stock installcert and
|
||||
# renew execute through the actual persistent runner with the original home.
|
||||
export TEST_ACME_DIR="$temp" TEST_ACME_IP="$ipv4" TEST_ACME_ORIGINAL="$acme_home/upstream.sh"
|
||||
wrapper="$acme_home/acme.sh"
|
||||
cat > "$wrapper" <<'WRAPPER'
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
if [[ "$1" != --issue ]]; then exec sh "$TEST_ACME_ORIGINAL" "$@"; fi
|
||||
while [[ "$1" != --cert-home ]]; do shift; done
|
||||
domain="$TEST_ACME_IP"
|
||||
entry="$2/${domain}_ecc"
|
||||
mkdir -p "$entry"
|
||||
cp "$TEST_ACME_DIR/good.cer" "$entry/$domain.cer"
|
||||
cp "$TEST_ACME_DIR/good.key" "$entry/$domain.key"
|
||||
cp "$TEST_ACME_DIR/good.fullchain" "$entry/fullchain.cer"
|
||||
cp "$SSL_CERT_FILE" "$entry/ca.cer"
|
||||
printf "Le_Domain='%s'\nLe_CertCreateTime='1'\nLe_NextRenewTime='4000000000'\n" "$domain" > "$entry/$domain.conf"
|
||||
WRAPPER
|
||||
chmod +x "$wrapper"
|
||||
ip_ssl_issue_and_install "$wrapper" "$ipv4" "$ipv6" "$((20000+RANDOM))" "$managed" true
|
||||
cmp "$temp/good.fullchain" "$managed/fullchain.pem"
|
||||
cmp "$temp/good.key" "$managed/privkey.pem"
|
||||
cmp "$temp/account.conf" "$acme_home/account.conf"
|
||||
cmp "$temp/invalid.fullchain" "$legacy/fullchain.pem"
|
||||
state="$managed/.xui-ip-ssl"
|
||||
entry="$state/current/acme/${ipv4}_ecc"
|
||||
grep -Fxq "Le_RealKeyPath='staging/privkey.pem'" "$entry/$ipv4.conf"
|
||||
seed_order "$entry" invalid
|
||||
# Simulate an already-created certificate with a pending upstream install
|
||||
# retry. NO_ARI and a future timestamp keep stock --renew entirely offline.
|
||||
sed -i '/^Le_InstallCertSuccessTime=/d; /^Le_CertCreateTime=/d' "$entry/$ipv4.conf"
|
||||
printf "Le_InstallCertSuccessTime='1'\nLe_CertCreateTime='2'\n" >> "$entry/$ipv4.conf"
|
||||
cp "$entry/$ipv4.conf" "$temp/before-retry.conf"
|
||||
if "$state/renew-v1" > "$temp/retry.log" 2>&1; then
|
||||
printf 'FAIL: managed retry accepted candidate without IPv6 SAN (%s)\n' "$version" >&2
|
||||
return 1
|
||||
fi
|
||||
grep -q 'retrying install cert' "$temp/retry.log"
|
||||
grep -q 'Staged IP certificate failed' "$temp/retry.log"
|
||||
cmp "$temp/good.fullchain" "$managed/fullchain.pem"
|
||||
cmp "$temp/good.key" "$managed/privkey.pem"
|
||||
cmp "$temp/before-retry.conf" "$entry/$ipv4.conf"
|
||||
seed_order "$entry" renewed
|
||||
"$state/renew-v1" > "$temp/valid-retry.log" 2>&1
|
||||
cmp "$temp/renewed.fullchain" "$managed/fullchain.pem"
|
||||
cmp "$temp/renewed.key" "$managed/privkey.pem"
|
||||
cmp "$temp/account.conf" "$acme_home/account.conf"
|
||||
printf 'PASS: managed upstream %s installs privately, rejects failed retry, and promotes valid retry\n' "$version"
|
||||
)
|
||||
|
||||
if (($# == 0)); then printf 'Pass verified upstream acme.sh source paths as arguments.\n' >&2; exit 2; fi
|
||||
for acme_source in "$@"; do test_acme "$acme_source"; done
|
||||
@@ -6,115 +6,369 @@ source "${IP_SSL_TEST_HELPERS:-$ROOT/scripts/lib/ip-ssl.sh}"
|
||||
TEMP=$(mktemp -d)
|
||||
trap 'rm -rf "$TEMP"' EXIT
|
||||
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
|
||||
export ACME_LOG="$TEMP/arguments" RELOAD_LOG="$TEMP/reloads"
|
||||
export ISSUED_CERT="$TEMP/new.pem" ISSUED_KEY="$TEMP/new.key"
|
||||
export ACME_ISSUE_FAIL=0 ACME_INSTALL_FAIL=0 ACME_RELOAD_FAIL=0
|
||||
for name in old new v4 dns; do
|
||||
san='IP:198.51.100.8,IP:2001:4860::1'
|
||||
[[ "$name" != v4 ]] || san='IP:198.51.100.8'
|
||||
[[ "$name" != dns ]] || san='DNS:198.51.100.8,DNS:2001:4860::1'
|
||||
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -days 2 -subj /CN=ip-test -addext "subjectAltName=$san" -keyout "$TEMP/$name.key" -out "$TEMP/$name.pem" > /dev/null 2>&1
|
||||
export ACME_LOG="$TEMP/arguments" RELOAD_LOG="$TEMP/reloads" CRON_FILE="$TEMP/crontab"
|
||||
export ISSUE_MODE=ok RENEW_MODE=ok INSTALL_MODE=ok RELOAD_FAIL=0
|
||||
export SSL_CERT_FILE="$TEMP/ca.pem"
|
||||
ipv4=198.51.100.8
|
||||
ipv6=2001:4860::1
|
||||
port=$((20000+RANDOM))
|
||||
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -days 3 \
|
||||
-subj /CN=IP-fixture-CA -keyout "$TEMP/ca.key" -out "$SSL_CERT_FILE" >/dev/null 2>&1
|
||||
serial=0
|
||||
touch "$TEMP/ca-index"
|
||||
printf '01\n' > "$TEMP/ca-serial"
|
||||
cat > "$TEMP/ca.conf" <<EOF
|
||||
[ca]
|
||||
default_ca = fixture
|
||||
[fixture]
|
||||
database = $TEMP/ca-index
|
||||
serial = $TEMP/ca-serial
|
||||
new_certs_dir = $TEMP
|
||||
default_md = sha256
|
||||
policy = subject
|
||||
[subject]
|
||||
commonName = supplied
|
||||
EOF
|
||||
for name in old new later v4 dns expired; do
|
||||
san="IP:$ipv4,IP:$ipv6"
|
||||
case "$name" in
|
||||
v4) san="IP:$ipv4" ;;
|
||||
dns) san="DNS:$ipv4,DNS:$ipv6" ;;
|
||||
esac
|
||||
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \
|
||||
-subj "/CN=$name" -keyout "$TEMP/$name.key" -out "$TEMP/$name.csr" >/dev/null 2>&1
|
||||
printf 'subjectAltName=%s\n' "$san" > "$TEMP/ext"
|
||||
if [[ "$name" == expired ]]; then
|
||||
openssl ca -config "$TEMP/ca.conf" -in "$TEMP/$name.csr" -cert "$SSL_CERT_FILE" -keyfile "$TEMP/ca.key" \
|
||||
-startdate 20200101000000Z -enddate 20200102000000Z -batch -notext \
|
||||
-extfile "$TEMP/ext" -out "$TEMP/$name.pem" >/dev/null 2>&1
|
||||
else
|
||||
openssl x509 -req -in "$TEMP/$name.csr" -CA "$SSL_CERT_FILE" -CAkey "$TEMP/ca.key" \
|
||||
-set_serial "$((++serial))" -days 2 -extfile "$TEMP/ext" -out "$TEMP/$name.pem" >/dev/null 2>&1
|
||||
fi
|
||||
cat "$SSL_CERT_FILE" >> "$TEMP/$name.pem"
|
||||
done
|
||||
mkdir "$TEMP/cert's directory"
|
||||
|
||||
mkdir "$TEMP/mockbin" "$TEMP/acme home" "$TEMP/legacy certificates"
|
||||
export PATH="$TEMP/mockbin:$PATH"
|
||||
printf '13 0 * * * /unrelated-cron\n' > "$CRON_FILE"
|
||||
acme="$TEMP/acme home/acme.sh"
|
||||
legacy="$TEMP/legacy certificates"
|
||||
cert_dir="$TEMP/cert's directory"
|
||||
state="$cert_dir/.xui-ip-ssl"
|
||||
runner="$state/renew-v1"
|
||||
mkdir -p "$cert_dir" "$legacy/$ipv4" "$legacy/${ipv4}_ecc" "$legacy/example.net_ecc"
|
||||
printf "CERT_HOME='%s'\n" "$legacy" > "${acme%/*}/account.conf"
|
||||
printf 'unrelated certificate\n' > "$legacy/example.net_ecc/marker"
|
||||
for suffix in '' _ecc; do
|
||||
printf "Le_Alt='no'\nLe_NextRenewTime='1'\nLe_RealKeyPath='%s/privkey.pem'\nLe_RealFullChainPath='%s/fullchain.pem'\n" \
|
||||
"$cert_dir" "$cert_dir" > "$legacy/$ipv4$suffix/$ipv4.conf"
|
||||
done
|
||||
cp "$legacy/${ipv4}_ecc/$ipv4.conf" "$TEMP/legacy.conf"
|
||||
cp "${acme%/*}/account.conf" "$TEMP/account.conf"
|
||||
cp "$TEMP/old.pem" "$cert_dir/fullchain.pem"
|
||||
cp "$TEMP/old.key" "$cert_dir/privkey.pem"
|
||||
cat > "$TEMP/mock acme" <<'MOCK'
|
||||
|
||||
cat > "$TEMP/mockbin/crontab" <<'MOCK'
|
||||
#!/usr/bin/env bash
|
||||
case "$1" in
|
||||
-l) cat "$CRON_FILE" ;;
|
||||
-) cat > "$CRON_FILE" ;;
|
||||
*) exit 1 ;;
|
||||
esac
|
||||
MOCK
|
||||
cat > "$acme" <<'MOCK'
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
action=$1
|
||||
printf 'CALL\n' >> "$ACME_LOG"
|
||||
printf 'arg=%s\n' "$@" >> "$ACME_LOG"
|
||||
case "$1" in
|
||||
shift
|
||||
home='' cert_home='' account='' domain='' san='' key='' chain='' reload=''
|
||||
while (($#)); do
|
||||
case "$1" in
|
||||
--home) home=$2; shift ;;
|
||||
--cert-home) cert_home=$2; shift ;;
|
||||
--accountconf) account=$2; shift ;;
|
||||
-d)
|
||||
if [[ -z "$domain" ]]; then domain=$2; else san=$2; fi
|
||||
shift ;;
|
||||
--key-file) key=$2; shift ;;
|
||||
--fullchain-file) chain=$2; shift ;;
|
||||
--reloadcmd) reload=$2; shift ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
# Like upstream, load the account config after applying command-line arguments.
|
||||
CERT_HOME=${cert_home:-${home:-${0%/*}}}
|
||||
if [[ -z "$account" ]]; then account="${home:-${0%/*}}/account.conf"; fi
|
||||
# shellcheck disable=SC1090
|
||||
if [[ -f "$account" ]]; then source "$account"; fi
|
||||
entry="$CERT_HOME/${domain}_ecc"
|
||||
conf="$entry/$domain.conf"
|
||||
case "$action" in
|
||||
--version) printf 'v3.1.5\n' ;;
|
||||
--issue)
|
||||
# Like acme.sh, record the requested SAN set before the order can fail.
|
||||
mkdir -p "$ACME_HOME/198.51.100.8_ecc"
|
||||
printf "Le_Alt='%s'\n" "$*" > "$ACME_HOME/198.51.100.8_ecc/198.51.100.8.conf"
|
||||
[[ "$ACME_ISSUE_FAIL" != 1 ]]
|
||||
--issue|--renew)
|
||||
mkdir -p "$entry"
|
||||
if [[ "$action" == --issue ]]; then
|
||||
printf "Le_Alt='%s'\n" "${san:-no}" > "$conf"
|
||||
if [[ "$ISSUE_MODE" == fail ]]; then exit 1; fi
|
||||
else
|
||||
if [[ "$RENEW_MODE" == skip ]]; then exit 2; fi
|
||||
if [[ -n "${RENEW_BARRIER:-}" ]]; then
|
||||
: > "$RENEW_BARRIER.ready"
|
||||
read -r _ < "$RENEW_BARRIER"
|
||||
fi
|
||||
# Renew reuses stored destinations and advances the schedule before copying.
|
||||
# shellcheck disable=SC1090
|
||||
source "$conf"
|
||||
printf "Le_NextRenewTime='advanced'\n" >> "$conf"
|
||||
cp "$ISSUED_KEY" "$Le_RealKeyPath"
|
||||
cp "$ISSUED_CERT" "$Le_RealFullChainPath"
|
||||
if [[ "$RENEW_MODE" == fail ]]; then exit 1; fi
|
||||
exit 0
|
||||
fi
|
||||
printf "Le_NextRenewTime='issued'\n" >> "$conf"
|
||||
;;
|
||||
--upgrade|--register-account) : ;;
|
||||
--installcert)
|
||||
key=''; cert=''; reload=''
|
||||
while (($#)); do
|
||||
case "$1" in --key-file) key="$2"; shift;; --fullchain-file) cert="$2"; shift;; --reloadcmd) reload="$2"; shift;; esac
|
||||
shift
|
||||
done
|
||||
if [[ "$ACME_INSTALL_FAIL" == 1 ]]; then printf 'broken certificate\n' > "$cert"; exit 1; fi
|
||||
cp "$ISSUED_KEY" "$key"; cp "$ISSUED_CERT" "$cert"
|
||||
printf '%s' "$reload" > "$RELOAD_LOG.command"
|
||||
sh -c "$reload"
|
||||
[[ "$ACME_RELOAD_FAIL" != 1 ]]
|
||||
printf 'Le_RealKeyPath=%q\nLe_RealFullChainPath=%q\n' "$key" "$chain" >> "$conf"
|
||||
[[ -z "$account" || "$account" == */pending/account.conf ]] || exit 1
|
||||
if [[ "$INSTALL_MODE" == fail ]]; then printf 'broken chain\n' > "$chain"; exit 1; fi
|
||||
if [[ "$INSTALL_MODE" != no-key ]]; then cp "$ISSUED_KEY" "$key"; fi
|
||||
if [[ "$INSTALL_MODE" != no-chain ]]; then cp "$ISSUED_CERT" "$chain"; fi
|
||||
if [[ -n "$reload" ]]; then sh -c "$reload"; fi
|
||||
;;
|
||||
*) printf 'Unexpected ACME operation: %s\n' "$1" >&2; exit 1 ;;
|
||||
*) printf 'Unexpected ACME operation: %s\n' "$action" >&2; exit 1 ;;
|
||||
esac
|
||||
MOCK
|
||||
chmod +x "$TEMP/mock acme"
|
||||
acme="$TEMP/mock acme"
|
||||
export ACME_HOME="$TEMP"
|
||||
reload="printf 'reloaded\\n' >> '$RELOAD_LOG'"
|
||||
port=$((20000+RANDOM))
|
||||
env chmod +x "$acme" "$TEMP/mockbin/crontab"
|
||||
reload="test \"\$RELOAD_FAIL\" = 0 && printf 'reloaded\\n' >> '$RELOAD_LOG'"
|
||||
|
||||
ACME_ISSUE_FAIL=1
|
||||
if ip_ssl_issue_and_install "$acme" 198.51.100.8 '' "$port" "$cert_dir" "$reload"; then fail 'ACME failure reported success'; fi
|
||||
cmp "$TEMP/old.pem" "$cert_dir/fullchain.pem" || fail 'issuance failure removed existing certificate'
|
||||
cmp "$TEMP/old.key" "$cert_dir/privkey.pem" || fail 'issuance failure removed existing key'
|
||||
[[ ! -f "$RELOAD_LOG" ]] || fail 'issuance failure reloaded panel'
|
||||
[[ ! -e "$ACME_HOME/198.51.100.8_ecc" ]] || fail 'a failed first order left a renewal entry that cron would retry every day'
|
||||
printf 'PASS: ACME failure preserves existing certificate and key\n'
|
||||
fixture() {
|
||||
export ISSUED_CERT="$TEMP/$1.pem" ISSUED_KEY="$TEMP/${2:-$1}.key"
|
||||
}
|
||||
|
||||
mkdir -p "$ACME_HOME/198.51.100.8_ecc"
|
||||
printf "Le_Alt='no'\nLe_NextRenewTime='1'\n" > "$ACME_HOME/198.51.100.8_ecc/198.51.100.8.conf"
|
||||
cp "$ACME_HOME/198.51.100.8_ecc/198.51.100.8.conf" "$TEMP/working.conf"
|
||||
if ip_ssl_issue_and_install "$acme" 198.51.100.8 '2001:4860::1' "$port" "$cert_dir" "$reload"; then fail 'ACME failure reported success'; fi
|
||||
cmp "$TEMP/working.conf" "$ACME_HOME/198.51.100.8_ecc/198.51.100.8.conf" || fail 'a failed IPv6 attempt rewrote the working certificate renewal entry'
|
||||
[[ -z "$(find "$ACME_HOME" -maxdepth 1 -name '.xui-acme.*')" ]] || fail 'the renewal entry backup was left behind'
|
||||
printf 'PASS: a failed order restores the renewal entry of the working certificate\n'
|
||||
rm -rf "$ACME_HOME/198.51.100.8_ecc"
|
||||
: > "$ACME_LOG"
|
||||
ACME_ISSUE_FAIL=0
|
||||
issue() {
|
||||
ip_ssl_issue_and_install "$acme" "${1:-$ipv4}" "$ipv6" "$port" "${2:-$cert_dir}" "$reload"
|
||||
}
|
||||
|
||||
ip_ssl_issue_and_install "$acme" 198.51.100.8 '2001:4860::1' "$port" "$cert_dir" "$reload"
|
||||
cmp "$TEMP/new.pem" "$cert_dir/fullchain.pem" || fail 'new certificate not installed'
|
||||
[[ "$(stat -c %a "$cert_dir/privkey.pem")" == 600 ]] || fail 'private key permissions changed'
|
||||
[[ "$(stat -c %a "$cert_dir/fullchain.pem")" == 644 ]] || fail 'certificate permissions changed'
|
||||
openssl verify -trusted "$cert_dir/fullchain.pem" -partial_chain -verify_ip 198.51.100.8 "$cert_dir/fullchain.pem" > /dev/null
|
||||
openssl verify -trusted "$cert_dir/fullchain.pem" -partial_chain -verify_ip 2001:4860::1 "$cert_dir/fullchain.pem" > /dev/null
|
||||
if ! grep -qx 'arg=--listen-v4' "$ACME_LOG" || ! grep -qx 'arg=--listen-v6' "$ACME_LOG"; then fail 'dual-stack flags absent'; fi
|
||||
[[ "$(grep -cx 'arg=2001:4860::1' "$ACME_LOG")" == 1 ]] || fail 'IPv6 SAN duplicated or missing'
|
||||
[[ "$(cat "$RELOAD_LOG")" == reloaded ]] || fail 'valid certificate did not reload'
|
||||
sh -c "$(cat "$RELOAD_LOG.command")"
|
||||
[[ "$(wc -l < "$RELOAD_LOG")" == 2 ]] || fail 'saved renewal reload command failed'
|
||||
printf 'PASS: both IP SANs, quoting, install permissions and saved reload command\n'
|
||||
live_is() {
|
||||
cmp "$TEMP/$1.pem" "$cert_dir/fullchain.pem" && cmp "$TEMP/$1.key" "$cert_dir/privkey.pem"
|
||||
}
|
||||
|
||||
cp "$TEMP/v4.pem" "$cert_dir/fullchain.pem"
|
||||
cp "$TEMP/v4.key" "$cert_dir/privkey.pem"
|
||||
if sh -c "$(cat "$RELOAD_LOG.command")" > /dev/null 2>&1; then fail 'renewal guard accepted a missing IPv6 SAN'; fi
|
||||
[[ "$(wc -l < "$RELOAD_LOG")" == 2 ]] || fail 'invalid renewed certificate reloaded the panel'
|
||||
cp "$TEMP/new.pem" "$cert_dir/fullchain.pem"
|
||||
cp "$TEMP/new.key" "$cert_dir/privkey.pem"
|
||||
printf 'PASS: saved renewal guard blocks reload after a missing IPv6 SAN\n'
|
||||
legacy_is_intact() {
|
||||
cmp "$TEMP/legacy.conf" "$legacy/$ipv4/$ipv4.conf" &&
|
||||
cmp "$TEMP/legacy.conf" "$legacy/${ipv4}_ecc/$ipv4.conf" &&
|
||||
cmp "$TEMP/account.conf" "${acme%/*}/account.conf" &&
|
||||
grep -qx 'unrelated certificate' "$legacy/example.net_ecc/marker"
|
||||
}
|
||||
|
||||
for bad in v4 dns; do
|
||||
ISSUED_CERT="$TEMP/$bad.pem"; ISSUED_KEY="$TEMP/$bad.key"
|
||||
if ip_ssl_issue_and_install "$acme" 198.51.100.8 '2001:4860::1' "$port" "$cert_dir" "$reload"; then fail 'missing IP SAN accepted'; fi
|
||||
cmp "$TEMP/new.pem" "$cert_dir/fullchain.pem" || fail 'bad SAN replaced working certificate'
|
||||
cmp "$TEMP/new.key" "$cert_dir/privkey.pem" || fail 'bad SAN replaced working key'
|
||||
[[ "$(wc -l < "$RELOAD_LOG")" == 2 ]] || fail 'invalid IP SAN reloaded panel'
|
||||
reject() {
|
||||
if "$@" > "$TEMP/rejected" 2>&1; then fail "unexpected success: $*"; fi
|
||||
}
|
||||
|
||||
fixture new
|
||||
ISSUE_MODE=fail
|
||||
reject issue
|
||||
if ! live_is old || ! legacy_is_intact; then fail 'failed dual-stack order changed prior certificate or ACME state'; fi
|
||||
reject issue 198.51.100.9 "$TEMP/first"
|
||||
[[ ! -e "$legacy/198.51.100.9_ecc" && ! -e "$TEMP/first/fullchain.pem" ]] || fail 'failed first order left public files or a renewable entry'
|
||||
ISSUE_MODE=ok
|
||||
for mode in fail no-key no-chain; do
|
||||
INSTALL_MODE="$mode"
|
||||
reject issue
|
||||
if ! live_is old || ! legacy_is_intact; then fail "$mode installation changed prior certificate or ACME state"; fi
|
||||
done
|
||||
ISSUED_CERT="$TEMP/new.pem"; ISSUED_KEY="$TEMP/old.key"
|
||||
if ip_ssl_issue_and_install "$acme" 198.51.100.8 '' "$port" "$cert_dir" "$reload"; then fail 'mismatched private key accepted'; fi
|
||||
cmp "$TEMP/new.key" "$cert_dir/privkey.pem" || fail 'mismatched key was not rolled back'
|
||||
ACME_INSTALL_FAIL=1
|
||||
if ip_ssl_issue_and_install "$acme" 198.51.100.8 '' "$port" "$cert_dir" "$reload"; then fail 'partial installation accepted'; fi
|
||||
cmp "$TEMP/new.pem" "$cert_dir/fullchain.pem" || fail 'partial install was not rolled back'
|
||||
ACME_INSTALL_FAIL=0
|
||||
ISSUED_CERT="$TEMP/old.pem"; ISSUED_KEY="$TEMP/old.key"; ACME_RELOAD_FAIL=1
|
||||
ip_ssl_issue_and_install "$acme" 198.51.100.8 '' "$port" "$cert_dir" "$reload"
|
||||
cmp "$TEMP/old.pem" "$cert_dir/fullchain.pem" || fail 'reload error discarded valid newly installed cert'
|
||||
printf 'PASS: SAN/key mismatch and partial-install rollback; valid copy survives reload errors\n'
|
||||
INSTALL_MODE=ok
|
||||
for invalid in v4 dns; do
|
||||
fixture "$invalid"
|
||||
reject issue
|
||||
if ! live_is old || ! legacy_is_intact; then fail 'missing IP SAN changed prior certificate or ACME state'; fi
|
||||
done
|
||||
fixture new old
|
||||
reject issue
|
||||
if ! live_is old || ! legacy_is_intact; then fail 'wrong key changed prior certificate or ACME state'; fi
|
||||
|
||||
fixture new
|
||||
# Fault hooks are called indirectly by the sourced lifecycle functions.
|
||||
# shellcheck disable=SC2317
|
||||
chmod() {
|
||||
if [[ "$1" == 600 && "$*" == *'/gen.'* ]]; then return 1; fi
|
||||
command chmod "$@"
|
||||
}
|
||||
reject issue
|
||||
unset -f chmod
|
||||
if ! live_is old || ! legacy_is_intact; then fail 'chmod failure changed prior certificate or ACME state'; fi
|
||||
printf 'PASS: issuance, installation, SAN, key, partial and permission failures preserve prior state\n'
|
||||
|
||||
# shellcheck disable=SC2317
|
||||
mv() {
|
||||
if [[ "$*" == *'.xui-retired-'* && "${!#}" == "$legacy/${ipv4}_ecc" ]]; then return 1; fi
|
||||
command mv "$@"
|
||||
}
|
||||
ISSUE_MODE=fail
|
||||
reject issue
|
||||
grep -Fq "$state/pending" "$TEMP/rejected" || fail 'journal location missing from recovery error'
|
||||
compgen -G "$legacy/.xui-retired-*-${ipv4}_ecc" >/dev/null || fail 'only renewal recovery copy was discarded'
|
||||
unset -f mv
|
||||
reject issue
|
||||
legacy_is_intact || fail 'subsequent attempt did not finish ACME recovery'
|
||||
ISSUE_MODE=ok
|
||||
|
||||
# shellcheck disable=SC2317
|
||||
mv() {
|
||||
if [[ "${!#}" == "$cert_dir/privkey.pem" || ( "$*" == *'/pending/restore '* && "${!#}" == "$cert_dir/fullchain.pem" ) ]]; then
|
||||
return 1
|
||||
fi
|
||||
command mv "$@"
|
||||
}
|
||||
reject issue
|
||||
grep -Fq "$state/pending" "$TEMP/rejected" || fail 'live recovery error omitted journal location'
|
||||
cmp "$TEMP/old.pem" "$state/pending/live/fullchain.pem" || fail 'live recovery copy was lost'
|
||||
unset -f mv
|
||||
ISSUE_MODE=fail
|
||||
reject issue
|
||||
if ! live_is old || ! legacy_is_intact; then fail 'subsequent attempt did not finish live restoration'; fi
|
||||
[[ ! -L "$cert_dir/fullchain.pem" ]] || fail 'original regular file type was not recovered'
|
||||
ISSUE_MODE=ok
|
||||
|
||||
# shellcheck disable=SC2317
|
||||
mv() {
|
||||
command mv "$@" || return
|
||||
if [[ "${!#}" == "$cert_dir/fullchain.pem" && "$*" == *'/pending/link '* ]]; then kill -KILL "$BASHPID"; fi
|
||||
}
|
||||
reject issue
|
||||
unset -f mv
|
||||
if [[ ! -d "$state/pending" ]] || ! live_is old; then fail 'interrupted transition exposed a mixed pair'; fi
|
||||
ISSUE_MODE=fail
|
||||
reject issue
|
||||
if ! live_is old || ! legacy_is_intact; then fail 'interrupted transition failed to recover'; fi
|
||||
ISSUE_MODE=ok
|
||||
printf 'PASS: restoration failures retain backups and interruptions recover on next run\n'
|
||||
|
||||
: > "$ACME_LOG"
|
||||
RELOAD_FAIL=1
|
||||
issue
|
||||
if ! live_is new || [[ ! -f "$state/current/reload-pending" ]]; then fail 'reload failure discarded valid deployment'; fi
|
||||
[[ "$(stat -Lc %a "$cert_dir/privkey.pem")" == 600 ]] || fail 'private key permissions changed'
|
||||
[[ "$(stat -Lc %a "$cert_dir/fullchain.pem")" == 644 ]] || fail 'certificate permissions changed'
|
||||
[[ ! -e "$legacy/$ipv4" && ! -e "$legacy/${ipv4}_ecc" ]] || fail 'old cron can still target live paths'
|
||||
[[ "$(grep -cx 'arg=--listen-v4' "$ACME_LOG")" == 1 && "$(grep -cx 'arg=--listen-v6' "$ACME_LOG")" == 1 ]] || fail 'dual-stack flags absent'
|
||||
[[ "$(grep -cx "arg=$ipv6" "$ACME_LOG")" == 1 ]] || fail 'IPv6 SAN duplicated or missing'
|
||||
grep -Fq '/unrelated-cron' "$CRON_FILE" || fail 'crontab lost an unrelated entry'
|
||||
[[ "$(grep -c 'x-ui managed IP certificate' "$CRON_FILE")" == 1 ]] || fail 'persistent cron missing or duplicated'
|
||||
cmp "$TEMP/account.conf" "${acme%/*}/account.conf" || fail 'global account config changed'
|
||||
grep -qx 'unrelated certificate' "$legacy/example.net_ecc/marker"
|
||||
export RELOAD_FAIL=0 RENEW_MODE=skip
|
||||
"$runner"
|
||||
[[ ! -f "$state/current/reload-pending" && "$(cat "$RELOAD_LOG")" == reloaded ]] || fail 'fresh-shell reload retry failed'
|
||||
printf 'PASS: validated commit preserves modes, retires old live destinations and retries panel reload\n'
|
||||
|
||||
working=$(readlink "$state/current")
|
||||
working_conf="$state/current/acme/${ipv4}_ecc/$ipv4.conf"
|
||||
cp "$working_conf" "$TEMP/working.conf"
|
||||
RENEW_MODE=ok
|
||||
for invalid in v4 expired; do fixture "$invalid"; reject "$runner"; live_is new || fail "$invalid renewal exposed invalid files"; done
|
||||
fixture later old
|
||||
reject "$runner"
|
||||
live_is new || fail 'wrong renewal key replaced live pair'
|
||||
fixture later
|
||||
for mode in no-key no-chain; do INSTALL_MODE="$mode"; reject "$runner"; done
|
||||
INSTALL_MODE=ok
|
||||
if ! live_is new || ! cmp "$TEMP/working.conf" "$working_conf"; then fail 'rejected renewal committed advanced ACME state'; fi
|
||||
[[ "$(readlink "$state/current")" == "$working" ]] || fail 'rejected renewal changed generation'
|
||||
# Start a fresh TLS process using the unchanged panel paths after rejecting a renewal.
|
||||
(
|
||||
openssl s_server -quiet -www -accept "127.0.0.1:$port" -cert "$cert_dir/fullchain.pem" \
|
||||
-key "$cert_dir/privkey.pem" > "$TEMP/tls.log" 2>&1 &
|
||||
tls_pid=$!
|
||||
trap 'if kill -0 "$tls_pid" 2>/dev/null; then kill "$tls_pid"; fi; wait "$tls_pid" 2>/dev/null || [[ $? == 143 ]]' EXIT
|
||||
for ((attempt=0; attempt<100; attempt++)); do
|
||||
kill -0 "$tls_pid" || break
|
||||
if curl --noproxy '*' --silent --fail --cacert "$SSL_CERT_FILE" --connect-timeout 1 --max-time 3 \
|
||||
--connect-to "$ipv4:$port:127.0.0.1:$port" "https://$ipv4:$port/" >/dev/null; then exit 0; fi
|
||||
sleep .01
|
||||
done
|
||||
cat "$TEMP/tls.log" >&2
|
||||
fail 'TLS restart could not load the last known-good certificate'
|
||||
)
|
||||
# shellcheck disable=SC2317
|
||||
mv() {
|
||||
if [[ "${!#}" == "$state/current" ]]; then return 1; fi
|
||||
command mv "$@"
|
||||
}
|
||||
reject ip_ssl_scheduled "$cert_dir"
|
||||
unset -f mv
|
||||
if ! live_is new || ! cmp "$TEMP/working.conf" "$working_conf"; then fail 'promotion failure damaged the previous generation'; fi
|
||||
"$runner"
|
||||
if ! live_is later || ! grep -q advanced "$working_conf"; then fail 'valid persistent renewal did not commit files and ACME state'; fi
|
||||
printf 'PASS: actual TLS startup can read last known-good pair after rejected renewals; valid renewal promotes both files\n'
|
||||
|
||||
# Pause renewal while it holds the destination lock. An unrelated destination
|
||||
# completes during the wait, while two competing manual requests share the lock.
|
||||
export RENEW_BARRIER="$TEMP/renewal"
|
||||
mkfifo "$RENEW_BARRIER"
|
||||
fixture v4
|
||||
"$runner" > "$TEMP/concurrent-renewal" 2>&1 &
|
||||
renew_pid=$!
|
||||
for ((attempt=0; attempt<500; attempt++)); do
|
||||
[[ ! -f "$RENEW_BARRIER.ready" ]] || break
|
||||
sleep .01
|
||||
done
|
||||
[[ -f "$RENEW_BARRIER.ready" ]] || fail 'renewal did not reach the deterministic barrier'
|
||||
if flock -n "$state/lock" true; then fail 'renewal was not holding the destination lock'; fi
|
||||
fixture old
|
||||
issue "$ipv4" > "$TEMP/concurrent-manual" 2>&1 &
|
||||
manual_pid=$!
|
||||
fixture new
|
||||
issue 198.51.100.9 > "$TEMP/concurrent-invalid" 2>&1 &
|
||||
invalid_pid=$!
|
||||
issue "$ipv4" "$TEMP/independent"
|
||||
live_is later || fail 'a competing request changed live files while renewal held the lock'
|
||||
printf 'continue\n' > "$RENEW_BARRIER"
|
||||
if wait "$renew_pid"; then fail 'invalid concurrent renewal succeeded'; fi
|
||||
wait "$manual_pid" || { cat "$TEMP/concurrent-manual"; fail 'concurrent manual issuance failed'; }
|
||||
if wait "$invalid_pid"; then fail 'wrong identifier unexpectedly committed'; fi
|
||||
live_is old || fail 'failed operation rolled back newer successful deployment'
|
||||
unset RENEW_BARRIER
|
||||
printf 'PASS: manual, renewal and shared-destination promotion serialize; unrelated destination progresses\n'
|
||||
|
||||
# Fail the first pointer swap while a second valid operation is waiting to
|
||||
# promote. Its new state must survive cleanup of the earlier transaction.
|
||||
promotion="$TEMP/promotion"
|
||||
mkfifo "$promotion"
|
||||
# shellcheck disable=SC2317
|
||||
mv() {
|
||||
if [[ "${!#}" == "$state/current" && "${FAIL_PROMOTION:-0}" == 1 ]]; then
|
||||
: > "$promotion.ready"
|
||||
read -r _ < "$promotion"
|
||||
return 1
|
||||
fi
|
||||
command mv "$@"
|
||||
}
|
||||
fixture new
|
||||
FAIL_PROMOTION=1 issue > "$TEMP/failed-promotion" 2>&1 &
|
||||
failed_pid=$!
|
||||
for ((attempt=0; attempt<500; attempt++)); do
|
||||
[[ ! -f "$promotion.ready" ]] || break
|
||||
sleep .01
|
||||
done
|
||||
[[ -f "$promotion.ready" ]] || fail 'first operation did not reach promotion'
|
||||
fixture later
|
||||
issue > "$TEMP/waiting-promotion" 2>&1 &
|
||||
waiting_pid=$!
|
||||
if flock -n "$state/lock" true; then fail 'promotion was not holding the destination lock'; fi
|
||||
live_is old || fail 'uncommitted promotion changed live files'
|
||||
printf 'continue\n' > "$promotion"
|
||||
if wait "$failed_pid"; then fail 'injected promotion failure reported success'; fi
|
||||
wait "$waiting_pid" || { cat "$TEMP/waiting-promotion"; fail 'second promotion failed'; }
|
||||
unset -f mv
|
||||
live_is later || fail 'rollback overwrote a newer promotion'
|
||||
printf 'PASS: a failed promotion cannot roll back another operation on the same paths\n'
|
||||
|
||||
before=$(wc -l < "$ACME_LOG")
|
||||
if ip_ssl_issue_and_install "$acme" 198.51.100.8 "2001:4860::1;touch $TEMP/injected" "$port" "$cert_dir" "$reload"; then fail 'injection accepted'; fi
|
||||
reject ip_ssl_issue_and_install "$acme" "$ipv4" "$ipv6;touch $TEMP/injected" "$port" "$cert_dir" "$reload"
|
||||
[[ ! -e "$TEMP/injected" && "$(wc -l < "$ACME_LOG")" == "$before" ]] || fail 'invalid IP reached shell or ACME'
|
||||
printf 'PASS: invalid SAN never reaches ACME or a shell command\n'
|
||||
|
||||
@@ -147,7 +401,7 @@ ip() {
|
||||
[[ "$*" != '-6 route get '* ]] || return 0
|
||||
printf '2: eth0 inet6 2001:4860::1/64 scope global valid_lft forever preferred_lft forever\n'
|
||||
}
|
||||
ACME_RELOAD_FAIL=0; ISSUED_CERT="$TEMP/new.pem"; ISSUED_KEY="$TEMP/new.key"
|
||||
fixture new
|
||||
source_function "$ROOT/x-ui.sh" confirm
|
||||
source_function "$ROOT/x-ui.sh" ssl_cert_issue_for_ip
|
||||
if ! ssl_cert_issue_for_ip "$TEMP/menu-cert" <<< "$(printf 'y\ny\n%s\ny\n' "$port")" > "$TEMP/menu-output" 2>&1; then
|
||||
|
||||
@@ -24,6 +24,9 @@ prompt_and_setup_ssl 2053 / 198.51.100.8 > "$TEMP/invalid" 2>&1 || fail 'an inva
|
||||
grep -q 'Invalid public IPv6' "$TEMP/invalid" || fail 'invalid environment value has no clear diagnostic'
|
||||
[[ "$SSL_HOST" == 198.51.100.8 ]] || fail 'the installer lost its access host after skipping the certificate'
|
||||
unset XUI_SSL_IPV6
|
||||
called=0; stopped=0
|
||||
prompt_and_setup_ssl 2053 / 198.51.100.900 > "$TEMP/invalid-v4" 2>&1 || fail 'an invalid IPv4 aborted the installer'
|
||||
[[ "$called" == 0 && "$stopped" == 0 ]] || fail 'invalid IPv4 stopped panel or reached issuance'
|
||||
called=0
|
||||
prompt_and_setup_ssl 2053 / 198.51.100.8 </dev/null > "$TEMP/unattended" 2>&1
|
||||
[[ "$called" == 1 && -z "$captured" ]] || fail 'unattended unset override did not retain IPv4-only issuance'
|
||||
|
||||
@@ -266,91 +266,396 @@ 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"
|
||||
)
|
||||
# END GENERATED IP SSL HELPERS
|
||||
is_ip() {
|
||||
@@ -1797,7 +2102,13 @@ ssl_cert_issue_main() {
|
||||
# tracks the cert under the actual IP address(es). Resolve those so renewal
|
||||
# state is torn down too; otherwise the acme.sh cron re-creates the deleted cert.
|
||||
local acme_ids="${domain}"
|
||||
if [[ "${domain}" == "ip" ]]; then
|
||||
if [[ "${domain}" == "ip" && -f /root/cert/ip/.xui-ip-ssl/current/config ]]; then
|
||||
if ! ip_ssl_revoke_managed /root/cert/ip; then
|
||||
LOGE "Could not remove the managed IP certificate."
|
||||
return 1
|
||||
fi
|
||||
acme_ids=''
|
||||
elif [[ "${domain}" == "ip" ]]; then
|
||||
acme_ids=$(~/.acme.sh/acme.sh --list 2> /dev/null | awk 'NR>1 {print $1}' | grep -E '^([0-9]{1,3}\.){3}[0-9]{1,3}$|:')
|
||||
fi
|
||||
for id in ${acme_ids}; do
|
||||
@@ -1808,7 +2119,9 @@ ssl_cert_issue_main() {
|
||||
rm -rf ~/.acme.sh/"${id}" ~/.acme.sh/"${id}_ecc"
|
||||
done
|
||||
# Delete the local certificate files for this domain.
|
||||
rm -rf "/root/cert/${domain}"
|
||||
if [[ "${domain}" != "ip" || ! -d /root/cert/ip/.xui-ip-ssl ]]; then
|
||||
rm -rf "/root/cert/${domain}"
|
||||
fi
|
||||
LOGI "Certificate revoked and removed for domain: ${domain}"
|
||||
|
||||
# If the panel currently serves this domain's cert, clear the stored paths
|
||||
@@ -1834,8 +2147,16 @@ ssl_cert_issue_main() {
|
||||
echo "$domains"
|
||||
read -rp "Please enter a domain from the list to renew the SSL certificate: " domain
|
||||
if echo "$domains" | grep -qw "$domain"; then
|
||||
~/.acme.sh/acme.sh --renew -d ${domain} --force
|
||||
LOGI "Certificate forcefully renewed for domain: $domain"
|
||||
if [[ "$domain" == ip && -f /root/cert/ip/.xui-ip-ssl/current/config ]]; then
|
||||
if ip_ssl_scheduled /root/cert/ip renew-force; then
|
||||
LOGI "IP certificate renewed."
|
||||
else
|
||||
LOGE "IP certificate renewal failed."
|
||||
fi
|
||||
else
|
||||
~/.acme.sh/acme.sh --renew -d ${domain} --force
|
||||
LOGI "Certificate forcefully renewed for domain: $domain"
|
||||
fi
|
||||
else
|
||||
echo "Invalid domain entered."
|
||||
fi
|
||||
@@ -2031,7 +2352,7 @@ ssl_cert_issue_for_ip() {
|
||||
zypper refresh > /dev/null 2>&1 && zypper -q install -y socat > /dev/null 2>&1
|
||||
;;
|
||||
alpine)
|
||||
apk add socat curl openssl > /dev/null 2>&1
|
||||
apk add socat curl openssl flock > /dev/null 2>&1
|
||||
;;
|
||||
*)
|
||||
LOGW "Unsupported OS for automatic socat installation"
|
||||
@@ -2094,7 +2415,7 @@ ssl_cert_issue_for_ip() {
|
||||
LOGI "Panel paths set for IP: $server_ip"
|
||||
LOGI " - Certificate File: $webCertFile"
|
||||
LOGI " - Private Key File: $webKeyFile"
|
||||
LOGI " - Validity: ~6 days (auto-renews via acme.sh cron)"
|
||||
LOGI " - Validity: ~6 days (auto-renews via the x-ui IP certificate job)"
|
||||
echo -e "${green}Access URL: https://${server_ip}:${existing_port}${existing_webBasePath}${plain}"
|
||||
if [[ -n "$ipv6_addr" ]]; then
|
||||
echo -e "${green}IPv6 access URL: https://[${ipv6_addr}]:${existing_port}${existing_webBasePath}${plain}"
|
||||
|
||||
Reference in New Issue
Block a user