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

Adjustments to work better with cloudflare and reverse proxies #110

Closed
wants to merge 25 commits into from

Conversation

OmniTroid
Copy link
Contributor

@OmniTroid OmniTroid commented Nov 22, 2023

Rationale

The current setup assumes that when an incoming connection is established with X-Forwarded-For, the reverse proxy is always hosted on the same server (127.0.0.1). However, this may not always be the case. Cloudflare's normal proxying setup connects from their servers, so we should trust those as well. To facilitate this, we implement a new class and refactor some of the connection handling code.

IPIDs

While it's perfectly understandable to not show IPs to mods or players, it's bog standard practice to log the IP address on connections. Every webserver does this, so implement it here too.

Since client.ip was actually a getter for ipid, replace every instance of client.ip with ipid.

Will note that ipid is a very misleading name. It's a numeric, unique ID of each client and in principle it has nothing to do with IP.

It also seems that IPs are completely absent from the sqlite bans table? This is actually baffling.

ProxyManager

In order to get the "true" IP of a client connecting through a (reverse) proxy using websockets, we need to check the X-Forwarded-For header. There is, however, a catch. The X-Forwarded-For header can be set arbitrarily, so if it exists on the connection, the server needs to check that the request originates from a trusted proxy (eg. cloudflare). If it does not, the connection is considered untrustworthy and should be rejected. In order to facilitate this, I made a new class to handle setting up this proxy whitelist and check if a connection is valid or not.

Move client connect logic into client manager

Conceptually, there is a number of checks a connection must pass before being promoted to a client (not being banned, not claiming to be an unauthorized proxy). I think this is the client manager's job, so to speak, so these checks have been moved there. It is somewhat odd that we check for rangebanned IPs, but not for specific IP bans at this stage.

Changes in TransportWrapper

TransportWrapper has been renamed to WSTransport and made a proper subclass of asyncio.Transport. Comments suggest this was first implemented as a wrapper, but has clear signs of overriding features. I think there's a better way to design this. Also removed the get_extra_info overload and replaced it with get_client_ip in client manager.

Things that need to be tested before merging

Connections, bans and rangebans from TCP and WS connections. (could be covered by autotests. the complexity of this project more than warrants a handful of them).

While it's perfectly understandable to not show IPs to mods
or players, it's bog standard practice to log the IP address
on connections. Every webserver does this.

Since client.ip was actually a getter for ipid, replace
every instance of client.ip with ipid.

Will note that ipid is a very misleading name. It's a numeric, unique
ID of each client and in principle it has nothing to do with IP.
Rename TransportWrapper to WSTransport and make it a proper subclass.
Implement comprehensive checking of X-Forwarded-For using proxy manager.
Make get_extra_info more robust by using super().
Tsuserver already has a lot in its class, and checking for
valid client connections is probably more suited in
the clientmanager class. Valid client connections includes
checking for bogus proxies, ip range bans and some other things.
@OmniTroid OmniTroid changed the title WIP: Adjustments to work with cloudflare (ws and wss) WIP: Adjustments to work better with cloudflare and reverse proxies Nov 26, 2023
@OmniTroid OmniTroid marked this pull request as ready for review November 28, 2023 21:37
@OmniTroid
Copy link
Contributor Author

It looks like WS and TCP connections are working correctly now, as well as rangebans.

@OmniTroid
Copy link
Contributor Author

review plz

Copy link
Owner

@Crystalwarrior Crystalwarrior left a comment

Choose a reason for hiding this comment

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

Not my area of expertise, I'll mark it as approved but live testing is absolutely needed @OmniTroid

@OmniTroid OmniTroid changed the title WIP: Adjustments to work better with cloudflare and reverse proxies Adjustments to work better with cloudflare and reverse proxies Aug 1, 2024
@OmniTroid OmniTroid closed this Oct 13, 2024
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