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>
11 use Friendica\Core\Hook;
13 use Jaybizzle\CrawlerDetect\CrawlerDetect;
14 use Friendica\Core\Logger;
15 use Friendica\Core\Renderer;
16 use Friendica\Network\HTTPException\ForbiddenException;
18 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
20 function blockbot_install()
22 Hook::register('init_1', __FILE__, 'blockbot_init_1');
25 function blockbot_addon_admin(string &$o)
27 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/blockbot/');
29 $o = Renderer::replaceMacros($t, [
30 '$submit' => DI::l10n()->t('Save Settings'),
31 '$good_crawlers' => ['good_crawlers', DI::l10n()->t('Allow "good" crawlers'), DI::config()->get('blockbot', 'good_crawlers'), DI::l10n()->t("Don't block fediverse crawlers, relay servers and other bots with good purposes.")],
32 '$socialmedia_agents' => ['socialmedia_agents', DI::l10n()->t('Allow preview agents'), DI::config()->get('blockbot', 'socialmedia_agents'), DI::l10n()->t("Don't block agents from social media systems that want to generate preview data for links that had been set by their users.")],
33 '$block_gab' => ['block_gab', DI::l10n()->t('Block GabSocial'), DI::config()->get('blockbot', 'block_gab'), DI::l10n()->t('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.')],
34 '$training' => ['training', DI::l10n()->t('Training mode'), DI::config()->get('blockbot', 'training'), DI::l10n()->t("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.")],
38 function blockbot_addon_admin_post()
40 DI::config()->set('blockbot', 'good_crawlers', $_POST['good_crawlers'] ?? false);
41 DI::config()->set('blockbot', 'socialmedia_agents', $_POST['socialmedia_agents'] ?? false);
42 DI::config()->set('blockbot', 'block_gab', $_POST['block_gab'] ?? false);
43 DI::config()->set('blockbot', 'training', $_POST['training'] ?? false);
46 function blockbot_init_1()
48 if (empty($_SERVER['HTTP_USER_AGENT'])) {
52 $logdata = ['agent' => $_SERVER['HTTP_USER_AGENT'], 'uri' => $_SERVER['REQUEST_URI']];
54 // List of "good" crawlers, mostly from the fediverse.
56 'fediverse.space crawler', 'fediverse.network crawler', 'Active_Pods_CheckBot_3.0',
57 'Social-Relay/', 'Test Certificate Info', 'Uptimebot/', 'GNUSocialBot', 'UptimeRobot/',
58 'PTST/', 'Zabbix', 'Poduptime/', 'FediFetcher', 'lemmy-stats-crawler',
59 'FedditLemmyverseCrawler/', 'kbinBot/', 'lemmy-explorer-crawler/',
62 // List of agents from social media systems that fetch preview data via opem graph or twitter cards.
63 $socialmedia_agents = ['Twitterbot/', 'facebookexternalhit/', 'SkypeUriPreview Preview/',
64 'TelegramBot', 'WhatsApp/', 'github-camo', 'Bluesky Cardyb/', 'XING-contenttabreceiver/',
65 'LinkedInBot/', 'Instagram ', 'Synapse (bot; ', 'Discordbot/', 'SummalyBot/'];
67 // List of known unwanted crawlers.
69 'SemrushBot', 's~feedly-nikon3', 'Qwantify/Bleriot/', 'ltx71', 'Sogou web spider/',
70 'Diffbot/', 'YisouSpider', 'evc-batch/', 'LivelapBot/', 'TrendsmapResolver/',
71 'PaperLiBot/', 'Nuzzel', 'um-LN/', 'Google Favicon', 'Datanyze', 'BLEXBot/', '360Spider',
72 'adscanner/', 'HeadlessChrome', 'wpif', 'startmebot/', 'Googlebot/', 'Applebot/',
73 'GoogleImageProxy', 'bingbot/', 'heritrix/', 'ldspider',
74 'AwarioRssBot/', 'TweetmemeBot/', 'dcrawl/', 'PhantomJS/', 'Googlebot-Image/',
75 'CrowdTanglebot/', 'Mediapartners-Google', 'Baiduspider/', 'datagnionbot',
76 'MegaIndex.ru/', 'SMUrlExpander', 'Hatena-Favicon/', 'Wappalyzer', 'FlipboardProxy/',
77 'NetcraftSurveyAgent/', 'Dataprovider.com', 'SMTBot/', 'Nimbostratus-Bot/',
78 'DuckDuckGo-Favicons-Bot/', 'IndieWebCards/', 'proximic', 'netEstate NE Crawler',
79 'AhrefsBot/', 'YandexBot/', 'Exabot/', 'Mediumbot-MetaTagFetcher/',
80 'SurdotlyBot/', 'BingPreview/', 'SabsimBot/', 'CCBot/', 'WbSrch/',
81 'DuckDuckBot-Https/', 'HTTP Banner Detection', 'YandexImages/', 'archive.org_bot',
82 'ArchiveTeam ArchiveBot/', 'yacybot', 'https://developers.google.com/+/web/snippet/',
83 'Scrapy/', 'MJ12bot/', 'DotBot/', 'Pinterestbot/', 'Jooblebot/',
84 'Cliqzbot/', 'YaK/', 'Mediatoolkitbot', 'Snacktory', 'FunWebProducts', 'oBot/',
85 '7Siters/', 'KOCMOHABT', 'Google-SearchByImage', 'FemtosearchBot/',
86 'HubSpot Crawler', 'DomainStatsBot/', 'Re-re Studio', 'AwarioSmartBot/',
87 'DNSResearchBot/', 'PetalBot;', 'Nmap Scripting Engine;',
88 'Google-Apps-Script; beanserver;', 'woorankreview/', 'Seekport Crawler;', 'AHC/',
89 'Semanticbot/', 'Embed PHP library', 'XoviOnpageCrawler;', 'Pinterest/',
90 'GetHPinfo.com-Bot/', 'BoardReader Favicon Fetcher', 'Google-Adwords-Instant', 'newspaper/',
91 'YurichevBot/', 'Crawling at Home Project', 'InfoTigerBot/',
92 'AdIdxBot/', 'MicrosoftPreview/', 'masscan/'
95 if (!DI::config()->get('blockbot', 'good_crawlers')) {
96 $agents = array_merge($agents, $good_agents);
98 foreach ($good_agents as $good_agent) {
99 if (stristr($_SERVER['HTTP_USER_AGENT'], $good_agent)) {
105 if (!DI::config()->get('blockbot', 'socialmedia_agents')) {
106 $agents = array_merge($agents, $socialmedia_agents);
108 foreach ($socialmedia_agents as $socialmedia_agent) {
109 if (stristr($_SERVER['HTTP_USER_AGENT'], $socialmedia_agent)) {
115 if (DI::config()->get('blockbot', 'block_gab')) {
116 $agents[] = 'GabSocial/';
119 foreach ($agents as $agent) {
120 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
121 throw new ForbiddenException('Bots are not allowed. If you consider this a mistake, create an issue at https://github.com/friendica/friendica');
125 // This switch here is only meant for developers who want to add more bots to the list above, it is not safe for production.
126 if (!DI::config()->get('blockbot', 'training')) {
130 $crawlerDetect = new CrawlerDetect();
132 if (!$crawlerDetect->isCrawler()) {
133 logger::debug('Good user agent detected', $logdata);
137 // List of false positives' strings of known "good" agents.
139 'curl', 'zgrab', 'Go-http-client', 'curb', 'github.com', 'reqwest', 'Feedly/',
140 'Python-urllib/', 'Liferea/', 'aiohttp/', 'WordPress.com Reader', 'hackney/',
141 'Faraday v', 'okhttp', 'UniversalFeedParser', 'PixelFedBot', 'python-requests',
142 'WordPress/', 'http.rb/', 'Apache-HttpClient/', 'WordPress.com;', 'Pleroma',
143 'Dispatch/', 'Ruby', 'Java/', 'libwww-perl/', 'Mastodon/', 'FeedlyApp/',
144 'lua-resty-http/', 'Tiny Tiny RSS/', 'Wget/', 'PostmanRuntime/',
145 'W3C_Validator/', 'NetNewsWire', 'FeedValidator/', 'theoldreader.com', 'axios/',
146 'Paw/', 'PeerTube/', 'fedi.inex.dev', 'FediDB/', 'index.community crawler',
147 'Slackbot-LinkExpanding', 'Firefish/', 'Takahe/', 'Akkoma ', 'Misskey/'
150 if (DI::config()->get('blockbot', 'good_crawlers')) {
151 $agents = array_merge($agents, $good_agents);
154 foreach ($agents as $agent) {
155 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
156 logger::info('False positive', $logdata);
161 logger::notice('Blocked bot', $logdata);
162 throw new ForbiddenException('Bots are not allowed. If you consider this a mistake, create an issue at https://github.com/friendica/friendica');