Merged master

This commit is contained in:
Vincent
2024-09-18 13:37:07 +02:00
139 changed files with 8418 additions and 2409 deletions

View File

@@ -0,0 +1,74 @@
<?php
namespace App\Service;
use App\Config\WebhookScheme;
use App\Entity\WatchList;
use App\Notifier\DomainWatchdogNotification;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Recipient\NoRecipient;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
readonly class ChatNotificationService
{
public function __construct(
private LoggerInterface $logger
) {
}
public function sendChatNotification(WatchList $watchList, DomainWatchdogNotification $notification): void
{
$webhookDsn = $watchList->getWebhookDsn();
if (null !== $webhookDsn && 0 !== count($webhookDsn)) {
foreach ($webhookDsn as $dsnString) {
try {
$dsn = new Dsn($dsnString);
} catch (InvalidArgumentException $exception) {
throw new BadRequestHttpException($exception->getMessage());
}
$scheme = $dsn->getScheme();
$webhookScheme = WebhookScheme::tryFrom($scheme);
if (null === $webhookScheme) {
throw new BadRequestHttpException("The DSN scheme ($scheme) is not supported");
}
$transportFactoryClass = $webhookScheme->getChatTransportFactory();
/** @var AbstractTransportFactory $transportFactory */
$transportFactory = new $transportFactoryClass();
$push = $notification->asPushMessage(new NoRecipient());
$chat = $notification->asChatMessage(new NoRecipient(), $webhookScheme->value);
try {
$factory = $transportFactory->create($dsn);
if ($factory->supports($push)) {
$factory->send($push);
} elseif ($factory->supports($chat)) {
$factory->send($chat);
} else {
throw new BadRequestHttpException('Unsupported message type');
}
$this->logger->info('Chat message sent with {schema} for Watchlist {token}',
[
'scheme' => $webhookScheme->name,
'token' => $watchList->getToken(),
]);
} catch (\Throwable $exception) {
$this->logger->error('Unable to send a chat message to {scheme} for Watchlist {token}',
[
'scheme' => $webhookScheme->name,
'token' => $watchList->getToken(),
]);
throw new BadRequestHttpException($exception->getMessage());
}
}
}
}
}

View File

