There are in fact many false positives ...
[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.1
6  * Author: Philipp Holzer <admin@philipp.info>
7  *
8  */
9
10 use Friendica\App;
11 use Friendica\Core\Hook;
12 use Friendica\Core\System;
13 use Jaybizzle\CrawlerDetect\CrawlerDetect;
14 use Friendica\Core\Logger;
15
16 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
17
18 function blockbot_install() {
19         Hook::register('init_1', __FILE__, 'blockbot_init_1');
20 }
21
22
23 function blockbot_uninstall() {
24         Hook::unregister('init_1', __FILE__, 'blockbot_init_1');
25 }
26
27 function blockbot_init_1(App $a) {
28         $crawlerDetect = new CrawlerDetect();
29
30         $logdata = ['agent' => $_SERVER['HTTP_USER_AGENT'], 'uri' => $_SERVER['REQUEST_URI']];
31
32         if (!$crawlerDetect->isCrawler()) {
33                 logger::debug('Good user agent detected', $logdata);
34                 return;
35         }
36
37         // List of strings of reported false positives
38         $agents = ['Mastodon', 'hackney', 'Faraday', 'okhttp', 'UniversalFeedParser', 'PixelFedBot', 'python-requests',
39                 'WordPress'];
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('Already reported wrong detection', $logdata);
44                         return;
45                 }
46         }
47
48         // List of strings of known "good" agents
49         $agents = ['diaspora-connection-tester', 'DiasporaFederation', 'Friendica', '(compatible; zot)',
50                 'Micro.blog', 'GangGo', 'python/federation', 'GNU social', 'winHttp',
51                 'Go-http-client', 'Mr.4x3 Powered', 'Test Certificate Info', 'WordPress.com', 'zgrab',
52                 'curl/', 'StatusNet', 'OpenGraphReader/', 'Uptimebot/', 'python-opengraph-jaywink',
53                 'fediverse.network crawler', 'Active_Pods_CheckBot_3.0', 'Social-Relay'];
54
55         foreach ($agents as $agent) {
56                 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
57                         // Report every false positive here: https://github.com/JayBizzle/Crawler-Detect/issues/
58                         // After report move it into the array above
59                         logger::notice('False positive', $logdata);
60                         return;
61                 }
62         }
63
64         // List of known crawlers. They are added here to avoid having them logged at the end of the function.
65         // This helps to detect false positives
66         $agents = ['Mozilla/5.0 (compatible; SemrushBot/3~bl; +http://www.semrush.com/bot.html)', 'SEMrushBot',
67                 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 AppEngine-Google; (+http://code.google.com/appengine; appid: s~feedly-nikon3)'];
68
69         foreach ($agents as $agent) {
70                 if ($_SERVER['HTTP_USER_AGENT'] == $agent) {
71                         System::httpExit(403, 'Bots are not allowed');
72                 }
73         }
74
75         logger::info('Blocked bot', $logdata);
76         System::httpExit(403, 'Bots are not allowed');
77 }