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

Allow multiple upstream DNS servers (incl. IPv6) #357

Merged
merged 3 commits into from Jan 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions scripts/pi-hole/js/settings.js
Expand Up @@ -2,6 +2,17 @@ $(function () {
$("[data-mask]").inputmask();
});

$(function(){
$("#custom1val").ipAddress({s:4});
$("#custom2val").ipAddress({s:4});
$("#custom3val").ipAddress({v:6});
$("#custom4val").ipAddress({v:6});

$("#DHCPfrom").ipAddress({s:4});
$("#DHCPto").ipAddress({s:4});
$("#DHCProuter").ipAddress({s:4});
});

$(".confirm-reboot").confirm({
text: "Are you sure you want to send a reboot command to your Pi-Hole?",
title: "Confirmation required",
Expand Down
84 changes: 37 additions & 47 deletions scripts/pi-hole/php/savesettings.php
Expand Up @@ -6,7 +6,7 @@
}

function validIP($address){
return !filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false;
return !filter_var($address, FILTER_VALIDATE_IP) === false;
}

// Check for existance of variable
Expand All @@ -32,20 +32,17 @@ function validDomain($domain_name)
return ( $validChars && $lengthCheck && $labelLengthCheck ); //length of each label
}

$primaryDNSservers = [
"8.8.8.8" => "Google",
"208.67.222.222" => "OpenDNS",
"4.2.2.1" => "Level3",
"199.85.126.10" => "Norton",
"8.26.56.26" => "Comodo"
];

