$max) { return null; } return array($bin, $bits); } function realbots_ip_in($ip_bin, $cidr) { list($net, $bits) = $cidr; if (strlen($net) !== strlen($ip_bin)) { return false; } $bytes = intdiv($bits, 8); if ($bytes && substr($ip_bin, 0, $bytes) !== substr($net, 0, $bytes)) { return false; } $rem = $bits % 8; if ($rem) { $mask = (0xff << (8 - $rem)) & 0xff; if ((ord($ip_bin[$bytes]) & $mask) !== (ord($net[$bytes]) & $mask)) { return false; } } return true; } function realbots_ip_in_any($ip, $cidrs) { $bin = @inet_pton($ip); if ($bin === false) { return false; } foreach ($cidrs as $c) { if (realbots_ip_in($bin, $c)) { return true; } } return false; } function realbots_is_private($ip) { return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false; } /** * The real client IP. Proxy headers are only trusted when the direct peer * is a proxy we can vouch for: a private/loopback address (reverse proxy on * the same host/network) or a Cloudflare edge IP. Otherwise X-Forwarded-For * is attacker-controlled and would let a scanner "become" GPTBot. * Returns [ip, hidden] — hidden = we only see a proxy address. */ function realbots_client_ip() { $peer = isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) : ''; $trusted = $peer !== '' && (realbots_is_private($peer) || realbots_ip_in_any($peer, realbots_cloudflare_cidrs())); if ($trusted) { foreach (array('HTTP_CF_CONNECTING_IP', 'HTTP_X_REAL_IP', 'HTTP_X_FORWARDED_FOR') as $h) { if (!empty($_SERVER[$h])) { $cand = trim(explode(',', sanitize_text_field(wp_unslash($_SERVER[$h])))[0]); if (filter_var($cand, FILTER_VALIDATE_IP)) { return array($cand, false); } } } } return array($peer, $peer === '' || realbots_is_private($peer)); }