fix: restore Trojan certificates and schedule host renewals
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user