Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for Teleporter #1276

Merged
merged 8 commits into from
May 12, 2020
130 changes: 3 additions & 127 deletions scripts/pi-hole/php/customdns.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,134 +6,10 @@

switch ($_REQUEST['action'])
{
case 'get': echo json_encode(echoCustomDNSEntries()); break;
case 'add': echo json_encode(addCustomDNSEntry()); break;
case 'delete': echo json_encode(deleteCustomDNSEntry()); break;
case 'get': echo json_encode(echoCustomDNSEntries()); break;
case 'add': echo json_encode(addCustomDNSEntry()); break;
case 'delete': echo json_encode(deleteCustomDNSEntry()); break;
default:
die("Wrong action");
}

function echoCustomDNSEntries()
{
$entries = getCustomDNSEntries();

$data = [];
foreach ($entries as $entry)
$data[] = [ $entry->domain, $entry->ip ];

return [ "data" => $data ];
}

function getCustomDNSEntries()
{
global $customDNSFile;

$entries = [];

$handle = fopen($customDNSFile, "r");
if ($handle)
{
while (($line = fgets($handle)) !== false) {
$line = str_replace("\r","", $line);
$line = str_replace("\n","", $line);
$explodedLine = explode (" ", $line);

if (count($explodedLine) != 2)
continue;

$data = new \stdClass();
$data->ip = $explodedLine[0];
$data->domain = $explodedLine[1];
$entries[] = $data;
}

fclose($handle);
}

return $entries;
}

function addCustomDNSEntry()
{
try
{
$ip = !empty($_REQUEST['ip']) ? $_REQUEST['ip']: "";
$domain = !empty($_REQUEST['domain']) ? $_REQUEST['domain']: "";

if (empty($ip))
return errorJsonResponse("IP must be set");

$ipType = get_ip_type($ip);

if (!$ipType)
return errorJsonResponse("IP must be valid");

if (empty($domain))
return errorJsonResponse("Domain must be set");

if (!is_valid_domain_name($domain))
return errorJsonResponse("Domain must be valid");

$existingEntries = getCustomDNSEntries();

foreach ($existingEntries as $entry)
if ($entry->domain == $domain)
if (get_ip_type($entry->ip) == $ipType)
return errorJsonResponse("This domain already has a custom DNS entry for an IPv" . $ipType);

pihole_execute("-a addcustomdns ".$ip." ".$domain);

return successJsonResponse();
}
catch (\Exception $ex)
{
return errorJsonResponse($ex->getMessage());
}
}

function deleteCustomDNSEntry()
{
try
{
$ip = !empty($_REQUEST['ip']) ? $_REQUEST['ip']: "";
$domain = !empty($_REQUEST['domain']) ? $_REQUEST['domain']: "";

if (empty($ip))
return errorJsonResponse("IP must be set");

if (empty($domain))
return errorJsonResponse("Domain must be set");

$existingEntries = getCustomDNSEntries();

$found = false;
foreach ($existingEntries as $entry)
if ($entry->domain == $domain)
if ($entry->ip == $ip) {
$found = true;
break;
}

if (!$found)
return errorJsonResponse("This domain/ip association does not exist");

pihole_execute("-a removecustomdns ".$ip." ".$domain);

return successJsonResponse();
}
catch (\Exception $ex)
{
return errorJsonResponse($ex->getMessage());
}
}

function successJsonResponse($message = "")
{
return [ "success" => true, "message" => $message ];
}

function errorJsonResponse($message = "")
{
return [ "success" => false, "message" => $message ];
}
?>
173 changes: 173 additions & 0 deletions scripts/pi-hole/php/func.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,177 @@ function pihole_execute($argument_string, $error_on_failure = true) {
return $output;
}

function echoCustomDNSEntries()
{
$entries = getCustomDNSEntries();

$data = [];
foreach ($entries as $entry)
$data[] = [ $entry->domain, $entry->ip ];

return [ "data" => $data ];
}

function getCustomDNSEntries()
{
global $customDNSFile;

$entries = [];

$handle = fopen($customDNSFile, "r");
if ($handle)
{
while (($line = fgets($handle)) !== false) {
$line = str_replace("\r","", $line);
$line = str_replace("\n","", $line);
$explodedLine = explode (" ", $line);

if (count($explodedLine) != 2)
continue;

$data = new \stdClass();
$data->ip = $explodedLine[0];
$data->domain = $explodedLine[1];
$entries[] = $data;
}

fclose($handle);
}

return $entries;
}

function addCustomDNSEntry($ip="", $domain="", $json_reply=true)
{
function error($msg)
{
global $json_reply;
if($json_reply)
return errorJsonResponse($msg);
else {
echo $msg."<br>";
return false;
}
}

try
{
if(isset($_REQUEST['ip']))
$ip = $_REQUEST['ip'];

if(isset($_REQUEST['domain']))
$domain = $_REQUEST['domain'];

if (empty($ip))
return error("IP must be set");

$ipType = get_ip_type($ip);

if (!$ipType)
return error("IP must be valid");

if (empty($domain))
return error("Domain must be set");

if (!is_valid_domain_name($domain))
return error("Domain must be valid");

// 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 error("This domain already has a custom DNS entry for an IPv" . $ipType);
}

// Add record
pihole_execute("-a addcustomdns ".$ip." ".$domain);

return $json_reply ? successJsonResponse() : true;
}
catch (\Exception $ex)
{
return error($ex->getMessage());
}
}

function deleteCustomDNSEntry()
{
try
{
$ip = !empty($_REQUEST['ip']) ? $_REQUEST['ip']: "";
$domain = !empty($_REQUEST['domain']) ? $_REQUEST['domain']: "";

if (empty($ip))
return errorJsonResponse("IP must be set");

if (empty($domain))
return errorJsonResponse("Domain must be set");

$existingEntries = getCustomDNSEntries();

$found = false;
foreach ($existingEntries as $entry)
if ($entry->domain == $domain)
if ($entry->ip == $ip) {
$found = true;
break;
}

if (!$found)
return errorJsonResponse("This domain/ip association does not exist");

pihole_execute("-a removecustomdns ".$ip." ".$domain);

return successJsonResponse();
}
catch (\Exception $ex)
{
return errorJsonResponse($ex->getMessage());
}
}

function deleteAllCustomDNSEntries()
{
$handle = fopen($customDNSFile, "r");
if ($handle)
{
try
{
while (($line = fgets($handle)) !== false) {
$line = str_replace("\r","", $line);
$line = str_replace("\n","", $line);
$explodedLine = explode (" ", $line);

if (count($explodedLine) != 2)
continue;

$ip = $explodedLine[0];
$domain = $explodedLine[1];

pihole_execute("-a removecustomdns ".$ip." ".$domain);
}
}
catch (\Exception $ex)
{
return errorJsonResponse($ex->getMessage());
}

fclose($handle);
}

return successJsonResponse();
}

function successJsonResponse($message = "")
{
return [ "success" => true, "message" => $message ];
}

function errorJsonResponse($message = "")
{
return [ "success" => false, "message" => $message ];
}

?>
Loading