feat: add internal rate limit for rdap queries

This commit is contained in:
Maël Gangloff
2025-10-28 12:09:38 +01:00
parent 12c1a9bb97
commit 98d92ce8f8
17 changed files with 203 additions and 92 deletions

View File

@@ -6,7 +6,7 @@ use App\Entity\Domain;
use App\Entity\DomainEvent;
use App\Entity\DomainStatus;
use App\Entity\Watchlist;
use App\Message\SendDomainEventNotif;
use App\Message\DetectDomainChange;
use App\Notifier\DomainStatusUpdateNotification;
use App\Notifier\DomainUpdateNotification;
use App\Repository\DomainEventRepository;
@@ -25,7 +25,7 @@ use Symfony\Component\Mime\Address;
use Symfony\Component\Notifier\Recipient\Recipient;
#[AsMessageHandler]
final readonly class SendDomainEventNotifHandler
final readonly class DetectDomainChangeHandler
{
private Address $sender;
@@ -51,7 +51,7 @@ final readonly class SendDomainEventNotifHandler
* @throws TransportExceptionInterface
* @throws \Exception
*/
public function __invoke(SendDomainEventNotif $message): void
public function __invoke(DetectDomainChange $message): void
{
/** @var Watchlist $watchlist */
$watchlist = $this->watchlistRepository->findOneBy(['token' => $message->watchlistToken]);

View File

@@ -3,8 +3,8 @@
namespace App\MessageHandler;
use App\Entity\Watchlist;
use App\Message\ProcessWatchlistTrigger;
use App\Message\UpdateDomainsFromWatchlist;
use App\Message\ProcessAllWatchlist;
use App\Message\ProcessWatchlist;
use App\Repository\WatchlistRepository;
use Random\Randomizer;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
@@ -12,7 +12,7 @@ use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
#[AsMessageHandler]
final readonly class ProcessWatchlistTriggerHandler
final readonly class ProcessAllWatchlistHandler
{
public function __construct(
private WatchlistRepository $watchlistRepository,
@@ -23,11 +23,13 @@ final readonly class ProcessWatchlistTriggerHandler
/**
* @throws ExceptionInterface
*/
public function __invoke(ProcessWatchlistTrigger $message): void
public function __invoke(ProcessAllWatchlist $message): void
{
/*
* We shuffle the watch lists to process them in an order that we consider random.
* The shuffling is provided by a high-level API of a CSPRNG number generator.
*
* ProcessAllWatchlist -> ProcessWatchlist -> UpdateDomain -> (DetectDomainChange & OrderDomain)
*/
$randomizer = new Randomizer();
@@ -35,7 +37,7 @@ final readonly class ProcessWatchlistTriggerHandler
/** @var Watchlist $watchlist */
foreach ($watchlists as $watchlist) {
$this->bus->dispatch(new UpdateDomainsFromWatchlist($watchlist->getToken()));
$this->bus->dispatch(new ProcessWatchlist($watchlist->getToken()));
}
}
}

View File

@@ -4,53 +4,36 @@ namespace App\MessageHandler;
use App\Entity\Domain;
use App\Entity\Watchlist;
use App\Exception\DomainNotFoundException;
use App\Exception\TldNotSupportedException;
use App\Exception\UnknownRdapServerException;
use App\Message\OrderDomain;
use App\Message\SendDomainEventNotif;
use App\Message\UpdateDomainsFromWatchlist;
use App\Notifier\DomainDeletedNotification;
use App\Repository\DomainRepository;
use App\Message\ProcessWatchlist;
use App\Message\UpdateDomain;
use App\Repository\WatchlistRepository;
use App\Service\ChatNotificationService;
use App\Service\Provider\AbstractProvider;
use App\Service\Provider\CheckDomainProviderInterface;
use App\Service\RDAPService;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Notifier\Recipient\Recipient;
#[AsMessageHandler]
final readonly class UpdateDomainsFromWatchlistHandler
final readonly class ProcessWatchlistHandler
{
private Address $sender;
public function __construct(
private RDAPService $RDAPService,
private ChatNotificationService $chatNotificationService,
private MailerInterface $mailer,
string $mailerSenderEmail,
string $mailerSenderName,
private MessageBusInterface $bus,
private WatchlistRepository $watchlistRepository,
private LoggerInterface $logger,
#[Autowire(service: 'service_container')]
private ContainerInterface $locator,
private DomainRepository $domainRepository,
) {
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
}
/**
* @throws \Throwable
*/
public function __invoke(UpdateDomainsFromWatchlist $message): void
public function __invoke(ProcessWatchlist $message): void
{
/** @var Watchlist $watchlist */
$watchlist = $this->watchlistRepository->findOneBy(['token' => $message->watchlistToken]);
@@ -98,37 +81,7 @@ final readonly class UpdateDomainsFromWatchlistHandler
/** @var Domain $domain */
foreach ($watchlist->getDomains()->filter(fn ($domain) => $this->RDAPService->isToBeUpdated($domain, false, null !== $watchlist->getConnector())) as $domain
) {
$updatedAt = $domain->getUpdatedAt();
$deleted = $domain->getDeleted();
try {
/*
* Domain name update
* We send messages that correspond to the sending of notifications that will not be processed here.
*/
$this->RDAPService->registerDomain($domain->getLdhName());
$this->bus->dispatch(new SendDomainEventNotif($watchlist->getToken(), $domain->getLdhName(), $updatedAt));
} catch (DomainNotFoundException) {
$newDomain = $this->domainRepository->findOneBy(['ldhName' => $domain->getLdhName()]);
if (!$deleted && null !== $newDomain && $newDomain->getDeleted()) {
$notification = new DomainDeletedNotification($this->sender, $domain);
$this->mailer->send($notification->asEmailMessage(new Recipient($watchlist->getUser()->getEmail()))->getMessage());
$this->chatNotificationService->sendChatNotification($watchlist, $notification);
}
if ($watchlist->getConnector()) {
/*
* If the domain name no longer appears in the WHOIS AND a connector is associated with this Watchlist,
* this connector is used to purchase the domain name.
*/
$this->bus->dispatch(new OrderDomain($watchlist->getToken(), $domain->getLdhName()));
}
} catch (TldNotSupportedException|UnknownRdapServerException) {
/*
* In this case, the domain name can no longer be updated. Unfortunately, there is nothing more that can be done.
*/
}
$this->bus->dispatch(new UpdateDomain($domain->getLdhName(), $watchlist->getToken()));
}
}

View File

@@ -0,0 +1,127 @@
<?php
namespace App\MessageHandler;
use App\Entity\RdapServer;
use App\Exception\DomainNotFoundException;
use App\Exception\MalformedDomainException;
use App\Exception\TldNotSupportedException;
use App\Exception\UnknownRdapServerException;
use App\Exception\UnsupportedDsnSchemeException;
use App\Message\DetectDomainChange;
use App\Message\OrderDomain;
use App\Message\UpdateDomain;
use App\Notifier\DomainDeletedNotification;
use App\Repository\DomainRepository;
use App\Repository\WatchlistRepository;
use App\Service\ChatNotificationService;
use App\Service\RDAPService;
use Doctrine\ORM\OptimisticLockException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
#[AsMessageHandler]
final readonly class UpdateDomainHandler
{
private Address $sender;
public function __construct(
private RDAPService $RDAPService,
private ChatNotificationService $chatNotificationService,
private MailerInterface $mailer,
string $mailerSenderEmail,
string $mailerSenderName,
private MessageBusInterface $bus,
private WatchlistRepository $watchlistRepository,
private DomainRepository $domainRepository,
private RateLimiterFactory $rdapRequestsLimiter, private LoggerInterface $logger,
) {
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
}
/**
* @throws TransportExceptionInterface
* @throws \Symfony\Component\Notifier\Exception\TransportExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws OptimisticLockException
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
* @throws UnsupportedDsnSchemeException
* @throws ServerExceptionInterface
* @throws MalformedDomainException
* @throws ExceptionInterface
*/
public function __invoke(UpdateDomain $message): void
{
$domain = $this->domainRepository->findOneBy(['ldhName' => $message->ldhName]);
/** @var ?RdapServer $rdapServer */
$rdapServer = $domain->getTld()->getRdapServers()->first();
if (null === $rdapServer) {
$this->logger->warning('No RDAP server for this domain name', [
'ldhName' => $domain->getLdhName(),
]);
return;
}
$limiter = $this->rdapRequestsLimiter->create($rdapServer->getUrl());
$limit = $limiter->consume();
if (!$limit->isAccepted()) {
$retryAfter = $limit->getRetryAfter()->getTimestamp() - time();
$this->logger->warning('Security rate limit reached for this RDAP server', [
'url' => $rdapServer->getUrl(),
'retryAfter' => $retryAfter,
]);
throw new RecoverableMessageHandlingException('Rate limit reached', 0, null, $retryAfter);
}
$watchlist = $this->watchlistRepository->findOneBy(['token' => $message->watchlistToken]);
$updatedAt = $domain->getUpdatedAt();
$deleted = $domain->getDeleted();
try {
/*
* Domain name update
* We send messages that correspond to the sending of notifications that will not be processed here.
*/
$this->RDAPService->registerDomain($domain->getLdhName());
$this->bus->dispatch(new DetectDomainChange($watchlist->getToken(), $domain->getLdhName(), $updatedAt));
} catch (DomainNotFoundException) {
$newDomain = $this->domainRepository->findOneBy(['ldhName' => $domain->getLdhName()]);
if (!$deleted && null !== $newDomain && $newDomain->getDeleted()) {
$notification = new DomainDeletedNotification($this->sender, $domain);
$this->mailer->send($notification->asEmailMessage(new Recipient($watchlist->getUser()->getEmail()))->getMessage());
$this->chatNotificationService->sendChatNotification($watchlist, $notification);
}
if ($watchlist->getConnector()) {
/*
* If the domain name no longer appears in the WHOIS AND a connector is associated with this Watchlist,
* this connector is used to purchase the domain name.
*/
$this->bus->dispatch(new OrderDomain($watchlist->getToken(), $domain->getLdhName()));
}
} catch (TldNotSupportedException|UnknownRdapServerException) {
/*
* In this case, the domain name can no longer be updated. Unfortunately, there is nothing more that can be done.
*/
}
}
}