$secondaryDNSservers = [
"8.8.4.4" => "Google",
"208.67.220.220" => "OpenDNS",
"4.2.2.2" => "Level3",
"199.85.127.10" => "Norton",
"8.20.247.20" => "Comodo"
$DNSserverslist = [
"8.8.8.8" => "Google (Primary)",
"208.67.222.222" => "OpenDNS (Primary)",
"4.2.2.1" => "Level3 (Primary)",
"199.85.126.10" => "Norton (Primary)",
"8.26.56.26" => "Comodo (Primary)",
"8.8.4.4" => "Google (Secondary)",
"208.67.220.220" => "OpenDNS (Secondary)",
"4.2.2.2" => "Level3 (Secondary)",
"199.85.127.10" => "Norton (Secondary)",
"8.20.247.20" => "Comodo (Secondary)"
];

$error = "";
Expand All @@ -57,46 +54,38 @@ function validDomain($domain_name)
switch ($_POST["field"]) {
// Set DNS server
case "DNS":
$primaryDNS = $_POST["primaryDNS"];
$secondaryDNS = $_POST["secondaryDNS"];

// Get primary DNS server IP address
if($primaryDNS === "Custom")
{
$primaryIP = $_POST["DNS1IP"];
}
else
{
$primaryIP = array_flip($primaryDNSservers)[$primaryDNS];
}

// Validate primary IP
if (!validIP($primaryIP))
$DNSservers = [];
// Add selected predefined servers to list
foreach ($DNSserverslist as $key => $value)
{
$error .= "Primary IP (".$primaryIP.") is invalid!<br>";
if(array_key_exists("DNSserver".str_replace(".","_",$key),$_POST))
{
array_push($DNSservers,$key);
}
}

// Get secondary DNS server IP address
if($secondaryDNS === "Custom")
// Test custom server fields
for($i=1;$i<=4;$i++)
{
if(strlen($_POST["DNS2IP"]) > 0)
{
$secondaryIP = $_POST["DNS2IP"];
}
else
if(array_key_exists("custom".$i,$_POST))
{
$secondaryIP = "none";
$IP = $_POST["custom".$i."val"];
if(validIP($IP))
{
array_push($DNSservers,$IP);
}
else
{
$error .= "IP (".$IP.") is invalid!<br>";
}
}
}
else
{
$secondaryIP = array_flip($secondaryDNSservers)[$secondaryDNS];
}

// Validate secondary IP
if (!validIP($secondaryIP) && $secondaryIP != "none" && strlen($secondaryIP) > 0)
// Check if at least one DNS server has been added
if(count($DNSservers) < 1)
{
$error .= "Secondary IP (".$secondaryIP.") is invalid!<br>";
$error .= "No DNS server has been selected.<br>";
}

// Check if domain-needed is requested
Expand Down Expand Up @@ -132,8 +121,9 @@ function validDomain($domain_name)
// If there has been no error we can save the new DNS server IPs
if(!strlen($error))
{
exec("sudo pihole -a setdns ".$primaryIP." ".$secondaryIP." ".$extra);
$success .= "The DNS settings have been updated";
$IPs = implode (",", $DNSservers);
exec("sudo pihole -a setdns ".$IPs." ".$extra);
$success .= "The DNS settings have been updated (using ".count($DNSservers)." DNS servers)";
}
else
{
Expand Down
7 changes: 7 additions & 0 deletions scripts/vendor/jquery.input-ip-address-control-1.0.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 67 additions & 43 deletions settings.php
Expand Up @@ -198,15 +198,15 @@
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">From</div>
<input type="text" class="form-control DHCPgroup" name="from" value="<?php echo $DHCPstart; ?>" data-inputmask="'alias': 'ip'" data-mask <?php if(!$DHCP){ ?>disabled<?php } ?>>
<input type="text" class="form-control DHCPgroup" name="from" id="DHCPfrom" value="<?php echo $DHCPstart; ?>" <?php if(!$DHCP){ ?>disabled<?php } ?>>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">To</div>
<input type="text" class="form-control DHCPgroup" name="to" value="<?php echo $DHCPend; ?>" data-inputmask="'alias': 'ip'" data-mask <?php if(!$DHCP){ ?>disabled<?php } ?>>
<input type="text" class="form-control DHCPgroup" name="to" id="DHCPto" value="<?php echo $DHCPend; ?>" <?php if(!$DHCP){ ?>disabled<?php } ?>>
</div>
</div>
</div>
Expand All @@ -215,7 +215,7 @@
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">Router</div>
<input type="text" class="form-control DHCPgroup" name="router" value="<?php echo $DHCProuter; ?>" data-inputmask="'alias': 'ip'" data-mask <?php if(!$DHCP){ ?>disabled<?php } ?>>
<input type="text" class="form-control DHCPgroup" name="router" id="DHCProuter" value="<?php echo $DHCProuter; ?>" <?php if(!$DHCP){ ?>disabled<?php } ?>>
</div>
</div>
</div>
Expand Down Expand Up @@ -360,30 +360,41 @@ function convertseconds($argument) {

<?php
// DNS settings
if(isset($setupVars["PIHOLE_DNS_1"])){
if(isset($primaryDNSservers[$setupVars["PIHOLE_DNS_1"]]))
{
$piHoleDNS1 = $primaryDNSservers[$setupVars["PIHOLE_DNS_1"]];
}
else
{
$piHoleDNS1 = "Custom";
}
} else {
$piHoleDNS1 = "unknown";
}

if(isset($setupVars["PIHOLE_DNS_2"])){
if(isset($secondaryDNSservers[$setupVars["PIHOLE_DNS_2"]]))
{
$piHoleDNS2 = $secondaryDNSservers[$setupVars["PIHOLE_DNS_2"]];
}
else
{
$piHoleDNS2 = "Custom";
$DNSservers = [];
$DNSactive = [];
for($i=1;$i<=12;$i++)
{
if(isset($setupVars["PIHOLE_DNS_".$i])){
if(isset($DNSserverslist[$setupVars["PIHOLE_DNS_".$i]]))
{
$DNSservers[] = [$setupVars["PIHOLE_DNS_".$i],$DNSserverslist[$setupVars["PIHOLE_DNS_".$i]]];
array_push($DNSactive,$setupVars["PIHOLE_DNS_".$i]);
}
elseif(strpos($setupVars["PIHOLE_DNS_".$i],"."))
{
$DNSservers[] = [$setupVars["PIHOLE_DNS_".$i],"CustomIPv4"];
if(!isset($custom1))
{
$custom1 = $setupVars["PIHOLE_DNS_".$i];
}
else
{
$custom2 = $setupVars["PIHOLE_DNS_".$i];
}
}
elseif(strpos($setupVars["PIHOLE_DNS_".$i],":"))
{
$DNSservers[] = [$setupVars["PIHOLE_DNS_".$i],"CustomIPv6"];
if(!isset($custom3))
{
$custom3 = $setupVars["PIHOLE_DNS_".$i];
}
else
{
$custom4 = $setupVars["PIHOLE_DNS_".$i];
}
}
}
} else {
$piHoleDNS2 = "unknown";
}

if(isset($setupVars["DNS_FQDN_REQUIRED"])){
Expand Down Expand Up @@ -432,28 +443,41 @@ function convertseconds($argument) {
<div class="box-body">
<form role="form" method="post">
<div class="col-lg-6">
<label>Primary DNS Server</label>
<label>Upstream DNS Servers</label>
<div class="form-group">
<?php foreach ($primaryDNSservers as $key => $value) { ?> <div class="radio"><label><input type="radio" name="primaryDNS" value="<?php echo $value;?>" <?php if($piHoleDNS1 === $value){ ?>checked<?php } ?> ><?php echo $value;?> (<?php echo $key;?>)</label></div> <?php } ?>
<label>Custom</label>
<div class="input-group">
<div class="input-group-addon"><input type="radio" name="primaryDNS" value="Custom"
<?php if($piHoleDNS1 === "Custom"){ ?>checked<?php } ?>></div>
<input type="text" name="DNS1IP" class="form-control" data-inputmask="'alias': 'ip'" data-mask
<?php if($piHoleDNS1 === "Custom"){ ?>value="<?php echo $setupVars["PIHOLE_DNS_1"]; ?>"<?php } ?>>
</div>
<?php foreach ($DNSserverslist as $key => $value) { ?>
<div class="checkbox">
<label title="<?php echo $key;?>">
<input type="checkbox" name="DNSserver<?php echo $key;?>" value="true" <?php if(in_array($key,$DNSactive)){ ?>checked<?php } ?> ><?php echo $value;?></label>
</div> <?php } ?>
</div>
</div>
<div class="col-lg-6">
<label>Secondary DNS Server</label>
<label>&nbsp;</label>
<div class="form-group">
<?php foreach ($secondaryDNSservers as $key => $value) { ?> <div class="radio"><label><input type="radio" name="secondaryDNS" value="<?php echo $value;?>" <?php if($piHoleDNS2 === $value){ ?>checked<?php } ?> ><?php echo $value;?> (<?php echo $key;?>)</label></div> <?php } ?>
<label>Custom</label>
<label>Custom 1 (IPv4)</label>
<div class="input-group">
<div class="input-group-addon"><input type="checkbox" name="custom1" value="Customv4"
<?php if(isset($custom1)){ ?>checked<?php } ?>></div>
<input type="text" name="custom1val" class="form-control" id="custom1val" <?php if(isset($custom1)){ ?>value="<?php echo $custom1; ?>"<?php } ?>>
</div>
<label>Custom 2 (IPv4)</label>
<div class="input-group">
<div class="input-group-addon"><input type="checkbox" name="custom2" value="Customv4"
<?php if(isset($custom2)){ ?>checked<?php } ?>></div>
<input type="text" name="custom2val" class="form-control" id="custom2val" <?php if(isset($custom2)){ ?>value="<?php echo $custom2; ?>"<?php } ?>>
</div>
<label>Custom 3 (IPv6)</label>
<div class="input-group">
<div class="input-group-addon"><input type="checkbox" name="custom3" value="Customv6"
<?php if(isset($custom3)){ ?>checked<?php } ?>></div>
<input type="text" name="custom3val" class="form-control" id="custom3val" <?php if(isset($custom3)){ ?>value="<?php echo $custom3; ?>"<?php } ?>>
</div>
<label>Custom 4 (IPv6)</label>
<div class="input-group">
<div class="input-group-addon"><input type="radio" name="secondaryDNS" value="Custom"
<?php if($piHoleDNS2 === "Custom"){ ?>checked<?php } ?>></div>
<input type="text" name="DNS2IP" class="form-control" data-inputmask="'alias': 'ip'" data-mask
<?php if($piHoleDNS2 === "Custom"){ ?>value="<?php echo $setupVars["PIHOLE_DNS_2"]; ?>"<?php } ?>>
<div class="input-group-addon"><input type="checkbox" name="custom4" value="Customv6"
<?php if(isset($custom4)){ ?>checked<?php } ?>></div>
<input type="text" name="custom4val" class="form-control" id="custom4val" <?php if(isset($custom4)){ ?>value="<?php echo $custom4; ?>"<?php } ?>>
</div>
</div>
</div>
Expand Down Expand Up @@ -736,6 +760,6 @@ function convertseconds($argument) {
?>

<script src="scripts/vendor/jquery.inputmask.js"></script>
<script src="scripts/vendor/jquery.inputmask.extensions.js"></script>
<script src="scripts/vendor/jquery.input-ip-address-control-1.0.min.js"></script>
<script src="scripts/vendor/jquery.confirm.min.js"></script>
<script src="scripts/pi-hole/js/settings.js"></script>