mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-29 16:15:04 +00:00
feat: RDAP url are stored in db
This commit is contained in:
32
migrations/Version20240718170120.php
Normal file
32
migrations/Version20240718170120.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
5426
src/Config/dns.json
5426
src/Config/dns.json
File diff suppressed because it is too large
Load Diff
71
src/Entity/RdapServer.php
Normal file
71
src/Entity/RdapServer.php
Normal 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'));
|
||||||
|
}
|
||||||
|
}
|
||||||
43
src/Repository/RdapServerRepository.php
Normal file
43
src/Repository/RdapServerRepository.php
Normal 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()
|
||||||
|
// ;
|
||||||
|
// }
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@ use App\Entity\Entity;
|
|||||||
use App\Entity\EntityEvent;
|
use App\Entity\EntityEvent;
|
||||||
use App\Entity\Nameserver;
|
use App\Entity\Nameserver;
|
||||||
use App\Entity\NameserverEntity;
|
use App\Entity\NameserverEntity;
|
||||||
|
use App\Entity\RdapServer;
|
||||||
use App\Repository\DomainEntityRepository;
|
use App\Repository\DomainEntityRepository;
|
||||||
use App\Repository\DomainEventRepository;
|
use App\Repository\DomainEventRepository;
|
||||||
use App\Repository\DomainRepository;
|
use App\Repository\DomainRepository;
|
||||||
@@ -19,10 +20,10 @@ use App\Repository\EntityEventRepository;
|
|||||||
use App\Repository\EntityRepository;
|
use App\Repository\EntityRepository;
|
||||||
use App\Repository\NameserverEntityRepository;
|
use App\Repository\NameserverEntityRepository;
|
||||||
use App\Repository\NameserverRepository;
|
use App\Repository\NameserverRepository;
|
||||||
|
use App\Repository\RdapServerRepository;
|
||||||
use DateTimeImmutable;
|
use DateTimeImmutable;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
@@ -37,8 +38,8 @@ readonly class RDAPService
|
|||||||
private NameserverEntityRepository $nameserverEntityRepository,
|
private NameserverEntityRepository $nameserverEntityRepository,
|
||||||
private EntityEventRepository $entityEventRepository,
|
private EntityEventRepository $entityEventRepository,
|
||||||
private DomainEntityRepository $domainEntityRepository,
|
private DomainEntityRepository $domainEntityRepository,
|
||||||
private EntityManagerInterface $em,
|
private RdapServerRepository $rdapServerRepository,
|
||||||
private ParameterBagInterface $params
|
private EntityManagerInterface $em
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -49,27 +50,26 @@ readonly class RDAPService
|
|||||||
*/
|
*/
|
||||||
public function registerDomains(array $domains): void
|
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) {
|
foreach ($domains as $fqdn) {
|
||||||
$this->registerDomain($dnsRoot, $fqdn);
|
$this->registerDomain($fqdn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function registerDomain(array $dnsRoot, string $fqdn): void
|
private function registerDomain(string $fqdn): void
|
||||||
{
|
{
|
||||||
$idnDomain = idn_to_ascii($fqdn);
|
$idnDomain = idn_to_ascii($fqdn);
|
||||||
try {
|
|
||||||
$rdapServer = $this->getRDAPServer($dnsRoot, RDAPService::getTld($idnDomain));
|
/** @var RdapServer|null $rdapServer */
|
||||||
} catch (Exception) {
|
$rdapServer = $this->rdapServerRepository->findOneBy(["tld" => RDAPService::getTld($idnDomain)]);
|
||||||
throw new Exception("Unable to determine which RDAP server to contact");
|
|
||||||
}
|
if ($rdapServer === null) throw new Exception("Unable to determine which RDAP server to contact");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$res = $this->client->request(
|
$res = $this->client->request(
|
||||||
'GET', $rdapServer . 'domain/' . $idnDomain
|
'GET', $rdapServer->getUrl() . 'domain/' . $idnDomain
|
||||||
)->toArray();
|
)->toArray();
|
||||||
} catch (Throwable) {
|
} catch (Throwable) {
|
||||||
throw new Exception("Unable to contact RDAP server");
|
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
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@@ -240,4 +228,27 @@ readonly class RDAPService
|
|||||||
}
|
}
|
||||||
return $entity;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user