https://kotlinlang.org logo
#ktor
Title
# ktor
r

rebok

03/18/2024, 11:35 AM
Does anyone know how to get the unchanged IP address of the client? Because in the docs of call.request.origin.remoteAddress It says that it can be easily overridden
s

Sam

03/18/2024, 11:50 AM
The exact answer depends on what you need it for and what kind of environment your server is running in, but you can find a comprehensive explanation here: https://adam-p.ca/blog/2022/03/x-forwarded-for/ tl;dr: this is a minefield, so if you can avoid relying on the client IP you should. If you have no other option, you'll need to craft a solution based on the specifics of your own infrastructure.
r

rebok

03/18/2024, 12:35 PM
So ktor doesn't provide a way to do it?
s

Sam

03/18/2024, 12:39 PM
There is no "it". There are many different headers you might need to inspect and different ways of parsing them depending on what other layers sit between your server and your client.
👆 2
r

rebok

03/18/2024, 1:35 PM
I mean like the raw IP without any headers
s

Sam

03/18/2024, 2:25 PM
The only way you'll have access to that is if your client is connecting directly to your Ktor server. That's unlikely to be the case in any modern deployment. If there is any other server in between, the IP you see will be the IP of that server instead of your actual client.
r

rebok

03/18/2024, 2:26 PM
Ok, so if it's a client from the outside then the real IP will be shown?
s

Sam

03/18/2024, 2:27 PM
I'm not sure I understand what you mean by that
r

rebok

03/18/2024, 2:27 PM
I mean if it's not a reverse proxy and just a normal client
s

Sam

03/18/2024, 2:29 PM
If you genuinely have a client connecting directly to your server with no reverse proxy of any kind in between the two, then yes, you will get the real client IP.
r

rebok

03/18/2024, 2:29 PM
Ok, thanks for help
4 Views