@@ -1,13 +0,0 @@
<?php
namespace App\Service\Connector;
abstract class AbstractConnector implements ConnectorInterface
{
protected array $authData;
public function authenticate(array $authData)
{
$this->authData = $authData;
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace App\Service\Connector;
use App\Entity\Domain;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
abstract class AbstractProvider
{
protected array $authData;
public function __construct(
protected CacheItemPoolInterface $cacheItemPool
) {
}
abstract public static function verifyAuthData(array $authData, HttpClientInterface $client): array;
abstract public function orderDomain(Domain $domain, bool $dryRun): void;
public function isSupported(Domain ...$domainList): bool
{
$item = $this->getCachedTldList();
if (!$item->isHit()) {
$supportedTldList = $this->getSupportedTldList();
$item
->set($supportedTldList)
->expiresAfter(new \DateInterval('PT1H'));
$this->cacheItemPool->saveDeferred($item);
} else {
$supportedTldList = $item->get();
}
$extensionList = [];
foreach ($domainList as $domain) {
// We want to check the support of TLDs and SLDs here.
// For example, it is not enough for the Connector to support .fr for it to support the domain name example.asso.fr.
// It must support .asso.fr.
$extension = explode('.', $domain->getLdhName(), 2)[1];
if (!in_array($extension, $extensionList)) {
$extensionList[] = $extension;
}
}
foreach ($extensionList as $extension) {
if (!in_array($extension, $supportedTldList)) {
return false;
}
}
return true;
}
public function authenticate(array $authData): void
{
$this->authData = $authData;
}
abstract protected function getCachedTldList(): CacheItemInterface;
abstract protected function getSupportedTldList(): array;
}

View File

@@ -1,15 +0,0 @@
<?php
namespace App\Service\Connector;
use App\Entity\Domain;
use Symfony\Contracts\HttpClient\HttpClientInterface;
interface ConnectorInterface
{
public function authenticate(array $authData);
public function orderDomain(Domain $domain, bool $dryRun): void;
public static function verifyAuthData(array $authData, HttpClientInterface $client): array;
}

View File

@@ -3,18 +3,21 @@
namespace App\Service\Connector;
use App\Entity\Domain;
use http\Exception\InvalidArgumentException;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\HttpClient\HttpOptions;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
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;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class GandiConnector extends AbstractConnector
class GandiProvider extends AbstractProvider
{
private const BASE_URL = 'https://api.gandi.net/v5';
private const BASE_URL = 'https://api.gandi.net';
public function __construct(private HttpClientInterface $client)
{
@@ -30,12 +33,12 @@ class GandiConnector extends AbstractConnector
public function orderDomain(Domain $domain, bool $dryRun = false): void
{
if (!$domain->getDeleted()) {
throw new InvalidArgumentException('The domain name still appears in the WHOIS database');
throw new \InvalidArgumentException('The domain name still appears in the WHOIS database');
}
$ldhName = $domain->getLdhName();
if (!$ldhName) {
throw new InvalidArgumentException('Domain name cannot be null');
throw new \InvalidArgumentException('Domain name cannot be null');
}
$authData = self::verifyAuthData($this->authData, $this->client);
@@ -130,4 +133,32 @@ class GandiConnector extends AbstractConnector
return $authDataReturned;
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
protected function getSupportedTldList(): array
{
$authData = self::verifyAuthData($this->authData, $this->client);
$response = $this->client->request('GET', '/v5/domain/tlds', (new HttpOptions())
->setAuthBearer($authData['token'])
->setHeader('Accept', 'application/json')
->setBaseUri(self::BASE_URL)
->toArray())->toArray();
return array_map(fn ($tld) => $tld['name'], $response);
}
/**
* @throws \Psr\Cache\InvalidArgumentException
*/
protected function getCachedTldList(): CacheItemInterface
{
return $this->cacheItemPool->getItem('app.provider.ovh.supported-tld');
}
}

View File

@@ -3,11 +3,12 @@
namespace App\Service\Connector;
use App\Entity\Domain;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Symfony\Contracts\HttpClient\HttpClientInterface;
#[Autoconfigure(public: true)]
class NamecheapConnector extends AbstractConnector
class NamecheapConnector extends AbstractProvider
{
public const BASE_URL = 'https://api.namecheap.com/xml.response';
@@ -83,4 +84,14 @@ class NamecheapConnector extends AbstractConnector
{
return $authData;
}
protected function getCachedTldList(): CacheItemInterface
{
// TODO: Implement getCachedTldList() method.
}
protected function getSupportedTldList(): array
{
// TODO: Implement getSupportedTldList() method.
}
}

View File

@@ -3,14 +3,22 @@
namespace App\Service\Connector;
use App\Entity\Domain;
use GuzzleHttp\Exception\ClientException;
use Ovh\Api;
use Ovh\Exceptions\InvalidParameterException;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class OvhConnector extends AbstractConnector
class OvhProvider extends AbstractProvider
{
public const REQUIRED_ROUTES = [
[
'method' => 'GET',
'path' => '/domain/extensions',
],
[
'method' => 'GET',
'path' => '/order/cart',
@@ -45,12 +53,12 @@ class OvhConnector extends AbstractConnector
public function orderDomain(Domain $domain, bool $dryRun = false): void
{
if (!$domain->getDeleted()) {
throw new \Exception('The domain name still appears in the WHOIS database');
throw new \InvalidArgumentException('The domain name still appears in the WHOIS database');
}
$ldhName = $domain->getLdhName();
if (!$ldhName) {
throw new \Exception('Domain name cannot be null');
throw new \InvalidArgumentException('Domain name cannot be null');
}
$authData = self::verifyAuthData($this->authData, $this->client);
@@ -87,7 +95,7 @@ class OvhConnector extends AbstractConnector
);
if (empty($offer)) {
$conn->delete("/order/cart/{$cartId}");
throw new \Exception('Cannot buy this domain name');
throw new \InvalidArgumentException('Cannot buy this domain name');
}
$item = $conn->post("/order/cart/{$cartId}/domain", [
@@ -163,14 +171,18 @@ class OvhConnector extends AbstractConnector
$consumerKey
);
$res = $conn->get('/auth/currentCredential');
if (null !== $res['expiration'] && new \DateTime($res['expiration']) < new \DateTime()) {
throw new \Exception('These credentials have expired');
}
try {
$res = $conn->get('/auth/currentCredential');
if (null !== $res['expiration'] && new \DateTime($res['expiration']) < new \DateTime()) {
throw new BadRequestHttpException('These credentials have expired');
}
$status = $res['status'];
if ('validated' !== $status) {
throw new \Exception("The status of these credentials is not valid ($status)");
$status = $res['status'];
if ('validated' !== $status) {
throw new BadRequestHttpException("The status of these credentials is not valid ($status)");
}
} catch (ClientException $exception) {
throw new BadRequestHttpException($exception->getMessage());
}
foreach (self::REQUIRED_ROUTES as $requiredRoute) {
@@ -186,7 +198,7 @@ class OvhConnector extends AbstractConnector
}
if (!$ok) {
throw new BadRequestHttpException('The credentials provided do not have enough permissions to purchase a domain name.');
throw new BadRequestHttpException('This Connector does not have enough permissions on the Provider API. Please recreate this Connector.');
}
}
@@ -202,4 +214,33 @@ class OvhConnector extends AbstractConnector
'waiveRetractationPeriod' => $waiveRetractationPeriod,
];
}
/**
* @throws InvalidParameterException
* @throws \JsonException
* @throws \Exception
*/
protected function getSupportedTldList(): array
{
$authData = self::verifyAuthData($this->authData, $this->client);
$conn = new Api(
$authData['appKey'],
$authData['appSecret'],
$authData['apiEndpoint'],
$authData['consumerKey']
);
return $conn->get('/domain/extensions', [
'ovhSubsidiary' => $authData['ovhSubsidiary'],
]);
}
/**
* @throws InvalidArgumentException
*/
protected function getCachedTldList(): CacheItemInterface
{
return $this->cacheItemPool->getItem('app.provider.ovh.supported-tld');
}
}

View File

@@ -28,6 +28,7 @@ use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Yaml\Yaml;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
@@ -73,7 +74,17 @@ readonly class RDAPService
'xn--hlcj6aya9esc7a',
];
public const IMPORTANT_EVENTS = [EventAction::Deletion->value, EventAction::Expiration->value];
private const IMPORTANT_EVENTS = [EventAction::Deletion->value, EventAction::Expiration->value];
private const IMPORTANT_STATUS = [
'redemption period',
'pending delete',
'pending create',
'pending renew',
'pending restore',
'pending transfer',
'pending update',
'add period',
];
public function __construct(private HttpClientInterface $client,
private EntityRepository $entityRepository,
@@ -86,7 +97,8 @@ readonly class RDAPService
private RdapServerRepository $rdapServerRepository,
private TldRepository $tldRepository,
private EntityManagerInterface $em,
private LoggerInterface $logger
private LoggerInterface $logger,
private StatService $statService
) {
}
@@ -96,10 +108,11 @@ readonly class RDAPService
*
* @throws \Exception
*/
public static function isToBeWatchClosely(Domain $domain, \DateTimeImmutable $updatedAt): bool
public static function isToBeWatchClosely(Domain $domain): bool
{
if ($updatedAt->diff(new \DateTimeImmutable('now'))->days < 1) {
return false;
$status = $domain->getStatus();
if ((!empty($status) && count(array_intersect($status, self::IMPORTANT_STATUS))) || $domain->getDeleted()) {
return true;
}
/** @var DomainEvent[] $events */
@@ -162,6 +175,8 @@ readonly class RDAPService
]);
try {
$this->statService->incrementStat('stats.rdap_queries.count');
$res = $this->client->request(
'GET', $rdapServerUrl.'domain/'.$idnDomain
)->toArray();
@@ -213,6 +228,11 @@ readonly class RDAPService
$this->em->persist($domain);
$this->em->flush();
/** @var DomainEvent $event */
foreach ($domain->getEvents()->getIterator() as $event) {
$event->setDeleted(true);
}
foreach ($res['events'] as $rdapEvent) {
if ($rdapEvent['eventAction'] === EventAction::LastUpdateOfRDAPDatabase->value) {
continue;
@@ -229,7 +249,14 @@ readonly class RDAPService
}
$domain->addEvent($event
->setAction($rdapEvent['eventAction'])
->setDate(new \DateTimeImmutable($rdapEvent['eventDate'])));
->setDate(new \DateTimeImmutable($rdapEvent['eventDate']))
->setDeleted(false)
);
}
/** @var DomainEntity $domainEntity */
foreach ($domain->getDomainEntities()->getIterator() as $domainEntity) {
$domainEntity->setDeleted(true);
}
if (array_key_exists('entities', $res) && is_array($res['entities'])) {
@@ -270,8 +297,9 @@ readonly class RDAPService
$domain->addDomainEntity($domainEntity
->setDomain($domain)
->setEntity($entity)
->setRoles($roles))
->updateTimestamps();
->setRoles($roles)
->setDeleted(false)
);
$this->em->persist($domainEntity);
$this->em->flush();
@@ -279,10 +307,18 @@ readonly class RDAPService
}
if (array_key_exists('nameservers', $res) && is_array($res['nameservers'])) {
$domain->getNameservers()->clear();
foreach ($res['nameservers'] as $rdapNameserver) {
$nameserver = $this->nameserverRepository->findOneBy([
'ldhName' => strtolower($rdapNameserver['ldhName']),
]);
$domainNS = $domain->getNameservers()->findFirst(fn (int $key, Nameserver $ns) => $ns->getLdhName() === $rdapNameserver['ldhName']);
if (null !== $domainNS) {
$nameserver = $domainNS;
}
if (null === $nameserver) {
$nameserver = new Nameserver();
}
@@ -349,7 +385,7 @@ readonly class RDAPService
if (false === $lastDotPosition) {
throw new BadRequestException('Domain must contain at least one dot');
}
$tld = strtolower(substr($domain, $lastDotPosition + 1));
$tld = strtolower(idn_to_ascii(substr($domain, $lastDotPosition + 1)));
return $this->tldRepository->findOneBy(['tld' => $tld]);
}
@@ -365,7 +401,7 @@ readonly class RDAPService
if (null === $entity) {
$entity = new Entity();
} else {
$this->logger->info('The entity {handle} was not known to this Domain Watchdog instance.', [
'handle' => $rdapEntity['handle'],
]);
@@ -392,6 +428,14 @@ readonly class RDAPService
return $entity;
}
/** @var EntityEvent $event */
foreach ($entity->getEvents()->getIterator() as $event) {
$event->setDeleted(true);
}
$this->em->persist($entity);
$this->em->flush();
foreach ($rdapEntity['events'] as $rdapEntityEvent) {
$eventAction = $rdapEntityEvent['eventAction'];
if ($eventAction === EventAction::LastChanged->value || $eventAction === EventAction::LastUpdateOfRDAPDatabase->value) {
@@ -403,13 +447,15 @@ readonly class RDAPService
]);
if (null !== $event) {
$event->setDeleted(false);
continue;
}
$entity->addEvent(
(new EntityEvent())
->setEntity($entity)
->setAction($rdapEntityEvent['eventAction'])
->setDate(new \DateTimeImmutable($rdapEntityEvent['eventDate'])));
->setDate(new \DateTimeImmutable($rdapEntityEvent['eventDate']))
->setDeleted(false));
}
return $entity;
@@ -423,14 +469,23 @@ readonly class RDAPService
* @throws ClientExceptionInterface
* @throws ORMException
*/
public function updateRDAPServers(): void
public function updateRDAPServersFromIANA(): void
{
$this->logger->info('Started updating the RDAP server list.');
$this->logger->info('Start of update the RDAP server list from IANA.');
$dnsRoot = $this->client->request(
'GET', 'https://data.iana.org/rdap/dns.json'
)->toArray();
$this->updateRDAPServers($dnsRoot);
}
/**
* @throws ORMException
* @throws \Exception
*/
private function updateRDAPServers(array $dnsRoot): void
{
foreach ($dnsRoot['services'] as $service) {
foreach ($service[0] as $tld) {
if ('' === $tld) {
@@ -442,7 +497,10 @@ readonly class RDAPService
if (null === $server) {
$server = new RdapServer();
}
$server->setTld($tldReference)->setUrl($rdapServerUrl)->updateTimestamps();
$server
->setTld($tldReference)
->setUrl($rdapServerUrl)
->setUpdatedAt(new \DateTimeImmutable(array_key_exists('publication', $dnsRoot) ? $dnsRoot['publication'] : 'now'));
$this->em->persist($server);
}
@@ -451,6 +509,19 @@ readonly class RDAPService
$this->em->flush();
}
/**
* @throws ORMException
*/
public function updateRDAPServersFromFile(string $fileName): void
{
if (!file_exists($fileName)) {
return;
}
$this->logger->info('Start of update the RDAP server list from custom config file.');
$this->updateRDAPServers(Yaml::parseFile($fileName));
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Service;
use Psr\Cache\CacheItemPoolInterface;
readonly class StatService
{
public function __construct(
private CacheItemPoolInterface $pool
) {
}
public function incrementStat(string $key): bool
{
try {
$item = $this->pool->getItem($key);
$item->set(($item->get() ?? 0) + 1);
return $this->pool->save($item);
} catch (\Throwable) {
}
return false;
}
}