This commit is contained in:
2026-05-18 22:48:12 +08:00
parent bd4caa0f09
commit f49c2b135d
11 changed files with 151 additions and 155 deletions

View File

@@ -1,8 +1,8 @@
#!/usr/bin/env sh
set -eu
# 为缺失证书的域名生成临时 dummy 证书。
# 已有完整正式证书时直接跳过,不覆盖线上可用证书。
# Create temporary self-signed certificates for domains whose certificate files
# are missing. Existing real certificates are never overwritten.
ROOT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"
CERT_ROOT="${CERT_ROOT:-./certs/live}"
@@ -10,12 +10,12 @@ CERT_ROOT="${CERT_ROOT:-./certs/live}"
cd "$ROOT_DIR"
if [ -z "${DOMAINS:-}" ]; then
echo "错误:必须设置 DOMAINS" >&2
echo "Error: DOMAINS is required." >&2
exit 1
fi
if ! command -v openssl >/dev/null 2>&1; then
echo "错误:缺少 openssl 命令。" >&2
echo "Error: openssl is required." >&2
exit 1
fi
@@ -31,38 +31,30 @@ for domain in $DOMAINS; do
if [ -f "$cert_file" ] && [ -f "$key_file" ]; then
if [ -f "$marker_file" ]; then
# 上一次脚本可能中途失败,留下了 dummy 证书。
# 继续把它当作 dummy 处理,后面会删除并重新申请正式证书。
echo "复用已存在的 dummy 证书:$domain" >&2
echo "Reusing existing dummy certificate: $domain" >&2
echo "$domain"
continue
fi
# 已经有正式证书时不覆盖,避免误删线上可用证书。
echo "跳过已存在的正式证书:$domain" >&2
echo "Skipping existing real certificate: $domain" >&2
continue
fi
if [ -f "$cert_file" ] || [ -f "$key_file" ]; then
if [ -f "$marker_file" ]; then
# 上次生成 dummy 证书时可能中途退出,留下了不完整文件。
# 这些文件由本脚本创建,可以安全清理后重建。
echo "清理不完整的 dummy 证书:$domain" >&2
echo "Cleaning incomplete dummy certificate: $domain" >&2
rm -f "$cert_file" "$key_file" "$marker_file" "$tmp_cert_file" "$tmp_key_file"
else
# 只存在证书或只存在私钥,状态不完整。
# 自动处理可能误删用户文件,所以直接停止,让用户手工确认。
echo "错误:$domain 存在不完整的证书文件,请手动检查目录:$cert_dir" >&2
echo "Error: incomplete certificate files exist for $domain: $cert_dir" >&2
exit 1
fi
fi
if [ ! -f "$cert_file" ] && [ ! -f "$key_file" ]; then
# marker 先创建,避免 openssl 成功后脚本中断时留下无 marker 的 dummy 文件。
rm -f "$tmp_cert_file" "$tmp_key_file"
: > "$marker_file"
echo "创建 dummy 证书:$domain" >&2
echo "Creating dummy certificate: $domain" >&2
if ! openssl req -x509 -nodes -newkey rsa:2048 -days 1 \
-keyout "$tmp_key_file" \
-out "$tmp_cert_file" \