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

Multiple conditional forwarding fields #1542

Closed
wants to merge 15 commits into from
Closed
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
2 changes: 1 addition & 1 deletion db_queries.php
Expand Up @@ -159,7 +159,7 @@
</div>
</div>
<!-- /.row -->

<script src="scripts/pi-hole/js/ip-address-sorting.js"></script>
<script src="scripts/vendor/daterangepicker.min.js"></script>
<script src="scripts/pi-hole/js/db_queries.js"></script>

Expand Down
2 changes: 1 addition & 1 deletion queries.php
Expand Up @@ -150,7 +150,7 @@
</div>
</div>
<!-- /.row -->

<script src="scripts/pi-hole/js/ip-address-sorting.js"></script>
<script src="scripts/pi-hole/js/utils.js"></script>
<script src="scripts/pi-hole/js/queries.js"></script>

Expand Down
2 changes: 1 addition & 1 deletion scripts/pi-hole/js/auditlog.js
Expand Up @@ -99,7 +99,7 @@ function add(domain, list) {
domain: domain,
list: list,
token: token,
action: "add_domain",
action: list === "audit" ? "add_audit" : "add_domain",
comment: "Added from Audit Log"
},
success: function () {
Expand Down
2 changes: 1 addition & 1 deletion scripts/pi-hole/js/db_queries.js
Expand Up @@ -373,7 +373,7 @@ $(function () {
},
{ width: "10%" },
{ width: "40%" },
{ width: "20%" },
{ width: "20%", type: "ip-address" },
{ width: "10%" },
{ width: "5%" }
],
Expand Down
9 changes: 7 additions & 2 deletions scripts/pi-hole/js/index.js
Expand Up @@ -287,6 +287,7 @@ function updateQueriesOverTime() {
});
}

