mirror of
https://github.com/bunkerity/bunkerweb
synced 2026-05-24 09:28:37 +00:00
Add better custom certificate cache handling + made a few tweaks with the jobs utils
This commit is contained in:
parent
14172ab70b
commit
8c3ec6b24c
3 changed files with 75 additions and 25 deletions
|
|
@ -18,7 +18,7 @@ for deps_path in [
|
|||
if deps_path not in sys_path:
|
||||
sys_path.append(deps_path)
|
||||
|
||||
from jobs import cache_file, cache_hash, file_hash
|
||||
from jobs import del_file_in_db, cache_file, cache_hash, file_hash
|
||||
from Database import Database # type: ignore
|
||||
from logger import setup_logger # type: ignore
|
||||
|
||||
|
|
@ -102,9 +102,8 @@ try:
|
|||
cert_data = b64decode(getenv("CUSTOM_SSL_CERT_DATA", ""))
|
||||
key_data = b64decode(getenv("CUSTOM_SSL_KEY_DATA", ""))
|
||||
for file, data in (("cert.pem", cert_data), ("key.pem", key_data)):
|
||||
if data != b"":
|
||||
if data:
|
||||
file_path = Path(sep, "var", "tmp", "bunkerweb", "customcert", f"{first_server}.{file}")
|
||||
file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
file_path.write_bytes(data)
|
||||
if file == "cert.pem":
|
||||
cert_path = str(file_path)
|
||||
|
|
@ -119,7 +118,28 @@ try:
|
|||
status = 1
|
||||
else:
|
||||
logger.info(f"No change for certificate {cert_path}")
|
||||
|
||||
elif not cert_path or not key_path:
|
||||
logger.warning("Both variables CUSTOM_SSL_CERT and CUSTOM_SSL_KEY (or CUSTOM_SSL_CERT_DATA and CUSTOM_SSL_KEY_DATA) have to be set to use custom certificates, clearing cache ...")
|
||||
cert_cache_path = Path(
|
||||
sep,
|
||||
"var",
|
||||
"cache",
|
||||
"bunkerweb",
|
||||
"customcert",
|
||||
f"{first_server}.cert.pem",
|
||||
)
|
||||
cert_cache_path.unlink(missing_ok=True)
|
||||
del_file_in_db(f"{first_server}.cert.pem", db, service_id=first_server)
|
||||
key_cache_path = Path(
|
||||
sep,
|
||||
"var",
|
||||
"cache",
|
||||
"bunkerweb",
|
||||
"customcert",
|
||||
f"{first_server}.key.pem",
|
||||
)
|
||||
key_cache_path.unlink(missing_ok=True)
|
||||
del_file_in_db(f"{first_server}.key.pem", db, service_id=first_server)
|
||||
elif getenv("MULTISITE", "no") == "yes":
|
||||
servers = getenv("SERVER_NAME") or []
|
||||
|
||||
|
|
@ -162,6 +182,28 @@ try:
|
|||
logger.info(
|
||||
f"No change for certificate {cert_path}",
|
||||
)
|
||||
elif not cert_path or not key_path:
|
||||
logger.warning("Both variables CUSTOM_SSL_CERT and CUSTOM_SSL_KEY (or CUSTOM_SSL_CERT_DATA and CUSTOM_SSL_KEY_DATA) have to be set to use custom certificates, clearing cache ...")
|
||||
cert_cache_path = Path(
|
||||
sep,
|
||||
"var",
|
||||
"cache",
|
||||
"bunkerweb",
|
||||
"customcert",
|
||||
f"{first_server}.cert.pem",
|
||||
)
|
||||
cert_cache_path.unlink(missing_ok=True)
|
||||
del_file_in_db(f"{first_server}.cert.pem", db)
|
||||
key_cache_path = Path(
|
||||
sep,
|
||||
"var",
|
||||
"cache",
|
||||
"bunkerweb",
|
||||
"customcert",
|
||||
f"{first_server}.key.pem",
|
||||
)
|
||||
key_cache_path.unlink(missing_ok=True)
|
||||
del_file_in_db(f"{first_server}.key.pem", db)
|
||||
except:
|
||||
status = 2
|
||||
logger.error(f"Exception while running custom-cert.py :\n{format_exc()}")
|
||||
|
|
|
|||
|
|
@ -1043,10 +1043,10 @@ class Database:
|
|||
|
||||
return ""
|
||||
|
||||
def delete_job_cache(self, file_name: str, *, job_name: Optional[str] = None):
|
||||
def delete_job_cache(self, file_name: str, *, job_name: Optional[str] = None, service_id: Optional[str] = None):
|
||||
job_name = job_name or basename(getsourcefile(_getframe(1))).replace(".py", "")
|
||||
with self.__db_session() as session:
|
||||
session.query(Jobs_cache).filter_by(job_name=job_name, file_name=file_name).delete()
|
||||
session.query(Jobs_cache).filter_by(job_name=job_name, file_name=file_name, service_id=service_id).delete()
|
||||
|
||||
def update_job_cache(
|
||||
self,
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ from hashlib import sha512
|
|||
from inspect import getsourcefile
|
||||
from io import BufferedReader
|
||||
from json import dumps, loads
|
||||
from os.path import basename, normpath
|
||||
from os.path import basename
|
||||
from pathlib import Path
|
||||
from sys import _getframe
|
||||
from threading import Lock
|
||||
|
|
@ -28,17 +28,19 @@ def is_cached_file(
|
|||
expire: Union[Literal["hour"], Literal["day"], Literal["week"], Literal["month"]],
|
||||
db=None,
|
||||
) -> bool:
|
||||
if not isinstance(file, Path):
|
||||
file = Path(file)
|
||||
|
||||
is_cached = False
|
||||
cached_file = None
|
||||
try:
|
||||
file = normpath(file)
|
||||
file_path = Path(f"{file}.md")
|
||||
if not file_path.is_file():
|
||||
if not db:
|
||||
return False
|
||||
cached_file = db.get_job_cache_file(
|
||||
basename(getsourcefile(_getframe(1))).replace(".py", ""),
|
||||
basename(file),
|
||||
file.name,
|
||||
with_info=True,
|
||||
)
|
||||
|
||||
|
|
@ -65,16 +67,13 @@ def is_cached_file(
|
|||
is_cached = False
|
||||
|
||||
if is_cached and cached_file:
|
||||
Path(file).write_bytes(cached_file.data)
|
||||
file.write_bytes(cached_file.data)
|
||||
|
||||
return is_cached and cached_file
|
||||
|
||||
|
||||
def get_file_in_db(file: Union[str, Path], db, *, job_name: Optional[str] = None) -> Optional[bytes]:
|
||||
cached_file = db.get_job_cache_file(
|
||||
job_name or basename(getsourcefile(_getframe(1))).replace(".py", ""),
|
||||
normpath(file),
|
||||
)
|
||||
cached_file = db.get_job_cache_file(job_name or basename(getsourcefile(_getframe(1))).replace(".py", ""), file)
|
||||
if not cached_file:
|
||||
return None
|
||||
return cached_file.data
|
||||
|
|
@ -107,10 +106,10 @@ def set_file_in_db(
|
|||
return ret, err
|
||||
|
||||
|
||||
def del_file_in_db(name: str, db) -> Tuple[bool, str]:
|
||||
def del_file_in_db(name: str, db, *, service_id: Optional[str] = None) -> Tuple[bool, str]:
|
||||
ret, err = True, "success"
|
||||
try:
|
||||
db.delete_job_cache(name, job_name=basename(getsourcefile(_getframe(1))).replace(".py", ""))
|
||||
db.delete_job_cache(name, job_name=basename(getsourcefile(_getframe(1))).replace(".py", ""), service_id=service_id)
|
||||
except:
|
||||
return False, f"exception :\n{format_exc()}"
|
||||
return ret, err
|
||||
|
|
@ -118,7 +117,10 @@ def del_file_in_db(name: str, db) -> Tuple[bool, str]:
|
|||
|
||||
def file_hash(file: Union[str, Path]) -> str:
|
||||
_sha512 = sha512()
|
||||
with open(normpath(file), "rb") as f:
|
||||
if not isinstance(file, Path):
|
||||
file = Path(file)
|
||||
|
||||
with file.open("rb") as f:
|
||||
while True:
|
||||
data = f.read(1024)
|
||||
if not data:
|
||||
|
|
@ -139,18 +141,24 @@ def bytes_hash(bio: BufferedReader) -> str:
|
|||
|
||||
|
||||
def cache_hash(cache: Union[str, Path], db=None) -> Optional[str]:
|
||||
checksum = None
|
||||
with suppress(BaseException):
|
||||
return loads(Path(normpath(f"{cache}.md")).read_text(encoding="utf-8")).get("checksum", None)
|
||||
if db:
|
||||
checksum = loads(Path(f"{cache}.md").read_text(encoding="utf-8")).get("checksum", None)
|
||||
|
||||
if not checksum and db:
|
||||
if not isinstance(cache, Path):
|
||||
cache = Path(cache)
|
||||
|
||||
cached_file = db.get_job_cache_file(
|
||||
basename(getsourcefile(_getframe(1))).replace(".py", ""),
|
||||
basename(normpath(cache)),
|
||||
cache.name,
|
||||
with_info=True,
|
||||
with_data=False,
|
||||
)
|
||||
checksum = cached_file.checksum if cached_file else None
|
||||
|
||||
if cached_file:
|
||||
return cached_file.checksum
|
||||
if checksum:
|
||||
return checksum
|
||||
return None
|
||||
|
||||
|
||||
|
|
@ -166,9 +174,9 @@ def cache_file(
|
|||
ret, err = True, "success"
|
||||
try:
|
||||
if not isinstance(file, Path):
|
||||
file = Path(normpath(file))
|
||||
file = Path(file)
|
||||
if not isinstance(cache, Path):
|
||||
cache = Path(normpath(cache))
|
||||
cache = Path(cache)
|
||||
|
||||
content = file.read_bytes()
|
||||
cache.write_bytes(content)
|
||||
|
|
@ -181,7 +189,7 @@ def cache_file(
|
|||
|
||||
if db:
|
||||
return set_file_in_db(
|
||||
basename(str(cache)),
|
||||
cache.name,
|
||||
content,
|
||||
db,
|
||||
job_name=basename(getsourcefile(_getframe(1))).replace(".py", ""),
|
||||
|
|
|
|||
Loading…
Reference in a new issue