From 080f8f6b1a5951c9387f8e11d10ff15e05326e8e Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sun, 15 Jun 2025 15:34:32 +0200 Subject: [PATCH] Work behind reverse proxy --- .env.example | 8 +++++++- Containerfile | 2 +- pyproject.toml | 2 +- src/invidious_export_server/__init__.py | 9 ++++++++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index 8133371..349b729 100644 --- a/.env.example +++ b/.env.example @@ -5,4 +5,10 @@ DATABASE_URL=postgres://kemal:kemal@[fc6c:b484:25b1:d25d::1]/invidious QUART_SECRET_KEY=0359e1c42e06fbaa284567ef7dc0faae # Any Invidious instance -ALTERNATIVE_INSTANCE_URL=https://redirect.invidious.io \ No newline at end of file +ALTERNATIVE_INSTANCE_URL=https://redirect.invidious.io + +# The amount of reverse proxies a request from a client is proxied through +PROXY_HOPS=0 + +# Whether to use RFC7239 +PROXY_MODERN=false \ No newline at end of file diff --git a/Containerfile b/Containerfile index aa6e9d1..bb2662c 100644 --- a/Containerfile +++ b/Containerfile @@ -68,4 +68,4 @@ ENTRYPOINT ["./entrypoint.sh"] # Define the default command to run the application # This is passed as arguments to the entrypoint script -CMD ["granian", "--interface", "asgi", "src.invidious_export_server:app"] \ No newline at end of file +CMD ["granian", "--host", "0.0.0.0", "--interface", "asgi", "src.invidious_export_server:fixed_app"] \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 2ee9d05..7b29a0e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ dependencies = [ packages = [{include = "invidious_export_server", from = "src"}] [tool.poetry.scripts] -start = "invidious_export_server:run" +debug = "invidious_export_server:run_debug" [build-system] requires = ["poetry-core>=2.0.0,<3.0.0"] diff --git a/src/invidious_export_server/__init__.py b/src/invidious_export_server/__init__.py index 570b19a..57bcb45 100644 --- a/src/invidious_export_server/__init__.py +++ b/src/invidious_export_server/__init__.py @@ -2,6 +2,7 @@ from quart import Quart, redirect, request, render_template, make_response, url_ from quart_db import QuartDB from quart_bcrypt import Bcrypt from quart_rate_limiter import RateLimiter +from hypercorn.middleware import ProxyFixMiddleware from os import environ from dotenv import load_dotenv @@ -14,6 +15,8 @@ load_dotenv() app.config.from_prefixed_env() app.config['QUART_DB_DATABASE_URL'] = environ.get("DATABASE_URL") app.config['ALTERNATIVE_INSTANCE_URL'] = environ.get("ALTERNATIVE_INSTANCE_URL", "https://redirect.invidious.io") +app.config['PROXY_MODERN'] = environ.get("PROXY_MODERN", "false").lower() == 'true' +app.config['PROXY_HOPS'] = int(environ.get("PROXY_HOPS", 0)) db = QuartDB(app) bcrypt = Bcrypt(app) @@ -21,6 +24,10 @@ rate_limiter = RateLimiter(app) blueprints.register_all(app) +fixed_app = ProxyFixMiddleware(app, + mode='modern' if app.config['PROXY_MODERN'] else 'legacy', + trusted_hops=app.config['PROXY_HOPS']) + @app.context_processor def add_variables(): return { @@ -55,5 +62,5 @@ def favicon(): def health(): return 'OK' -def run() -> None: +def run_debug() -> None: app.run(debug=True)