Skip to content

Instantly share code, notes, and snippets.

@carehart
Last active October 1, 2024 23:13
Show Gist options
  • Save carehart/abb9835ce37421a8123ae3758014cea2 to your computer and use it in GitHub Desktop.
Save carehart/abb9835ce37421a8123ae3758014cea2 to your computer and use it in GitHub Desktop.
Tomcat RemoteIPValve using Cloudflare IP CIDR ranges
<!-- for more, see my Aug 15 2024 comment in my post at https://coldfusion.adobe.com/2019/04/get-cf-know-users-real-ip-address-behind-proxy-load-balancer-caching-solution-etc/#comment-48778 -->
<Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="CF-Connecting-IP"
trustedProxies="
^(173\.245\.(?:4[8-9]|5[0-9]|6[0-3])\.(?:[0-9]|[1-9][0-9]|1(?:[0-9][0-9])|2(?:[0-4][0-9]|5[0-5])))$|^(103\.21\.(?:2(?:4[4-7]))\.(?:[0-9]|[1-9][0-9]|1(?:[0-9][0-9])|2(?:[0-4][0-9]|5[0-5])))$|^(103\.22\.(?:2(?:0[0-3]))\.(?:[0-9]|[1-9][0-9]|1(?:[0-9][0-9])|2(?:[0-4][0-9]|5[0-5])))$|^(103\.31\.(?:[4-7])\.(?:[0-9]|[1-9][0-9]|1(?:[0-9][0-9])|2(?:[0-4][0-9]|5[0-5])))$|^(141\.101\.(?:6[4-9]|[7-9][0-9]|1(?:[0-1][0-9]|2[0-7]))\.(?:[0-9]|[1-9][0-9]|1(?:[0-9][0-9])|2(?:[0-4][0-9]|5[0-5])))$|^(108\.162\.(?:1(?:9[2-9])|2(?:[0-4][0-9]|5[0-5]))\.(?:[0-9]|[1-9][0-9]|1(?:[0-9][0-9])|2(?:[0-4][0-9]|5[0-5])))$|^(190\.93\.(?:2(?:4[0-9]|5[0-5]))\.(?:[0-9]|[1-9][0-9]|1(?:[0-9][0-9])|2(?:[0-4][0-9]|5[0-5])))$|^(188\.114\.(?:9[6-9]|1(?:0[0-9]|1[0-1]))\.(?:[0-9]|[1-9][0-9]|1(?:[0-9][0-9])|2(?:[0-4][0-9]|5[0-5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(:(:[0-9A-Fa-f]{1,4}){0,3}|:[0-9A-Fa-f]{1,4}(:(:[0-9A-Fa-f]{1,4}){0,2}|:[0-9A-Fa-f]{1,4}(:(:[0-9A-Fa-f]{1,4})?|:[0-9A-Fa-f]{1,4}(:|:[0-9A-Fa-f]{1,4})))):(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3})
" />
@carehart
Copy link
Author

carehart commented Oct 1, 2024

So sorry, folks. When I first put this up, I quickly threw in a comment to the top of the gist...and I used a CFML comment (3 dashes). That's definitely NOT appropriate if you were to LEAVE it in the Tomcat xml file where you'd typically place this (server.xml). That could keep Tomcat from coming up, as XML processors like that used to load up Tomcat XML files would choke on the 3 dashes.

Sincere apologies. I just noticed it. Thankfully had yet commented or complained about it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment