From 64ce3d1c968548dd23658d526fd29ca0d1e83f53 Mon Sep 17 00:00:00 2001 From: Florent Daigniere Date: Wed, 9 Aug 2023 15:28:07 +0200 Subject: [PATCH] Implement a busy loop for letsencrypt --- core/nginx/letsencrypt.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/core/nginx/letsencrypt.py b/core/nginx/letsencrypt.py index 993e7f9f..ddac05b1 100755 --- a/core/nginx/letsencrypt.py +++ b/core/nginx/letsencrypt.py @@ -1,9 +1,15 @@ #!/usr/bin/env python3 +import logging as log import os -import time +import requests +import sys import subprocess +import time +from threading import Thread +from http.server import HTTPServer, SimpleHTTPRequestHandler +log.basicConfig(stream=sys.stderr, level="WARNING") hostnames = ','.join(set(host.strip() for host in os.environ['HOSTNAMES'].split(','))) command = [ @@ -39,8 +45,25 @@ command2 = [ # Wait for nginx to start time.sleep(5) +def serve_one_request(): + with HTTPServer(("0.0.0.0", 8008), SimpleHTTPRequestHandler) as server: + server.handle_request() + # Run certbot every day while True: + while True: + hostname = os.environ['HOSTNAMES'].split(' ')[0] + target = f'http://{hostname}/.well-known/acme-challenge/testing' + thread = Thread(target=serve_one_request) + thread.start() + r = requests.get(target) + if r.status_code != 404: + log.error(f"Can't reach {target}!, please ensure it's fixed or change the TLS_FLAVOR.") + time.sleep(5) + else: + break + thread.join() + subprocess.call(command) subprocess.call(command2) time.sleep(86400)