From 7c2f0bf0cb278c6c0b2d9a101f951c5b696dba06 Mon Sep 17 00:00:00 2001 From: pstruebi Date: Tue, 10 Feb 2026 16:37:34 +0100 Subject: [PATCH] add HTTP to HTTPS redirect server on port 80 --- src/auracast/server/http_to_https_redirect.py | 42 +++++++++++++++++++ src/auracast/server/start_frontend_https.sh | 7 ++++ 2 files changed, 49 insertions(+) create mode 100644 src/auracast/server/http_to_https_redirect.py diff --git a/src/auracast/server/http_to_https_redirect.py b/src/auracast/server/http_to_https_redirect.py new file mode 100644 index 0000000..8a79e11 --- /dev/null +++ b/src/auracast/server/http_to_https_redirect.py @@ -0,0 +1,42 @@ +"""Minimal HTTP server that redirects all requests to HTTPS (port 443). + +Run on port 80 alongside the HTTPS Streamlit frontend so that users who +type a bare IP address into their browser are automatically forwarded. +""" + +import http.server +import sys + + +class RedirectHandler(http.server.BaseHTTPRequestHandler): + def do_GET(self): + host = self.headers.get("Host", "").split(":")[0] or self.server.server_address[0] + target = f"https://{host}{self.path}" + self.send_response(301) + self.send_header("Location", target) + self.end_headers() + + # Handle every method the same way + do_POST = do_GET + do_PUT = do_GET + do_DELETE = do_GET + do_HEAD = do_GET + + def log_message(self, format, *args): + # Keep logging minimal + sys.stderr.write(f"[http-redirect] {self.address_string()} -> https {args[0] if args else ''}\n") + + +def main(): + port = int(sys.argv[1]) if len(sys.argv) > 1 else 80 + server = http.server.HTTPServer(("0.0.0.0", port), RedirectHandler) + print(f"HTTP->HTTPS redirect server listening on 0.0.0.0:{port}") + try: + server.serve_forever() + except KeyboardInterrupt: + pass + server.server_close() + + +if __name__ == "__main__": + main() diff --git a/src/auracast/server/start_frontend_https.sh b/src/auracast/server/start_frontend_https.sh index 6e145ff..844e7eb 100755 --- a/src/auracast/server/start_frontend_https.sh +++ b/src/auracast/server/start_frontend_https.sh @@ -33,5 +33,12 @@ echo "Using Avahi domain: $AVAHI_DOMAIN" # Path to poetry binary POETRY_BIN="/home/caster/.local/bin/poetry" +# Start HTTP->HTTPS redirect server on port 80 (background) +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +python3 "$SCRIPT_DIR/http_to_https_redirect.py" 80 & +REDIRECT_PID=$! +echo "HTTP->HTTPS redirect server started (PID $REDIRECT_PID)" +trap "kill $REDIRECT_PID 2>/dev/null" EXIT + # Start Streamlit HTTPS server (port 443) $POETRY_BIN run streamlit run multicast_frontend.py --server.port 443 --server.address 0.0.0.0 --server.enableCORS false --server.enableXsrfProtection false --server.headless true --server.sslCertFile "$CERT" --server.sslKeyFile "$KEY" --browser.gatherUsageStats false