From fb2c3606d951aab77e4f8b61a9d97ddc43a6a87d Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Thu, 14 Mar 2024 10:03:30 -0700 Subject: [PATCH] Get `client_ip` from `asgi.scope` (#2808) * Get `client_ip` from `asgi.scope` It seems like REMOTE_ADDR is always 127.0.0.1, which is not super useful when trying to figure out where the websocket connection is originating from. Of course this isn't a silver bullet because most-likely the WS will be passed through a reverse proxy anyway... in that case, the client IP is likely in the headers under `x_forwarded_for` * client_ip: fallback to REMOTE_ADDR --- reflex/app.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/reflex/app.py b/reflex/app.py index 081f83842..5f1ef96f5 100644 --- a/reflex/app.py +++ b/reflex/app.py @@ -1211,7 +1211,10 @@ class EventNamespace(AsyncNamespace): } # Get the client IP - client_ip = environ["REMOTE_ADDR"] + try: + client_ip = environ["asgi.scope"]["client"][0] + except (KeyError, IndexError): + client_ip = environ.get("REMOTE_ADDR", "0.0.0.0") # Process the events. async for update in process(self.app, event, sid, headers, client_ip):