Files
RustProxy/scripts/generate_cf_pages.py
T
2026-11-28 17:36:53 +01:00

151 lines
11 KiB
Python
Executable File

#!/usr/bin/env python3
"""Generate self-contained RustProxy status pages with classic 3-node error layout.
Visual reference: donlon/cloudflare-error-page (MIT license), without its
Cloudflare logos, links, remote assets or fictitious Cloudflare diagnostics.
The generated HTML is consumed using the existing include_str! page mechanism.
"""
from __future__ import annotations
from html import escape
from pathlib import Path
WEB = Path(__file__).resolve().parents[1] / "src/admin/web"
ROOT = WEB / "cf_page"
LOGO = '<span class="brand-mark" aria-label="RustProxy">r<span>/</span>p</span>'
STATUSES = [
(200, "Service ready", "RustProxy received and handled the request.", "No further action is required for this response.", "Working", "Not checked", "ok", "unknown"),
(400, "Bad request", "The request was malformed or could not be understood.", "Check the request and try again.", "Request rejected", "Not contacted", "error", "unknown"),
(401, "Authentication required", "This resource requires valid credentials.", "Sign in with an authorized account and retry.", "Authentication required", "Not contacted", "error", "unknown"),
(403, "Access forbidden", "Access to the requested resource is not permitted.", "Contact the website administrator if you believe this is an error.", "Access denied", "Not contacted", "error", "unknown"),
(404, "Page not found", "The requested resource could not be found.", "Check the address or return to the website homepage.", "Not found", "Not confirmed", "error", "unknown"),
(408, "Request timeout", "The request took too long to complete.", "Try again when your connection is stable.", "Timed out", "Not confirmed", "error", "unknown"),
(410, "Resource gone", "The requested resource is no longer available.", "Update any saved links to this resource.", "No longer available", "Not contacted", "error", "unknown"),
(413, "Content too large", "The request body exceeds the allowed size.", "Reduce the size of the request and try again.", "Request rejected", "Not contacted", "error", "unknown"),
(429, "Too many requests", "The request rate limit has been exceeded.", "Wait a moment before trying again.", "Rate limited", "Not contacted", "error", "unknown"),
(500, "Internal server error", "An unexpected error occurred while processing this request.", "Try again shortly. If the issue persists, contact the administrator.", "Error", "Not confirmed", "error", "unknown"),
(502, "Bad gateway", "The gateway could not obtain a valid response from the upstream service.", "Try again in a few minutes. If the problem continues, contact the administrator.", "Working", "Connection error", "ok", "error"),
(503, "Service unavailable", "The service is temporarily unable to handle this request.", "Please try again in a few minutes.", "Temporarily unavailable", "Not confirmed", "error", "unknown"),
(504, "Gateway timeout", "The upstream service did not respond in time.", "Try again shortly. If the problem continues, contact the administrator.", "Working", "Timed out", "ok", "error"),
]
CSS = """
:root {color-scheme:light;font-family:-apple-system,BlinkMacSystemFont,"Helvetica Neue",Helvetica,Arial,sans-serif;color:#333}
* {box-sizing:border-box}
body {margin:0;background:#fff;min-height:100vh;font-size:15px;line-height:1.5;-webkit-font-smoothing:antialiased}
.container {max-width:1030px;margin:0 auto;padding:0 24px}
.intro {padding:54px 24px 44px}
h1 {font-size:46px;line-height:1.2;font-weight:300;margin:0;letter-spacing:-1.1px;color:#313131;overflow-wrap:anywhere}
.status-code {font-weight:300;color:#737373;white-space:nowrap}
.eyebrow {font-size:12px;letter-spacing:.12em;text-transform:uppercase;font-weight:600;color:#686868;margin:0 0 12px}
.success .eyebrow {color:#527b24}
.placeholder .eyebrow {color:#686868}
.diagnostics-band {background:#f1f1f1;border-top:1px solid #e9e9e9;border-bottom:1px solid #e5e5e5}
.diagnostics {display:grid;grid-template-columns:minmax(0,1fr) 76px minmax(0,1fr) 76px minmax(0,1fr);align-items:start;padding:39px 24px 36px;text-align:center}
.node {min-width:0;text-align:center}
.symbol {height:107px;position:relative;margin:0 auto 11px;width:142px;max-width:100%;display:flex;align-items:center;justify-content:center}
.symbol svg {width:118px;height:100px;display:block;overflow:visible}
/* Dashboard brand mark, matching css/20-shell.css (standalone page colors). */
.symbol .brand-mark {font:700 33px ui-monospace,SFMono-Regular,Consolas,monospace;border:1px solid #f69b74;color:#f69b74;place-content:center;position:relative;box-sizing:border-box;padding-right:2px;display:flex;align-items:center;justify-content:center;letter-spacing:-3px;flex-shrink:0;width:47px;height:47px;font-size:29px}
.symbol .brand-mark span {font-size:22px;left:29px;top:19px;color:#cfe0ac;position:static}
.symbol .ind {position:absolute;right:10px;bottom:10px;display:grid;place-items:center;border:3px solid #f1f1f1;border-radius:50%;height:37px;width:37px;font-family:Arial,sans-serif;font-size:24px;font-weight:700;line-height:1;color:white;background:#9bca3e}
.symbol .ind.error {background:#c93135}
.symbol .ind.unknown {background:#aaa;font-weight:500;font-size:26px}
.location {font-size:14px;color:#676767;margin:0;overflow-wrap:anywhere}
.node h2 {font-size:21px;font-weight:400;margin:1px 0 0;color:#545454;overflow-wrap:anywhere}
.state {font-size:16px;font-weight:500;margin:2px 0 0;color:#527b24;overflow-wrap:anywhere}
.state.error {color:#c93135}
.state.unknown {color:#666}
.connector {padding-top:53px;min-width:0;color:#aaa}
.connector svg {height:30px;width:100%;display:block;fill:none;stroke:currentColor;stroke-width:4;stroke-linecap:round;stroke-linejoin:round}
.help {display:grid;grid-template-columns:1fr 1fr;gap:58px;padding:41px 0 45px}
.help h2 {font-size:20px;line-height:1.35;font-weight:400;color:#333;margin:0 0 12px}
.help p {font-size:15px;line-height:1.6;margin:0;color:#555}
.bottom {border-top:1px solid #e7e7e7;margin-top:7px;padding:22px 0 35px;color:#666;font-size:12px;text-align:center}
.brand {color:#454545;font-weight:600;letter-spacing:.02em}
@media(max-width:700px) {
.intro {padding:35px 24px 30px}h1{font-size:36px;letter-spacing:-.7px}
.diagnostics{grid-template-columns:repeat(3,minmax(0,1fr));padding:25px 14px;gap:4px}
.connector{display:none}.symbol{height:76px;width:86px;margin-bottom:6px}.symbol svg{width:77px;height:70px}
.symbol .ind{width:28px;height:28px;font-size:18px;border-width:2px;right:0;bottom:1px}
.symbol .ind.unknown{font-size:21px}.location{font-size:12px}.node h2{font-size:15px}.state{font-size:13px}
.help{grid-template-columns:1fr;gap:24px;padding:31px 0}
}
@media(max-width:360px){.container{padding-inline:14px}.intro{padding-top:27px}h1{font-size:31px}.symbol{width:70px}.symbol svg{width:69px}.node h2{font-size:14px}.state{font-size:12px}}
""".strip()
ICONS = {
"browser": '''<svg viewBox="0 0 120 106" aria-hidden="true" focusable="false"><rect x="13" y="16" width="94" height="69" rx="4" fill="#aaa"/><rect x="21" y="33" width="78" height="44" fill="#f1f1f1"/><circle cx="23" cy="24" r="3" fill="#f1f1f1"/><circle cx="34" cy="24" r="3" fill="#f1f1f1"/><path d="M14 31h93" stroke="#f1f1f1" stroke-width="2"/></svg>''',
"proxy": LOGO,
"origin": '''<svg viewBox="0 0 120 106" aria-hidden="true" focusable="false"><rect x="22" y="13" width="76" height="27" rx="4" fill="#aaa"/><rect x="22" y="46" width="76" height="27" rx="4" fill="#aaa"/><circle cx="34" cy="27" r="4" fill="#f1f1f1"/><circle cx="34" cy="60" r="4" fill="#f1f1f1"/><path d="M48 27h38M48 60h38" stroke="#d9d9d9" stroke-width="4" stroke-linecap="round"/><path d="M60 73v14m-21 0h42" stroke="#aaa" stroke-width="7" stroke-linecap="round"/></svg>''',
}
def node(icon: str, location: str, title: str, state: str, indicator: str) -> str:
indicator_glyph = {"ok": "&#10003;", "error": "&#215;", "unknown": "&#8211;"}[indicator]
return (
f'<div class="node"><div class="symbol">{ICONS[icon]}'
f'<span class="ind {indicator}" aria-hidden="true">{indicator_glyph}</span></div>'
f'<p class="location">{escape(location)}</p><h2>{escape(title)}</h2>'
f'<p class="state {indicator}">{escape(state)}</p></div>'
)
def make_page(code: int | None, title: str, happened: str, advice: str, gateway: str, origin: str,
gateway_indicator: str, origin_indicator: str, *, variant: str = "status") -> str:
arrow = '<div class="connector" aria-hidden="true"><svg viewBox="0 0 100 30"><path d="M8 15h84m-74-10L8 15l10 10M82 5l10 10-10 10"/></svg></div>'
is_error = code is not None and code >= 400
theme = "error" if is_error else "placeholder" if variant == "placeholder" else "success"
status = f"Error {code}" if is_error else f"HTTP {code}" if code is not None else ""
code_markup = f' <span class="status-code">{status}</span>' if status else ""
eyebrow = {"welcome": "Welcome", "placeholder": "Placeholder", "status": "Request status"}[variant]
heading = "What happened?" if is_error else "About this page"
next_heading = "What can I do?" if is_error else "What comes next?"
return f'''<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="robots" content="noindex,nofollow">
<title>{escape(title)}{f' | {status}' if status else ''} | RustProxy</title>
<style>{CSS}</style>
</head>
<body class="{theme}">
<header class="container intro"><p class="eyebrow">{eyebrow}</p><h1>{escape(title)}{code_markup}</h1></header>
<section class="diagnostics-band" aria-label="Request status"><div class="container diagnostics">
{node('browser', 'You', 'Browser', 'Request sent', 'ok')}
{arrow}
{node('proxy', 'Gateway', 'RustProxy', gateway, gateway_indicator)}
{arrow}
{node('origin', 'Origin', 'Host', origin, origin_indicator)}
</div></section>
<main class="container"><section class="help" aria-label="Response information">
<div><h2>{heading}</h2><p>{escape(happened)}</p></div>
<div><h2>{next_heading}</h2><p>{escape(advice)}</p></div>
</section><footer class="bottom">Powered by <span class="brand">RustProxy</span></footer>
</main>
</body>
</html>
'''
VARIANTS = [
("welcome", "Congratulations! RustProxy is running.",
"RustProxy is installed and serving this page. This does not verify an upstream application.",
"The administrator can configure a domain and backend or publish a local web service.",
"Ready", "Not checked", "ok", "unknown"),
("placeholder", "Site placeholder",
"This address is displaying a RustProxy placeholder instead of application content.",
"Check the address or contact the administrator for information about this service.",
"Page served", "Not checked", "ok", "unknown"),
]
def main() -> None:
ROOT.mkdir(parents=True, exist_ok=True)
for entry in STATUSES:
path = ROOT / f"error_{entry[0]}.html"
path.write_text(make_page(*entry), encoding="utf-8")
for variant, *entry in VARIANTS:
(ROOT / f"{variant}.html").write_text(make_page(None, *entry, variant=variant), encoding="utf-8")
print(f"Generated {len(STATUSES) + len(VARIANTS)} offline RustProxy-branded pages")
if __name__ == "__main__":
main()