Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get client_ip from asgi.scope #2808

Merged
merged 2 commits into from
Mar 14, 2024
Merged

Get client_ip from asgi.scope #2808

merged 2 commits into from
Mar 14, 2024

Conversation

masenf
Copy link
Collaborator

@masenf masenf commented Mar 7, 2024

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

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`
reflex/app.py Outdated
@@ -1146,7 +1146,7 @@ async def on_event(self, sid, data):
}

# Get the client IP
client_ip = environ["REMOTE_ADDR"]
client_ip = environ["asgi.scope"]["client"][0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to add any get or try/catch here, or will these key lookups always work? We should avoid crashing outside the event loop is my concern

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i can try/catch it. i didn't because we didn't before, but this is multiple levels of derefs, so more could go wrong

picklelo
picklelo previously approved these changes Mar 13, 2024
@masenf masenf merged commit fb2c360 into main Mar 14, 2024
47 checks passed
@masenf masenf deleted the masenf/client-ip branch March 14, 2024 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants