diff --git a/migrations/Version20240717182345.php b/migrations/Version20240717182345.php deleted file mode 100644 index a9c39ca..0000000 --- a/migrations/Version20240717182345.php +++ /dev/null @@ -1,37 +0,0 @@ -addSql('ALTER TABLE domain ADD COLUMN created_at DATE NOT NULL'); - $this->addSql('ALTER TABLE domain ADD COLUMN updated_at DATE NOT NULL'); - } - - public function down(Schema $schema): void - { - // this down() migration is auto-generated, please modify it to your needs - $this->addSql('CREATE TEMPORARY TABLE __temp__domain AS SELECT ldh_name, handle, status FROM domain'); - $this->addSql('DROP TABLE domain'); - $this->addSql('CREATE TABLE domain (ldh_name VARCHAR(255) NOT NULL, handle VARCHAR(255) NOT NULL, status CLOB NOT NULL --(DC2Type:simple_array) - , PRIMARY KEY(ldh_name))'); - $this->addSql('INSERT INTO domain (ldh_name, handle, status) SELECT ldh_name, handle, status FROM __temp__domain'); - $this->addSql('DROP TABLE __temp__domain'); - } -} diff --git a/migrations/Version20240718170120.php b/migrations/Version20240718170120.php deleted file mode 100644 index 0a24834..0000000 --- a/migrations/Version20240718170120.php +++ /dev/null @@ -1,32 +0,0 @@ -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'); - } -} diff --git a/migrations/Version20240713145845.php b/migrations/Version20240719124300.php similarity index 85% rename from migrations/Version20240713145845.php rename to migrations/Version20240719124300.php index 226f47e..c947866 100644 --- a/migrations/Version20240713145845.php +++ b/migrations/Version20240719124300.php @@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration; /** * Auto-generated Migration: Please modify to your needs! */ -final class Version20240713145845 extends AbstractMigration +final class Version20240719124300 extends AbstractMigration { public function getDescription(): string { @@ -20,8 +20,11 @@ final class Version20240713145845 extends AbstractMigration public function up(Schema $schema): void { // this up() migration is auto-generated, please modify it to your needs - $this->addSql('CREATE TABLE domain (ldh_name VARCHAR(255) NOT NULL, handle VARCHAR(255) NOT NULL, status CLOB NOT NULL --(DC2Type:simple_array) - , PRIMARY KEY(ldh_name))'); + $this->addSql('CREATE TABLE domain (ldh_name VARCHAR(255) NOT NULL, tld_id VARCHAR(63) NOT NULL, handle VARCHAR(255) NOT NULL, status CLOB NOT NULL --(DC2Type:simple_array) + , created_at DATE NOT NULL --(DC2Type:date_immutable) + , updated_at DATE NOT NULL --(DC2Type:date_immutable) + , PRIMARY KEY(ldh_name), CONSTRAINT FK_A7A91E0B50F7084E FOREIGN KEY (tld_id) REFERENCES tld (tld) NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('CREATE INDEX IDX_A7A91E0B50F7084E ON domain (tld_id)'); $this->addSql('CREATE TABLE domain_nameservers (domain_ldh_name VARCHAR(255) NOT NULL, nameserver_ldh_name VARCHAR(255) NOT NULL, PRIMARY KEY(domain_ldh_name, nameserver_ldh_name), CONSTRAINT FK_B6E6B63AAF923913 FOREIGN KEY (domain_ldh_name) REFERENCES domain (ldh_name) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_B6E6B63AA6496BFE FOREIGN KEY (nameserver_ldh_name) REFERENCES nameserver (ldh_name) NOT DEFERRABLE INITIALLY IMMEDIATE)'); $this->addSql('CREATE INDEX IDX_B6E6B63AAF923913 ON domain_nameservers (domain_ldh_name)'); $this->addSql('CREATE INDEX IDX_B6E6B63AA6496BFE ON domain_nameservers (nameserver_ldh_name)'); @@ -43,6 +46,10 @@ final class Version20240713145845 extends AbstractMigration , PRIMARY KEY(nameserver_id, entity_id), CONSTRAINT FK_A269AFB41A555619 FOREIGN KEY (nameserver_id) REFERENCES nameserver (ldh_name) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_A269AFB481257D5D FOREIGN KEY (entity_id) REFERENCES entity (handle) NOT DEFERRABLE INITIALLY IMMEDIATE)'); $this->addSql('CREATE INDEX IDX_A269AFB41A555619 ON nameserver_entity (nameserver_id)'); $this->addSql('CREATE INDEX IDX_A269AFB481257D5D ON nameserver_entity (entity_id)'); + $this->addSql('CREATE TABLE rdap_server (url VARCHAR(255) NOT NULL, tld_id VARCHAR(63) NOT NULL, updated_at DATE NOT NULL --(DC2Type:date_immutable) + , PRIMARY KEY(url, tld_id), CONSTRAINT FK_CCBF17A850F7084E FOREIGN KEY (tld_id) REFERENCES tld (tld) NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('CREATE INDEX IDX_CCBF17A850F7084E ON rdap_server (tld_id)'); + $this->addSql('CREATE TABLE tld (tld VARCHAR(63) NOT NULL, PRIMARY KEY(tld))'); $this->addSql('CREATE TABLE user (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, email VARCHAR(180) NOT NULL, roles CLOB NOT NULL --(DC2Type:json) , password VARCHAR(255) NOT NULL)'); $this->addSql('CREATE UNIQUE INDEX UNIQ_IDENTIFIER_EMAIL ON user (email)'); @@ -71,6 +78,8 @@ final class Version20240713145845 extends AbstractMigration $this->addSql('DROP TABLE entity_event'); $this->addSql('DROP TABLE nameserver'); $this->addSql('DROP TABLE nameserver_entity'); + $this->addSql('DROP TABLE rdap_server'); + $this->addSql('DROP TABLE tld'); $this->addSql('DROP TABLE user'); $this->addSql('DROP TABLE watch_list'); $this->addSql('DROP TABLE watch_lists_domains'); diff --git a/migrations/Version20240719164643.php b/migrations/Version20240719164643.php new file mode 100644 index 0000000..7100076 --- /dev/null +++ b/migrations/Version20240719164643.php @@ -0,0 +1,40 @@ +addSql('ALTER TABLE tld ADD COLUMN contract_terminated BOOLEAN DEFAULT NULL'); + $this->addSql('ALTER TABLE tld ADD COLUMN date_of_contract_signature DATE DEFAULT NULL'); + $this->addSql('ALTER TABLE tld ADD COLUMN delegation_date DATE DEFAULT NULL'); + $this->addSql('ALTER TABLE tld ADD COLUMN registry_operator VARCHAR(255) DEFAULT NULL'); + $this->addSql('ALTER TABLE tld ADD COLUMN removal_date DATE DEFAULT NULL'); + $this->addSql('ALTER TABLE tld ADD COLUMN specification13 BOOLEAN DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE TEMPORARY TABLE __temp__tld AS SELECT tld FROM tld'); + $this->addSql('DROP TABLE tld'); + $this->addSql('CREATE TABLE tld (tld VARCHAR(63) NOT NULL, PRIMARY KEY(tld))'); + $this->addSql('INSERT INTO tld (tld) SELECT tld FROM __temp__tld'); + $this->addSql('DROP TABLE __temp__tld'); + } +} diff --git a/src/Entity/Domain.php b/src/Entity/Domain.php index 3cb411f..d463765 100644 --- a/src/Entity/Domain.php +++ b/src/Entity/Domain.php @@ -105,6 +105,10 @@ class Domain #[ORM\Column(type: Types::DATE_IMMUTABLE)] private ?DateTimeImmutable $updatedAt = null; + #[ORM\ManyToOne] + #[ORM\JoinColumn(referencedColumnName: 'tld', nullable: false)] + private ?Tld $tld = null; + public function __construct() { $this->events = new ArrayCollection(); @@ -293,4 +297,16 @@ class Domain $this->createdAt = $createdAt; } + + public function getTld(): ?Tld + { + return $this->tld; + } + + public function setTld(?Tld $tld): static + { + $this->tld = $tld; + + return $this; + } } diff --git a/src/Entity/RdapServer.php b/src/Entity/RdapServer.php index 158d507..f780804 100644 --- a/src/Entity/RdapServer.php +++ b/src/Entity/RdapServer.php @@ -10,9 +10,6 @@ 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)] @@ -21,23 +18,17 @@ class RdapServer #[ORM\Column(type: Types::DATE_IMMUTABLE)] private ?DateTimeImmutable $updatedAt = null; + #[ORM\Id] + #[ORM\ManyToOne(inversedBy: 'rdapServers')] + #[ORM\JoinColumn(referencedColumnName: 'tld', nullable: false)] + private ?Tld $tld = 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; @@ -68,4 +59,16 @@ class RdapServer { $this->setUpdatedAt(new DateTimeImmutable('now')); } + + public function getTld(): ?Tld + { + return $this->tld; + } + + public function setTld(?Tld $tld): static + { + $this->tld = $tld; + + return $this; + } } diff --git a/src/Entity/Tld.php b/src/Entity/Tld.php new file mode 100644 index 0000000..2ffff18 --- /dev/null +++ b/src/Entity/Tld.php @@ -0,0 +1,160 @@ + + */ + #[ORM\OneToMany(targetEntity: RdapServer::class, mappedBy: 'tld', orphanRemoval: true)] + private Collection $rdapServers; + + #[ORM\Column(nullable: true)] + private ?bool $contractTerminated = null; + + #[ORM\Column(type: Types::DATE_IMMUTABLE, nullable: true)] + private ?DateTimeImmutable $dateOfContractSignature = null; + + #[ORM\Column(type: Types::DATE_IMMUTABLE, nullable: true)] + private ?DateTimeImmutable $delegationDate = null; + + #[ORM\Column(length: 255, nullable: true)] + private ?string $registryOperator = null; + + #[ORM\Column(type: Types::DATE_IMMUTABLE, nullable: true)] + private ?DateTimeImmutable $removalDate = null; + + #[ORM\Column(nullable: true)] + private ?bool $specification13 = null; + + public function __construct() + { + $this->rdapServers = new ArrayCollection(); + } + + /** + * @return Collection + */ + public function getRdapServers(): Collection + { + return $this->rdapServers; + } + + public function addRdapServer(RdapServer $rdapServer): static + { + if (!$this->rdapServers->contains($rdapServer)) { + $this->rdapServers->add($rdapServer); + $rdapServer->setTld($this); + } + + return $this; + } + + public function removeRdapServer(RdapServer $rdapServer): static + { + if ($this->rdapServers->removeElement($rdapServer)) { + // set the owning side to null (unless already changed) + if ($rdapServer->getTld() === $this) { + $rdapServer->setTld(null); + } + } + + return $this; + } + + public function getTld(): ?string + { + return $this->tld; + } + + public function setTld(string $tld): static + { + $this->tld = strtolower($tld); + + return $this; + } + + public function isContractTerminated(): ?bool + { + return $this->contractTerminated; + } + + public function setContractTerminated(?bool $contractTerminated): static + { + $this->contractTerminated = $contractTerminated; + + return $this; + } + + public function getDateOfContractSignature(): ?DateTimeImmutable + { + return $this->dateOfContractSignature; + } + + public function setDateOfContractSignature(?DateTimeImmutable $dateOfContractSignature): static + { + $this->dateOfContractSignature = $dateOfContractSignature; + + return $this; + } + + public function getDelegationDate(): ?DateTimeImmutable + { + return $this->delegationDate; + } + + public function setDelegationDate(?DateTimeImmutable $delegationDate): static + { + $this->delegationDate = $delegationDate; + + return $this; + } + + public function getRegistryOperator(): ?string + { + return $this->registryOperator; + } + + public function setRegistryOperator(?string $registryOperator): static + { + $this->registryOperator = $registryOperator; + + return $this; + } + + public function getRemovalDate(): ?DateTimeImmutable + { + return $this->removalDate; + } + + public function setRemovalDate(?DateTimeImmutable $removalDate): static + { + $this->removalDate = $removalDate; + + return $this; + } + + public function isSpecification13(): ?bool + { + return $this->specification13; + } + + public function setSpecification13(?bool $specification13): static + { + $this->specification13 = $specification13; + + return $this; + } +} diff --git a/src/MessageHandler/UpdateRdapServersHandler.php b/src/MessageHandler/UpdateRdapServersHandler.php index a3c2ae6..e99af4b 100644 --- a/src/MessageHandler/UpdateRdapServersHandler.php +++ b/src/MessageHandler/UpdateRdapServersHandler.php @@ -10,6 +10,7 @@ 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 Throwable; #[AsMessageHandler] final readonly class UpdateRdapServersHandler @@ -25,10 +26,28 @@ final readonly class UpdateRdapServersHandler * @throws ServerExceptionInterface * @throws RedirectionExceptionInterface * @throws DecodingExceptionInterface - * @throws ClientExceptionInterface + * @throws ClientExceptionInterface|Throwable */ public function __invoke(UpdateRdapServers $message): void { - $this->RDAPService->updateRDAPServers(); + /** @var Throwable[] $throws */ + $throws = []; + try { + $this->RDAPService->updateTldListIANA(); + } catch (Throwable $throwable) { + $throws[] = $throwable; + } + + try { + $this->RDAPService->updateGTldListICANN(); + } catch (Throwable $throwable) { + $throws[] = $throwable; + } + try { + $this->RDAPService->updateRDAPServers(); + } catch (Throwable $throwable) { + $throws[] = $throwable; + } + if (!empty($throwable)) throw $throws[0]; } } diff --git a/src/Repository/TldRepository.php b/src/Repository/TldRepository.php new file mode 100644 index 0000000..19c5542 --- /dev/null +++ b/src/Repository/TldRepository.php @@ -0,0 +1,43 @@ + + */ +class TldRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Tld::class); + } + + // /** + // * @return Tld[] Returns an array of Tld objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('t') + // ->andWhere('t.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('t.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?Tld + // { + // return $this->createQueryBuilder('t') + // ->andWhere('t.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } +} diff --git a/src/Scheduler/UpdateRdapServersSchedule.php b/src/Scheduler/UpdateRdapServersSchedule.php index 7d8a7d2..8c97942 100644 --- a/src/Scheduler/UpdateRdapServersSchedule.php +++ b/src/Scheduler/UpdateRdapServersSchedule.php @@ -22,7 +22,7 @@ final readonly class UpdateRdapServersSchedule implements ScheduleProviderInterf { return (new Schedule()) ->add( - RecurringMessage::every('1 month', new UpdateRdapServers()), + RecurringMessage::every('30 seconds', new UpdateRdapServers()), )->stateful($this->cache); } } diff --git a/src/Service/RDAPService.php b/src/Service/RDAPService.php index 3fdda2d..0d03365 100644 --- a/src/Service/RDAPService.php +++ b/src/Service/RDAPService.php @@ -13,6 +13,7 @@ use App\Entity\EntityEvent; use App\Entity\Nameserver; use App\Entity\NameserverEntity; use App\Entity\RdapServer; +use App\Entity\Tld; use App\Repository\DomainEntityRepository; use App\Repository\DomainEventRepository; use App\Repository\DomainRepository; @@ -21,8 +22,10 @@ use App\Repository\EntityRepository; use App\Repository\NameserverEntityRepository; use App\Repository\NameserverRepository; use App\Repository\RdapServerRepository; +use App\Repository\TldRepository; use DateTimeImmutable; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Exception\ORMException; use Exception; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; @@ -44,6 +47,7 @@ readonly class RDAPService private EntityEventRepository $entityEventRepository, private DomainEntityRepository $domainEntityRepository, private RdapServerRepository $rdapServerRepository, + private TldRepository $tldRepository, private EntityManagerInterface $em ) { @@ -66,9 +70,10 @@ readonly class RDAPService private function registerDomain(string $fqdn): void { $idnDomain = idn_to_ascii($fqdn); + $tld = $this->getTld($idnDomain); /** @var RdapServer|null $rdapServer */ - $rdapServer = $this->rdapServerRepository->findOneBy(["tld" => RDAPService::getTld($idnDomain)]); + $rdapServer = $this->rdapServerRepository->findOneBy(["tld" => $tld], ["updatedAt" => "DESC"]); if ($rdapServer === null) throw new Exception("Unable to determine which RDAP server to contact"); @@ -84,6 +89,7 @@ readonly class RDAPService if ($domain === null) $domain = new Domain(); $domain + ->setTld($tld) ->setLdhName($res['ldhName']) ->setHandle($res['handle']) ->setStatus($res['status']); @@ -178,13 +184,15 @@ readonly class RDAPService /** * @throws Exception */ - private static function getTld($domain): string + private function getTld($domain): ?object { $lastDotPosition = strrpos($domain, '.'); if ($lastDotPosition === false) { throw new Exception("Domain must contain at least one dot"); } - return strtolower(substr($domain, $lastDotPosition + 1)); + $tld = strtolower(substr($domain, $lastDotPosition + 1)); + + return $this->tldRepository->findOneBy(["tld" => $tld]); } /** @@ -240,6 +248,7 @@ readonly class RDAPService * @throws RedirectionExceptionInterface * @throws DecodingExceptionInterface * @throws ClientExceptionInterface + * @throws ORMException */ public function updateRDAPServers(): void { @@ -250,11 +259,11 @@ readonly class RDAPService foreach ($dnsRoot['services'] as $service) { foreach ($service[0] as $tld) { + $tldReference = $this->em->getReference(Tld::class, $tld); foreach ($service[1] as $rdapServerUrl) { - $server = $this->rdapServerRepository->findOneBy(["tld" => $tld, "url" => $rdapServerUrl]); + $server = $this->rdapServerRepository->findOneBy(["tld" => $tldReference, "url" => $rdapServerUrl]); //ICI if ($server === null) $server = new RdapServer(); - - $server->setTld($tld)->setUrl($rdapServerUrl)->updateTimestamps(); + $server->setTld($tldReference)->setUrl($rdapServerUrl)->updateTimestamps(); $this->em->persist($server); } @@ -263,4 +272,60 @@ readonly class RDAPService } $this->em->flush(); } + + /** + * @throws TransportExceptionInterface + * @throws ServerExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ClientExceptionInterface + */ + public function updateTldListIANA(): void + { + $tldList = array_map( + fn($tld) => strtolower($tld), + explode(PHP_EOL, + $this->client->request( + 'GET', 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt' + )->getContent() + )); + array_shift($tldList); + $storedTldList = array_map(fn($tld) => $tld->getTld(), $this->tldRepository->findAll()); + + + foreach (array_diff($tldList, $storedTldList) as $tld) { + $this->em->persist((new Tld())->setTld($tld)); + } + $this->em->flush(); + } + + /** + * @throws TransportExceptionInterface + * @throws ServerExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ClientExceptionInterface + * @throws DecodingExceptionInterface + * @throws Exception + */ + public function updateGTldListICANN(): void + { + $gTldList = $this->client->request( + 'GET', 'https://www.icann.org/resources/registries/gtlds/v2/gtlds.json' + )->toArray()['gTLDs']; + + foreach ($gTldList as $gTld) { + $gtTldEntity = $this->tldRepository->findOneBy(['tld' => $gTld['gTLD']]); + if ($gtTldEntity === null) $gtTldEntity = new Tld(); + + $gtTldEntity + ->setTld($gTld['gTLD']) + ->setContractTerminated($gTld['contractTerminated']) + ->setRegistryOperator($gTld['registryOperator']) + ->setSpecification13($gTld['specification13']); + if ($gTld['removalDate'] !== null) $gtTldEntity->setRemovalDate(new DateTimeImmutable($gTld['removalDate'])); + if ($gTld['delegationDate'] !== null) $gtTldEntity->setDelegationDate(new DateTimeImmutable($gTld['delegationDate'])); + if ($gTld['dateOfContractSignature'] !== null) $gtTldEntity->setDateOfContractSignature(new DateTimeImmutable($gTld['dateOfContractSignature'])); + $this->em->persist($gtTldEntity); + } + $this->em->flush(); + } } \ No newline at end of file