9
0
mirror of https://github.com/donlon/cloudflare-error-page.git synced 2025-12-19 14:59:28 +00:00

examples: update url extraction rule

This commit is contained in:
Anthony Donlon
2025-11-20 00:40:15 +08:00
parent 71898c4558
commit 53e1f4d4a0
2 changed files with 24 additions and 8 deletions

2
examples/example.py Normal file → Executable file
View File

@@ -1,3 +1,5 @@
#!/usr/bin/env python3
import os import os
import sys import sys
import webbrowser import webbrowser

30
examples/flask_server.py Normal file → Executable file
View File

@@ -1,3 +1,5 @@
#!/usr/bin/env python3
import os import os
import re import re
import sys import sys
@@ -14,6 +16,7 @@ examples_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(examples_dir)) sys.path.append(os.path.dirname(examples_dir))
from cloudflare_error_page import get_resources_folder, render as render_cf_error_page from cloudflare_error_page import get_resources_folder, render as render_cf_error_page
# TODO: use blueprint
app = Flask(__name__) app = Flask(__name__)
# You can use resources from Cloudflare CDN. But in case of changing, you can set use_cdn = False to use bundled resources. # You can use resources from Cloudflare CDN. But in case of changing, you can set use_cdn = False to use bundled resources.
@@ -24,7 +27,7 @@ if use_cdn:
# This handler is used to provide stylesheet and icon resources for the error page. If you pass use_cdn=True to render_cf_error_page # This handler is used to provide stylesheet and icon resources for the error page. If you pass use_cdn=True to render_cf_error_page
# or if your site is under proxy of Cloudflare (the cdn-cgi folder is already provided by Cloudflare), this handler can be removed. # or if your site is under proxy of Cloudflare (the cdn-cgi folder is already provided by Cloudflare), this handler can be removed.
@app.route("/cdn-cgi/<path:path>") @app.route('/cdn-cgi/<path:path>')
def cdn_cgi(path): def cdn_cgi(path):
return send_from_directory(res_folder, path) return send_from_directory(res_folder, path)
@@ -32,7 +35,7 @@ if use_cdn:
param_cache: dict[str, dict] = {} param_cache: dict[str, dict] = {}
def get_page_params(name: str) -> dict: def get_page_params(name: str) -> dict:
name = re.sub(r'[/\\]', '', name) name = re.sub(r'[^\w]', '', name)
params = param_cache.get(name) params = param_cache.get(name)
if params is not None: if params is not None:
return params return params
@@ -45,9 +48,11 @@ def get_page_params(name: str) -> dict:
return None return None
@app.route("/", defaults={'name': 'default'}) @app.route('/', defaults={'name': 'default'})
@app.route("/<name>") @app.route('/<path:name>')
def index(name: str): def index(name: str):
name = os.path.basename(name) # keep only the base name
params = get_page_params(name) params = get_page_params(name)
if params is None: if params is None:
abort(404) abort(404)
@@ -62,13 +67,22 @@ def index(name: str):
params = { params = {
**params, **params,
"ray_id": ray_id, 'ray_id': ray_id,
"client_ip": client_ip, 'client_ip': client_ip,
} }
# Render the error page # Render the error page
return render_cf_error_page(params, use_cdn=use_cdn), 500 return render_cf_error_page(params, use_cdn=use_cdn), 500
if __name__ == "__main__": if __name__ == '__main__':
app.run(debug=True) if len(sys.argv) > 1:
host = sys.argv[1]
else:
host = None
if len(sys.argv) > 2:
port = int(sys.argv[2])
else:
port = None
app.run(debug=True, host=host, port=port)