Automatic Cloudflare detection for rate limiting (trusts CF-Connecting-IP only from Cloudflare ranges)
- Core\CidrMatcher (IPv4/IPv6), Core\CloudflareRanges (published ranges, filterable) - ClientIp 'auto' mode, default for new installs; diagnostics show whether the connection is from Cloudflare Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5ea1e1d273
commit
43106d9edc
8 files changed
+171
-6
No files matched your search
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Core;
|
||||
|
||||
/**
|
||||
* Cloudflare's published proxy address ranges (https://www.cloudflare.com/ips-v4 and /ips-v6), fetched 2026-11-15.
|
||||
* They change very rarely. Sites can extend them with the `streamgoals_cloudflare_ranges` filter.
|
||||
* Used ONLY to decide whether a connection really comes from Cloudflare, so a forged CF-Connecting-IP header
|
||||
* from anywhere else is ignored.
|
||||
*/
|
||||
final class CloudflareRanges {
|
||||
|
||||
public const RANGES = array(
|
||||
'173.245.48.0/20', '103.21.244.0/22', '103.22.200.0/22', '103.31.4.0/22', '141.101.64.0/18', '108.162.192.0/18',
|
||||
'190.93.240.0/20', '188.114.96.0/20', '197.234.240.0/22', '198.41.128.0/17', '162.158.0.0/15', '104.16.0.0/13',
|
||||
'104.24.0.0/14', '172.64.0.0/13', '131.0.72.0/22',
|
||||
'2400:cb00::/32', '2606:4700::/32', '2803:f800::/32', '2405:b500::/32', '2405:8100::/32', '2a06:98c0::/29', '2c0f:f248::/32',
|
||||
);
|
||||
|
||||
/** @param string[]|null $extra additional ranges (from the filter) */
|
||||
public static function isCloudflare( string $ip, ?array $extra = null ): bool {
|
||||
return CidrMatcher::contains( $ip, array_merge( self::RANGES, $extra ?? array() ) );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user