4 * Description: Blocking bots based on detecting bots/crawlers/spiders via the user agent and http_from header.
6 * Author: Philipp Holzer <admin@philipp.info>
11 use Friendica\Core\Hook;
12 use Friendica\Core\System;
13 use Jaybizzle\CrawlerDetect\CrawlerDetect;
14 use Friendica\Core\Logger;
16 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
18 function blockbot_install() {
19 Hook::register('init_1', __FILE__, 'blockbot_init_1');
23 function blockbot_uninstall() {
24 Hook::unregister('init_1', __FILE__, 'blockbot_init_1');
27 function blockbot_init_1(App $a) {
28 $crawlerDetect = new CrawlerDetect();
30 $logdata = ['agent' => $_SERVER['HTTP_USER_AGENT'], 'uri' => $_SERVER['REQUEST_URI']];
32 if (!$crawlerDetect->isCrawler()) {
33 logger::debug('Good user agent detected', $logdata);
37 // List of strings of reported false positives
38 $agents = ['hackney/', 'Faraday v', 'okhttp', 'UniversalFeedParser', 'PixelFedBot', 'python-requests',
39 'WordPress/', 'http.rb/'];
40 foreach ($agents as $agent) {
41 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
42 // The agents had been reported to https://github.com/JayBizzle/Crawler-Detect/issues/
43 logger::notice('Reported false positive', $logdata);
48 // List of false positives' strings of known "good" agents we haven't reported (yet)
49 $agents = ['fediverse.network crawler', 'Active_Pods_CheckBot_3.0', 'Social-Relay/',
50 'curl', 'zgrab', 'Go-http-client', 'curb', 'github.com', 'reqwest', 'Feedly/',
51 'Python-urllib/', 'Liferea/', 'aiohttp/', 'WordPress.com Reader'];
53 foreach ($agents as $agent) {
54 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
55 logger::notice('Unreported falsely detected agent', $logdata);
60 // List of known crawlers. They are added here to avoid having them logged at the end of the function.
61 // This helps to detect false positives.
62 $agents = ['SEMrushBot', 's~feedly-nikon3', 'Qwantify/Bleriot/', 'ltx71', 'Sogou web spider/',
63 'Diffbot/', 'Twitterbot/', 'YisouSpider/', 'evc-batch/', 'LivelapBot/', 'TrendsmapResolver/',
64 'PaperLiBot/', 'Nuzzel', 'um-LN/', 'Google Favicon', 'Datanyze', 'BLEXBot/', '360Spider',
65 'adscanner/', 'HeadlessChrome', 'wpif', 'startmebot/', 'Googlebot/', 'Applebot/',
66 'facebookexternalhit/', 'GoogleImageProxy', 'bingbot/', 'heritrix/', 'ldspider'];
68 foreach ($agents as $agent) {
69 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
70 System::httpExit(403, 'Bots are not allowed');
74 logger::info('Blocked bot', $logdata);
75 System::httpExit(403, 'Bots are not allowed');