Files
trojanZ/trojan-master/asset/renew-trojan-certificate

32 lines
1.7 KiB
Bash

#!/bin/bash
set -euo pipefail
# 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 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