bunkerweb/core/customcert/jobs/custom-cert.py
2022-06-03 17:24:14 +02:00

67 lines
2.4 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/python3
import sys, os, subprocess, traceback
sys.path.append("/opt/bunkerweb/deps/python")
sys.path.append("/opt/bunkerweb/utils")
import logger, jobs
def check_cert(cert_path) :
try :
cache_path = "/opt/bunkerweb/cache/customcert/" + cert_path.replace("/", "_") + ".hash"
current_hash = jobs.file_hash(cert_path)
if not os.path.isfile(cache_path) :
with open(cache_path, "w") as f :
f.write(current_hash)
old_hash = jobs.file_hash(cache_path)
if old_hash == current_hash :
return False
with open(cache_path, "w") as f :
f.write(current_hash)
return True
except :
logger.log("CUSTOM-CERT", "", "Exception while running custom-cert.py (check_cert) :")
print(traceback.format_exc())
return False
status = 0
try :
os.makedirs("/opt/bunkerweb/cache/customcert/", exist_ok=True)
# Multisite case
if os.getenv("MULTISITE") == "yes" :
for first_server in os.getenv("SERVER_NAME").split(" ") :
if os.getenv(first_server + "_USE_CUSTOM_HTTPS", os.getenv("USE_CUSTOM_HTTPS")) != "yes" :
continue
if first_server == "" :
continue
cert_path = os.getenv(first_server + "_CUSTOM_HTTPS_CERT")
logger.log("CUSTOM-CERT", "", "Checking if certificate " + cert_path + " changed ...")
need_reload = check_cert(cert_path)
if need_reload :
logger.log("CUSTOM-CERT", "", "Detected change for certificate " + cert_path)
status = 1
else :
logger.log("CUSTOM-CERT", "", "No change for certificate " + cert_path)
# Singlesite case
elif os.getenv("USE_CUSTOM_HTTPS") == "yes" and os.getenv("SERVER_NAME") != "" :
cert_path = os.getenv("CUSTOM_HTTPS_CERT")
logger.log("CUSTOM-CERT", "", "Checking if certificate " + cert_path + " changed ...")
need_reload = check_cert(cert_path)
if need_reload :
logger.log("CUSTOM-CERT", "", "Detected change for certificate " + cert_path)
status = 1
else :
logger.log("CUSTOM-CERT", "", "No change for certificate " + cert_path)
except :
status = 2
logger.log("CUSTOM-CERT", "", "Exception while running custom-cert.py :")
print(traceback.format_exc())
sys.exit(status)