domain-watchdog/src/Service/RDAPService.php

865 lines
30 KiB
PHP
Raw Normal View History

2024-07-13 23:57:07 +02:00
<?php
namespace App\Service;
2025-08-26 16:18:29 +02:00
use App\Config\DnsKey\Algorithm;
use App\Config\DnsKey\DigestType;
2024-07-13 23:57:07 +02:00
use App\Config\EventAction;
2024-07-24 18:52:19 +02:00
use App\Config\TldType;
2025-08-26 16:18:29 +02:00
use App\Entity\DnsKey;
2024-07-13 23:57:07 +02:00
use App\Entity\Domain;
use App\Entity\DomainEntity;
use App\Entity\DomainEvent;
use App\Entity\DomainStatus;
2024-07-13 23:57:07 +02:00
use App\Entity\Entity;
use App\Entity\EntityEvent;
use App\Entity\Nameserver;
use App\Entity\NameserverEntity;
2024-07-18 19:13:06 +02:00
use App\Entity\RdapServer;
2024-07-19 18:59:21 +02:00
use App\Entity\Tld;
2024-07-13 23:57:07 +02:00
use App\Repository\DomainEntityRepository;
use App\Repository\DomainEventRepository;
use App\Repository\DomainRepository;
use App\Repository\EntityEventRepository;
use App\Repository\EntityRepository;
use App\Repository\NameserverEntityRepository;
use App\Repository\NameserverRepository;
2024-07-18 19:13:06 +02:00
use App\Repository\RdapServerRepository;
2024-07-19 18:59:21 +02:00
use App\Repository\TldRepository;
use Doctrine\DBAL\LockMode;
2024-07-13 23:57:07 +02:00
use Doctrine\ORM\EntityManagerInterface;
2024-07-19 18:59:21 +02:00
use Doctrine\ORM\Exception\ORMException;
2024-08-04 04:39:05 +02:00
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
2024-08-07 16:21:41 +02:00
use Symfony\Component\HttpClient\Exception\ClientException;
2024-08-06 21:43:37 +02:00
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2025-08-26 16:18:29 +02:00
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
2024-08-19 18:06:11 +02:00
use Symfony\Component\Yaml\Yaml;
2024-07-19 01:17:33 +02:00
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
2024-07-25 16:19:57 +02:00
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
2024-07-19 01:17:33 +02:00
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2024-07-13 23:57:07 +02:00
use Symfony\Contracts\HttpClient\HttpClientInterface;
2025-09-06 10:35:07 +02:00
class RDAPService
2024-07-13 23:57:07 +02:00
{
/* @see https://www.iana.org/domains/root/db */
2024-08-02 23:24:52 +02:00
public const ISO_TLD_EXCEPTION = ['ac', 'eu', 'uk', 'su', 'tp'];
public const INFRA_TLD = ['arpa'];
public const SPONSORED_TLD = [
2024-07-25 00:58:19 +02:00
'aero',
'asia',
'cat',
'coop',
'edu',
'gov',
'int',
'jobs',
'mil',
'museum',
'post',
'tel',
'travel',
'xxx',
];
2024-08-02 23:24:52 +02:00
public const TEST_TLD = [
2024-07-24 18:52:19 +02:00
'xn--kgbechtv',
'xn--hgbk6aj7f53bba',
'xn--0zwm56d',
'xn--g6w251d',
'xn--80akhbyknj4f',
'xn--11b5bs3a9aj6g',
'xn--jxalpdlp',
'xn--9t4b11yi5a',
'xn--deba0ad',
'xn--zckzah',
2024-08-02 23:24:52 +02:00
'xn--hlcj6aya9esc7a',
2024-07-24 18:52:19 +02:00
];
2024-07-13 23:57:07 +02:00
public const ENTITY_HANDLE_BLACKLIST = [
'REDACTED_FOR_PRIVACY',
'ANO00-FRNIC',
'not applicable',
2025-08-11 01:18:43 +02:00
'REDACTED FOR PRIVACY-REGISTRANT',
2025-08-11 14:15:43 +02:00
'REDACTED FOR PRIVACY-TECH',
2025-08-11 01:18:43 +02:00
'REDACTED FOR PRIVACY',
2025-08-11 14:15:43 +02:00
'REDACTED-SIDN',
2025-08-11 01:18:43 +02:00
'REGISTRANT',
'REGISTRAR',
2025-08-11 14:15:43 +02:00
'ABUSE-CONTACT',
2025-08-11 01:18:43 +02:00
'None',
2025-08-11 14:15:43 +02:00
'Private',
];
/* @see https://www.iana.org/assignments/registrar-ids/registrar-ids.xhtml */
2024-12-27 21:37:19 +01:00
public const IANA_RESERVED_IDS = [
1, 3, 8, 119, 365, 376, 9994, 9995, 9996, 9997, 9998, 9999, 10009, 4000001, 8888888,
];
2024-08-02 23:24:52 +02:00
public function __construct(private HttpClientInterface $client,
private EntityRepository $entityRepository,
private DomainRepository $domainRepository,
private DomainEventRepository $domainEventRepository,
private NameserverRepository $nameserverRepository,
private NameserverEntityRepository $nameserverEntityRepository,
private EntityEventRepository $entityEventRepository,
private DomainEntityRepository $domainEntityRepository,
private RdapServerRepository $rdapServerRepository,
private TldRepository $tldRepository,
2024-08-04 04:39:05 +02:00
private EntityManagerInterface $em,
2024-08-22 18:11:07 +02:00
private LoggerInterface $logger,
private StatService $statService,
2024-12-08 00:52:03 +01:00
private InfluxdbService $influxService,
#[Autowire(param: 'influxdb_enabled')]
private bool $influxdbEnabled,
2024-08-02 23:24:52 +02:00
) {
2024-07-13 23:57:07 +02:00
}
2024-07-14 11:20:04 +02:00
/**
2024-07-25 16:19:57 +02:00
* @throws HttpExceptionInterface
* @throws TransportExceptionInterface
* @throws DecodingExceptionInterface
2024-07-14 11:20:04 +02:00
*/
public function registerDomains(array $domains): void
{
foreach ($domains as $fqdn) {
2024-07-18 19:13:06 +02:00
$this->registerDomain($fqdn);
}
}
/**
2024-07-25 16:19:57 +02:00
* @throws TransportExceptionInterface
2024-08-07 16:21:41 +02:00
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
2024-07-25 16:19:57 +02:00
* @throws DecodingExceptionInterface
2024-08-07 16:21:41 +02:00
* @throws ClientExceptionInterface
2024-12-07 20:08:29 +01:00
* @throws \Exception
*/
public function registerDomain(string $fqdn): Domain
2024-07-13 23:57:07 +02:00
{
2025-05-21 13:14:38 +02:00
$idnDomain = RDAPService::convertToIdn($fqdn);
2024-07-19 18:59:21 +02:00
$tld = $this->getTld($idnDomain);
2024-07-18 19:13:06 +02:00
2024-08-04 04:39:05 +02:00
$this->logger->info('An update request for domain name {idnDomain} is requested.', [
'idnDomain' => $idnDomain,
]);
2024-12-20 22:25:41 +01:00
$rdapServer = $this->fetchRdapServer($tld);
$domain = $this->domainRepository->findOneBy(['ldhName' => $idnDomain]);
$rdapData = $this->fetchRdapResponse($rdapServer, $idnDomain, $domain);
$this->em->beginTransaction();
2024-12-20 22:25:41 +01:00
if (null === $domain) {
$domain = $this->initNewDomain($idnDomain, $tld);
$this->em->persist($domain);
2024-12-20 22:25:41 +01:00
}
$this->em->lock($domain, LockMode::PESSIMISTIC_WRITE);
2024-12-20 22:25:41 +01:00
$this->updateDomainStatus($domain, $rdapData);
2024-12-22 23:06:52 +01:00
if (in_array('free', $domain->getStatus())) {
throw new NotFoundHttpException("The domain name $idnDomain is not present in the WHOIS database.");
2024-12-22 23:06:52 +01:00
}
$domain
->setRdapServer($rdapServer)
->setDelegationSigned(isset($rdapData['secureDNS']['delegationSigned']) && $rdapData['secureDNS']['delegationSigned']);
2024-12-20 22:25:41 +01:00
$this->updateDomainHandle($domain, $rdapData);
$this->updateDomainEvents($domain, $rdapData);
$this->updateDomainEntities($domain, $rdapData);
$this->updateDomainNameservers($domain, $rdapData);
2025-08-26 16:18:29 +02:00
$this->updateDomainDsData($domain, $rdapData);
2024-12-20 22:25:41 +01:00
2025-01-29 20:43:26 +01:00
$domain->setDeleted(false)->updateTimestamps();
2024-12-21 21:41:33 +01:00
2024-12-20 22:25:41 +01:00
$this->em->flush();
$this->em->commit();
2024-12-20 22:25:41 +01:00
return $domain;
}
2025-05-19 09:47:21 +02:00
public function getTld(string $domain): Tld
2024-12-20 22:25:41 +01:00
{
if (!str_contains($domain, '.')) {
2025-03-04 19:51:46 +01:00
$tldEntity = $this->tldRepository->findOneBy(['tld' => '.']);
2025-02-09 01:12:17 +01:00
if (null == $tldEntity) {
throw new NotFoundHttpException("The requested TLD $domain is not yet supported, please try again with another one");
2025-02-09 01:12:17 +01:00
}
return $tldEntity;
2024-12-20 22:25:41 +01:00
}
2025-02-09 01:12:17 +01:00
2024-12-20 22:25:41 +01:00
$lastDotPosition = strrpos($domain, '.');
2025-02-09 01:12:17 +01:00
2024-12-20 22:25:41 +01:00
if (false === $lastDotPosition) {
throw new BadRequestException('Domain must contain at least one dot');
}
2025-09-09 19:43:35 +02:00
$tld = self::convertToIdn((substr($domain, $lastDotPosition + 1)));
$tldEntity = $this->tldRepository->findOneBy(['tld' => $tld]);
2025-02-09 01:12:17 +01:00
if (null === $tldEntity) {
throw new NotFoundHttpException("The requested TLD $tld is not yet supported, please try again with another one");
}
2024-12-20 22:25:41 +01:00
return $tldEntity;
2024-12-20 22:25:41 +01:00
}
2025-05-21 13:14:38 +02:00
public static function convertToIdn(string $fqdn): string
2024-12-20 22:25:41 +01:00
{
return strtolower(idn_to_ascii($fqdn));
}
private function fetchRdapServer(Tld $tld): RdapServer
{
$tldString = $tld->getTld();
$rdapServer = $this->rdapServerRepository->findOneBy(['tld' => $tldString], ['updatedAt' => 'DESC']);
2024-07-18 19:13:06 +02:00
2024-08-02 23:24:52 +02:00
if (null === $rdapServer) {
throw new NotFoundHttpException("TLD $tldString : Unable to determine which RDAP server to contact");
2024-08-02 23:24:52 +02:00
}
2024-07-13 23:57:07 +02:00
2024-12-20 22:25:41 +01:00
return $rdapServer;
}
2024-07-25 16:19:57 +02:00
2024-12-20 22:25:41 +01:00
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws \Exception
*/
private function fetchRdapResponse(RdapServer $rdapServer, string $idnDomain, ?Domain $domain): array
{
2024-08-04 04:39:05 +02:00
$rdapServerUrl = $rdapServer->getUrl();
$this->logger->notice('An RDAP query to update the domain name {idnDomain} will be made to {server}.', [
'idnDomain' => $idnDomain,
'server' => $rdapServerUrl,
]);
2024-07-14 11:20:04 +02:00
try {
2024-08-25 03:31:09 +02:00
$this->statService->incrementStat('stats.rdap_queries.count');
2024-08-22 18:11:07 +02:00
2024-12-20 22:25:41 +01:00
$req = $this->client->request('GET', $rdapServerUrl.'domain/'.$idnDomain);
2024-08-07 16:29:37 +02:00
2024-12-20 22:25:41 +01:00
return $req->toArray();
} catch (\Exception $e) {
throw $this->handleRdapException($e, $idnDomain, $domain);
} finally {
if ($this->influxdbEnabled && isset($req)) {
2024-12-08 14:19:54 +01:00
$this->influxService->addRdapQueryPoint($rdapServer, $idnDomain, $req->getInfo());
}
2024-07-14 11:20:04 +02:00
}
2024-12-20 22:25:41 +01:00
}
2024-07-13 23:57:07 +02:00
2024-12-20 22:25:41 +01:00
/**
* @throws TransportExceptionInterface
* @throws \Exception
*/
private function handleRdapException(\Exception $e, string $idnDomain, ?Domain $domain): \Exception
{
if ($e instanceof ClientException && 404 === $e->getResponse()->getStatusCode()) {
if (null !== $domain) {
$this->logger->notice('The domain name {idnDomain} has been deleted from the WHOIS database.', [
'idnDomain' => $idnDomain,
]);
2024-08-04 04:39:05 +02:00
$domain->updateTimestamps();
if (!$domain->getDeleted() && $domain->getUpdatedAt() !== $domain->getCreatedAt()) {
$this->em->persist((new DomainStatus())
->setDomain($domain)
->setCreatedAt($domain->getUpdatedAt())
->setDate($domain->getUpdatedAt())
->setAddStatus([])
->setDeleteStatus($domain->getStatus()));
}
$domain->setDeleted(true);
2024-12-20 22:25:41 +01:00
$this->em->persist($domain);
$this->em->flush();
}
throw new NotFoundHttpException("The domain name $idnDomain is not present in the WHOIS database.");
2024-08-02 23:24:52 +02:00
}
2024-08-04 04:39:05 +02:00
2024-12-20 22:25:41 +01:00
return $e;
}
private function initNewDomain(string $idnDomain, Tld $tld): Domain
{
$domain = new Domain();
$this->logger->info('The domain name {idnDomain} was not known to this Domain Watchdog instance.', [
'idnDomain' => $idnDomain,
]);
2024-07-13 23:57:07 +02:00
2024-12-21 21:41:33 +01:00
return $domain->setTld($tld)->setLdhName($idnDomain)->setDeleted(false);
2024-12-20 22:25:41 +01:00
}
private function updateDomainStatus(Domain $domain, array $rdapData): void
{
if (isset($rdapData['status'])) {
2024-12-20 22:25:41 +01:00
$status = array_unique($rdapData['status']);
$addedStatus = array_diff($status, $domain->getStatus());
$deletedStatus = array_diff($domain->getStatus(), $status);
$domain->setStatus($status);
if (count($addedStatus) > 0 || count($deletedStatus) > 0) {
$this->em->persist($domain);
if ($domain->getUpdatedAt() !== $domain->getCreatedAt()) {
$this->em->persist((new DomainStatus())
->setDomain($domain)
->setCreatedAt(new \DateTimeImmutable('now'))
->setDate($domain->getUpdatedAt())
->setAddStatus($addedStatus)
->setDeleteStatus($deletedStatus));
}
}
2024-08-04 14:45:27 +02:00
} else {
$this->logger->warning('The domain name {idnDomain} has no WHOIS status.', [
2024-12-20 22:25:41 +01:00
'idnDomain' => $domain->getLdhName(),
2024-08-04 14:45:27 +02:00
]);
2024-08-02 23:24:52 +02:00
}
2024-12-20 22:25:41 +01:00
}
2024-08-04 14:45:27 +02:00
2024-12-20 22:25:41 +01:00
private function updateDomainHandle(Domain $domain, array $rdapData): void
{
if (isset($rdapData['handle'])) {
2024-12-20 22:25:41 +01:00
$domain->setHandle($rdapData['handle']);
2024-08-04 14:45:27 +02:00
} else {
$this->logger->warning('The domain name {idnDomain} has no handle key.', [
2024-12-20 22:25:41 +01:00
'idnDomain' => $domain->getLdhName(),
2024-08-04 14:45:27 +02:00
]);
2024-08-02 23:24:52 +02:00
}
2024-12-20 22:25:41 +01:00
}
2024-12-20 22:25:41 +01:00
/**
* @throws \DateMalformedStringException
* @throws \Exception
*/
private function updateDomainEvents(Domain $domain, array $rdapData): void
{
2024-09-01 21:26:07 +02:00
foreach ($domain->getEvents()->getIterator() as $event) {
$event->setDeleted(true);
}
if (isset($rdapData['events']) && is_array($rdapData['events'])) {
2024-12-20 22:25:41 +01:00
foreach ($rdapData['events'] as $rdapEvent) {
if ($rdapEvent['eventAction'] === EventAction::LastUpdateOfRDAPDatabase->value) {
continue;
}
2024-09-22 19:49:35 +02:00
$event = $this->domainEventRepository->findOneBy([
'action' => $rdapEvent['eventAction'],
'date' => new \DateTimeImmutable($rdapEvent['eventDate']),
'domain' => $domain,
]);
2024-09-22 19:49:35 +02:00
if (null === $event) {
$event = new DomainEvent();
}
2024-12-20 22:25:41 +01:00
$domain->addEvent($event
->setAction($rdapEvent['eventAction'])
->setDate(new \DateTimeImmutable($rdapEvent['eventDate']))
2024-12-20 22:25:41 +01:00
->setDeleted(false));
$this->em->persist($domain);
2024-08-02 23:24:52 +02:00
}
2024-07-13 23:57:07 +02:00
}
2024-12-20 22:25:41 +01:00
}
2024-07-13 23:57:07 +02:00
2024-12-20 22:25:41 +01:00
/**
* @throws \DateMalformedStringException
* @throws \Exception
2024-12-20 22:25:41 +01:00
*/
private function updateDomainEntities(Domain $domain, array $rdapData): void
{
foreach ($domain->getDomainEntities()->getIterator() as $domainEntity) {
$domainEntity->setDeleted(true);
}
if (!isset($rdapData['entities']) || !is_array($rdapData['entities'])) {
return;
}
foreach ($rdapData['entities'] as $rdapEntity) {
$roles = $this->extractEntityRoles($rdapData['entities'], $rdapEntity);
$entity = $this->registerEntity($rdapEntity, $roles, $domain->getLdhName(), $domain->getTld());
$domainEntity = $this->domainEntityRepository->findOneBy([
'domain' => $domain,
'entity' => $entity,
]);
2024-07-23 03:13:51 +02:00
if (null === $domainEntity) {
$domainEntity = new DomainEntity();
2024-07-13 23:57:07 +02:00
}
$domain->addDomainEntity($domainEntity
->setDomain($domain)
->setEntity($entity)
->setRoles($roles)
->setDeleted(false));
$this->em->persist($domainEntity);
$this->em->flush();
2024-07-23 03:13:51 +02:00
}
2024-12-20 22:25:41 +01:00
}
2024-07-13 23:57:07 +02:00
/**
* @throws \DateMalformedStringException
*/
2024-12-20 22:25:41 +01:00
private function updateDomainNameservers(Domain $domain, array $rdapData): void
{
if (array_key_exists('nameservers', $rdapData) && is_array($rdapData['nameservers'])) {
2024-08-17 18:22:24 +02:00
$domain->getNameservers()->clear();
$this->em->persist($domain);
2024-08-17 18:22:24 +02:00
2024-12-20 22:25:41 +01:00
foreach ($rdapData['nameservers'] as $rdapNameserver) {
$nameserver = $this->fetchOrCreateNameserver($rdapNameserver, $domain);
2025-02-18 01:29:29 +01:00
$this->updateNameserverEntities($nameserver, $rdapNameserver, $domain->getTld());
2024-07-23 03:13:51 +02:00
if (!$domain->getNameservers()->contains($nameserver)) {
$domain->addNameserver($nameserver);
}
2024-07-23 03:13:51 +02:00
}
2024-08-04 14:45:27 +02:00
} else {
$this->logger->warning('The domain name {idnDomain} has no nameservers.', [
2024-12-20 22:25:41 +01:00
'idnDomain' => $domain->getLdhName(),
2024-08-04 14:45:27 +02:00
]);
2024-07-13 23:57:07 +02:00
}
2024-12-20 22:25:41 +01:00
}
2024-07-13 23:57:07 +02:00
2024-12-20 22:25:41 +01:00
private function fetchOrCreateNameserver(array $rdapNameserver, Domain $domain): Nameserver
{
$nameserver = $this->nameserverRepository->findOneBy([
'ldhName' => strtolower($rdapNameserver['ldhName']),
]);
2024-07-13 23:57:07 +02:00
2024-12-20 22:25:41 +01:00
$existingDomainNS = $domain->getNameservers()->findFirst(fn (int $key, Nameserver $ns) => $ns->getLdhName() === $rdapNameserver['ldhName']);
if (null !== $existingDomainNS) {
return $existingDomainNS;
} elseif (null === $nameserver) {
$nameserver = new Nameserver();
}
$nameserver->setLdhName($rdapNameserver['ldhName']);
return $nameserver;
2024-07-13 23:57:07 +02:00
}
/**
* @throws \DateMalformedStringException
*/
2025-02-18 01:29:29 +01:00
private function updateNameserverEntities(Nameserver $nameserver, array $rdapNameserver, Tld $tld): void
2024-07-13 23:57:07 +02:00
{
if (!isset($rdapNameserver['entities']) || !is_array($rdapNameserver['entities'])) {
2024-12-20 22:25:41 +01:00
return;
2024-12-20 17:43:35 +01:00
}
2024-12-20 22:25:41 +01:00
foreach ($rdapNameserver['entities'] as $rdapEntity) {
$roles = $this->extractEntityRoles($rdapNameserver['entities'], $rdapEntity);
2025-02-18 01:29:29 +01:00
$entity = $this->registerEntity($rdapEntity, $roles, $nameserver->getLdhName(), $tld);
2024-12-20 22:25:41 +01:00
$nameserverEntity = $this->nameserverEntityRepository->findOneBy([
'nameserver' => $nameserver,
'entity' => $entity,
]);
if (null === $nameserverEntity) {
$nameserverEntity = new NameserverEntity();
}
$nameserver->addNameserverEntity($nameserverEntity
->setNameserver($nameserver)
->setEntity($entity)
->setStatus(array_unique($rdapNameserver['status']))
->setRoles($roles));
$this->em->persist($nameserverEntity);
$this->em->flush();
2024-07-13 23:57:07 +02:00
}
2024-12-20 22:25:41 +01:00
}
2024-07-19 18:59:21 +02:00
2024-12-20 22:25:41 +01:00
private function extractEntityRoles(array $entities, array $targetEntity): array
{
$roles = array_map(
fn ($e) => $e['roles'],
array_filter(
$entities,
fn ($e) => isset($targetEntity['handle']) && isset($e['handle'])
? $targetEntity['handle'] === $e['handle']
: (
isset($targetEntity['vcardArray']) && isset($e['vcardArray'])
? $targetEntity['vcardArray'] === $e['vcardArray']
: $targetEntity === $e
)
2024-12-20 22:25:41 +01:00
)
);
if (count($roles) !== count($roles, COUNT_RECURSIVE)) {
$roles = array_merge(...$roles);
}
return $roles;
2024-07-13 23:57:07 +02:00
}
2024-07-14 11:20:04 +02:00
/**
2024-12-20 22:25:41 +01:00
* @throws \DateMalformedStringException
2024-08-02 23:24:52 +02:00
* @throws \Exception
2024-07-14 11:20:04 +02:00
*/
2025-02-18 01:29:29 +01:00
private function registerEntity(array $rdapEntity, array $roles, string $domain, Tld $tld): Entity
2024-07-13 23:57:07 +02:00
{
/*
* If the RDAP server transmits the entity's IANA number, it is used as a priority to identify the entity
*/
$isIANAid = false;
if (isset($rdapEntity['publicIds'])) {
foreach ($rdapEntity['publicIds'] as $publicId) {
if ('IANA Registrar ID' === $publicId['type'] && isset($publicId['identifier']) && '' !== $publicId['identifier']) {
$rdapEntity['handle'] = $publicId['identifier'];
$isIANAid = true;
break;
}
}
}
/*
* If there is no number to identify the entity, one is generated from the domain name and the roles associated with this entity
*/
if (!isset($rdapEntity['handle']) || '' === $rdapEntity['handle'] || in_array($rdapEntity['handle'], self::ENTITY_HANDLE_BLACKLIST)) {
sort($roles);
$rdapEntity['handle'] = 'DW-FAKEHANDLE-'.$domain.'-'.implode(',', $roles);
2024-12-20 19:41:48 +01:00
$this->logger->warning('The entity {handle} has no handle key.', [
'handle' => $rdapEntity['handle'],
]);
2024-12-20 19:41:48 +01:00
}
2024-12-20 17:43:35 +01:00
2024-07-13 23:57:07 +02:00
$entity = $this->entityRepository->findOneBy([
2024-08-02 23:24:52 +02:00
'handle' => $rdapEntity['handle'],
2025-02-18 01:29:29 +01:00
'tld' => is_numeric($rdapEntity['handle']) ? null : $tld,
2024-07-13 23:57:07 +02:00
]);
2024-08-02 23:24:52 +02:00
if (null === $entity) {
$entity = new Entity();
2024-08-15 19:54:36 +02:00
2024-08-04 14:45:27 +02:00
$this->logger->info('The entity {handle} was not known to this Domain Watchdog instance.', [
'handle' => $rdapEntity['handle'],
]);
2024-08-02 23:24:52 +02:00
}
2024-07-13 23:57:07 +02:00
2025-02-18 01:29:29 +01:00
$entity->setHandle($rdapEntity['handle'])->setTld(is_numeric($rdapEntity['handle']) ? null : $tld);
2024-07-13 23:57:07 +02:00
if (isset($rdapEntity['remarks']) && is_array($rdapEntity['remarks']) && !is_numeric($rdapEntity['handle'])) {
2024-12-28 19:26:25 +01:00
$entity->setRemarks($rdapEntity['remarks']);
}
if (isset($rdapEntity['vcardArray']) && !in_array($rdapEntity['handle'], self::IANA_RESERVED_IDS)) {
2024-07-23 03:05:35 +02:00
if (empty($entity->getJCard())) {
2025-08-11 00:39:44 +02:00
if (!array_key_exists('elements', $rdapEntity['vcardArray'])) {
$entity->setJCard($rdapEntity['vcardArray']);
} else {
/*
* UZ registry
*/
$entity->setJCard([
'vcard',
$rdapEntity['vcardArray']['elements'],
]);
}
2024-07-23 03:05:35 +02:00
} else {
$properties = [];
2025-08-11 00:39:44 +02:00
if (!array_key_exists('elements', $rdapEntity['vcardArray'])) {
foreach ($rdapEntity['vcardArray'][1] as $prop) {
$properties[$prop[0]] = $prop;
}
} else {
/*
* UZ registry
*/
foreach ($rdapEntity['vcardArray']['elements'] as $prop) {
$properties[$prop[0]] = $prop;
}
2024-07-23 03:05:35 +02:00
}
foreach ($entity->getJCard()[1] as $prop) {
$properties[$prop[0]] = $prop;
}
2024-08-02 23:24:52 +02:00
$entity->setJCard(['vcard', array_values($properties)]);
2024-07-13 23:57:07 +02:00
}
}
if ($isIANAid || !isset($rdapEntity['events']) || in_array($rdapEntity['handle'], self::IANA_RESERVED_IDS)) {
2024-08-02 23:24:52 +02:00
return $entity;
}
2024-07-13 23:57:07 +02:00
2024-09-01 21:26:07 +02:00
/** @var EntityEvent $event */
foreach ($entity->getEvents()->getIterator() as $event) {
$event->setDeleted(true);
}
$this->em->persist($entity);
2024-07-13 23:57:07 +02:00
foreach ($rdapEntity['events'] as $rdapEntityEvent) {
2024-08-02 23:24:52 +02:00
$eventAction = $rdapEntityEvent['eventAction'];
if ($eventAction === EventAction::LastChanged->value || $eventAction === EventAction::LastUpdateOfRDAPDatabase->value) {
continue;
}
2024-07-13 23:57:07 +02:00
$event = $this->entityEventRepository->findOneBy([
2024-08-02 23:24:52 +02:00
'action' => $rdapEntityEvent['eventAction'],
'date' => new \DateTimeImmutable($rdapEntityEvent['eventDate']),
2024-07-13 23:57:07 +02:00
]);
2024-08-02 23:24:52 +02:00
if (null !== $event) {
2024-09-01 21:26:07 +02:00
$event->setDeleted(false);
2024-08-02 23:24:52 +02:00
continue;
}
2024-07-13 23:57:07 +02:00
$entity->addEvent(
(new EntityEvent())
->setEntity($entity)
2024-08-02 23:24:52 +02:00
->setAction($rdapEntityEvent['eventAction'])
2024-09-01 21:26:07 +02:00
->setDate(new \DateTimeImmutable($rdapEntityEvent['eventDate']))
->setDeleted(false));
2024-07-13 23:57:07 +02:00
}
2024-08-02 23:24:52 +02:00
2024-12-20 19:41:48 +01:00
$this->em->persist($entity);
$this->em->flush();
2024-12-20 19:41:48 +01:00
2024-07-13 23:57:07 +02:00
return $entity;
}
2024-07-18 19:13:06 +02:00
2025-08-26 16:18:29 +02:00
private function updateDomainDsData(Domain $domain, array $rdapData): void
{
$domain->getDnsKey()->clear();
$this->em->persist($domain);
$this->em->flush();
if (array_key_exists('secureDNS', $rdapData) && array_key_exists('dsData', $rdapData['secureDNS']) && is_array($rdapData['secureDNS']['dsData'])) {
foreach ($rdapData['secureDNS']['dsData'] as $rdapDsData) {
$dsData = new DnsKey();
if (array_key_exists('keyTag', $rdapDsData)) {
$dsData->setKeyTag(pack('n', $rdapDsData['keyTag']));
}
if (array_key_exists('algorithm', $rdapDsData)) {
$dsData->setAlgorithm(Algorithm::from($rdapDsData['algorithm']));
}
if (array_key_exists('digest', $rdapDsData)) {
$blob = hex2bin($rdapDsData['digest']);
if (false === $blob) {
throw new ServiceUnavailableHttpException('DNSSEC digest is not a valid hexadecimal value.');
}
$dsData->setDigest($blob);
}
if (array_key_exists('digestType', $rdapDsData)) {
$dsData->setDigestType(DigestType::from($rdapDsData['digestType']));
}
$domain->addDnsKey($dsData);
$this->em->persist($dsData);
}
} else {
$this->logger->warning('The domain name {idnDomain} has no DS record.', [
'idnDomain' => $domain->getLdhName(),
]);
}
}
2024-07-19 01:17:33 +02:00
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
2024-07-19 18:59:21 +02:00
* @throws ORMException
2024-07-19 01:17:33 +02:00
*/
2024-08-19 18:06:11 +02:00
public function updateRDAPServersFromIANA(): void
2024-07-18 19:13:06 +02:00
{
2024-08-19 18:06:11 +02:00
$this->logger->info('Start of update the RDAP server list from IANA.');
2024-08-04 04:39:05 +02:00
2024-07-18 19:13:06 +02:00
$dnsRoot = $this->client->request(
'GET', 'https://data.iana.org/rdap/dns.json'
)->toArray();
2024-08-19 18:06:11 +02:00
$this->updateRDAPServers($dnsRoot);
}
/**
* @throws ORMException
* @throws \Exception
2024-08-19 18:06:11 +02:00
*/
private function updateRDAPServers(array $dnsRoot): void
{
2024-07-18 19:13:06 +02:00
foreach ($dnsRoot['services'] as $service) {
foreach ($service[0] as $tld) {
2025-02-09 01:12:17 +01:00
if ('' === $tld && null === $this->tldRepository->findOneBy(['tld' => $tld])) {
$this->em->persist((new Tld())->setTld('')->setType(TldType::root));
$this->em->flush();
2024-08-02 23:24:52 +02:00
}
2025-02-09 01:12:17 +01:00
2024-07-19 18:59:21 +02:00
$tldReference = $this->em->getReference(Tld::class, $tld);
2024-12-20 17:43:35 +01:00
2024-07-18 19:13:06 +02:00
foreach ($service[1] as $rdapServerUrl) {
2024-08-02 23:24:52 +02:00
$server = $this->rdapServerRepository->findOneBy(['tld' => $tldReference, 'url' => $rdapServerUrl]);
2025-02-09 01:12:17 +01:00
2024-08-02 23:24:52 +02:00
if (null === $server) {
$server = new RdapServer();
}
2025-02-09 01:12:17 +01:00
$server
->setTld($tldReference)
->setUrl($rdapServerUrl)
->setUpdatedAt(new \DateTimeImmutable($dnsRoot['publication'] ?? 'now'));
2024-07-18 19:13:06 +02:00
$this->em->persist($server);
}
}
}
$this->em->flush();
}
2024-07-19 18:59:21 +02:00
/**
* @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));
}
2024-07-19 18:59:21 +02:00
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function updateTldListIANA(): void
{
2024-08-04 04:39:05 +02:00
$this->logger->info('Start of retrieval of the list of TLDs according to IANA.');
2024-07-19 18:59:21 +02:00
$tldList = array_map(
2024-08-02 23:24:52 +02:00
fn ($tld) => strtolower($tld),
2024-07-19 18:59:21 +02:00
explode(PHP_EOL,
$this->client->request(
'GET', 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt'
)->getContent()
));
array_shift($tldList);
2024-07-24 18:52:19 +02:00
foreach ($tldList as $tld) {
2024-08-02 23:24:52 +02:00
if ('' === $tld) {
continue;
}
2024-07-25 17:03:00 +02:00
2024-07-24 18:52:19 +02:00
$tldEntity = $this->tldRepository->findOneBy(['tld' => $tld]);
2024-07-25 17:03:00 +02:00
2024-08-02 23:24:52 +02:00
if (null === $tldEntity) {
2024-07-25 17:03:00 +02:00
$tldEntity = new Tld();
$tldEntity->setTld($tld);
2024-08-04 04:39:05 +02:00
$this->logger->notice('New TLD detected according to IANA ({tld}).', [
'tld' => $tld,
]);
2024-07-25 17:03:00 +02:00
}
2024-07-24 18:52:19 +02:00
2024-07-24 22:17:54 +02:00
$type = $this->getTldType($tld);
2024-07-25 17:03:00 +02:00
2024-08-02 23:24:52 +02:00
if (null !== $type) {
2024-07-24 22:17:54 +02:00
$tldEntity->setType($type);
2024-08-02 23:24:52 +02:00
} elseif (null === $tldEntity->isContractTerminated()) { // ICANN managed, must be a ccTLD
2024-07-24 22:17:54 +02:00
$tldEntity->setType(TldType::ccTLD);
} else {
$tldEntity->setType(TldType::gTLD);
2024-07-24 21:58:45 +02:00
}
2024-07-24 18:52:19 +02:00
$this->em->persist($tldEntity);
2024-07-19 18:59:21 +02:00
}
$this->em->flush();
}
2024-07-24 18:52:19 +02:00
private function getTldType(string $tld): ?TldType
{
if (in_array(strtolower($tld), self::ISO_TLD_EXCEPTION)) {
2024-08-02 23:24:52 +02:00
return TldType::ccTLD;
}
if (in_array(strtolower($tld), self::INFRA_TLD)) {
return TldType::iTLD;
}
if (in_array(strtolower($tld), self::SPONSORED_TLD)) {
return TldType::sTLD;
}
if (in_array(strtolower($tld), self::TEST_TLD)) {
return TldType::tTLD;
}
2024-07-24 18:52:19 +02:00
2024-07-24 21:58:45 +02:00
return null;
2024-07-24 18:52:19 +02:00
}
2024-07-19 18:59:21 +02:00
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
2024-08-02 23:24:52 +02:00
* @throws \Exception
2024-07-19 18:59:21 +02:00
*/
public function updateGTldListICANN(): void
{
2024-08-04 04:39:05 +02:00
$this->logger->info('Start of retrieval of the list of gTLDs according to ICANN.');
2024-07-19 18:59:21 +02:00
$gTldList = $this->client->request(
'GET', 'https://www.icann.org/resources/registries/gtlds/v2/gtlds.json'
)->toArray()['gTLDs'];
foreach ($gTldList as $gTld) {
2024-08-02 23:24:52 +02:00
if ('' === $gTld['gTLD']) {
continue;
}
2024-12-20 17:43:35 +01:00
/** @var Tld|null $gtTldEntity */
$gtTldEntity = $this->tldRepository->findOneBy(['tld' => $gTld['gTLD']]);
2024-07-25 17:03:00 +02:00
2024-12-20 17:43:35 +01:00
if (null === $gtTldEntity) {
2024-07-25 17:03:00 +02:00
$gtTldEntity = new Tld();
$gtTldEntity->setTld($gTld['gTLD']);
2024-08-04 04:39:05 +02:00
$this->logger->notice('New gTLD detected according to ICANN ({tld}).', [
'tld' => $gTld['gTLD'],
]);
2024-07-25 17:03:00 +02:00
}
2024-07-19 18:59:21 +02:00
2024-07-24 21:58:45 +02:00
$gtTldEntity
->setContractTerminated($gTld['contractTerminated'])
2024-07-19 18:59:21 +02:00
->setRegistryOperator($gTld['registryOperator'])
->setSpecification13($gTld['specification13']);
// NOTICE: sTLDs are listed in ICANN's gTLD list
2024-07-24 18:52:19 +02:00
2024-08-02 23:24:52 +02:00
if (null !== $gTld['removalDate']) {
$gtTldEntity->setRemovalDate(new \DateTimeImmutable($gTld['removalDate']));
}
if (null !== $gTld['delegationDate']) {
$gtTldEntity->setDelegationDate(new \DateTimeImmutable($gTld['delegationDate']));
}
if (null !== $gTld['dateOfContractSignature']) {
$gtTldEntity->setDateOfContractSignature(new \DateTimeImmutable($gTld['dateOfContractSignature']));
}
2024-07-19 18:59:21 +02:00
$this->em->persist($gtTldEntity);
}
2024-07-25 17:03:00 +02:00
2024-07-19 18:59:21 +02:00
$this->em->flush();
}
2024-08-02 23:24:52 +02:00
}