1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
<?php
/**
* webtrees: online genealogy
* 'Copyright (C) 2023 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Http\Middleware;
use Fig\Http\Message\StatusCodeInterface;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Validator;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Iodev\Whois\Loaders\CurlLoader;
use Iodev\Whois\Modules\Asn\AsnRouteInfo;
use Iodev\Whois\Whois;
use IPLib\Address\AddressInterface;
use IPLib\Factory as IPFactory;
use IPLib\Range\RangeInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Throwable;
use function array_filter;
use function array_map;
use function assert;
use function gethostbyaddr;
use function gethostbyname;
use function preg_match_all;
use function random_int;
use function response;
use function str_contains;
use function str_ends_with;
/**
* Middleware to block bad robots before they waste our valuable CPU cycles.
*/
class BadBotBlocker implements MiddlewareInterface
{
private const REGEX_OCTET = '(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)';
private const REGEX_IPV4 = '/\\b' . self::REGEX_OCTET . '(?:\\.' . self::REGEX_OCTET . '){3}\\b/';
// Cache whois requests. Try to avoid all caches expiring at the same time.
private const WHOIS_TTL_MIN = 28 * 86400;
private const WHOIS_TTL_MAX = 35 * 86400;
private const WHOIS_TIMEOUT = 5;
// Bad robots - SEO optimisers, advertisers, etc. This list is shared with robots.txt.
public const BAD_ROBOTS = [
'admantx',
'Adsbot',
'AhrefsBot',
'Amazonbot', // Until it understands crawl-delay and noindex / nofollow
'AspiegelBot',
'Barkrowler',
'BLEXBot',
'DataForSEO',
'DataForSeoBot', // https://dataforseo.com/dataforseo-bot
'DotBot',
'Grapeshot',
'Honolulu-bot', // Aggressive crawer, no info available
'ia_archiver',
'linabot', // Aggressive crawer, no info available
'Linguee',
'MegaIndex.ru',
'MJ12bot',
'netEstate NE',
'panscient',
'PetalBot',
'proximic',
'SeekportBot', // Pretends to be a search engine - but isn't
'SemrushBot',
'serpstatbot',
'SEOkicks',
'SiteKiosk',
'Turnitin',
'wp_is_mobile', // Nothing to do with wordpress
'XoviBot',
'ZoominfoBot',
];
/**
* Some search engines use reverse/forward DNS to verify the IP address.
*
* @see https://developer.amazon.com/support/amazonbot
* @see https://support.google.com/webmasters/answer/80553?hl=en
* @see https://www.bing.com/webmaster/help/which-crawlers-does-bing-use-8c184ec0
* @see https://www.bing.com/webmaster/help/how-to-verify-bingbot-3905dc26
* @see https://yandex.com/support/webmaster/robot-workings/check-yandex-robots.html
* @see https://www.mojeek.com/bot.html
* @see https://support.apple.com/en-gb/HT204683
*/
private const ROBOT_REV_FWD_DNS = [
'Amazonbot' => ['.crawl.amazon.com'],
'Applebot' => ['.applebot.apple.com'],
'bingbot' => ['.search.msn.com'],
'BingPreview' => ['.search.msn.com'],
'Google' => ['.google.com', '.googlebot.com'],
'MojeekBot' => ['.mojeek.com'],
'Mail.RU_Bot' => ['.mail.ru'],
'msnbot' => ['.search.msn.com'],
'Qwantify' => ['.search.qwant.com'],
'Sogou' => ['.crawl.sogou.com'],
'Yahoo' => ['.crawl.yahoo.net'],
'Yandex' => ['.yandex.ru', '.yandex.net', '.yandex.com'],
];
/**
* Some search engines only use reverse DNS to verify the IP address.
*
* @see https://help.baidu.com/question?prod_id=99&class=0&id=3001
* @see https://napoveda.seznam.cz/en/full-text-search/seznambot-crawler
* @see https://www.ionos.de/terms-gtc/faq-crawler
*/
private const ROBOT_REV_ONLY_DNS = [
'Baiduspider' => ['.baidu.com', '.baidu.jp'],
'FreshBot' => ['.seznam.cz'],
'IonCrawl' => ['.1und1.org'],
'Neevabot' => ['.neeva.com'],
];
/**
* Some search engines operate from designated IP addresses.
*
* @see https://www.apple.com/go/applebot
* @see https://help.duckduckgo.com/duckduckgo-help-pages/results/duckduckbot
*/
private const ROBOT_IPS = [
'AppleBot' => [
'17.0.0.0/8',
],
'Ask Jeeves' => [
'65.214.45.143',
'65.214.45.148',
'66.235.124.192',
'66.235.124.7',
'66.235.124.101',
'66.235.124.193',
'66.235.124.73',
'66.235.124.196',
'66.235.124.74',
'63.123.238.8',
'202.143.148.61',
],
'DuckDuckBot' => [
'23.21.227.69',
'50.16.241.113',
'50.16.241.114',
'50.16.241.117',
'50.16.247.234',
'52.204.97.54',
'52.5.190.19',
'54.197.234.188',
'54.208.100.253',
'54.208.102.37',
'107.21.1.8',
],
];
/**
* Some search engines operate from designated IP addresses.
*
* @see https://bot.seekport.com/
*/
private const ROBOT_IP_FILES = [
'SeekportBot' => 'https://bot.seekport.com/seekportbot_ips.txt',
];
/**
* Some search engines operate from within a designated autonomous system.
*
* @see https://developers.facebook.com/docs/sharing/webmasters/crawler
* @see https://www.facebook.com/peering/
*/
private const ROBOT_ASNS = [
'facebook' => ['AS32934', 'AS63293'],
'twitter' => ['AS13414'],
];
/**
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$ua = Validator::serverParams($request)->string('HTTP_USER_AGENT', '');
$ip = Validator::attributes($request)->string('client-ip');
$address = IPFactory::parseAddressString($ip);
assert($address instanceof AddressInterface);
foreach (self::BAD_ROBOTS as $robot) {
if (str_contains($ua, $robot)) {
return $this->response();
}
}
foreach (self::ROBOT_REV_FWD_DNS as $robot => $valid_domains) {
if (str_contains($ua, $robot) && !$this->checkRobotDNS($ip, $valid_domains, false)) {
return $this->response();
}
}
foreach (self::ROBOT_REV_ONLY_DNS as $robot => $valid_domains) {
if (str_contains($ua, $robot) && !$this->checkRobotDNS($ip, $valid_domains, true)) {
return $this->response();
}
}
foreach (self::ROBOT_IPS as $robot => $valid_ip_ranges) {
if (str_contains($ua, $robot)) {
foreach ($valid_ip_ranges as $ip_range) {
$range = IPFactory::parseRangeString($ip_range);
if ($range instanceof RangeInterface && $range->contains($address)) {
continue 2;
}
}
return $this->response();
}
}
foreach (self::ROBOT_IP_FILES as $robot => $url) {
if (str_contains($ua, $robot)) {
$valid_ip_ranges = $this->fetchIpRangesForUrl($robot, $url);
foreach ($valid_ip_ranges as $ip_range) {
$range = IPFactory::parseRangeString($ip_range);
if ($range instanceof RangeInterface && $range->contains($address)) {
continue 2;
}
}
return $this->response();
}
}
foreach (self::ROBOT_ASNS as $robot => $asns) {
foreach ($asns as $asn) {
if (str_contains($ua, $robot)) {
foreach ($this->fetchIpRangesForAsn($asn) as $range) {
if ($range->contains($address)) {
continue 2;
}
}
return $this->response();
}
}
}
// Allow sites to block access from entire networks.
$block_asn = Validator::attributes($request)->string('block_asn', '');
preg_match_all('/(AS\d+)/', $block_asn, $matches);
foreach ($matches[1] as $asn) {
foreach ($this->fetchIpRangesForAsn($asn) as $range) {
if ($range->contains($address)) {
return $this->response();
}
}
}
return $handler->handle($request);
}
/**
* Check that an IP address belongs to a robot operator using a forward/reverse DNS lookup.
*
* @param string $ip
* @param array<string> $valid_domains
* @param bool $reverse_only
*
* @return bool
*/
private function checkRobotDNS(string $ip, array $valid_domains, bool $reverse_only): bool
{
$host = gethostbyaddr($ip);
if ($host === false) {
return false;
}
foreach ($valid_domains as $domain) {
if (str_ends_with($host, $domain)) {
return $reverse_only || $ip === gethostbyname($host);
}
}
return false;
}
/**
* Perform a whois search for an ASN.
*
* @param string $asn - The autonomous system number to query
*
* @return array<RangeInterface>
*/
private function fetchIpRangesForAsn(string $asn): array
{
return Registry::cache()->file()->remember('whois-asn-' . $asn, static function () use ($asn): array {
$mapper = static fn (AsnRouteInfo $route_info): ?RangeInterface => IPFactory::parseRangeString($route_info->route ?: $route_info->route6);
try {
$loader = new CurlLoader(self::WHOIS_TIMEOUT);
$whois = new Whois($loader);
$info = $whois->loadAsnInfo($asn);
$routes = $info->routes;
$ranges = array_map($mapper, $routes);
return array_filter($ranges);
} catch (Throwable) {
return [];
}
}, random_int(self::WHOIS_TTL_MIN, self::WHOIS_TTL_MAX));
}
/**
* Fetch a list of IP addresses from a remote file.
*
* @param string $ua
* @param string $url
*
* @return array<string>
*/
private function fetchIpRangesForUrl(string $ua, string $url): array
{
return Registry::cache()->file()->remember('url-ip-list-' . $ua, static function () use ($url): array {
try {
$client = new Client();
$response = $client->get($url, ['timeout' => 5]);
$contents = $response->getBody()->getContents();
preg_match_all(self::REGEX_IPV4, $contents, $matches);
return $matches[0];
} catch (GuzzleException) {
return [];
}
}, random_int(self::WHOIS_TTL_MIN, self::WHOIS_TTL_MAX));
}
/**
* @return ResponseInterface
*/
private function response(): ResponseInterface
{
return response('Not acceptable', StatusCodeInterface::STATUS_NOT_ACCEPTABLE);
}
}
|