feat: RDAP url are stored in db

This commit is contained in:
Maël Gangloff
2024-07-18 19:13:06 +02:00
parent 271e642ac7
commit d4546ae027
5 changed files with 181 additions and 5450 deletions

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240718170120 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE rdap_server (tld VARCHAR(63) NOT NULL, url VARCHAR(255) NOT NULL, updated_at DATE NOT NULL --(DC2Type:date_immutable)
, PRIMARY KEY(tld, url))');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE rdap_server');
}
}

File diff suppressed because it is too large Load Diff

71
src/Entity/RdapServer.php Normal file
View File

@@ -0,0 +1,71 @@
<?php
namespace App\Entity;
use App\Repository\RdapServerRepository;
use DateTimeImmutable;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: RdapServerRepository::class)]
class RdapServer
{
#[ORM\Id]
#[ORM\Column(length: 63)]
private ?string $tld = null;
#[ORM\Id]
#[ORM\Column(length: 255)]
private ?string $url = null;
#[ORM\Column(type: Types::DATE_IMMUTABLE)]
private ?DateTimeImmutable $updatedAt = null;
public function __construct()
{
$this->updatedAt = new DateTimeImmutable('now');
}
public function getTld(): ?string
{
return $this->tld;
}
public function setTld(string $tld): static
{
$this->tld = $tld;
return $this;
}
public function getUrl(): ?string
{
return $this->url;
}
public function setUrl(string $url): static
{
$this->url = $url;
return $this;
}
public function getUpdatedAt(): ?DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(DateTimeImmutable $updatedAt): static
{
$this->updatedAt = $updatedAt;
return $this;
}
#[ORM\PrePersist]
#[ORM\PreUpdate]
public function updateTimestamps(): void
{
$this->setUpdatedAt(new DateTimeImmutable('now'));
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace App\Repository;
use App\Entity\RdapServer;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<RdapServer>
*/
class RdapServerRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, RdapServer::class);
}
// /**
// * @return RdapServer[] Returns an array of RdapServer objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('r')
// ->andWhere('r.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('r.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?RdapServer
// {
// return $this->createQueryBuilder('r')
// ->andWhere('r.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}

View File

@@ -12,6 +12,7 @@ use App\Entity\Entity;
use App\Entity\EntityEvent;
use App\Entity\Nameserver;
use App\Entity\NameserverEntity;
use App\Entity\RdapServer;
use App\Repository\DomainEntityRepository;
use App\Repository\DomainEventRepository;
use App\Repository\DomainRepository;
@@ -19,10 +20,10 @@ use App\Repository\EntityEventRepository;
use App\Repository\EntityRepository;
use App\Repository\NameserverEntityRepository;
use App\Repository\NameserverRepository;
use App\Repository\RdapServerRepository;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Throwable;
@@ -37,8 +38,8 @@ readonly class RDAPService
private NameserverEntityRepository $nameserverEntityRepository,
private EntityEventRepository $entityEventRepository,
private DomainEntityRepository $domainEntityRepository,
private EntityManagerInterface $em,
private ParameterBagInterface $params
private RdapServerRepository $rdapServerRepository,
private EntityManagerInterface $em
)
{
@@ -49,27 +50,26 @@ readonly class RDAPService
*/
public function registerDomains(array $domains): void
{
$dnsRoot = json_decode(file_get_contents($this->params->get('kernel.project_dir') . '/src/Config/dns.json'))->services;
foreach ($domains as $fqdn) {
$this->registerDomain($dnsRoot, $fqdn);
$this->registerDomain($fqdn);
}
}
/**
* @throws Exception
*/
private function registerDomain(array $dnsRoot, string $fqdn): void
private function registerDomain(string $fqdn): void
{
$idnDomain = idn_to_ascii($fqdn);
try {
$rdapServer = $this->getRDAPServer($dnsRoot, RDAPService::getTld($idnDomain));
} catch (Exception) {
throw new Exception("Unable to determine which RDAP server to contact");
}
/** @var RdapServer|null $rdapServer */
$rdapServer = $this->rdapServerRepository->findOneBy(["tld" => RDAPService::getTld($idnDomain)]);
if ($rdapServer === null) throw new Exception("Unable to determine which RDAP server to contact");
try {
$res = $this->client->request(
'GET', $rdapServer . 'domain/' . $idnDomain
'GET', $rdapServer->getUrl() . 'domain/' . $idnDomain
)->toArray();
} catch (Throwable) {
throw new Exception("Unable to contact RDAP server");
@@ -170,18 +170,6 @@ readonly class RDAPService
}
/**
* @throws Exception
*/
private function getRDAPServer(array $dnsRoot, string $tld)
{
foreach ($dnsRoot as $dns) {
if (in_array($tld, $dns[0])) return $dns[1][0];
}
throw new Exception("This TLD ($tld) is not supported");
}
/**
* @throws Exception
*/
@@ -240,4 +228,27 @@ readonly class RDAPService
}
return $entity;
}
public function updateRDAPServers(): void
{
$dnsRoot = $this->client->request(
'GET', 'https://data.iana.org/rdap/dns.json'
)->toArray();
foreach ($dnsRoot['services'] as $service) {
foreach ($service[0] as $tld) {
foreach ($service[1] as $rdapServerUrl) {
$server = $this->rdapServerRepository->findOneBy(["tld" => $tld, "url" => $rdapServerUrl]);
if ($server === null) $server = new RdapServer();
$server->setTld($tld)->setUrl($rdapServerUrl)->updateTimestamps();
$this->em->persist($server);
}
}
}
$this->em->flush();
}
}