1
0
mirror of https://github.com/Mailu/Mailu.git synced 2025-03-05 14:55:20 +02:00
Mailu/core/nginx/letsencrypt.py

65 lines
1.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2023-08-09 15:28:07 +02:00
import logging as log
import os
2023-08-09 15:28:07 +02:00
import requests
import sys
import subprocess
2023-08-09 15:28:07 +02:00
import time
2023-08-09 15:28:07 +02:00
log.basicConfig(stream=sys.stderr, level="WARNING")
2022-03-17 11:35:31 +01:00
hostnames = ','.join(set(host.strip() for host in os.environ['HOSTNAMES'].split(',')))
2022-03-10 12:31:01 +01:00
command = [
"certbot",
"-n", "--agree-tos", # non-interactive
2022-03-10 12:31:01 +01:00
"-d", hostnames, "--expand", "--allow-subset-of-names",
"-m", "{}@{}".format(os.environ["POSTMASTER"], os.environ["DOMAIN"]),
"certonly", "--standalone",
"--cert-name", "mailu",
"--preferred-challenges", "http", "--http-01-port", "8008",
"--keep-until-expiring",
"--allow-subset-of-names",
2021-08-23 19:41:44 +02:00
"--renew-with-new-domains",
2021-08-09 21:06:15 +02:00
"--config-dir", "/certs/letsencrypt",
"--post-hook", "/config.py"
]
command2 = [
"certbot",
"-n", "--agree-tos", # non-interactive
2022-03-10 12:31:01 +01:00
"-d", hostnames, "--expand", "--allow-subset-of-names",
2021-08-09 21:06:15 +02:00
"-m", "{}@{}".format(os.environ["POSTMASTER"], os.environ["DOMAIN"]),
"certonly", "--standalone",
"--cert-name", "mailu-ecdsa",
"--preferred-challenges", "http", "--http-01-port", "8008",
"--keep-until-expiring",
"--allow-subset-of-names",
2021-08-09 21:06:15 +02:00
"--key-type", "ecdsa",
2021-08-23 19:41:44 +02:00
"--renew-with-new-domains",
"--config-dir", "/certs/letsencrypt",
"--post-hook", "/config.py"
]
# Wait for nginx to start
time.sleep(5)
# Run certbot every day
while True:
2023-08-09 15:28:07 +02:00
while True:
2023-10-17 13:58:38 +02:00
hostname = os.environ['HOSTNAMES'].split(',')[0]
2023-08-09 15:28:07 +02:00
target = f'http://{hostname}/.well-known/acme-challenge/testing'
2024-09-12 19:21:18 +02:00
try:
r = requests.get(target)
if r.status_code != 204:
log.critical(f"Can't reach {target}!, please ensure it's fixed or change the TLS_FLAVOR.")
time.sleep(5)
else:
break
except Exception as e:
log.error(f"Exception while fetching {target}!", exc_info = e)
2024-09-12 22:48:20 +02:00
time.sleep(15)
2023-08-09 15:28:07 +02:00
subprocess.call(command)
2021-08-09 21:06:15 +02:00
subprocess.call(command2)
time.sleep(86400)