mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-17 17:55:42 +00:00
feat: add propose domain endpoint
This commit is contained in:
parent
1b1b82189e
commit
c93037663f
@ -28,6 +28,7 @@ framework:
|
||||
App\Message\UpdateDomainsFromWatchlist: async
|
||||
App\Message\UpdateRdapServers: async
|
||||
App\Message\ValidateConnectorCredentials: async
|
||||
App\Message\ProposeDomainMessage: async
|
||||
|
||||
# Route your messages to the transports
|
||||
# 'App\Message\YourMessage': async
|
||||
|
||||
@ -24,3 +24,8 @@ framework:
|
||||
policy: sliding_window
|
||||
limit: 10
|
||||
interval: '1 hour'
|
||||
|
||||
propose_domain:
|
||||
policy: sliding_window
|
||||
limit: 10
|
||||
interval: '1 hour'
|
||||
|
||||
@ -63,6 +63,7 @@ security:
|
||||
- { path: "^/api/watchlists/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/calendar$", roles: PUBLIC_ACCESS }
|
||||
- { path: "^/api/watchlists/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/rss", roles: PUBLIC_ACCESS }
|
||||
- { path: "^/api/config$", roles: PUBLIC_ACCESS }
|
||||
- { path: "^/api/propose-domain", roles: PUBLIC_ACCESS }
|
||||
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
|
||||
|
||||
when@test:
|
||||
|
||||
@ -3,18 +3,13 @@
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Domain;
|
||||
use App\Entity\WatchList;
|
||||
use App\Message\SendDomainEventNotif;
|
||||
use App\Repository\DomainRepository;
|
||||
use App\Service\RDAPService;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Random\Randomizer;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Symfony\Component\Messenger\Exception\ExceptionInterface;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\RateLimiter\RateLimiterFactory;
|
||||
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
|
||||
@ -22,10 +17,9 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
|
||||
class DomainRefreshController extends AbstractController
|
||||
{
|
||||
public function __construct(private readonly DomainRepository $domainRepository,
|
||||
public function __construct(
|
||||
private readonly RDAPService $RDAPService,
|
||||
private readonly RateLimiterFactory $rdapRequestsLimiter,
|
||||
private readonly MessageBusInterface $bus,
|
||||
private readonly LoggerInterface $logger,
|
||||
private readonly KernelInterface $kernel,
|
||||
) {
|
||||
@ -49,22 +43,6 @@ class DomainRefreshController extends AbstractController
|
||||
'idnDomain' => $idnDomain,
|
||||
]);
|
||||
|
||||
/** @var ?Domain $domain */
|
||||
$domain = $this->domainRepository->findOneBy(['ldhName' => $idnDomain]);
|
||||
// If the domain name exists in the database, recently updated and not important, we return the stored Domain
|
||||
if (null !== $domain
|
||||
&& !$domain->getDeleted()
|
||||
&& !$domain->isToBeUpdated(true, true)
|
||||
&& !$this->kernel->isDebug()
|
||||
&& true !== filter_var($request->get('forced', false), FILTER_VALIDATE_BOOLEAN)
|
||||
) {
|
||||
$this->logger->info('It is not necessary to update the information of the domain name {idnDomain} with the RDAP protocol.', [
|
||||
'idnDomain' => $idnDomain,
|
||||
]);
|
||||
|
||||
return $domain;
|
||||
}
|
||||
|
||||
if (false === $this->kernel->isDebug() && true === $this->getParameter('limited_features')) {
|
||||
$limiter = $this->rdapRequestsLimiter->create($userId);
|
||||
$limit = $limiter->consume();
|
||||
@ -74,17 +52,6 @@ class DomainRefreshController extends AbstractController
|
||||
}
|
||||
}
|
||||
|
||||
$updatedAt = null === $domain ? new \DateTimeImmutable('now') : $domain->getUpdatedAt();
|
||||
$domain = $this->RDAPService->registerDomain($idnDomain);
|
||||
|
||||
$randomizer = new Randomizer();
|
||||
$watchLists = $randomizer->shuffleArray($domain->getWatchLists()->toArray());
|
||||
|
||||
/** @var WatchList $watchList */
|
||||
foreach ($watchLists as $watchList) {
|
||||
$this->bus->dispatch(new SendDomainEventNotif($watchList->getToken(), $domain->getLdhName(), $updatedAt));
|
||||
}
|
||||
|
||||
return $domain;
|
||||
return $this->RDAPService->updateDomain($idnDomain, $request->get('forced', false));
|
||||
}
|
||||
}
|
||||
|
||||
48
src/Controller/ProposeDomainController.php
Normal file
48
src/Controller/ProposeDomainController.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Message\ProposeDomainMessage;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Symfony\Component\Messenger\Exception\ExceptionInterface;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\RateLimiter\RateLimiterFactory;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
|
||||
class ProposeDomainController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly KernelInterface $kernel,
|
||||
private readonly RateLimiterFactory $rdapRequestsLimiter,
|
||||
private readonly MessageBusInterface $bus,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws ExceptionInterface
|
||||
*/
|
||||
public function __invoke(string $ldhName, Request $request): Response
|
||||
{
|
||||
if (false === $this->kernel->isDebug()) {
|
||||
$limiter = $this->rdapRequestsLimiter->create($request->getClientIp());
|
||||
$limit = $limiter->consume();
|
||||
|
||||
if (!$limit->isAccepted()) {
|
||||
throw new TooManyRequestsHttpException($limit->getRetryAfter()->getTimestamp() - time());
|
||||
}
|
||||
}
|
||||
|
||||
$this->bus->dispatch(new ProposeDomainMessage($ldhName));
|
||||
|
||||
return new Response(null, 204);
|
||||
}
|
||||
}
|
||||
@ -4,8 +4,10 @@ namespace App\Entity;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use App\Config\EventAction;
|
||||
use App\Controller\DomainRefreshController;
|
||||
use App\Controller\ProposeDomainController;
|
||||
use App\Repository\DomainRepository;
|
||||
use App\Service\RDAPService;
|
||||
use App\State\AutoRegisterDomainProvider;
|
||||
@ -56,6 +58,14 @@ use Symfony\Component\Serializer\Attribute\SerializedName;
|
||||
],
|
||||
read: false
|
||||
),
|
||||
new Post(
|
||||
uriTemplate: '/propose-domain/{ldhName}',
|
||||
controller: ProposeDomainController::class,
|
||||
shortName: 'Propose Domain',
|
||||
input: false,
|
||||
read: false,
|
||||
write: false,
|
||||
),
|
||||
],
|
||||
provider: AutoRegisterDomainProvider::class
|
||||
)]
|
||||
|
||||
11
src/Message/ProposeDomainMessage.php
Normal file
11
src/Message/ProposeDomainMessage.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Message;
|
||||
|
||||
final class ProposeDomainMessage
|
||||
{
|
||||
public function __construct(
|
||||
public string $ldhName,
|
||||
) {
|
||||
}
|
||||
}
|
||||
35
src/MessageHandler/ProposeDomainMessageHandler.php
Normal file
35
src/MessageHandler/ProposeDomainMessageHandler.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\MessageHandler;
|
||||
|
||||
use App\Message\ProposeDomainMessage;
|
||||
use App\Service\RDAPService;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
use Symfony\Component\Messenger\Exception\ExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
|
||||
#[AsMessageHandler]
|
||||
final class ProposeDomainMessageHandler
|
||||
{
|
||||
public function __construct(
|
||||
private RDAPService $RDAPService,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws DecodingExceptionInterface
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws ExceptionInterface
|
||||
*/
|
||||
public function __invoke(ProposeDomainMessage $message): void
|
||||
{
|
||||
$this->RDAPService->updateDomain($message->ldhName);
|
||||
}
|
||||
}
|
||||
@ -18,6 +18,8 @@ use App\Entity\Nameserver;
|
||||
use App\Entity\NameserverEntity;
|
||||
use App\Entity\RdapServer;
|
||||
use App\Entity\Tld;
|
||||
use App\Entity\WatchList;
|
||||
use App\Message\SendDomainEventNotif;
|
||||
use App\Repository\DomainEntityRepository;
|
||||
use App\Repository\DomainEventRepository;
|
||||
use App\Repository\DomainRepository;
|
||||
@ -31,10 +33,14 @@ use Doctrine\DBAL\LockMode;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Exception\ORMException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Random\Randomizer;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpClient\Exception\ClientException;
|
||||
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Symfony\Component\Messenger\Exception\ExceptionInterface;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
|
||||
@ -110,6 +116,8 @@ class RDAPService
|
||||
private InfluxdbService $influxService,
|
||||
#[Autowire(param: 'influxdb_enabled')]
|
||||
private bool $influxdbEnabled,
|
||||
private readonly KernelInterface $kernel,
|
||||
private readonly MessageBusInterface $bus,
|
||||
) {
|
||||
}
|
||||
|
||||
@ -936,4 +944,45 @@ class RDAPService
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws DecodingExceptionInterface
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws ExceptionInterface
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function updateDomain(string $idnDomain, bool $forced = false): ?Domain
|
||||
{
|
||||
/** @var ?Domain $domain */
|
||||
$domain = $this->domainRepository->findOneBy(['ldhName' => $idnDomain]);
|
||||
// If the domain name exists in the database, recently updated and not important, we return the stored Domain
|
||||
if (null !== $domain
|
||||
&& !$domain->getDeleted()
|
||||
&& !$domain->isToBeUpdated(true, true)
|
||||
&& !$this->kernel->isDebug()
|
||||
&& true !== filter_var($forced, FILTER_VALIDATE_BOOLEAN)
|
||||
) {
|
||||
$this->logger->info('It is not necessary to update the information of the domain name {idnDomain} with the RDAP protocol.', [
|
||||
'idnDomain' => $idnDomain,
|
||||
]);
|
||||
|
||||
return $domain;
|
||||
}
|
||||
|
||||
$updatedAt = null === $domain ? new \DateTimeImmutable('now') : $domain->getUpdatedAt();
|
||||
$domain = $this->registerDomain($idnDomain);
|
||||
|
||||
$randomizer = new Randomizer();
|
||||
$watchLists = $randomizer->shuffleArray($domain->getWatchLists()->toArray());
|
||||
|
||||
/** @var WatchList $watchList */
|
||||
foreach ($watchLists as $watchList) {
|
||||
$this->bus->dispatch(new SendDomainEventNotif($watchList->getToken(), $domain->getLdhName(), $updatedAt));
|
||||
}
|
||||
|
||||
return $domain;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user