Some more bot agents added
[friendica-addons.git/.git] / blockbot / blockbot.php
1 <?php
2 /**
3  * Name: blockbot
4  * Description: Blocking bots based on detecting bots/crawlers/spiders via the user agent and http_from header.
5  * Version: 0.2
6  * Author: Philipp Holzer <admin@philipp.info>
7  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8  *
9  */
10
11 use Friendica\App;
12 use Friendica\Core\Hook;
13 use Friendica\Core\System;
14 use Friendica\DI;
15 use Jaybizzle\CrawlerDetect\CrawlerDetect;
16 use Friendica\Core\Logger;
17 use Friendica\Core\Renderer;
18
19 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
20
21 function blockbot_install() {
22         Hook::register('init_1', __FILE__, 'blockbot_init_1');
23 }
24
25
26 function blockbot_uninstall() {
27         Hook::unregister('init_1', __FILE__, 'blockbot_init_1');
28 }
29
30 function blockbot_addon_admin(&$a, &$o) {
31         $t = Renderer::getMarkupTemplate("admin.tpl", "addon/blockbot/");
32
33         $o = Renderer::replaceMacros($t, [
34                 '$submit' => DI::l10n()->t('Save Settings'),
35                 '$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."],
36                 '$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.'],
37                 '$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."],
38         ]);
39 }
40
41 function blockbot_addon_admin_post(&$a) {
42         DI::config()->set('blockbot', 'good_crawlers', $_POST['good_crawlers'] ?? false);
43         DI::config()->set('blockbot', 'block_gab', $_POST['block_gab'] ?? false);
44         DI::config()->set('blockbot', 'training', $_POST['training'] ?? false);
45         info(DI::l10n()->t('Settings updated.'). EOL);
46 }
47
48 function blockbot_init_1(App $a) {
49         if (empty($_SERVER['HTTP_USER_AGENT'])) {
50                 return;
51         }
52
53         $logdata = ['agent' => $_SERVER['HTTP_USER_AGENT'], 'uri' => $_SERVER['REQUEST_URI']];
54
55         // List of "good" crawlers
56         $good_agents = ['fediverse.space crawler', 'fediverse.network crawler', 'Active_Pods_CheckBot_3.0',
57                         'Social-Relay/', 'Test Certificate Info', 'Uptimebot/', 'GNUSocialBot', 'UptimeRobot/'];
58
59         // List of known crawlers.
60         $agents = ['SemrushBot', 's~feedly-nikon3', 'Qwantify/Bleriot/', 'ltx71', 'Sogou web spider/',
61                 'Diffbot/', 'Twitterbot/', 'YisouSpider', 'evc-batch/', 'LivelapBot/', 'TrendsmapResolver/',
62                 'PaperLiBot/', 'Nuzzel', 'um-LN/', 'Google Favicon', 'Datanyze', 'BLEXBot/', '360Spider',
63                 'adscanner/', 'HeadlessChrome', 'wpif', 'startmebot/', 'Googlebot/', 'Applebot/',
64                 'facebookexternalhit/', 'GoogleImageProxy', 'bingbot/', 'heritrix/', 'ldspider',
65                 'AwarioRssBot/', 'Zabbix', 'TweetmemeBot/', 'dcrawl/', 'PhantomJS/', 'Googlebot-Image/',
66                 'CrowdTanglebot/', 'Mediapartners-Google', 'Baiduspider/', 'datagnionbot',
67                 'MegaIndex.ru/', 'SMUrlExpander', 'Hatena-Favicon/', 'Wappalyzer', 'FlipboardProxy/',
68                 'NetcraftSurveyAgent/', 'Dataprovider.com', 'SMTBot/', 'Nimbostratus-Bot/',
69                 'DuckDuckGo-Favicons-Bot/', 'IndieWebCards/', 'proximic', 'netEstate NE Crawler',
70                 'AhrefsBot/', 'YandexBot/', 'Exabot/', 'Mediumbot-MetaTagFetcher/', 'WhatsApp/',
71                 'TelegramBot', 'SurdotlyBot/', 'BingPreview/', 'SabsimBot/', 'CCBot/', 'WbSrch/',
72                 'DuckDuckBot-Https/', 'HTTP Banner Detection', 'YandexImages/', 'archive.org_bot',
73                 'ArchiveTeam ArchiveBot/', 'yacybot', 'https://developers.google.com/+/web/snippet/',
74                 'Scrapy/', 'github-camo', 'MJ12bot/', 'DotBot/', 'Pinterestbot/', 'Jooblebot/',
75                 'Cliqzbot/', 'YaK/', 'Mediatoolkitbot', 'Snacktory', 'FunWebProducts', 'oBot/',
76                 '7Siters/', 'KOCMOHABT', 'Google-SearchByImage', 'FemtosearchBot/',
77                 'HubSpot Crawler', 'DomainStatsBot/', 'Re-re Studio', 'AwarioSmartBot/',
78                 'SummalyBot/', 'DNSResearchBot/', 'PetalBot;', 'Nmap Scripting Engine;',
79                 'Google-Apps-Script; beanserver;', 'woorankreview/', 'Seekport Crawler;', 'AHC/'];
80
81         if (!DI::config()->get('blockbot', 'good_crawlers')) {
82                 $agents = array_merge($agents, $good_agents);
83         } else {
84                 foreach ($good_agents as $good_agent) {
85                         if (stristr($_SERVER['HTTP_USER_AGENT'], $good_agent)) {
86                                 return;
87                         }
88                 }
89         }
90
91         if (DI::config()->get('blockbot', 'block_gab')) {
92                 $agents[] = 'GabSocial/';
93         }
94
95         foreach ($agents as $agent) {
96                 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
97                         System::httpExit(403, 'Bots are not allowed');
98                 }
99         }
100
101         // This switch here is only meant for developers who want to add more bots to the list above, it is not safe for production.
102         if (!DI::config()->get('blockbot', 'training')) {
103                 return;
104         }
105
106         $crawlerDetect = new CrawlerDetect();
107
108         if (!$crawlerDetect->isCrawler()) {
109                 logger::debug('Good user agent detected', $logdata);
110                 return;
111         }
112
113         // List of false positives' strings of known "good" agents.
114         $agents = ['curl', 'zgrab', 'Go-http-client', 'curb', 'github.com', 'reqwest', 'Feedly/',
115                 'Python-urllib/', 'Liferea/', 'aiohttp/', 'WordPress.com Reader', 'hackney/',
116                 'Faraday v', 'okhttp', 'UniversalFeedParser', 'PixelFedBot', 'python-requests',
117                 'WordPress/', 'http.rb/', 'Apache-HttpClient/', 'WordPress.com;', 'Pleroma',
118                 'Dispatch/', 'Ruby', 'Java/', 'libwww-perl/', 'Mastodon/',
119                 'lua-resty-http/', 'Tiny Tiny RSS/', 'Wget/', 'PostmanRuntime/',
120                 'W3C_Validator/', 'NetNewsWire'];
121
122         if (DI::config()->get('blockbot', 'good_crawlers')) {
123                 $agents = array_merge($agents, $good_agents);
124         }
125
126         foreach ($agents as $agent) {
127                 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
128                         logger::notice('False positive', $logdata);
129                         return;
130                 }
131         }
132
133         logger::info('Blocked bot', $logdata);
134         System::httpExit(403, 'Bots are not allowed');
135 }