web/caddy/generate_caddy_file.py

95 lines
2.4 KiB
Python
Raw Permalink Normal View History

#!/bin/env python3
2024-01-31 00:48:20 +00:00
from os import listdir
from os.path import isfile, join
from typing import Dict, List
2024-01-31 00:48:20 +00:00
from jinja2 import Template
2024-09-17 14:43:11 +00:00
# Constants
2024-09-19 15:52:42 +00:00
APP_ROOT = "/static"
2024-09-17 14:43:11 +00:00
STATIC_DIR = "./static"
ACCESS_DENIED_PATHS: List[str] = [
"websocket",
"meta.json",
"cdn-cgi/challenge-platform/scripts/jsd/main.js",
"cdn-cgi/rum",
"graphql/websocket",
"onboarding/*",
"wp-*",
".env",
"api*",
"apple-touch-icon-precomposed.png",
"rss.xml",
".git/*",
"apple-touch-icon-120x120.png",
"apple-touch-icon-120x120-precomposed.png",
"apple-touch-icon-152x152.png",
"apple-touch-icon-152x152-precomposed.png",
".well-known/*",
"cdn-cgi/challenge-platform/h/b/orchestrate/chl_page/v1",
"cdn-cgi/challenge-platform/h/g/orchestrate/chl_page/v1",
]
CADDY_FILE_TEMPLATES: Dict[str, str] = {
"CaddyfileTemplate": "Caddyfile",
2024-09-17 14:43:11 +00:00
}
2024-01-31 00:48:20 +00:00
2024-09-17 14:43:11 +00:00
def get_static_files(directory: str) -> List[str]:
return [f for f in listdir(directory) if isfile(join(directory, f))]
2024-01-31 00:48:20 +00:00
2024-09-17 14:43:11 +00:00
def generate_static_file_rules(files: List[str]) -> List[str]:
template = Template(
"""
2024-01-31 00:48:20 +00:00
handle_path /{{ file }} {
2024-09-19 15:52:42 +00:00
root * {{ app_root }}/{{ file }}
file_server
2024-01-31 00:48:20 +00:00
}
2024-09-17 14:43:11 +00:00
"""
)
2024-09-19 15:52:42 +00:00
return [template.render(file=file, app_root=APP_ROOT) for file in files]
2024-01-31 00:48:20 +00:00
2024-09-17 14:43:11 +00:00
def generate_access_denied_rules(paths: List[str]) -> List[str]:
template = Template(
"""
2024-01-31 00:48:20 +00:00
handle_path /{{ file }} {
respond "Access denied" 403
}
2024-09-17 14:43:11 +00:00
"""
)
return [template.render(file=path) for path in paths]
2024-01-31 00:48:20 +00:00
2024-09-17 14:43:11 +00:00
def generate_caddy_rules() -> str:
static_files = get_static_files(STATIC_DIR)
static_rules = generate_static_file_rules(static_files)
denied_rules = generate_access_denied_rules(ACCESS_DENIED_PATHS)
return "\n".join(static_rules + denied_rules)
2024-01-31 00:48:20 +00:00
2024-09-17 14:43:11 +00:00
def render_caddy_file(template_path: str, output_path: str, rules: str) -> None:
try:
with open(template_path, "r") as file:
template = Template(file.read())
2024-01-31 00:48:20 +00:00
2024-09-17 14:43:11 +00:00
rendered_content = template.render(template=rules)
with open(output_path, "w") as file:
file.write(rendered_content)
except IOError as e:
print(f"Error processing {template_path}: {e}")
def main() -> None:
rules = generate_caddy_rules()
for template_file, output_file in CADDY_FILE_TEMPLATES.items():
render_caddy_file(template_file, output_file, rules)
2024-01-31 00:48:20 +00:00
2024-09-17 14:43:11 +00:00
if __name__ == "__main__":
main()