var querytypeids = [];
function updateQueryTypesPie() {
$.getJSON("api.php?getQueryTypes", function (data) {
if ("FTLnotrunning" in data) {
Expand All @@ -305,12 +306,16 @@ function updateQueryTypesPie() {
iter = data;
}

querytypeids = [];
Object.keys(iter).forEach(function (key) {
if (iter[key] > 0) {
v.push(iter[key]);
c.push(THEME_COLORS[i++ % THEME_COLORS.length]);
c.push(THEME_COLORS[i % THEME_COLORS.length]);
k.push(key);
querytypeids.push(i + 1);
}

i++;
});

// Build a single dataset with the data to be pushed
Expand Down Expand Up @@ -342,7 +347,7 @@ function updateQueryTypesPie() {
ci.update();
} else if (e.which === 1) {
// which == 1 is left mouse button
window.open("queries.php?querytype=" + ($(this).index() + 1), "_self");
window.open("queries.php?querytype=" + querytypeids[$(this).index()], "_self");
}
});
}).done(function () {
Expand Down
2 changes: 1 addition & 1 deletion scripts/pi-hole/js/queries.js
Expand Up @@ -363,7 +363,7 @@ $(function () {
},
{ width: "4%" },
{ width: "36%", render: $.fn.dataTable.render.text() },
{ width: "8%", render: $.fn.dataTable.render.text() },
{ width: "8%", type: "ip-address", render: $.fn.dataTable.render.text() },
{ width: "14%", orderData: 4 },
{ width: "8%", orderData: 6 },
{ width: "10%", orderData: 4 }
Expand Down
8 changes: 4 additions & 4 deletions scripts/pi-hole/js/settings.js
Expand Up @@ -247,10 +247,10 @@ $(function () {

// En-/disable conditional forwarding input fields based
// on the checkbox state
$('input[name="rev_server"]').click(function () {
$('input[name="rev_server_cidr"]').prop("disabled", !this.checked);
$('input[name="rev_server_target"]').prop("disabled", !this.checked);
$('input[name="rev_server_domain"]').prop("disabled", !this.checked);
$('input[name^="rev_server_chk"]').click(function () {
$('input[name^="rev_server_cidr"]').prop("disabled", !this.checked);
$('input[name^="rev_server_target"]').prop("disabled", !this.checked);
$('input[name^="rev_server_domain"]').prop("disabled", !this.checked);
});
});

Expand Down
47 changes: 47 additions & 0 deletions scripts/pi-hole/php/groups.php
Expand Up @@ -893,6 +893,53 @@ function JSON_error($message = null)
} catch (\Exception $ex) {
JSON_error($ex->getMessage());
}
} elseif ($_POST['action'] == 'add_audit') {
// Add new domain
try {
$domains = explode(' ', html_entity_decode(trim($_POST['domain'])));
$before = intval($db->querySingle("SELECT COUNT(*) FROM domain_audit;"));
$total = count($domains);
$added = 0;
$stmt = $db->prepare('REPLACE INTO domain_audit (domain) VALUES (:domain)');
if (!$stmt) {
throw new Exception('While preparing statement: ' . $db->lastErrorMsg());
}

foreach ($domains as $domain) {
$input = $domain;

if (!$stmt->bindValue(':domain', $domain, SQLITE3_TEXT)) {
throw new Exception('While binding domain: <strong>' . $db->lastErrorMsg() . '</strong><br>'.
'Added ' . $added . " out of ". $total . " domains");
}

if (!$stmt->execute()) {
throw new Exception('While executing: <strong>' . $db->lastErrorMsg() . '</strong><br>'.
'Added ' . $added . " out of ". $total . " domains");
}
$added++;
}

$after = intval($db->querySingle("SELECT COUNT(*) FROM domain_audit;"));
$difference = $after - $before;
if($total === 1) {
if($difference !== 1) {
$msg = "Not adding ". htmlentities(utf8_encode($domain)) . " as it is already on the list";
} else {
$msg = "Added " . htmlentities(utf8_encode($domain));
}
} else {
if($difference !== $total) {
$msg = "Added " . ($after-$before) . " out of ". $total . " domains (skipped duplicates)";
} else {
$msg = "Added " . $total . " domains";
}
}
$reload = true;
JSON_success($msg);
} catch (\Exception $ex) {
JSON_error($ex->getMessage());
}
} else {
log_and_die('Requested action not supported!');
}
Expand Down
131 changes: 79 additions & 52 deletions scripts/pi-hole/php/savesettings.php
Expand Up @@ -6,6 +6,8 @@
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

$REV_SERVER_COUNT = 6;

if(!in_array(basename($_SERVER['SCRIPT_FILENAME']), ["settings.php", "teleporter.php"], true))
{
die("Direct access to this script is forbidden!");
Expand Down Expand Up @@ -319,6 +321,47 @@ function addStaticDHCPLease($mac, $ip, $hostname) {
$error .= "No DNS server has been selected.<br>";
}

// Check if DNSinterface is set
if(isset($_POST["DNSinterface"]))
{
if($_POST["DNSinterface"] === "single")
{
$DNSinterface = "single";
}
elseif($_POST["DNSinterface"] === "all")
{
$DNSinterface = "all";
}
else
{
$DNSinterface = "local";
}
}
else
{
// Fallback
$DNSinterface = "local";
}
pihole_execute("-a -i ".$DNSinterface." -web");

// If there has been no error we can save the new DNS server IPs
if(!strlen($error))
{
$IPs = implode (",", $DNSservers);
$return = pihole_execute("-a setdns \"".$IPs."\" ".$extra);
$success .= htmlspecialchars(end($return))."<br>";
$success .= "The DNS settings have been updated (using ".$DNSservercount." DNS servers)";
}
else
{
$error .= "The settings have been reset to their previous values";
}

break;

// Advanced DNS settings
case "advanced_dns":

// Check if domain-needed is requested
if(isset($_POST["DNSrequiresFQDN"]))
{
Expand Down Expand Up @@ -350,75 +393,59 @@ function addStaticDHCPLease($mac, $ip, $hostname) {
}

// Check if rev-server is requested
if(isset($_POST["rev_server"]))
if(isset($_POST["rev_server_chk"]))
{
// Validate CIDR IP
$cidr = trim($_POST["rev_server_cidr"]);
if (!validCIDRIP($cidr))
// Loop over available fields
for($i = 0; $i < $REV_SERVER_COUNT; $i++)
{
$error .= "Conditional forwarding subnet (\"".htmlspecialchars($cidr)."\") is invalid!<br>".
"This field requires CIDR notation for local subnets (e.g., 192.168.0.0/16).<br>".
"Please use only subnets /8, /16, /24, and /32.<br>";
}
// Get IP in CIDR notation
$cidr = trim($_POST["rev_server_cidr".$i]);

// Validate target IP
$target = trim($_POST["rev_server_target"]);
if (!validIP($target))
{
$error .= "Conditional forwarding target IP (\"".htmlspecialchars($target)."\") is invalid!<br>";
}
// Skip empty fields
if(strlen($cidr) == 0)
continue;

// Validate conditional forwarding domain name (empty is okay)
$domain = trim($_POST["rev_server_domain"]);
if(strlen($domain) > 0 && !validDomain($domain))
{
$error .= "Conditional forwarding domain name (\"".htmlspecialchars($domain)."\") is invalid!<br>";
}
// Validate CIDR IP
if (!validCIDRIP($cidr))
{
$error .= "Conditional forwarding subnet (\"".htmlspecialchars($cidr)."\") is invalid!<br>".
"This field requires CIDR notation for local subnets (e.g., 192.168.0.0/16).<br>".
"Please use only subnets /8, /16, /24, and /32.<br>";
}

if(!$error)
{
$extra .= " rev-server ".$cidr." ".$target." ".$domain;
}
}
// Validate target IP
$target = trim($_POST["rev_server_target".$i]);
if (!validIP($target))
{
$error .= "Conditional forwarding target IP (\"".htmlspecialchars($target)."\") is invalid!<br>";
}

// Check if DNSinterface is set
if(isset($_POST["DNSinterface"]))
{
if($_POST["DNSinterface"] === "single")
{
$DNSinterface = "single";
}
elseif($_POST["DNSinterface"] === "all")
{
$DNSinterface = "all";
}
else
{
$DNSinterface = "local";
// Validate conditional forwarding domain name (empty is okay)
$domain = trim($_POST["rev_server_domain".$i]);
if(strlen($domain) > 0 && !validDomain($domain))
{
$error .= "Conditional forwarding domain name (\"".htmlspecialchars($domain)."\") is invalid!<br>";
}

if(!$error)
{
$extra .= " rev-server ".$cidr." ".$target." ".$domain;
}
}
}
else
{
// Fallback
$DNSinterface = "local";
}
pihole_execute("-a -i ".$DNSinterface." -web");

// If there has been no error we can save the new DNS server IPs
// If there has been no error we can save the new settings
if(!strlen($error))
{
$IPs = implode (",", $DNSservers);
$return = pihole_execute("-a setdns \"".$IPs."\" ".$extra);
$return = pihole_execute("-a setadvanced ".$extra);
$success .= htmlspecialchars(end($return))."<br>";
$success .= "The DNS settings have been updated (using ".$DNSservercount." DNS servers)";
$success .= "The advanced DNS settings have been updated";
}
else
{
$error .= "The settings have been reset to their previous values";
}

break;

// Set query logging
case "Logging":

Expand Down Expand Up @@ -737,7 +764,7 @@ function addStaticDHCPLease($mac, $ip, $hostname) {
break;
// Flush network table
case "flusharp":
pihole_execute("arpflush quiet", $output);
$output = pihole_execute("arpflush quiet");
$error = implode("<br>", $output);
if(strlen($error) == 0)
{
Expand Down
2 changes: 1 addition & 1 deletion scripts/pi-hole/php/teleporter.php
Expand Up @@ -540,7 +540,7 @@ function process_file($contents)
}
else
{
$hostname = gethostname() ? gethostname()."-" : "";
$hostname = gethostname() ? str_replace(".", "_", gethostname())."-" : "";
$tarname = "pi-hole-".$hostname."teleporter_".date("Y-m-d_H-i-s").".tar";
$filename = $tarname.".gz";
$archive_file_name = sys_get_temp_dir() ."/". $tarname;
Expand Down