Implement DNS control (enable/disable blocking, possibly with a given timeout)

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2023-05-07 09:04:24 +02:00
parent 4d3d64b904
commit bb7a75815b
3 changed files with 63 additions and 44 deletions

View File

@@ -18,27 +18,20 @@ function secondsTimeSpanToHMS(s) {
return h + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s); //zero padding on minutes and seconds return h + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s); //zero padding on minutes and seconds
} }
function piholeChanged(action) { function piholeChanged(blocking) {
var status = $("#status"); var status = $("#status");
var ena = $("#pihole-enable"); var ena = $("#pihole-enable");
var dis = $("#pihole-disable"); var dis = $("#pihole-disable");
switch (action) { if (blocking) {
case "enabled": status.html("<i class='fa fa-circle fa-fw text-green-light'></i>&nbsp;Active");
status.html("<i class='fa fa-circle text-green-light'></i> Active");
ena.hide(); ena.hide();
dis.show(); dis.show();
dis.removeClass("active"); dis.removeClass("active");
break; } else {
status.html("<i class='fa fa-circle fa-fw text-red'></i>&nbsp;Blocking disabled");
case "disabled":
status.html("<i class='fa fa-circle text-red'></i> Blocking disabled");
ena.show(); ena.show();
dis.hide(); dis.hide();
break;
default:
// nothing
} }
} }
@@ -66,41 +59,61 @@ function countDown() {
} }
} }
function checkBlocking() {
$.ajax({
url: "/api/dns/blocking",
method: "GET",
})
.done(function (data) {
piholeChanged(data.blocking);
setTimeout(checkBlocking, 2500);
})
.fail(function (data) {
apiFailure(data);
setTimeout(checkBlocking, 5000);
});
}
function piholeChange(action, duration) { function piholeChange(action, duration) {
var token = encodeURIComponent($("#token").text());
var enaT = $("#enableTimer"); var enaT = $("#enableTimer");
var btnStatus; var btnStatus;
switch (action) { switch (action) {
case "enable": case "enable":
btnStatus = $("#flip-status-enable"); btnStatus = $("#flip-status-enable");
btnStatus.html("<i class='fa fa-spinner'> </i>");
$.getJSON("api.lp?enable&token=" + token, function (data) {
if (data.status === "enabled") {
btnStatus.html("");
piholeChanged("enabled");
}
});
break; break;
case "disable": case "disable":
btnStatus = $("#flip-status-disable"); btnStatus = $("#flip-status-disable");
btnStatus.html("<i class='fa fa-spinner'> </i>"); break;
$.getJSON("api.lp?disable=" + duration + "&token=" + token, function (data) { default: // Do nothing
if (data.status === "disabled") { break;
}
btnStatus.html("<i class='fa fa-spinner fa-spin'> </i>");
const blocking = action === "enable";
$.ajax({
url: "/api/dns/blocking",
method: "POST",
data: JSON.stringify({
blocking: blocking,
timer: parseInt(duration, 10) > 0 ? parseInt(duration, 10) : null,
}),
})
.done(function (data) {
if (data.blocking === blocking) {
btnStatus.html(""); btnStatus.html("");
piholeChanged("disabled"); piholeChanged(blocking);
if (duration > 0) { if (duration > 0) {
enaT.html(Date.now() + duration * 1000); enaT.html(Date.now() + duration * 1000);
setTimeout(countDown, 100); setTimeout(countDown, 100);
} }
} }
})
.fail(function (data) {
apiFailure(data);
}); });
break;
default:
// nothing
}
} }
function testCookies() { function testCookies() {
@@ -558,6 +571,7 @@ $("#settings-level").on("change", function () {
}); });
$(function () { $(function () {
checkBlocking();
initSettingsLevel(); initSettingsLevel();
}); });

View File

@@ -5,6 +5,8 @@
* This file is copyright under the latest version of the EUPL. * This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */ * Please see LICENSE file for your rights under this license. */
/* global apiFailure:false */
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
var groups = []; var groups = [];
@@ -17,5 +19,8 @@ function getGroups() {
success: function (data) { success: function (data) {
groups = data.groups; groups = data.groups;
}, },
error: function (data) {
apiFailure(data);
},
}); });
} }

View File

@@ -17,7 +17,7 @@
</div> </div>
<div class="pull-left info"> <div class="pull-left info">
<p>Status</p> <p>Status</p>
<span id="status"></span> <span id="status"></span><br/>
<span id="temperature"></span><br/> <span id="temperature"></span><br/>
<span id="cpu"></span><br/> <span id="cpu"></span><br/>
<span id="memory"></span> <span id="memory"></span>
@@ -81,7 +81,7 @@
<!-- Enable/Disable Blocking --> <!-- Enable/Disable Blocking -->
<li id="pihole-disable" class="menu-dns treeview"> <li id="pihole-disable" class="menu-dns treeview">
<a href="#"> <a href="#">
<i class="fa fa-fw menu-icon fa-stop"></i> <span class="text-red">Disable Blocking&nbsp;&nbsp;&nbsp;<span id="flip-status-disable"></span></span> <i class="fa fa-fw menu-icon fa-stop"></i> <span class="text-green">Disable Blocking&nbsp;&nbsp;&nbsp;<span id="flip-status-disable"></span></span>
<span class="pull-right-container"> <span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i> <i class="fa fa-angle-left pull-right"></i>
</span> </span>
@@ -115,10 +115,10 @@
</ul> </ul>
<!-- <a href="#" id="flip-status"><i class="fa fa-stop"></i> <span>Disable</span></a> --> <!-- <a href="#" id="flip-status"><i class="fa fa-stop"></i> <span>Disable</span></a> -->
</li> </li>
<li id="pihole-enable" class="menu-dns treeview"> <li id="pihole-enable" class="menu-dns treeview" style="display: none;">
<a href="#"> <a href="#">
<i class="fa fa-fw menu-icon fa-play"></i> <i class="fa fa-fw menu-icon fa-play"></i>
<span id="enableLabel" class="text-red">Enable Blocking&nbsp;&nbsp;&nbsp; <span id="enableLabel" class="text-green">Enable Blocking&nbsp;&nbsp;&nbsp;
<span id="flip-status-enable"></span> <span id="flip-status-enable"></span>
</span> </span>
</a> </a>