Update scripts/generate-caddyfile.sh

This commit is contained in:
2026-06-15 09:32:06 +00:00
parent 1b6c775e56
commit bea2659041
+9 -14
View File
@@ -234,30 +234,25 @@ with open(caddy_file, "a", encoding="utf-8") as f:
for method in rule["methods"]:
allowed_by_method.setdefault(method, []).append(rule["ip_cidr"])
for method in WRITE_METHODS:
matcher = f"blocked_{safe_name}_{method.lower()}"
for method in ALL_METHODS:
allowed_ips = sorted(set(allowed_by_method.get(method, [])))
# Core rule:
# If a method has no rules, it stays open.
# If a method has at least one rule, it becomes IP-whitelisted.
if not allowed_ips:
continue
matcher = f"blocked_{safe_name}_{method.lower()}"
f.write(f" @{matcher} {{\n")
f.write(f" path /apps/{app_id}/*\n")
f.write(f" method {method}\n")
f.write(" not path " + " ".join(open_paths) + "\n")
if allowed_ips:
f.write(" not remote_ip " + " ".join(allowed_ips) + "\n")
f.write(" }\n")
f.write(f" respond @{matcher} \"Forbidden\" 403\n\n")
get_allowed_ips = sorted(set(allowed_by_method.get("GET", [])))
if get_allowed_ips:
matcher = f"blocked_{safe_name}_get"
f.write(f" @{matcher} {{\n")
f.write(f" path /apps/{app_id}/*\n")
f.write(" method GET\n")
f.write(" not path " + " ".join(open_paths) + "\n")
f.write(" not remote_ip " + " ".join(get_allowed_ips) + "\n")
f.write(" }\n")
f.write(f" respond @{matcher} \"Forbidden\" 403\n\n")
f.write(f" handle_path /apps/{app_id}/* {{\n")
f.write(f" reverse_proxy {app_id}:{port}\n")
f.write(" }\n\n")