From b80c97f6d33c73016ebcfb2741a9e5bf00427542 Mon Sep 17 00:00:00 2001 From: yubiuser Date: Tue, 12 Oct 2021 21:37:06 +0200 Subject: [PATCH] Do only reload pihole once after Local DNS Records or CNAME have been modified Signed-off-by: yubiuser --- scripts/pi-hole/php/func.php | 22 ++++++++++++++-------- scripts/pi-hole/php/teleporter.php | 26 ++++++++++++++++++++------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/scripts/pi-hole/php/func.php b/scripts/pi-hole/php/func.php index 139d97a5..15230f1f 100644 --- a/scripts/pi-hole/php/func.php +++ b/scripts/pi-hole/php/func.php @@ -195,7 +195,7 @@ function getCustomDNSEntries() return $entries; } -function addCustomDNSEntry($ip="", $domain="", $json=true) +function addCustomDNSEntry($ip="", $domain="", $reload="", $json=true) { try { @@ -205,6 +205,9 @@ function addCustomDNSEntry($ip="", $domain="", $json=true) if(isset($_REQUEST['domain'])) $domain = trim($_REQUEST['domain']); + if(isset($_REQUEST['reload'])) + $reload = $_REQUEST['reload']; + if (empty($ip)) return returnError("IP must be set", $json); @@ -229,7 +232,7 @@ function addCustomDNSEntry($ip="", $domain="", $json=true) } // Add record - pihole_execute("-a addcustomdns ".$ip." ".$domain); + pihole_execute("-a addcustomdns ".$ip." ".$domain." ".$reload); return returnSuccess("", $json); } @@ -280,9 +283,9 @@ function deleteAllCustomDNSEntries() try { $existingEntries = getCustomDNSEntries(); - + // passing false to pihole_execute stops pihole from reloading after each enty has been deleted foreach ($existingEntries as $entry) { - pihole_execute("-a removecustomdns ".$entry->ip." ".$entry->domain); + pihole_execute("-a removecustomdns ".$entry->ip." ".$entry->domain." false"); } } @@ -341,7 +344,7 @@ function getCustomCNAMEEntries() return $entries; } -function addCustomCNAMEEntry($domain="", $target="", $json=true) +function addCustomCNAMEEntry($domain="", $target="", $reload="", $json=true) { try { @@ -351,6 +354,9 @@ function addCustomCNAMEEntry($domain="", $target="", $json=true) if(isset($_REQUEST['target'])) $target = trim($_REQUEST['target']); + if(isset($_REQUEST['reload'])) + $reload = $_REQUEST['reload']; + if (empty($domain)) return returnError("Domain must be set", $json); @@ -375,7 +381,7 @@ function addCustomCNAMEEntry($domain="", $target="", $json=true) if (in_array($d, $entry->domains)) return returnError("There is already a CNAME record for '$d'", $json); - pihole_execute("-a addcustomcname ".$domain." ".$target); + pihole_execute("-a addcustomcname ".$domain." ".$target." ".$reload); return returnSuccess("", $json); } @@ -426,9 +432,9 @@ function deleteAllCustomCNAMEEntries() try { $existingEntries = getCustomCNAMEEntries(); - + // passing false to pihole_execute stops pihole from reloading after each enty has been deleted foreach ($existingEntries as $entry) { - pihole_execute("-a removecustomcname ".$entry->domain." ".$entry->target); + pihole_execute("-a removecustomcname ".$entry->domain." ".$entry->target." false"); } } diff --git a/scripts/pi-hole/php/teleporter.php b/scripts/pi-hole/php/teleporter.php index 373771e6..5af1c35b 100644 --- a/scripts/pi-hole/php/teleporter.php +++ b/scripts/pi-hole/php/teleporter.php @@ -361,6 +361,7 @@ if(isset($_POST["action"])) $archive = new PharData($fullfilename); $importedsomething = false; + $fullpiholerestart = false; $reloadsettingspage = false; $flushtables = isset($_POST["flushtables"]); @@ -520,19 +521,22 @@ if(isset($_POST["action"])) ob_start(); if($flushtables) { // Defined in func.php included via auth.php + // will not restart Pi-hole deleteAllCustomDNSEntries(); } $num = 0; $localdnsrecords = process_file(file_get_contents($file)); + $reload="false"; foreach($localdnsrecords as $record) { list($ip,$domain) = explode(" ",$record); - if(addCustomDNSEntry($ip, $domain, false)) + if(addCustomDNSEntry($ip, $domain, $reload, false)) $num++; } ob_end_clean(); echo "Processed local DNS records (".$num.noun($num).")
\n"; if($num > 0) { - $importedsomething = true; + // we need a full pihole restart + $fullpiholerestart = true; } } @@ -541,11 +545,13 @@ if(isset($_POST["action"])) ob_start(); if($flushtables) { // Defined in func.php included via auth.php + // will not restart Pi-hole deleteAllCustomCNAMEEntries(); } $num = 0; $localcnamerecords = process_file(file_get_contents($file)); + $reload="false"; foreach($localcnamerecords as $record) { $line = str_replace("cname=","", $record); $line = str_replace("\r","", $line); @@ -555,22 +561,30 @@ if(isset($_POST["action"])) $domain = implode(",", array_slice($explodedLine, 0, -1)); $target = $explodedLine[count($explodedLine)-1]; - if(addCustomCNAMEEntry($domain, $target, false)) + if(addCustomCNAMEEntry($domain, $target, $reload, false)) $num++; } ob_end_clean(); echo "Processed local CNAME records (".$num.noun($num).")
\n"; if($num > 0) { - $importedsomething = true; + // we need a full pihole restart + $fullpiholerestart = true; } } } - if($importedsomething) + // do we need a full restart of Pi-hole or reloading the lists? + if($fullpiholerestart) { - pihole_execute("restartdns reload"); + pihole_execute("restartdns"); + } else { + if($importedsomething) + { + pihole_execute("restartdns reload"); + } } + unlink($fullfilename); echo "OK"; if($reloadsettingspage)