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 if (empty($_SERVER['HTTP_USER_AGENT'])) {
34 $logdata = ['agent' => $_SERVER['HTTP_USER_AGENT'], 'uri' => $_SERVER['REQUEST_URI']];
36 if (!$crawlerDetect->isCrawler()) {
37 logger::debug('Good user agent detected', $logdata);
41 // List of false positives' strings of known "good" agents.
42 $agents = ['fediverse.network crawler', 'Active_Pods_CheckBot_3.0', 'Social-Relay/',
43 'curl', 'zgrab', 'Go-http-client', 'curb', 'github.com', 'reqwest', 'Feedly/',
44 'Python-urllib/', 'Liferea/', 'aiohttp/', 'WordPress.com Reader', 'hackney/',
45 'Faraday v', 'okhttp', 'UniversalFeedParser', 'PixelFedBot', 'python-requests',
46 'WordPress/', 'http.rb/', 'Apache-HttpClient/', 'WordPress.com;'];
48 foreach ($agents as $agent) {
49 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
50 logger::notice('False positive', $logdata);
55 // List of known crawlers. They are added here to avoid having them logged at the end of the function.
56 // This helps to detect false positives.
57 $agents = ['SEMrushBot', 's~feedly-nikon3', 'Qwantify/Bleriot/', 'ltx71', 'Sogou web spider/',
58 'Diffbot/', 'Twitterbot/', 'YisouSpider/', 'evc-batch/', 'LivelapBot/', 'TrendsmapResolver/',
59 'PaperLiBot/', 'Nuzzel', 'um-LN/', 'Google Favicon', 'Datanyze', 'BLEXBot/', '360Spider',
60 'adscanner/', 'HeadlessChrome', 'wpif', 'startmebot/', 'Googlebot/', 'Applebot/',
61 'facebookexternalhit/', 'GoogleImageProxy', 'bingbot/', 'heritrix/', 'ldspider'];
63 foreach ($agents as $agent) {
64 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
65 System::httpExit(403, ['title' => 'Bots are not allowed']);
69 logger::info('Blocked bot', $logdata);
70 System::httpExit(403, ['title' => 'Bots are not allowed']);