forked from username/flaskpaste
client: auto-detect server URL from request headers
This commit is contained in:
@@ -247,12 +247,38 @@ def _get_client_id() -> str | None:
|
||||
|
||||
@bp.route("/client", methods=["GET"])
|
||||
def client():
|
||||
"""Download the fpaste CLI client."""
|
||||
"""Download the fpaste CLI client with server URL pre-configured."""
|
||||
import os
|
||||
|
||||
# Detect scheme (check reverse proxy headers first)
|
||||
scheme = (
|
||||
request.headers.get("X-Forwarded-Proto")
|
||||
or request.headers.get("X-Scheme")
|
||||
or request.scheme
|
||||
)
|
||||
|
||||
# Detect host (check reverse proxy headers first)
|
||||
host = (
|
||||
request.headers.get("X-Forwarded-Host")
|
||||
or request.headers.get("Host")
|
||||
or request.host
|
||||
)
|
||||
|
||||
# Build server URL with prefix
|
||||
prefix = current_app.config.get("URL_PREFIX", "")
|
||||
server_url = f"{scheme}://{host}{prefix}"
|
||||
|
||||
client_path = os.path.join(current_app.root_path, "..", "fpaste")
|
||||
try:
|
||||
with open(client_path, "r") as f:
|
||||
content = f.read()
|
||||
|
||||
# Replace default server URL
|
||||
content = content.replace(
|
||||
'"server": os.environ.get("FLASKPASTE_SERVER", "http://localhost:5000")',
|
||||
f'"server": os.environ.get("FLASKPASTE_SERVER", "{server_url}")',
|
||||
)
|
||||
|
||||
response = Response(content, mimetype="text/x-python")
|
||||
response.headers["Content-Disposition"] = "attachment; filename=fpaste"
|
||||
return response
|
||||
|
||||
Reference in New Issue
Block a user