Skip to content

Commit

Permalink
Get client_ip from asgi.scope (#2808)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
masenf authored Mar 14, 2024
1 parent e3db9ad commit fb2c360
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion reflex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,10 @@ async def on_event(self, sid, data):
}

# 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):
Expand Down

0 comments on commit fb2c360

Please sign in to comment.