fix: restore Trojan certificates and schedule host renewals

This commit is contained in:
chermack
2026-10-24 02:19:51 +08:00
parent 39255cc3dc
commit de8e02b33f
7 changed files with 330 additions and 21 deletions
+3 -1
View File
@@ -7,7 +7,9 @@ systemctl daemon-reload || true
systemctl start trojan
systemctl start trojan-web
systemctl start trojan-acme-challenge
systemctl enable trojan trojan-web trojan-acme-challenge trojan-acme-renew.timer || true
# systemctl.py does not run calendar timers; use the host renewal units shipped
# in trojan-stack/scripts rather than enabling an inert container timer.
systemctl enable trojan trojan-web trojan-acme-challenge || true
(
while sleep 300; do
/usr/local/sbin/rotate-trojan-log || true
+27 -16
View File
@@ -1,20 +1,31 @@
#!/bin/bash
set -euo pipefail
domain="${ACME_DOMAIN:?ACME_DOMAIN is required}"
home=/root/.acme.sh
cert_dir=/etc/trojan/certs
source_cert="$home/${domain}_ecc/fullchain.cer"
source_key="$home/${domain}_ecc/${domain}.key"
before=""
[[ -f "$cert_dir/fullchain.pem" ]] && before=$(openssl x509 -in "$cert_dir/fullchain.pem" -noout -fingerprint -sha256)
"$home/acme.sh" --cron --home "$home"
# For legacy/manual use. Docker scheduling is provided by the HOST systemd
# units in trojan-stack/scripts, not by the image's systemctl.py timer.
mapfile -t config < <(python -c 'import json; c=json.load(open("/usr/local/etc/trojan/config.json")); print(c["ssl"]["sni"]); print(c["ssl"]["cert"]); print(c["ssl"]["key"]); print(c["local_port"])')
[[ ${#config[@]} -eq 4 ]]
domain="${config[0]}"
acme_home=/root/.acme.sh
target_cert="${config[1]}"
target_key="${config[2]}"
source_cert="$acme_home/${domain}_ecc/fullchain.cer"
source_key="$acme_home/${domain}_ecc/${domain}.key"
before=$(timeout 10 openssl s_client -connect "127.0.0.1:${config[3]}" -servername "$domain" </dev/null 2>/dev/null | openssl x509 -noout -fingerprint -sha256) || before=""
result=0
"$acme_home/acme.sh" --renew --home "$acme_home" -d "$domain" --ecc \
--webroot "${ACME_WEBROOT:-/var/lib/trojan-acme-webroot}" || result=$?
[[ $result -eq 0 || $result -eq 2 ]]
test -r "$source_cert" -a -r "$source_key"
openssl x509 -in "$source_cert" -noout -checkend 0
mkdir -p "$cert_dir"
install -m 0644 "$source_cert" "$cert_dir/fullchain.pem"
install -m 0600 "$source_key" "$cert_dir/private.key"
openssl x509 -noout -modulus -in "$cert_dir/fullchain.pem" | openssl sha256 > /tmp/trojan-cert.modulus
openssl rsa -noout -modulus -in "$cert_dir/private.key" | openssl sha256 >> /tmp/trojan-cert.modulus
[[ $(cut -d' ' -f2 /tmp/trojan-cert.modulus | sort -u | wc -l) -eq 1 ]]
after=$(openssl x509 -in "$cert_dir/fullchain.pem" -noout -fingerprint -sha256)
openssl x509 -in "$source_cert" -noout -checkend 604800
cert_pub=$(openssl x509 -pubkey -noout -in "$source_cert" | openssl sha256)
key_pub=$(openssl pkey -pubout -in "$source_key" | openssl sha256)
[[ "$cert_pub" == "$key_pub" ]]
mkdir -p "$(dirname "$target_cert")" "$(dirname "$target_key")"
if [[ "$(readlink -f "$source_key")" != "$(readlink -f "$target_key")" ]]; then
install -m 0600 "$source_key" "$target_key"
fi
if [[ "$(readlink -f "$source_cert")" != "$(readlink -f "$target_cert")" ]]; then
install -m 0644 "$source_cert" "$target_cert"
fi
after=$(openssl x509 -in "$target_cert" -noout -fingerprint -sha256)
if [[ "$before" != "$after" ]]; then systemctl restart trojan; fi