added 404 fpr DynDNS

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-03-10 20:05:42 +01:00
parent 4d0be96915
commit 5a71d5efde
1 changed files with 66 additions and 60 deletions

View File

@ -13,9 +13,9 @@ use DI\ContainerBuilder;
use Monolog\Formatter\LineFormatter; use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler; use Monolog\Handler\StreamHandler;
use Monolog\Logger; use Monolog\Logger;
use OpenApi\Attributes as OAT;
use UnhandledMatchError; use UnhandledMatchError;
use function DI\autowire; use function DI\autowire;
use OpenApi\Attributes as OAT;
/** /**
* *
@ -499,15 +499,15 @@ class RequestController
* *
* @return string * @return string
*/ */
private function getDomain(String $host): string private function getDomain(string $host): string
{ {
$host = strtolower(string: trim(string: $host)); $host = strtolower(string: trim(string: $host));
$count = substr_count(haystack: $host, needle: '.'); $count = substr_count(haystack: $host, needle: '.');
if ($count == 2){ if ($count == 2) {
if (strlen(string: explode(separator: '.', string: $host)[1]) > 3) { if (strlen(string: explode(separator: '.', string: $host)[1]) > 3) {
$host = explode(separator: '.', string: $host, limit: 2)[1]; $host = explode(separator: '.', string: $host, limit: 2)[1];
} }
} else if ($count > 2) { } elseif ($count > 2) {
$host = $this->getDomain(host: explode(separator: '.', string: $host, limit: 2)[1]); $host = $this->getDomain(host: explode(separator: '.', string: $host, limit: 2)[1]);
} }
return $host; return $host;
@ -544,19 +544,19 @@ class RequestController
if (!empty($panel->getAaaa())) { if (!empty($panel->getAaaa())) {
$domainData = $this->apiController->sendCommand( $domainData = $this->apiController->sendCommand(
requestType: 'GET', requestType: 'GET',
serverName: $panel->getName(), serverName : $panel->getName(),
versionIP: 6, versionIP : 6,
apiKey: $panel->getApikey(), apiKey : $panel->getApikey(),
command: 'domains/name/' . $domainName, command : 'domains/name/' . $domainName,
serverType: 'panel'); serverType : 'panel');
} else { } else {
$domainData = $this->apiController->sendCommand( $domainData = $this->apiController->sendCommand(
requestType: 'GET', requestType: 'GET',
serverName: $panel->getName(), serverName : $panel->getName(),
versionIP: 4, versionIP : 4,
apiKey: $panel->getApikey(), apiKey : $panel->getApikey(),
command: 'domains/name/' . $domainName, command : 'domains/name/' . $domainName,
serverType: 'panel'); serverType : 'panel');
} }
$domainDecodedData = json_decode(json: $domainData['data']); $domainDecodedData = json_decode(json: $domainData['data']);
@ -565,27 +565,30 @@ class RequestController
if (!empty($panel->getAaaa())) { if (!empty($panel->getAaaa())) {
$dnsData = $this->apiController->sendCommand( $dnsData = $this->apiController->sendCommand(
requestType: 'GET', 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');
} else { } else {
$dnsData = $this->apiController->sendCommand( $dnsData = $this->apiController->sendCommand(
requestType: 'GET', 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');
} }
$dnsDataDecoded = json_decode(json: $dnsData['data']); $dnsDataDecoded = json_decode(json: $dnsData['data']);
$soa = $dnsDataDecoded->records->soa; $soa = $dnsDataDecoded->records->soa;
$others = $dnsDataDecoded->records->other; $others = $dnsDataDecoded->records->other;
$updateHost = function(object $host) use ($hostName, $a, $aaaa) { $hostFound = false;
$updateHost = function (object $host) use ($hostName, $a, $aaaa, &$hostFound) {
if ($host->host == $hostName) { if ($host->host == $hostName) {
$hostFound = true;
if ($host->type == 'A') { if ($host->type == 'A') {
if (!empty($a)) { if (!empty($a)) {
$host->value = $a; $host->value = $a;
@ -600,42 +603,45 @@ class RequestController
array_map(callback: $updateHost, array: $others); array_map(callback: $updateHost, array: $others);
$newDnsData = json_encode(value: [ if ($hostFound) {
'records' => [ $newDnsData = json_encode(value: [
'soa' => $soa, 'records' => [
'other' => $others 'soa' => $soa,
] 'other' => $others
]); ]
]);
if (!empty($panel->getAaaa())) { if (!empty($panel->getAaaa())) {
$result = $this->apiController->sendCommand( $result = $this->apiController->sendCommand(
requestType: 'PUT', requestType: 'PUT',
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) 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->status = json_encode(value: ['message' => 'DynDNS host successfully updated']);
}
} else { } else {
$result = $this->apiController->sendCommand( $this->header = '404 Not Found';
requestType: 'PUT', $this->status = 'Host ' . COLOR_YELLOW . $hostName . ' not found';
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->status = json_encode(value: ['message' => 'Domain successfully updated']);
} }
} }
} }
} }
} }