improved handling os dyndns endpoints
Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
parent
ddae287748
commit
b90d91fda2
|
@ -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,109 +549,159 @@ 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)) {
|
||||||
$panel = $this->panelRepository->findByName(name: $domain->getPanel());
|
$this->header = '404 Not Found';
|
||||||
|
$this->message = 'Domain ' . $domainName . ' not found';
|
||||||
if (!empty($panel->getAaaa())) {
|
|
||||||
$domainData = $this->apiController->sendCommand(
|
|
||||||
requestType: 'GET',
|
|
||||||
serverName : $panel->getName(),
|
|
||||||
versionIP : 6,
|
|
||||||
apiKey : $panel->getApikey(),
|
|
||||||
command : 'domains/name/' . $domainName,
|
|
||||||
serverType : 'panel');
|
|
||||||
} else {
|
} else {
|
||||||
$domainData = $this->apiController->sendCommand(
|
|
||||||
requestType: 'GET',
|
|
||||||
serverName : $panel->getName(),
|
|
||||||
versionIP : 4,
|
|
||||||
apiKey : $panel->getApikey(),
|
|
||||||
command : 'domains/name/' . $domainName,
|
|
||||||
serverType : 'panel');
|
|
||||||
}
|
|
||||||
|
|
||||||
$domainDecodedData = json_decode(json: $domainData['data']);
|
// check if address has changed
|
||||||
$domainID = $domainDecodedData->id;
|
if ($dynDNS = $this->dynDNSRepository->findByName(name: $host)) {
|
||||||
|
echo 'found host';
|
||||||
|
print_r($dynDNS);
|
||||||
|
|
||||||
if (!empty($panel->getAaaa())) {
|
print("a: $a");
|
||||||
$dnsData = $this->apiController->sendCommand(
|
print("aaaa: $aaaa");
|
||||||
requestType: 'GET',
|
|
||||||
serverName : $panel->getName(),
|
|
||||||
versionIP : 6,
|
|
||||||
apiKey : $panel->getApikey(),
|
|
||||||
command : 'dns/' . $domainID,
|
|
||||||
serverType : 'panel');
|
|
||||||
} else {
|
|
||||||
$dnsData = $this->apiController->sendCommand(
|
|
||||||
requestType: 'GET',
|
|
||||||
serverName : $panel->getName(),
|
|
||||||
versionIP : 4,
|
|
||||||
apiKey : $panel->getApikey(),
|
|
||||||
command : 'dns/' . $domainID,
|
|
||||||
serverType : 'panel');
|
|
||||||
}
|
|
||||||
|
|
||||||
$dnsDataDecoded = json_decode(json: $dnsData['data']);
|
$ipChanged = false;
|
||||||
$soa = $dnsDataDecoded->records->soa;
|
|
||||||
$others = $dnsDataDecoded->records->other;
|
|
||||||
|
|
||||||
$hostFound = false;
|
if (!empty($a)) {
|
||||||
|
if ($a != $dynDNS->getA()) {
|
||||||
$updateHost = function (object $host) use ($hostName, $a, $aaaa, &$hostFound) {
|
echo $a . '!=' . $dynDNS->getA();
|
||||||
if ($host->host == $hostName) {
|
$dynDNS->setA(a: $a);
|
||||||
$hostFound = true;
|
$ipChanged = true;
|
||||||
if ($host->type == 'A') {
|
|
||||||
if (!empty($a)) {
|
|
||||||
$host->value = $a;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!empty($aaaa)) {
|
|
||||||
$host->value = $aaaa;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
array_map(callback: $updateHost, array: $others);
|
|
||||||
|
|
||||||
if ($hostFound) {
|
$panel = $this->panelRepository->findByName(name: $domain->getPanel());
|
||||||
$newDnsData = json_encode(value: [
|
|
||||||
'records' => [
|
|
||||||
'soa' => $soa,
|
|
||||||
'other' => $others
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (!empty($panel->getAaaa())) {
|
if (!empty($panel->getAaaa())) {
|
||||||
$result = $this->apiController->sendCommand(
|
$domainData = $this->apiController->sendCommand(
|
||||||
requestType: 'PUT',
|
requestType: 'GET',
|
||||||
|
serverName : $panel->getName(),
|
||||||
|
versionIP : 6,
|
||||||
|
apiKey : $panel->getApikey(),
|
||||||
|
command : 'domains/name/' . $domainName,
|
||||||
|
serverType : 'panel');
|
||||||
|
} else {
|
||||||
|
$domainData = $this->apiController->sendCommand(
|
||||||
|
requestType: 'GET',
|
||||||
|
serverName : $panel->getName(),
|
||||||
|
versionIP : 4,
|
||||||
|
apiKey : $panel->getApikey(),
|
||||||
|
command : 'domains/name/' . $domainName,
|
||||||
|
serverType : 'panel');
|
||||||
|
}
|
||||||
|
|
||||||
|
$domainDecodedData = json_decode(json: $domainData['data']);
|
||||||
|
$domainID = $domainDecodedData->id;
|
||||||
|
|
||||||
|
if (!empty($panel->getAaaa())) {
|
||||||
|
$dnsData = $this->apiController->sendCommand(
|
||||||
|
requestType: 'GET',
|
||||||
serverName : $panel->getName(),
|
serverName : $panel->getName(),
|
||||||
versionIP : 6,
|
versionIP : 6,
|
||||||
apiKey : $panel->getApikey(),
|
apiKey : $panel->getApikey(),
|
||||||
command : 'dns/' . $domainID,
|
command : 'dns/' . $domainID,
|
||||||
serverType : 'panel',
|
serverType : 'panel');
|
||||||
body : json_decode(json: $newDnsData, associative: true)
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
$result = $this->apiController->sendCommand(
|
$dnsData = $this->apiController->sendCommand(
|
||||||
requestType: 'PUT',
|
requestType: 'GET',
|
||||||
serverName : $panel->getName(),
|
serverName : $panel->getName(),
|
||||||
versionIP : 4,
|
versionIP : 4,
|
||||||
apiKey : $panel->getApikey(),
|
apiKey : $panel->getApikey(),
|
||||||
command : 'dns/' . $domainID,
|
command : 'dns/' . $domainID,
|
||||||
serverType : 'panel',
|
serverType : 'panel');
|
||||||
body : json_decode(json: $newDnsData, associative: true)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if ($result['header'] == 200) {
|
|
||||||
$this->header = '200 OK';
|
$dnsDataDecoded = json_decode(json: $dnsData['data']);
|
||||||
$this->status = json_encode(value: ['message' => 'DynDNS host successfully updated']);
|
$soa = $dnsDataDecoded->records->soa;
|
||||||
|
$others = $dnsDataDecoded->records->other;
|
||||||
|
|
||||||
|
$hostFound = false;
|
||||||
|
|
||||||
|
$updateHost = function (object $host) use ($hostName, $a, $aaaa, &$hostFound) {
|
||||||
|
if ($host->host == $hostName) {
|
||||||
|
$hostFound = true;
|
||||||
|
if ($host->type == 'A') {
|
||||||
|
if (!empty($a)) {
|
||||||
|
$host->value = $a;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!empty($aaaa)) {
|
||||||
|
$host->value = $aaaa;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
array_map(callback: $updateHost, array: $others);
|
||||||
|
|
||||||
|
if ($hostFound) {
|
||||||
|
$newDnsData = json_encode(value: [
|
||||||
|
'records' => [
|
||||||
|
'soa' => $soa,
|
||||||
|
'other' => $others
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!empty($panel->getAaaa())) {
|
||||||
|
$result = $this->apiController->sendCommand(
|
||||||
|
requestType: 'PUT',
|
||||||
|
serverName : $panel->getName(),
|
||||||
|
versionIP : 6,
|
||||||
|
apiKey : $panel->getApikey(),
|
||||||
|
command : 'dns/' . $domainID,
|
||||||
|
serverType : 'panel',
|
||||||
|
body : json_decode(json: $newDnsData, associative: true)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$result = $this->apiController->sendCommand(
|
||||||
|
requestType: 'PUT',
|
||||||
|
serverName : $panel->getName(),
|
||||||
|
versionIP : 4,
|
||||||
|
apiKey : $panel->getApikey(),
|
||||||
|
command : 'dns/' . $domainID,
|
||||||
|
serverType : 'panel',
|
||||||
|
body : json_decode(json: $newDnsData, associative: true)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if ($result['header'] == 200) {
|
||||||
|
$this->header = '200 OK';
|
||||||
|
$this->message = 'DynDNS host successfully updated';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$this->header = '404 Not Found';
|
||||||
|
$this->message = 'Host ' . $hostName . ' not found';
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
$this->header = '404 Not Found';
|
|
||||||
$this->status = 'Host ' . COLOR_YELLOW . $hostName . ' not found';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue