2024-07-17 18:18:11 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
2024-07-20 09:45:13 +02:00
|
|
|
use App\Entity\Domain;
|
2024-07-21 16:55:39 +02:00
|
|
|
use App\Entity\WatchList;
|
|
|
|
|
use App\Message\ProcessDomainTrigger;
|
2024-07-20 09:45:13 +02:00
|
|
|
use App\Repository\DomainRepository;
|
2024-07-17 18:18:11 +02:00
|
|
|
use App\Service\RDAPService;
|
2024-08-04 14:45:27 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2024-08-04 21:26:32 +02:00
|
|
|
use Random\Randomizer;
|
2024-07-17 18:18:11 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2024-07-21 14:56:10 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
2024-07-21 19:47:25 +02:00
|
|
|
use Symfony\Component\HttpKernel\KernelInterface;
|
2024-07-21 16:55:39 +02:00
|
|
|
use Symfony\Component\Messenger\Exception\ExceptionInterface;
|
|
|
|
|
use Symfony\Component\Messenger\MessageBusInterface;
|
2024-07-21 14:56:10 +02:00
|
|
|
use Symfony\Component\RateLimiter\RateLimiterFactory;
|
2024-07-25 19:04:59 +02:00
|
|
|
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
|
|
|
|
|
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
|
|
|
|
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
2024-07-17 18:18:11 +02:00
|
|
|
|
|
|
|
|
class DomainRefreshController extends AbstractController
|
|
|
|
|
{
|
2024-08-02 23:24:52 +02:00
|
|
|
public function __construct(private readonly DomainRepository $domainRepository,
|
|
|
|
|
private readonly RDAPService $RDAPService,
|
2024-08-05 01:30:27 +02:00
|
|
|
private readonly RateLimiterFactory $rdapRequestsLimiter,
|
2024-08-04 14:45:27 +02:00
|
|
|
private readonly MessageBusInterface $bus,
|
|
|
|
|
private readonly LoggerInterface $logger
|
|
|
|
|
) {
|
2024-07-20 09:45:13 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-17 18:18:11 +02:00
|
|
|
/**
|
2024-07-25 19:04:59 +02:00
|
|
|
* @throws TransportExceptionInterface
|
|
|
|
|
* @throws HttpExceptionInterface
|
|
|
|
|
* @throws DecodingExceptionInterface
|
2024-07-21 16:55:39 +02:00
|
|
|
* @throws ExceptionInterface
|
2024-08-02 23:24:52 +02:00
|
|
|
* @throws \Exception
|
2024-07-17 18:18:11 +02:00
|
|
|
*/
|
2024-07-21 19:47:25 +02:00
|
|
|
public function __invoke(string $ldhName, KernelInterface $kernel): ?Domain
|
2024-07-17 18:18:11 +02:00
|
|
|
{
|
2024-08-04 13:22:31 +02:00
|
|
|
$idnDomain = strtolower(idn_to_ascii($ldhName));
|
2024-08-04 14:45:27 +02:00
|
|
|
$userId = $this->getUser()->getUserIdentifier();
|
|
|
|
|
|
|
|
|
|
$this->logger->info('User {username} wants to update the domain name {idnDomain}.', [
|
|
|
|
|
'username' => $userId,
|
|
|
|
|
'idnDomain' => $idnDomain,
|
|
|
|
|
]);
|
2024-08-04 13:22:31 +02:00
|
|
|
|
2024-08-03 00:30:03 +02:00
|
|
|
/** @var ?Domain $domain */
|
2024-08-04 13:22:31 +02:00
|
|
|
$domain = $this->domainRepository->findOneBy(['ldhName' => $idnDomain]);
|
2024-07-25 20:21:57 +02:00
|
|
|
|
|
|
|
|
// If the domain name exists in the database, recently updated and not important, we return the stored Domain
|
2024-08-02 23:24:52 +02:00
|
|
|
if (null !== $domain
|
|
|
|
|
&& !$domain->getDeleted()
|
|
|
|
|
&& ($domain->getUpdatedAt()->diff(new \DateTimeImmutable('now'))->days < 7)
|
|
|
|
|
&& !$this->RDAPService::isToBeWatchClosely($domain, $domain->getUpdatedAt())
|
|
|
|
|
) {
|
2024-08-04 14:45:27 +02:00
|
|
|
$this->logger->info('It is not necessary to update the information of the domain name {idnDomain} with the RDAP protocol.', [
|
|
|
|
|
'idnDomain' => $idnDomain,
|
|
|
|
|
]);
|
|
|
|
|
|
2024-08-02 23:24:52 +02:00
|
|
|
return $domain;
|
|
|
|
|
}
|
2024-07-20 09:45:13 +02:00
|
|
|
|
2024-08-04 22:00:20 +02:00
|
|
|
if (false === $kernel->isDebug() && true === $this->getParameter('limited_features')) {
|
2024-08-05 01:30:27 +02:00
|
|
|
$limiter = $this->rdapRequestsLimiter->create($userId);
|
2024-08-05 22:41:08 +02:00
|
|
|
$limit = $limiter->consume();
|
|
|
|
|
|
|
|
|
|
if (!$limit->isAccepted()) {
|
|
|
|
|
throw new TooManyRequestsHttpException($limit->getRetryAfter()->getTimestamp() - time());
|
2024-08-02 23:24:52 +02:00
|
|
|
}
|
2024-07-21 16:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-03 00:30:03 +02:00
|
|
|
$updatedAt = null === $domain ? new \DateTimeImmutable('now') : $domain->getUpdatedAt();
|
2024-08-04 13:22:31 +02:00
|
|
|
$domain = $this->RDAPService->registerDomain($idnDomain);
|
2024-07-25 20:21:57 +02:00
|
|
|
|
2024-08-04 21:26:32 +02:00
|
|
|
$randomizer = new Randomizer();
|
|
|
|
|
$watchLists = $randomizer->shuffleArray($domain->getWatchLists()->toArray());
|
|
|
|
|
|
2024-07-21 16:55:39 +02:00
|
|
|
/** @var WatchList $watchList */
|
2024-08-04 21:26:32 +02:00
|
|
|
foreach ($watchLists as $watchList) {
|
2024-07-21 16:55:39 +02:00
|
|
|
$this->bus->dispatch(new ProcessDomainTrigger($watchList->getToken(), $domain->getLdhName(), $updatedAt));
|
2024-07-20 09:45:13 +02:00
|
|
|
}
|
2024-07-21 16:55:39 +02:00
|
|
|
|
2024-07-20 09:45:13 +02:00
|
|
|
return $domain;
|
2024-07-17 18:18:11 +02:00
|
|
|
}
|
2024-08-02 23:24:52 +02:00
|
|
|
}
|