Skip to content

Commit

Permalink
Allow addding more than one local DNS/CNAME record (#2410)
Browse files Browse the repository at this point in the history
  • Loading branch information
DL6ER authored Oct 28, 2022
2 parents bc6f9fd + 7222e50 commit 807ea10
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cname_records.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<div class="row">
<div class="form-group col-md-6">
<label for="domain">Domain:</label>
<input id="domain" type="url" class="form-control" placeholder="Add a domain (example.com or sub.example.com)" autocomplete="off" spellcheck="false" autocapitalize="none" autocorrect="off">
<input id="domain" type="url" class="form-control" placeholder="Domain or comma-separated list of domains" autocomplete="off" spellcheck="false" autocapitalize="none" autocorrect="off">
</div>
<div class="form-group col-md-6">
<label for="target">Target Domain:</label>
Expand Down
2 changes: 1 addition & 1 deletion dns_records.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<div class="row">
<div class="form-group col-md-6">
<label for="domain">Domain:</label>
<input id="domain" type="url" class="form-control" placeholder="Add a domain (example.com or sub.example.com)" autocomplete="off" spellcheck="false" autocapitalize="none" autocorrect="off">
<input id="domain" type="url" class="form-control" placeholder="Domain or comma-separated list of domains" autocomplete="off" spellcheck="false" autocapitalize="none" autocorrect="off">
</div>
<div class="form-group col-md-6">
<label for="ip">IP Address:</label>
Expand Down
46 changes: 39 additions & 7 deletions scripts/pi-hole/php/func.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ function getCustomDNSEntries()
$data = new \stdClass();
$data->ip = $explodedLine[0];
$data->domain = $explodedLine[1];
$data->domains = array_slice($explodedLine, 0, -1);
$entries[] = $data;
}

Expand Down Expand Up @@ -234,22 +235,39 @@ function addCustomDNSEntry($ip = '', $domain = '', $reload = '', $json = true)
return returnError('Domain must be set', $json);
}

if (!validDomain($domain)) {
return returnError('Domain must be valid', $json);
$num = 0;
// Check if each submitted domain is valid
$domains = array_map('trim', explode(',', $domain));
foreach ($domains as $d) {
if (!validDomain($d)) {
return returnError("Domain '{$d}' is not valid", $json);
}
++$num;
}

// Only check for duplicates if adding new records from the web UI (not through Teleporter)
if (isset($_REQUEST['ip']) || isset($_REQUEST['domain'])) {
$existingEntries = getCustomDNSEntries();
foreach ($existingEntries as $entry) {
if ($entry->domain == $domain && get_ip_type($entry->ip) == $ipType) {
return returnError('This domain already has a custom DNS entry for an IPv'.$ipType, $json);
foreach ($domains as $d) {
if ($entry->domain == $d && get_ip_type($entry->ip) == $ipType) {
return returnError("The domain {$d} already has a custom DNS entry for an IPv".$ipType, $json);
}
}
}
}

// Add record
pihole_execute('-a addcustomdns '.$ip.' '.$domain.' '.$reload);
// if $reload is not set and more then one domain is supplied, restart FTL only once after all entries were added
if (($num > 0) && (empty($reload))) {
$reload = 'false';
}
// Add records
foreach ($domains as $domain) {
pihole_execute('-a addcustomdns '.$ip.' '.$domain.' '.$reload);
}
if ($num > 0) {
pihole_execute('restartdns');
}

return returnSuccess('', $json);
} catch (\Exception $ex) {
Expand Down Expand Up @@ -392,6 +410,7 @@ function addCustomCNAMEEntry($domain = '', $target = '', $reload = '', $json = t
return returnError('Target must be valid', $json);
}

$num = 0;
$domains = array_map('trim', explode(',', $domain));
foreach ($domains as $d) {
// Check if each submitted domain is valid
Expand All @@ -403,6 +422,8 @@ function addCustomCNAMEEntry($domain = '', $target = '', $reload = '', $json = t
if (strtolower($d) === strtolower($target)) {
return returnError('Domain and target cannot be the same', $json);
}

++$num;
}

$existingEntries = getCustomCNAMEEntries();
Expand All @@ -416,7 +437,18 @@ function addCustomCNAMEEntry($domain = '', $target = '', $reload = '', $json = t
}
}

pihole_execute('-a addcustomcname '.$domain.' '.$target.' '.$reload);
// if $reload is not set and more then one domain is supplied, restart FTL only once after all entries were added
if (($num > 0) && (empty($reload))) {
$reload = 'false';
}
// add records
foreach ($domains as $d) {
pihole_execute('-a addcustomcname '.$d.' '.$target.' '.$reload);
}

if ($num > 0) {
pihole_execute('restartdns');
}

return returnSuccess('', $json);
} catch (\Exception $ex) {
Expand Down

0 comments on commit 807ea10

Please sign in to comment.