array(label, user agent pattern, verification: ranges source or DNS suffixes) */ public static function crawlers() { return array( 'google' => array('Googlebot', '/Googlebot|Google-InspectionTool|GoogleOther|Storebot-Google|AdsBot-Google/i', array('ranges' => array('https://developers.google.com/static/search/apis/ipranges/googlebot.json', 'https://developers.google.com/static/search/apis/ipranges/special-crawlers.json'))), 'bing' => array('Bingbot', '/bingbot|BingPreview|msnbot/i', array('ranges' => array('https://www.bing.com/toolbox/bingbot.json'))), 'apple' => array('Applebot', '/Applebot/i', array('dns' => array('.applebot.apple.com'))), 'yandex' => array('YandexBot', '/YandexBot|YandexImages|YandexMobileBot/i', array('dns' => array('.yandex.ru', '.yandex.net', '.yandex.com'))), 'baidu' => array('Baiduspider', '/Baiduspider/i', array('dns' => array('.baidu.com', '.baidu.jp'))), 'duckduckgo' => array('DuckDuckBot', '/DuckDuckBot/i', array('dns' => array('.duckduckgo.com'))), ); } /** * Crawler key claimed by a user agent, or '' */ public static function claimed($user_agent) { foreach (self::crawlers() as $key => $crawler) { if (preg_match($crawler[1], $user_agent)) { return $key; } } return ''; } /** * 'verified', 'failed' or 'pending' (verification queued); 'disabled' when switched off */ public static function verify($ip, $key) { if (!AOWPCS_Settings::get('verify_crawlers')) { return 'disabled'; } $crawlers = self::crawlers(); if (!isset($crawlers[$key])) { return 'failed'; } $method = $crawlers[$key][2]; if (isset($method['ranges'])) { $ranges = get_site_option(self::RANGES_OPTION, array()); if (empty($ranges[$key])) { self::queue($ip, $key); return 'pending'; } return AOWPCS_IP::in_any($ip, $ranges[$key]) ? 'verified' : 'failed'; } $cached = get_site_transient('aowpcs_cv_' . md5($key . '|' . $ip)); if ($cached === 'verified' || $cached === 'failed') { return $cached; } self::queue($ip, $key); return 'pending'; } private static function queue($ip, $key) { $queue = get_site_option(self::QUEUE_OPTION, array()); if (!is_array($queue)) { $queue = array(); } if (count($queue) >= 100 || isset($queue[$key . '|' . $ip])) { return; } $queue[$key . '|' . $ip] = time(); update_site_option(self::QUEUE_OPTION, $queue); if (!wp_next_scheduled('aowpcs_cron_verify_crawlers')) { wp_schedule_single_event(time() + 60, 'aowpcs_cron_verify_crawlers'); } } /** * Background verification of queued addresses (and range refresh) */ public static function process_queue() { $ranges = get_site_option(self::RANGES_OPTION, array()); if (empty($ranges['fetched']) || $ranges['fetched'] < time() - WEEK_IN_SECONDS) { self::refresh_ranges(); } $queue = get_site_option(self::QUEUE_OPTION, array()); update_site_option(self::QUEUE_OPTION, array()); if (!is_array($queue)) { return; } $crawlers = self::crawlers(); $until = microtime(true) + 20; foreach (array_keys($queue) as $item) { if (microtime(true) > $until) { break; } list($key, $ip) = explode('|', $item, 2); if (!isset($crawlers[$key][2]['dns'])) { continue; } $result = self::reverse_dns_matches($ip, $crawlers[$key][2]['dns']) ? 'verified' : 'failed'; set_site_transient('aowpcs_cv_' . md5($key . '|' . $ip), $result, WEEK_IN_SECONDS); } } /** * Forward-confirmed reverse DNS: the host name must end with an allowed suffix and resolve back to the address */ public static function reverse_dns_matches($ip, $suffixes) { $host = @gethostbyaddr($ip); if (!$host || $host === $ip) { return false; } $host = strtolower($host); $ok = false; foreach ($suffixes as $suffix) { $ok = $ok || substr($host, -strlen($suffix)) === $suffix; } if (!$ok) { return false; } if (strpos($ip, ':') !== false) { $records = function_exists('dns_get_record') ? @dns_get_record($host, DNS_AAAA) : array(); foreach ((array) $records as $record) { if (isset($record['ipv6']) && inet_pton($record['ipv6']) === inet_pton($ip)) { return true; } } return false; } $addresses = @gethostbynamel($host); return is_array($addresses) && in_array($ip, $addresses, true); } /** * Download the published crawler ranges (no data about the site is sent) */ public static function refresh_ranges() { $ranges = get_site_option(self::RANGES_OPTION, array()); if (!is_array($ranges)) { $ranges = array(); } foreach (self::crawlers() as $key => $crawler) { if (!isset($crawler[2]['ranges'])) { continue; } $prefixes = array(); foreach ($crawler[2]['ranges'] as $url) { $response = wp_remote_get($url, array('timeout' => 15)); if (is_wp_error($response) || (int) wp_remote_retrieve_response_code($response) !== 200) { continue 2; } $data = json_decode(wp_remote_retrieve_body($response), true); foreach ((array) (isset($data['prefixes']) ? $data['prefixes'] : array()) as $prefix) { foreach (array('ipv4Prefix', 'ipv6Prefix') as $field) { if (!empty($prefix[$field]) && AOWPCS_IP::is_valid_range($prefix[$field])) { $prefixes[] = $prefix[$field]; } } } } if ($prefixes) { $ranges[$key] = array_values(array_unique($prefixes)); } } $ranges['fetched'] = time(); update_site_option(self::RANGES_OPTION, $ranges); } }