improved handling os dyndns endpoints

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-04-06 16:28:38 +02:00
parent ddae287748
commit b90d91fda2
1 changed files with 149 additions and 86 deletions

View File

@ -5,8 +5,10 @@ namespace App\Controller;
error_reporting(error_level: E_ALL); error_reporting(error_level: E_ALL);
use App\Entity\Domain; use App\Entity\Domain;
use App\Entity\DynDNS;
use App\Repository\ApikeyRepository; use App\Repository\ApikeyRepository;
use App\Repository\DomainRepository; use App\Repository\DomainRepository;
use App\Repository\DynDNSRepository;
use App\Repository\PanelRepository; use App\Repository\PanelRepository;
use DI\Container; use DI\Container;
use DI\ContainerBuilder; use DI\ContainerBuilder;
@ -55,6 +57,7 @@ class RequestController
private DomainController $domainController; private DomainController $domainController;
private DomainRepository $domainRepository; private DomainRepository $domainRepository;
private PanelRepository $panelRepository; private PanelRepository $panelRepository;
private DynDNSRepository $DynDNSRepository;
private Container $container; private Container $container;
private string $header; private string $header;
private array $result; private array $result;
@ -73,7 +76,6 @@ class RequestController
{ {
$this->requestMethod = strtoupper(string: $requestMethod); $this->requestMethod = strtoupper(string: $requestMethod);
$dateFormat = "Y:m:d H:i:s"; $dateFormat = "Y:m:d H:i:s";
$output = "%datetime% %channel%.%level_name% %message%\n"; // %context% %extra% $output = "%datetime% %channel%.%level_name% %message%\n"; // %context% %extra%
$formatter = new LineFormatter(format: $output, dateFormat: $dateFormat); $formatter = new LineFormatter(format: $output, dateFormat: $dateFormat);
@ -84,6 +86,10 @@ class RequestController
$this->log = new Logger(name: 'bindAPI'); $this->log = new Logger(name: 'bindAPI');
$this->log->pushHandler(handler: $stream); $this->log->pushHandler(handler: $stream);
if ($this->config['debug']) {
$this->log->debug(message: 'RequestController::__construct');
}
$containerBuilder = new ContainerBuilder(); $containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([ $containerBuilder->addDefinitions([
@ -94,6 +100,9 @@ class RequestController
DomainRepository::class => autowire() DomainRepository::class => autowire()
->constructorParameter(parameter: 'config', value: $this->config) ->constructorParameter(parameter: 'config', value: $this->config)
->constructorParameter(parameter: 'log', value: $this->log), ->constructorParameter(parameter: 'log', value: $this->log),
DynDNSRepository::class => autowire()
->constructorParameter(parameter: 'config', value: $this->config)
->constructorParameter(parameter: 'log', value: $this->log),
]); ]);
$this->container = $containerBuilder->build(); $this->container = $containerBuilder->build();
@ -103,6 +112,7 @@ class RequestController
$this->domainController = $this->container->get(name: DomainController::class); $this->domainController = $this->container->get(name: DomainController::class);
$this->domainRepository = $this->container->get(name: DomainRepository::class); $this->domainRepository = $this->container->get(name: DomainRepository::class);
$this->panelRepository = $this->container->get(name: PanelRepository::class); $this->panelRepository = $this->container->get(name: PanelRepository::class);
$this->dynDNSRepository = $this->container->get(name: DynDNSRepository::class);
} }
/** /**
@ -288,7 +298,6 @@ class RequestController
echo $this->status; echo $this->status;
} else { } else {
echo json_encode(value: [ echo json_encode(value: [
'status' => $this->status ?? "Error: No status",
'message' => $this->message ?? "Error: No message." 'message' => $this->message ?? "Error: No message."
]); ]);
} }
@ -516,6 +525,10 @@ class RequestController
private function handleDynDNS() private function handleDynDNS()
{ {
if ($this->config['debug']) {
$this->log->debug(message: 'handleDynDNS()');
}
if ($this->checkPassword()) { if ($this->checkPassword()) {
$host = $this->uri[3] ?? ''; $host = $this->uri[3] ?? '';
@ -536,9 +549,58 @@ class RequestController
} }
} }
if ($this->config['debug']) {
$this->log->debug(message: 'a: ' . $a);
$this->log->debug(message: 'aaaa: ' . $aaaa);
}
$domainName = $this->getDomain(host: $host); $domainName = $this->getDomain(host: $host);
$hostName = str_replace(search: '.' . $domainName, replace: '', subject: $host); $hostName = str_replace(search: '.' . $domainName, replace: '', subject: $host);
$domain = $this->domainRepository->findByName(name: $domainName); if (!$domain = $this->domainRepository->findByName(name: $domainName)) {
$this->header = '404 Not Found';
$this->message = 'Domain ' . $domainName . ' not found';
} else {
// check if address has changed
if ($dynDNS = $this->dynDNSRepository->findByName(name: $host)) {
echo 'found host';
print_r($dynDNS);
print("a: $a");
print("aaaa: $aaaa");
$ipChanged = false;
if (!empty($a)) {
if ($a != $dynDNS->getA()) {
echo $a . '!=' . $dynDNS->getA();
$dynDNS->setA(a: $a);
$ipChanged = true;
}
}
if (!empty($aaaa)) {
if ($aaaa != $dynDNS->getAaaa()) {
$dynDNS->setAaaa(aaaa: $aaaa);
$ipChanged = true;
}
}
if (!$ipChanged) {
$this->header = '304 Not Modified';
$this->message = 'Not modified';
} else {
$this->dynDNSRepository->update(dynDNS: $dynDNS);
}
} else {
$dynDNS = new DynDNS(name: $host, a: $a, aaaa: $aaaa);
$this->dynDNSRepository->insert(dynDNS: $dynDNS);
}
$panel = $this->panelRepository->findByName(name: $domain->getPanel()); $panel = $this->panelRepository->findByName(name: $domain->getPanel());
if (!empty($panel->getAaaa())) { if (!empty($panel->getAaaa())) {
@ -634,11 +696,12 @@ class RequestController
} }
if ($result['header'] == 200) { if ($result['header'] == 200) {
$this->header = '200 OK'; $this->header = '200 OK';
$this->status = json_encode(value: ['message' => 'DynDNS host successfully updated']); $this->message = 'DynDNS host successfully updated';
} }
} else { } else {
$this->header = '404 Not Found'; $this->header = '404 Not Found';
$this->status = 'Host ' . COLOR_YELLOW . $hostName . ' not found'; $this->message = 'Host ' . $hostName . ' not found';
}
} }
} }
} }