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>
7 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
12 use Friendica\Core\Hook;
13 use Friendica\Core\System;
15 use Jaybizzle\CrawlerDetect\CrawlerDetect;
16 use Friendica\Core\Logger;
17 use Friendica\Core\Renderer;
19 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
21 function blockbot_install() {
22 Hook::register('init_1', __FILE__, 'blockbot_init_1');
25 function blockbot_addon_admin(&$a, &$o) {
26 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/blockbot/");
28 $o = Renderer::replaceMacros($t, [
29 '$submit' => DI::l10n()->t('Save Settings'),
30 '$good_crawlers' => ['good_crawlers', DI::l10n()->t('Allow "good" crawlers'), DI::config()->get('blockbot', 'good_crawlers'), "Don't block fediverse crawlers, relay servers and other bots with good purposes."],
31 '$block_gab' => ['block_gab', DI::l10n()->t('Block GabSocial'), DI::config()->get('blockbot', 'block_gab'), 'Block the software GabSocial. This will block every access for that software. You can block dedicated gab instances in the blocklist settings in the admin section.'],
32 '$training' => ['training', DI::l10n()->t('Training mode'), DI::config()->get('blockbot', 'training'), "Activates the training mode. This is only meant for developing purposes. Don't activate this on a production machine. This can cut communication with some systems."],
36 function blockbot_addon_admin_post(&$a) {
37 DI::config()->set('blockbot', 'good_crawlers', $_POST['good_crawlers'] ?? false);
38 DI::config()->set('blockbot', 'block_gab', $_POST['block_gab'] ?? false);
39 DI::config()->set('blockbot', 'training', $_POST['training'] ?? false);
42 function blockbot_init_1(App $a) {
43 if (empty($_SERVER['HTTP_USER_AGENT'])) {
47 $logdata = ['agent' => $_SERVER['HTTP_USER_AGENT'], 'uri' => $_SERVER['REQUEST_URI']];
49 // List of "good" crawlers
50 $good_agents = ['fediverse.space crawler', 'fediverse.network crawler', 'Active_Pods_CheckBot_3.0',
51 'Social-Relay/', 'Test Certificate Info', 'Uptimebot/', 'GNUSocialBot', 'UptimeRobot/',
54 // List of known crawlers.
55 $agents = ['SemrushBot', 's~feedly-nikon3', 'Qwantify/Bleriot/', 'ltx71', 'Sogou web spider/',
56 'Diffbot/', 'Twitterbot/', 'YisouSpider', 'evc-batch/', 'LivelapBot/', 'TrendsmapResolver/',
57 'PaperLiBot/', 'Nuzzel', 'um-LN/', 'Google Favicon', 'Datanyze', 'BLEXBot/', '360Spider',
58 'adscanner/', 'HeadlessChrome', 'wpif', 'startmebot/', 'Googlebot/', 'Applebot/',
59 'facebookexternalhit/', 'GoogleImageProxy', 'bingbot/', 'heritrix/', 'ldspider',
60 'AwarioRssBot/', 'Zabbix', 'TweetmemeBot/', 'dcrawl/', 'PhantomJS/', 'Googlebot-Image/',
61 'CrowdTanglebot/', 'Mediapartners-Google', 'Baiduspider/', 'datagnionbot',
62 'MegaIndex.ru/', 'SMUrlExpander', 'Hatena-Favicon/', 'Wappalyzer', 'FlipboardProxy/',
63 'NetcraftSurveyAgent/', 'Dataprovider.com', 'SMTBot/', 'Nimbostratus-Bot/',
64 'DuckDuckGo-Favicons-Bot/', 'IndieWebCards/', 'proximic', 'netEstate NE Crawler',
65 'AhrefsBot/', 'YandexBot/', 'Exabot/', 'Mediumbot-MetaTagFetcher/', 'WhatsApp/',
66 'TelegramBot', 'SurdotlyBot/', 'BingPreview/', 'SabsimBot/', 'CCBot/', 'WbSrch/',
67 'DuckDuckBot-Https/', 'HTTP Banner Detection', 'YandexImages/', 'archive.org_bot',
68 'ArchiveTeam ArchiveBot/', 'yacybot', 'https://developers.google.com/+/web/snippet/',
69 'Scrapy/', 'github-camo', 'MJ12bot/', 'DotBot/', 'Pinterestbot/', 'Jooblebot/',
70 'Cliqzbot/', 'YaK/', 'Mediatoolkitbot', 'Snacktory', 'FunWebProducts', 'oBot/',
71 '7Siters/', 'KOCMOHABT', 'Google-SearchByImage', 'FemtosearchBot/',
72 'HubSpot Crawler', 'DomainStatsBot/', 'Re-re Studio', 'AwarioSmartBot/',
73 'SummalyBot/', 'DNSResearchBot/', 'PetalBot;', 'Nmap Scripting Engine;',
74 'Google-Apps-Script; beanserver;', 'woorankreview/', 'Seekport Crawler;', 'AHC/',
75 'SkypeUriPreview Preview/', 'Semanticbot/', 'Embed PHP library', 'XoviOnpageCrawler;',
76 'GetHPinfo.com-Bot/', 'BoardReader Favicon Fetcher'];
78 if (!DI::config()->get('blockbot', 'good_crawlers')) {
79 $agents = array_merge($agents, $good_agents);
81 foreach ($good_agents as $good_agent) {
82 if (stristr($_SERVER['HTTP_USER_AGENT'], $good_agent)) {
88 if (DI::config()->get('blockbot', 'block_gab')) {
89 $agents[] = 'GabSocial/';
92 foreach ($agents as $agent) {
93 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
94 System::httpExit(403, 'Bots are not allowed');
98 // This switch here is only meant for developers who want to add more bots to the list above, it is not safe for production.
99 if (!DI::config()->get('blockbot', 'training')) {
103 $crawlerDetect = new CrawlerDetect();
105 if (!$crawlerDetect->isCrawler()) {
106 logger::debug('Good user agent detected', $logdata);
110 // List of false positives' strings of known "good" agents.
111 $agents = ['curl', 'zgrab', 'Go-http-client', 'curb', 'github.com', 'reqwest', 'Feedly/',
112 'Python-urllib/', 'Liferea/', 'aiohttp/', 'WordPress.com Reader', 'hackney/',
113 'Faraday v', 'okhttp', 'UniversalFeedParser', 'PixelFedBot', 'python-requests',
114 'WordPress/', 'http.rb/', 'Apache-HttpClient/', 'WordPress.com;', 'Pleroma',
115 'Dispatch/', 'Ruby', 'Java/', 'libwww-perl/', 'Mastodon/', 'FeedlyApp/',
116 'lua-resty-http/', 'Tiny Tiny RSS/', 'Wget/', 'PostmanRuntime/',
117 'W3C_Validator/', 'NetNewsWire', 'FeedValidator/', 'theoldreader.com'];
119 if (DI::config()->get('blockbot', 'good_crawlers')) {
120 $agents = array_merge($agents, $good_agents);
123 foreach ($agents as $agent) {
124 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
125 logger::notice('False positive', $logdata);
130 logger::info('Blocked bot', $logdata);
131 System::httpExit(403, 'Bots are not allowed');