1
0
Fork 0
mirror of https://github.com/fernwerker/ownDynDNS.git synced 2025-07-13 23:15:43 +02:00

can create entries if not existing

This commit is contained in:
Nils Blume 2023-08-23 13:08:10 +02:00
parent 49c53721c6
commit 7300eafd6f
3 changed files with 171 additions and 17 deletions

View file

@ -50,6 +50,11 @@ final class Config
*/ */
private $returnIp = true; private $returnIp = true;
/**
* @var bool
*/
private $allowCreate = false;
public function __construct(array $config) public function __construct(array $config)
{ {
@ -138,7 +143,6 @@ final class Config
return $this->debug; return $this->debug;
} }
/** /**
* @return bool * @return bool
*/ */
@ -146,4 +150,12 @@ final class Config
{ {
return $this->returnIp; return $this->returnIp;
} }
/**
* @return bool
*/
public function isAllowCreate()
{
return $this->allowCreate;
}
} }

View file

@ -126,24 +126,37 @@ final class Handler
} }
$infoHandle = $dnsClient->infoDnsRecords( $infoHandle = $dnsClient->infoDnsRecords(
$this->payload->getHostname(), $this->payload->getDomainName(),
$this->config->getCustomerId(), $this->config->getCustomerId(),
$this->config->getApiKey(), $this->config->getApiKey(),
$loginHandle->responsedata->apisessionid, $loginHandle->responsedata->apisessionid,
$clientRequestId $clientRequestId
); );
// test: create new entry if it does not exist
$createnewentry = true;
$exists = false;
$testing = true;
$changes = false;
$ipv4changes = false; $ipv4changes = false;
$ipv6changes = false; $ipv6changes = false;
$txtchanges = false; $txtchanges = false;
// TODO: delete, testing
// echo "--- EXISTING ENTRIES BELOW ---", PHP_EOL;
// $teststring = print_r($infoHandle->responsedata->dnsrecords, true);
// echo $teststring, PHP_EOL;
foreach ($infoHandle->responsedata->dnsrecords as $key => $record) { foreach ($infoHandle->responsedata->dnsrecords as $key => $record) {
$recordHostnameReal = (!in_array($record->hostname, $this->payload->getMatcher())) ? $record->hostname . '.' . $this->payload->getHostname() : $this->payload->getHostname(); $recordHostnameReal = (!in_array($record->hostname, $this->payload->getMatcher())) ? $record->hostname . '.' . $this->payload->getDomainName() : $this->payload->getDomainName();
if ($recordHostnameReal === $this->payload->getDomain()) { if ($recordHostnameReal === $this->payload->getDomain()) {
// found matching entry, no need to create one
$exists = true;
// update A Record if exists and IP has changed // update A Record if exists and IP has changed
if ('A' === $record->type && $this->payload->getIpv4() && if ('A' === $record->type && $this->payload->getIpv4() &&
( (
@ -152,8 +165,7 @@ final class Handler
) )
) { ) {
$record->destination = $this->payload->getIpv4(); $record->destination = $this->payload->getIpv4();
$this->doLog(sprintf('IPv4 for %s set to %s', $record->hostname . '.' . $this->payload->getHostname(), $this->payload->getIpv4())); $this->doLog(sprintf('IPv4 for %s set to %s', $record->hostname . '.' . $this->payload->getDomainName(), $this->payload->getIpv4()));
$changes = true;
$ipv4changes = true; $ipv4changes = true;
} }
@ -165,8 +177,7 @@ final class Handler
) )
) { ) {
$record->destination = $this->payload->getIpv6(); $record->destination = $this->payload->getIpv6();
$this->doLog(sprintf('IPv6 for %s set to %s', $record->hostname . '.' . $this->payload->getHostname(), $this->payload->getIpv6())); $this->doLog(sprintf('IPv6 for %s set to %s', $record->hostname . '.' . $this->payload->getDomainName(), $this->payload->getIpv6()));
$changes = true;
$ipv6changes = true; $ipv6changes = true;
} }
@ -178,19 +189,79 @@ final class Handler
) )
) { ) {
$record->destination = $this->payload->getTxt(); $record->destination = $this->payload->getTxt();
$this->doLog(sprintf('TXT for %s set to %s', $record->hostname . '.' . $this->payload->getHostname(), $this->payload->getTxt())); $this->doLog(sprintf('TXT for %s set to %s', $record->hostname . '.' . $this->payload->getDomainName(), $this->payload->getTxt()));
$changes = true;
$txtchanges = true; $txtchanges = true;
} }
} }
} }
if (true === $changes) { // echo "--- Exists ---", $exists, PHP_EOL;
// echo "--- Createnewentry ---", $createnewentry, PHP_EOL;
// TODO: if entry does not exist and createnewentry is true:
if ( !$exists && $this->payload->getCreate() && $this->config->isAllowCreate() )
{
// init new record set containing empty array
$newRecordSet = new Soap\Dnsrecordset();
$newRecordSet->dnsrecords = array();
foreach ($this->payload->getTypes() as $key => $type)
{
$record = new Soap\Dnsrecord();
// echo "getDomain: ", $this->payload->getDomain(), PHP_EOL;
// echo "getDomainName: ", $this->payload->getDomainName(), PHP_EOL;
// echo "getHost: ", $this->payload->getHost(), PHP_EOL;
$record->hostname = $this->payload->getHost();
$record->type = $type;
$record->priority = "0"; // only for MX, can possibly be removed
switch ($type) {
case 'A':
// echo "A record set: ", $this->payload->getIpv4(), PHP_EOL;
$record->destination = $this->payload->getIpv4();
break;
case 'AAAA':
$record->destination = $this->payload->getIpv6();
break;
case 'TXT':
$record->destination = $this->payload->getTxt();
break;
}
// echo "destination: ", $record->destination, PHP_EOL;
// echo "--- NEW ENTRY BELOW ---", PHP_EOL;
// $teststring = print_r($record, true);
// echo $teststring, PHP_EOL;
array_push($newRecordSet->dnsrecords, $record); // push new record into array
}
// echo "--- newRecordSet ---", PHP_EOL;
// $teststring = print_r($newRecordSet, true);
// echo $teststring, PHP_EOL;
$dnsClient->updateDnsRecords(
$this->payload->getDomainName(),
$this->config->getCustomerId(),
$this->config->getApiKey(),
$loginHandle->responsedata->apisessionid,
$clientRequestId,
$newRecordSet
);
$this->doLog('dns recordset created');
}
// if anything was changed, push the update and log
if ($ipv4changes or $ipv6changes or $txtchanges) {
$recordSet = new Soap\Dnsrecordset(); $recordSet = new Soap\Dnsrecordset();
$recordSet->dnsrecords = $infoHandle->responsedata->dnsrecords; $recordSet->dnsrecords = $infoHandle->responsedata->dnsrecords;
$dnsClient->updateDnsRecords( $dnsClient->updateDnsRecords(
$this->payload->getHostname(), $this->payload->getDomainName(),
$this->config->getCustomerId(), $this->config->getCustomerId(),
$this->config->getApiKey(), $this->config->getApiKey(),
$loginHandle->responsedata->apisessionid, $loginHandle->responsedata->apisessionid,
@ -218,13 +289,13 @@ final class Handler
if ($this->config->isReturnIp()) { if ($this->config->isReturnIp()) {
if ($ipv4changes) { if ($ipv4changes) {
echo "IPv4 changed: " . $this->payload->getIpv4() . "\n"; echo "IPv4 changed: " . $this->payload->getIpv4(), PHP_EOL;
} }
if ($ipv6changes) { if ($ipv6changes) {
echo "IPv6 changed: " . $this->payload->getIpv6() . "\n"; echo "IPv6 changed: " . $this->payload->getIpv6(), PHP_EOL;
} }
if ($txtchanges) { if ($txtchanges) {
echo "TXT changed: " . $this->payload->getTxt() . "\n"; echo "TXT changed: " . $this->payload->getTxt(), PHP_EOL;
} }
} }
return $this; return $this;

View file

@ -39,6 +39,16 @@ final class Payload
*/ */
private $txt; private $txt;
/**
* @var string
*/
private $host;
/**
* @var bool
*/
private $create = false;
/** /**
* @var bool * @var bool
*/ */
@ -96,7 +106,30 @@ final class Payload
*/ */
public function getDomain() public function getDomain()
{ {
return $this->domain; if (empty($this->host))
{
return $this->domain;
}
else
{
return $this->host . "." . $this->domain;
}
}
/**
* @return string
*/
public function getHost()
{
if (!empty($this->host))
{
return $this->host;
}
else
{
$domainParts = explode('.', $this->domain);
return $domainParts[0];
}
} }
/** /**
@ -116,6 +149,35 @@ final class Payload
} }
} }
/**
* @return bool
*/
public function getCreate()
{
return $this->create;
}
/**
* @return array
*/
public function getTypes()
{
$types = array();
if ($this->getIpv4() && $this->isValidIpv4())
{
array_push($types, "A");
}
if ($this->getIpv6() && $this->isValidIpv6())
{
array_push($types, "AAAA");
}
if ($this->getTxt())
{
array_push($types, "TXT");
}
return $types;
}
/** /**
* there is no good way to get the correct "registrable" Domain without external libs! * there is no good way to get the correct "registrable" Domain without external libs!
* *
@ -133,7 +195,7 @@ final class Payload
* *
* @return string * @return string
*/ */
public function getHostname() public function getDomainName()
{ {
// hack if top level domain are used for dynDNS // hack if top level domain are used for dynDNS
if (1 === substr_count($this->domain, '.')) { if (1 === substr_count($this->domain, '.')) {
@ -192,4 +254,13 @@ final class Payload
{ {
return $this->force; return $this->force;
} }
/**
* @return string
*/
public function getType()
{
// TODO:
return "A";
}
} }