Skip to content

Commit

Permalink
Merge pull request #1278 from marank/devel
Browse files Browse the repository at this point in the history
Added support for CNAME records add/remove
  • Loading branch information
DL6ER authored Jul 7, 2020
2 parents b1d69f5 + 773fed0 commit cb5b9c6
Show file tree
Hide file tree
Showing 8 changed files with 473 additions and 31 deletions.
97 changes: 97 additions & 0 deletions cname_records.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php /*
* Pi-hole: A black hole for Internet advertisements
* (c) 2017 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
require "scripts/pi-hole/php/header.php";

?>

<!-- Title -->
<div class="page-header">
<h1>Local CNAME Records</h1>
<small>On this page, you can add CNAME records.</small>
</div>

<!-- Domain Input -->
<div class="row">
<div class="col-md-12">
<div class="box">
<!-- /.box-header -->
<div class="box-header with-border">
<h3 class="box-title">
Add a new CNAME record
</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<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">
</div>
<div class="form-group col-md-6">
<label for="target">Target Domain:</label>
<input id="target" type="url" class="form-control" placeholder="Associated Target Domain" autocomplete="off" spellcheck="false" autocapitalize="none" autocorrect="off">
</div>
</div>
</div>
<div class="box-footer clearfix">
<button type="button" id="btnAdd" class="btn btn-primary pull-right">Add</button>
</div>
</div>
</div>
</div>

<!-- Alerts -->
<div id="alInfo" class="alert alert-info alert-dismissible fade in" role="alert" hidden>
<button type="button" class="close" data-hide="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
Updating CNAME records...
</div>
<div id="alSuccess" class="alert alert-success alert-dismissible fade in" role="alert" hidden>
<button type="button" class="close" data-hide="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
Success! The list will refresh.
</div>
<div id="alFailure" class="alert alert-danger alert-dismissible fade in" role="alert" hidden>
<button type="button" class="close" data-hide="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
Failure! Something went wrong, see output below:<br/><br/><pre><span id="err"></span></pre>
</div>
<div id="alWarning" class="alert alert-warning alert-dismissible fade in" role="alert" hidden>
<button type="button" class="close" data-hide="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
At least one domain was already present, see output below:<br/><br/><pre><span id="warn"></span></pre>
</div>
<div class="row">
<div class="col-md-12">
<div class="box" id="recent-queries">
<div class="box-header with-border">
<h3 class="box-title">
List of local CNAME records
</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="customCNAMETable" class="table table-striped table-bordered" width="100%">
<thead>
<tr>
<th>Domain</th>
<th>Target</th>
<th>Action</th>
</tr>
</thead>
</table>
<button type="button" id="resetButton" class="btn btn-default btn-sm text-red hidden">Clear Filters</button>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>

<script src="scripts/pi-hole/js/utils.js"></script>
<script src="scripts/pi-hole/js/customcname.js"></script>

<?php
require "scripts/pi-hole/php/footer.php";
?>
123 changes: 123 additions & 0 deletions scripts/pi-hole/js/customcname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* Pi-hole: A black hole for Internet advertisements
* (c) 2017 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

/* global utils:false */

var table;
var token = $("#token").text();

function showAlert(type, message) {
var alertElement = null;
var messageElement = null;

switch (type) {
case "info":
alertElement = $("#alInfo");
break;
case "success":
alertElement = $("#alSuccess");
break;
case "warning":
alertElement = $("#alWarning");
messageElement = $("#warn");
break;
case "error":
alertElement = $("#alFailure");
messageElement = $("#err");
break;
default:
return;
}

if (messageElement !== null) messageElement.html(message);

alertElement.fadeIn(200);
alertElement.delay(8000).fadeOut(2000);
}

$(function () {
$("#btnAdd").on("click", addCustomCNAME);

table = $("#customCNAMETable").DataTable({
ajax: {
url: "scripts/pi-hole/php/customcname.php",
data: { action: "get", token: token },
type: "POST"
},
columns: [{}, {}, { orderable: false, searchable: false }],
columnDefs: [
{
targets: 2,
render: function (data, type, row) {
return (
'<button type="button" class="btn btn-danger btn-xs deleteCustomCNAME" data-domain=\'' +
row[0] +
"' data-target='" +
row[1] +
"'>" +
'<span class="far fa-trash-alt"></span>' +
"</button>"
);
}
}
],
drawCallback: function () {
$(".deleteCustomCNAME").on("click", deleteCustomCNAME);
}
});
// Disable autocorrect in the search box
var input = document.querySelector("input[type=search]");
input.setAttribute("autocomplete", "off");
input.setAttribute("autocorrect", "off");
input.setAttribute("autocapitalize", "off");
input.setAttribute("spellcheck", false);
});

function addCustomCNAME() {
var domain = utils.escapeHtml($("#domain").val());
var target = utils.escapeHtml($("#target").val());

showAlert("info");
$.ajax({
url: "scripts/pi-hole/php/customcname.php",
method: "post",
dataType: "json",
data: { action: "add", domain: domain, target: target, token: token },
success: function (response) {
if (response.success) {
showAlert("success");
table.ajax.reload();
} else showAlert("error", response.message);
},
error: function () {
showAlert("error", "Error while adding this custom CNAME record");
}
});
}

function deleteCustomCNAME() {
var domain = $(this).attr("data-domain");
var target = $(this).attr("data-target");

showAlert("info");
$.ajax({
url: "scripts/pi-hole/php/customcname.php",
method: "post",
dataType: "json",
data: { action: "delete", domain: domain, target: target, token: token },
success: function (response) {
if (response.success) {
showAlert("success");
table.ajax.reload();
} else showAlert("error", response.message);
},
error: function (jqXHR, exception) {
showAlert("error", "Error while deleting this custom CNAME record");
console.log(exception); // eslint-disable-line no-console
}
});
}
26 changes: 26 additions & 0 deletions scripts/pi-hole/php/customcname.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

require_once "func.php";

require_once('auth.php');

// Authentication checks
if (isset($_POST['token'])) {
check_cors();
check_csrf($_POST['token']);
} else {
log_and_die('Not allowed (login session invalid or expired, please relogin on the Pi-hole dashboard)!');
}


switch ($_POST['action'])
{
case 'get': echo json_encode(echoCustomCNAMEEntries()); break;
case 'add': echo json_encode(addCustomCNAMEEntry()); break;
case 'delete': echo json_encode(deleteCustomCNAMEEntry()); break;
default:
die("Wrong action");
}


?>
2 changes: 0 additions & 2 deletions scripts/pi-hole/php/customdns.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

require_once "func.php";

$customDNSFile = "/etc/pihole/custom.list";

require_once('auth.php');

// Authentication checks
Expand Down
Loading

0 comments on commit cb5b9c6

Please sign in to comment.