how to set remoteAddr from http header Forwarded for or X-Forwarded for
In some environments are the TCP Client IP not the real Client IP. What's the best way to set https://deno.land/api@v1.38.0?s=Deno.ServeHandlerInfo#prop_remoteAddr from HTTP Header or Proxy protocol https://github.com/haproxy/haproxy/blob/master/doc/proxy-protocol.txt ?
2 Replies
The
remoteAddr
property of the Deno.ServeHandlerInfo
interface is read-only and is automatically set by Deno to represent the remote address of the connection. It cannot be manually set or modified.
However, if you're behind a proxy and want to get the original client's IP address, you can check the X-Forwarded-For
or Forwarded
headers of the HTTP request. These headers are commonly set by proxies to indicate the original client's IP address.
Here's a simple example of how you can do this:
In this example, handler
is a function that takes a Request
object. It retrieves the X-Forwarded-For
or Forwarded
header from the request, and if it exists, it logs the first IP address from the header (in case there are multiple IP addresses chained in the header).
Please note that this information is not explicitly stated in the Deno documentation, but it's a common practice in handling HTTP requests behind proxies. Also, be aware that these headers can be easily spoofed, so they should not be trusted blindly.Which place is the bast to get and set if for the whole application? Any chance to make it mutable?