$server The request's $_SERVER, unslashed. */ public static function address( array $server ): string { return self::resolve( $server )['address']; } /** * The address of the request being served, from the same server * variables Local reads, unslashed and sanitized as it reads them. An AI * agent's is checked against the list its company publishes, then dropped. */ public static function current(): string { $server = array(); foreach ( array( 'REMOTE_ADDR', 'HTTP_X_FORWARDED_FOR', 'HTTP_CF_CONNECTING_IP' ) as $key ) { if ( isset( $_SERVER[ $key ] ) && is_string( $_SERVER[ $key ] ) ) { $server[ $key ] = sanitize_text_field( wp_unslash( $_SERVER[ $key ] ) ); } } return self::address( $server ); } /** * Whether the request came through Cloudflare, by the same check that lets * its CF-Connecting-IP header name the visitor: the connection is from one * of Cloudflare's own addresses. Only then are its location headers real. * * @param array $server The request's $_SERVER, unslashed. */ public static function from_cloudflare( array $server ): bool { return self::resolve( $server )['cloudflare']; } /** * The visitor's address, and whether Cloudflare's header was believed for it. * * @param array $server The request's $_SERVER, unslashed. * @return array{address: string, cloudflare: bool} */ private static function resolve( array $server ): array { $hop = self::ip( $server['REMOTE_ADDR'] ?? '' ); $chain = isset( $server['HTTP_X_FORWARDED_FOR'] ) ? explode( ',', (string) $server['HTTP_X_FORWARDED_FOR'] ) : array(); $address = $hop; // Each proxy adds the address it was reached from on the right, so from the right, past this machine's and the private network's. while ( '' !== $address && self::internal( $address ) && array() !== $chain ) { $next = self::ip( (string) array_pop( $chain ) ); if ( '' === $next ) { break; } $address = $next; } $cloudflare = '' !== $address && self::cloudflare( $address ); if ( $cloudflare ) { $visitor = self::ip( $server['HTTP_CF_CONNECTING_IP'] ?? '' ); if ( '' !== $visitor ) { $address = $visitor; } } /** * Filters the address Runlight counts a visitor by and limits requests by. * It is hashed with a key that changes daily and never stored. * * @param string $address The address the plugin worked out. * @param array $server The request's server variables. */ $filtered = apply_filters( 'runlight_client_ip', $address, $server ); $filtered = is_string( $filtered ) ? self::ip( $filtered ) : ''; return array( 'address' => '' !== $filtered ? $filtered : $address, 'cloudflare' => $cloudflare, ); } /** * An address as text, without the port or brackets a header may give it, or * an empty string when it is not one. * * @param mixed $value A header's or variable's value. */ private static function ip( $value ): string { $value = trim( is_string( $value ) ? $value : '' ); if ( preg_match( '/^\[([^\]]+)\](?::\d+)?$/D', $value, $bracketed ) ) { $value = $bracketed[1]; } elseif ( preg_match( '/^(\d{1,3}(?:\.\d{1,3}){3}):\d+$/D', $value, $with_port ) ) { $value = $with_port[1]; } // An IPv4 address written as IPv6, as some servers give it, is the IPv4 address. if ( preg_match( '/^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/iD', $value, $mapped ) ) { $value = $mapped[1]; } return false !== filter_var( $value, FILTER_VALIDATE_IP ) ? $value : ''; } /** * Whether an address is this machine's or a private network's, where a * proxy in front of WordPress connects from. * * @param string $address An address. */ private static function internal( string $address ): bool { // 100.64.0.0/10, the shared range some hosts' own proxies use, counts too. return false === filter_var( $address, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) || ( preg_match( '/^100\.(\d+)\./', $address, $shared ) && (int) $shared[1] >= 64 && (int) $shared[1] <= 127 ); } /** * Whether an address is one of Cloudflare's. * * @param string $address An address. */ private static function cloudflare( string $address ): bool { $bytes = inet_pton( $address ); if ( false === $bytes ) { return false; } foreach ( self::CLOUDFLARE as $range ) { list( $base, $bits ) = explode( '/', $range ); $network = inet_pton( $base ); if ( false === $network || strlen( $network ) !== strlen( $bytes ) ) { continue; } $bits = (int) $bits; $whole = intdiv( $bits, 8 ); if ( substr( $bytes, 0, $whole ) !== substr( $network, 0, $whole ) ) { continue; } $rest = $bits % 8; if ( 0 === $rest || ( ( ord( $bytes[ $whole ] ) ^ ord( $network[ $whole ] ) ) & ( 0xff << ( 8 - $rest ) ) & 0xff ) === 0 ) { return true; } } return false; } }