👋🏽 Deno Server Remote IP #
In this Deno get client IP address post, we have a quick look at legitimate reasons for needing the remote user IP address, before turning to three solutions. Those methods should be useful when you are running a Deno server and want to know the IP address of connecting clients; for spam filtering, as an example.
You see both how to access the client IP using Deno APIs and also using HTTP headers and when you might use each. I hope it will be useful for you. You can drop a comment below or reach out for a chat on Element , as well as Twitter @mention if you have suggestions for improvements or questions.
📹 Video #
🗳 Poll #
🖥 Deno Get Client IP Address: Code #
🔗 Links #
- Deno Fresh testing article
- Cloudflare HTTP request headers docs
- GitHub repo with full code
- Element chat: #Rodney matrix chat
- Twitter handle: @askRodney
🏁 Deno Get Client IP Address: Summary #
How can you access remote user IP address in Deno? #
- The Deno HTTP server provides connection information metadata. You can access this meta from the parameters on the server handler function. The first parameter is the request, and the second, is the connInfo object. From connInfo, destructure remoteAddr.hostname. Typically, that hostname will be the connecting IP address as a string. This might not be the remote client IP address, though. For example, if the DNS for your Deno server is proxied through Cloudflare, the hostname will be a Cloudflare IP, instead. In this case, consider using the X-Forwarded-For HTTP header or a Cloudflare CF-Connecting-IP header.
How can you get remote client IP address on a Deno server behind a Cloudflare proxy? #
- If your server is behind a Cloudflare (or other proxy), the Deno APIs will probably give you the proxy address, rather than the remote client IP address as the remote address hostname. As an alternative, look for the CF-Connecting-IP HTTP header on the incoming request.
What is the X-Forwarded-For HTTP header? #
- When a client connects to your server via a proxy, the remote IP address, seen from your server, will appear to be the proxy IP address and not the remoter user’s IP. However, some proxies (Cloudflare, for example) will normally add the X-Forwarded-For HTTP header. The header value will be the remote user’s IP address; and this header makes the remove client IP address available to your server. When the request passes through multiple proxies, each will add their (adjacent) connecting IP to the header value. The header value can, therefore, be a comma separated list of IP addresses, which the request passed through. The remote client IP will be the first. Be sure to comply with appropriate privacy regulations when using the client IP address.
🙏🏽 Feedback #
Have you found the post useful? Would you prefer to see posts on another topic instead? Get in touch with ideas for new posts. Also, if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a few dollars, euros or pounds, please consider supporting me through Buy me a Coffee.
Just dropped a new video on how you can access remote client IP address form your 🦕 Deno server. Handy when running spam/bot detection.
— Rodney (@askRodney) July 14, 2023
Hope you find it useful!
#learndenohttps://t.co/Fak0uehRMI
Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter, @[email protected] on Mastodon and also the #rodney Element Matrix room. Also, see further ways to get in touch with Rodney Lab. I post regularly on Astro as well as Deno. Also, subscribe to the newsletter to keep up-to-date with our latest projects.