For CloudFlare users: how to use CF-* fields from HTTP header instead of normal IP?

סיכום מבוסס בינה מלאכותית

A user asks how to use CloudFlare’s CF-* fields from HTTP headers instead of the obfuscated IP address in Discourse. mreach suggests adding support for fields like CF-Connecting-IP and CF-IPCountry in the admin user page. Stephen replies that this is already handled automatically by using the Cloudflare template in the app.yml configuration. mreach expresses gratitude, noting the solution is simple but not clearly documented, and suggests it should be more visible.

Since CloudFlare obfuscates the visitor’s IP address, it would be useful to be able to capture additional CloudFlare specific fields such as CF-Connecting-IP, X-Forwarded-For, and CF-IPCountry from the HTTP header that they insert and then conditionally report these within the Admin > Users > username page in addition to the (not-useful with CF) IP fields + IP Lookup button.

Could we please have these field provided within this page when CF-* header fields are available?

One option that would be useful (and I’ve used in other Rails apps) is to just use those fields instead of the IP if they’re available. It’s been a while since I implemented that, but if I recall correctly I believe we would just set visitor’s IP to IP unless CF-Connecting-IP exists… but I thought there was something else I had done with X-Forwarded-For…

If you use the provided Cloudflare template this is all handled automatically. This has actually been available within Discourse for four or five years.

Assuming you followed the standard install:

cd /var/discourse
nano containers/app.yml

then add this line under templates:

- "templates/cloudflare.template.yml"

Then just rebuild:

./launcher rebuild app

At which point your install will show true user addresses.

That’s fantastic Stephen, thank you! Such a simple thing that it should probably be in there and commented out by default or documented somewhere. At least one can find it via “CloudFlare” searches here now in relation to this. Never even crossed my mind to look into the templates folder! :slight_smile: