Skip to content

Commit

Permalink
[FEATURE] Add blacklistcheck for ip addresse ranges from perplexity
Browse files Browse the repository at this point in the history
The downside here is 150ms slower than before the change on my local test environment
  • Loading branch information
einpraegsam committed Aug 16, 2024
1 parent f15f991 commit 25535a0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
31 changes: 31 additions & 0 deletions Classes/Domain/Tracker/StopTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

use In2code\Lux\Domain\Model\Fingerprint;
use In2code\Lux\Events\StopAnyProcessBeforePersistenceEvent;
use In2code\Lux\Exception\DisallowedIpAddressException;
use In2code\Lux\Exception\DisallowedUserAgentException;
use In2code\Lux\Utility\IpUtility;

/**
* Class StopTracking
Expand All @@ -15,6 +17,7 @@
* - If useragent contains stop words (e.g. lighthouse, sistrix)
* - If useragent turns out to be a blacklisted browser (e.g. "Googlebot")
* - If useragent turns out to be a bot (via WhichBrowser\Parser)
* - If IP address is in blacklisted range
*/
class StopTracking
{
Expand Down Expand Up @@ -120,6 +123,21 @@ class StopTracking
'youchat',
];

/**
* List e.g. from https://www.perplexity.ai/perplexitybot.json
*
* @var array
*/
protected array $ipRanges = [
'54.90.207.250/32',
'23.22.208.105/32',
'54.242.1.13/32',
'18.208.251.246/32',
'34.230.5.59/32',
'18.207.114.171/32',
'54.221.7.250/32',
];

/**
* Stop tracking if:
* - UserAgent is empty (probably a crawler like crawler or caretaker extension in TYPO3)
Expand All @@ -129,13 +147,15 @@ class StopTracking
* @param StopAnyProcessBeforePersistenceEvent $event
* @return void Throw exception if blacklisted
* @throws DisallowedUserAgentException
* @throws DisallowedIpAddressException
*/
public function __invoke(StopAnyProcessBeforePersistenceEvent $event)
{
$this->checkForEmptyUserAgent($event->getFingerprint());
$this->checkForBlacklistedUserAgentStrings($event->getFingerprint());
$this->checkForBlacklistedParsedUserAgent($event->getFingerprint());
$this->checkForBotUserAgent($event->getFingerprint());
$this->checkForBlacklistedIpAddressRanges();
}

/**
Expand Down Expand Up @@ -188,4 +208,15 @@ protected function checkForBotUserAgent(Fingerprint $fingerprint): void
throw new DisallowedUserAgentException('Stop tracking because of bot', 1608109683);
}
}

/**
* @return void
* @throws DisallowedIpAddressException
*/
protected function checkForBlacklistedIpAddressRanges(): void
{
if (IpUtility::isCurrentIpInGivenRanges($this->ipRanges)) {
throw new DisallowedIpAddressException('Stop tracking because of blacklisted IP', 1723793497);
}
}
}
10 changes: 10 additions & 0 deletions Classes/Exception/DisallowedIpAddressException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);
namespace In2code\Lux\Exception;

use Exception;

class DisallowedIpAddressException extends Exception
{
}
16 changes: 14 additions & 2 deletions Classes/Utility/IpUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class IpUtility
* Get visitors IP address. Also make a testing ip possible. And in addition get external IP if IP=="127.0.0.1" for
* local testing environment.
*
* @param string $testIp
* @param string $testIp for testing only
* @return string
*/
public static function getIpAddress(string $testIp = ''): string
{
$ipAddress = $testIp;
if (empty($ipAddress)) {
if ($ipAddress === '') {
$ipAddress = GeneralUtility::getIndpEnv('REMOTE_ADDR');
if ($ipAddress === '127.0.0.1') {
$externalIpAddress = GeneralUtility::makeInstance(RequestFactory::class)
Expand All @@ -35,6 +35,18 @@ public static function getIpAddress(string $testIp = ''): string
return $ipAddress;
}

public static function isCurrentIpInGivenRanges(array $ranges, string $testIp = ''): bool
{
$currentAddress = \IPLib\Factory::parseAddressString(self::getIpAddress($testIp));
foreach ($ranges as $range) {
$range = \IPLib\Factory::parseRangeString($range);
if ($currentAddress->matches($range) === true) {
return true;
}
}
return false;
}

public static function getIpAddressAnonymized(string $testIp = ''): string
{
return preg_replace(
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"typo3/cms-core": "^11.5 || ^12.0",
"symfony/expression-language": ">=4.0",
"buzz/which-browser-parser": "^2.1",
"in2code/google-image-grabber": "^1.0.4"
"in2code/google-image-grabber": "^1.0.4",
"mlocati/ip-lib": "^1.18"
},
"replace": {
"typo3-ter/lux": "self.version"
Expand Down

0 comments on commit 25535a0

Please sign in to comment.