Repository navigation
Expand file tree
/
Copy pathsslcheck.sh
More file actions
198 lines (154 loc) Β· 4.95 KB
/
Copy pathsslcheck.sh
File metadata and controls
198 lines (154 loc) Β· 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/bash
set -u
dir_path="/root/.acme.sh"
acme_bin="/root/.acme.sh/acme.sh"
THRESH_IP_DAYS=1
THRESH_DOMAIN_DAYS=2
SSL_BASE="/usr/local/nginx/ssl"
IP_SSL_DIR="$SSL_BASE/default"
PREFER_LOCAL_CERT=1
USE_ALPN_FOR_IP=0
FLAG="/tmp/acme_renew_need_restart_nginx.flag"
rm -f "$FLAG"
log() { echo -e "[$(date '+%F %T')] $*"; }
is_ip() { [[ "$1" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; }
nginx_stop_if_running() {
if systemctl is-active --quiet nginx; then
log "π Stopping nginx..."
systemctl stop nginx
fi
}
nginx_start_if_not_running() {
if ! systemctl is-active --quiet nginx; then
log "π Starting nginx..."
systemctl start nginx
fi
}
get_end_time_from_file() {
local cert_file="$1"
[ -s "$cert_file" ] || return 1
openssl x509 -in "$cert_file" -noout -enddate 2>/dev/null | awk -F= '{print $2}'
}
get_end_time_remote() {
local host="$1"
local connect_host="$host"
if [ "${PREFER_LOCAL_CERT:-0}" -eq 1 ]; then
:
fi
if is_ip "$host"; then
timeout 5 bash -c "echo | openssl s_client -connect '$connect_host:443' 2>/dev/null \
| openssl x509 -noout -enddate 2>/dev/null" | awk -F= '{print $2}'
else
timeout 5 bash -c "echo | openssl s_client -servername '$host' -connect '$connect_host:443' 2>/dev/null \
| openssl x509 -noout -enddate 2>/dev/null" | awk -F= '{print $2}'
fi
}
days_left_from_endtime() {
local end_time="$1"
local end_ts now_ts
end_ts=$(date -d "$end_time" +%s 2>/dev/null || true)
[ -z "${end_ts:-}" ] && return 1
now_ts=$(date -u +%s)
echo $(( (end_ts - now_ts) / 86400 ))
}
install_cert_to_dir() {
local domain="$1"
local ssl_dir="$2"
mkdir -p "$ssl_dir"
log "π₯ Installing cert to: $ssl_dir"
"$acme_bin" --install-cert -d "$domain" \
--ecc \
--key-file "$ssl_dir/key.pem" \
--fullchain-file "$ssl_dir/cert.pem" \
--ca-file "$ssl_dir/ca.pem" \
--reloadcmd "true" || true
if [ -s "$ssl_dir/key.pem" ] && [ -s "$ssl_dir/cert.pem" ] && [ -s "$ssl_dir/ca.pem" ]; then
touch "$FLAG"
log "β
Installed OK: $domain"
return 0
fi
log "β Install failed (files missing): $domain"
return 1
}
log "π Scanning acme.sh ECC dirs under: $dir_path"
found_any=0
while IFS= read -r -d '' full; do
found_any=1
dir="$(basename "$full")"
primary="${dir%_ecc}"
log ""
log "=============================="
log "π Target: $primary"
end_time=""
if [ "${PREFER_LOCAL_CERT:-0}" -eq 1 ]; then
if is_ip "$primary"; then
end_time="$(get_end_time_from_file "$IP_SSL_DIR/cert.pem" || true)"
[ -n "$end_time" ] || log "β οΈ Local cert not found: $IP_SSL_DIR/cert.pem"
else
end_time="$(get_end_time_from_file "$SSL_BASE/$primary/cert.pem" || true)"
[ -n "$end_time" ] || log "β οΈ Local cert not found: $SSL_BASE/$primary/cert.pem"
fi
fi
if [ -z "${end_time:-}" ]; then
end_time="$(get_end_time_remote "$primary" || true)"
[ -n "$end_time" ] || log "β οΈ Remote probe failed for $primary"
fi
if [ -z "${end_time:-}" ]; then
log "βοΈ Skip: cannot get end date for $primary"
continue
fi
left_days="$(days_left_from_endtime "$end_time" || true)"
if [ -z "${left_days:-}" ]; then
log "βοΈ Skip: cannot parse end date: $end_time"
continue
fi
log " π
Expiration Date: $end_time"
log " β³ Days remaining: $left_days"
if is_ip "$primary"; then
if [ "$left_days" -lt "$THRESH_IP_DAYS" ]; then
log "π IP cert needs renew (threshold=$THRESH_IP_DAYS)"
nginx_stop_if_running
issue_ok=0
if [ "${USE_ALPN_FOR_IP:-0}" -eq 1 ]; then
log "π Issue via ALPN(443): $primary"
if "$acme_bin" --issue --server letsencrypt -d "$primary" \
--certificate-profile shortlived --alpn \
--keylength ec-256 --force; then
issue_ok=1
fi
else
log "π Issue via standalone(80): $primary"
if "$acme_bin" --issue --server letsencrypt -d "$primary" \
--certificate-profile shortlived --standalone \
--keylength ec-256 --force; then
issue_ok=1
fi
fi
nginx_start_if_not_running
if [ "$issue_ok" -eq 1 ]; then
install_cert_to_dir "$primary" "$IP_SSL_DIR"
fi
fi
else
if [ "$left_days" -lt "$THRESH_DOMAIN_DAYS" ]; then
log "π Domain cert needs renew (threshold=$THRESH_DOMAIN_DAYS): $primary"
if "$acme_bin" --renew -d "$primary" --ecc --force; then
ssl_dir="$SSL_BASE/$primary"
install_cert_to_dir "$primary" "$ssl_dir"
else
log "β Renew failed: $primary"
fi
fi
fi
done < <(find "$dir_path" -maxdepth 1 -type d -name "*_ecc" -print0)
if [ "$found_any" -eq 0 ]; then
log "β οΈ No *_ecc directories found under $dir_path"
fi
if [ -f "$FLAG" ]; then
log "β»οΈ Restart nginx due to cert updates..."
systemctl restart nginx
rm -f "$FLAG"
log "β
nginx restarted."
else
log "βΉοΈ No cert installed. nginx restart not needed."
fi