From f36f14c3d06e138a6c968b226656445b5f7d4b67 Mon Sep 17 00:00:00 2001 From: hass-demacia Date: Sun, 24 Mar 2024 22:16:20 +0800 Subject: [PATCH] support reverse_proxy with prefix path like ** handle_path ** in caddy2 --- app.py | 1 + pastey/common.py | 31 ++++++++++++++++++++++++++++++- pastey/config.py | 5 ++++- pastey/routes.py | 21 +++++++++++++-------- static/themes/Dark.css | 2 +- static/themes/Light.css | 2 +- templates/401.html | 22 +++++++++++----------- templates/404.html | 22 +++++++++++----------- templates/config.html | 22 +++++++++++----------- templates/index.html | 30 +++++++++++++++--------------- templates/new.html | 24 ++++++++++++------------ templates/view.html | 38 +++++++++++++++++++------------------- 12 files changed, 129 insertions(+), 91 deletions(-) diff --git a/app.py b/app.py index b193c01..dc566de 100644 --- a/app.py +++ b/app.py @@ -31,6 +31,7 @@ config.recent_pastes = int(environ["PASTEY_RECENT_PASTES"]) if "PASTEY_RECENT_PA config.whitelist_cidr = environ["PASTEY_WHITELIST_CIDR"].split(",") if "PASTEY_WHITELIST_CIDR" in environ else config.whitelist_cidr config.blacklist_cidr = environ["PASTEY_BLACKLIST_CIDR"].split(",") if "PASTEY_BLACKLIST_CIDR" in environ else config.blacklist_cidr config.behind_proxy = bool(strtobool(environ["PASTEY_BEHIND_PROXY"])) if "PASTEY_BEHIND_PROXY" in environ else config.behind_proxy +config.handle_path = environ["PASTEY_HANDLE_PATH"] if "PASTEY_HANDLE_PATH" in environ else config.handle_path config.default_theme = environ["PASTEY_DEFAULT_THEME"] if "PASTEY_DEFAULT_THEME" in environ else config.default_theme config.purge_interval = int(environ["PASTEY_PURGE_INTERVAL"]) if "PASTEY_PURGE_INTERVAL" in environ else config.purge_interval config.force_show_recent = bool(strtobool(environ["PASTEY_FORCE_SHOW_RECENT"])) if "PASTEY_FORCE_SHOW_RECENT" in environ else config.force_show_recent diff --git a/pastey/common.py b/pastey/common.py index 5c1130e..a1c00c2 100644 --- a/pastey/common.py +++ b/pastey/common.py @@ -106,4 +106,33 @@ def build_url(request, path="/"): else: protocol = request.url.split('//')[0] if not config.force_https_links else "https:" - return protocol + "//" + domain + path + if path.startswith("/"): + path = path[1:] + + hp = handle_path(request) + + return protocol + "//" + domain + "/" + hp + path + +def redirect_url(request, path="/"): + if path.startswith("/"): + path = path[1:] + + hp = handle_path(request) + + return "/" + hp + path + +def handle_path(request): + + behind_proxy = config.behind_proxy + if not behind_proxy: + return "" + + # only detect reverse proxy HEADER, enable **handle_path** + if not 'X-Forwarded-Proto' in request.headers and not 'X-Forwarded-Port' in request.headers: + return "" + + handle_path = config.handle_path.strip() + + if handle_path: + handle_path += "/" + return handle_path \ No newline at end of file diff --git a/pastey/config.py b/pastey/config.py index 388b18d..9d6d1e8 100644 --- a/pastey/config.py +++ b/pastey/config.py @@ -31,7 +31,10 @@ guess_threshold = 0.20 recent_pastes = 10 # Try to use X-Real-IP or X-Forwarded-For HTTP headers -behind_proxy = False +behind_proxy = True + +# handle path while after reverse proxy, +handle_path = "pastey" # Default theme to display to users default_theme = "Light" diff --git a/pastey/routes.py b/pastey/routes.py index 84a4c28..eeb019d 100644 --- a/pastey/routes.py +++ b/pastey/routes.py @@ -36,17 +36,20 @@ def home(): themes=loaded_themes, force_show_recent=config.force_show_recent, show_cli_button=config.show_cli_button, - script_url=common.build_url(request, "/pastey")) + script_url=common.build_url(request, "/pastey"), + handle_path=common.handle_path(request)) # New paste page @app.route("/new") def new(): whitelisted = common.verify_whitelist(common.get_source_ip(request)) + print("handle_path: " + common.handle_path(request)) return render_template("new.html", whitelisted=whitelisted, languages=supported_languages, active_theme=common.set_theme(request), - themes=loaded_themes) + themes=loaded_themes, + handle_path=common.handle_path(request)) # Config page @app.route("/config") @@ -60,7 +63,8 @@ def config_page(): script_url=common.build_url(request, "/pastey"), whitelisted=whitelisted, active_theme=common.set_theme(request), - themes=loaded_themes) + themes=loaded_themes, + handle_path=common.handle_path(request)) # View paste page @app.route("/view/") @@ -73,7 +77,8 @@ def view(unique_id): url=common.build_url(request, "/view/" + unique_id), whitelisted=common.verify_whitelist(common.get_source_ip(request)), active_theme=common.set_theme(request), - themes=loaded_themes) + themes=loaded_themes, + handle_path=common.handle_path(request)) else: abort(404) @@ -96,7 +101,7 @@ def delete(unique_id): abort(401) functions.delete_paste(unique_id) - return redirect("/") + return redirect(common.redirect_url(request, "/")) # Script download @app.route("/pastey") @@ -127,7 +132,7 @@ def paste(): if 'cli' in request.form: abort(400) else: - return redirect("/new") + return redirect(common.redirect_url(request, "/new")) else: # Verify form options @@ -145,12 +150,12 @@ def paste(): if 'cli' in request.form: return common.build_url(request, "/view/" + unique_id + "#" + quote(key)), 200 else: - return redirect("/view/" + unique_id + "#" + quote(key)) + return redirect(common.redirect_url(request, "/view/" + unique_id + "#" + quote(key))) else: if 'cli' in request.form: return common.build_url(request, "/view/" + unique_id), 200 else: - return redirect("/view/" + unique_id) + return redirect(common.redirect_url(request, "/view/" + unique_id)) # POST new raw paste @app.route('/raw', methods = ['POST']) diff --git a/static/themes/Dark.css b/static/themes/Dark.css index 3b9bb6b..46a08f8 100644 --- a/static/themes/Dark.css +++ b/static/themes/Dark.css @@ -4,7 +4,7 @@ body, table.table tr { } .pastey-logo { - background-image: url('/static/img/pastey-dark.png'); + /* background-image: url('/static/img/pastey-dark.png'); */ } .pastey-navbar { diff --git a/static/themes/Light.css b/static/themes/Light.css index dc3dd9d..73555b6 100644 --- a/static/themes/Light.css +++ b/static/themes/Light.css @@ -1,5 +1,5 @@ .pastey-logo { - background-image: url('/static/img/pastey.png'); + /* background-image: url('/static/img/pastey.png'); */ } .pastey-link { diff --git a/templates/401.html b/templates/401.html index e36861b..35992f7 100644 --- a/templates/401.html +++ b/templates/401.html @@ -7,11 +7,11 @@ Unauthorized | Pastey - - - + + + - +
@@ -33,21 +33,21 @@ - - + +