From bb7a75815b64c6cb9414ff0c2c00c47cf2fa12c8 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 7 May 2023 09:04:24 +0200 Subject: [PATCH] Implement DNS control (enable/disable blocking, possibly with a given timeout) Signed-off-by: DL6ER --- scripts/pi-hole/js/footer.js | 94 +++++++++++++++++------------ scripts/pi-hole/js/groups-common.js | 5 ++ scripts/pi-hole/lua/sidebar.lp | 8 +-- 3 files changed, 63 insertions(+), 44 deletions(-) diff --git a/scripts/pi-hole/js/footer.js b/scripts/pi-hole/js/footer.js index 869e3d2f..3111b630 100644 --- a/scripts/pi-hole/js/footer.js +++ b/scripts/pi-hole/js/footer.js @@ -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 } -function piholeChanged(action) { +function piholeChanged(blocking) { var status = $("#status"); var ena = $("#pihole-enable"); var dis = $("#pihole-disable"); - switch (action) { - case "enabled": - status.html(" Active"); - ena.hide(); - dis.show(); - dis.removeClass("active"); - break; - - case "disabled": - status.html(" Blocking disabled"); - ena.show(); - dis.hide(); - break; - - default: - // nothing + if (blocking) { + status.html(" Active"); + ena.hide(); + dis.show(); + dis.removeClass("active"); + } else { + status.html(" Blocking disabled"); + ena.show(); + dis.hide(); } } @@ -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) { - var token = encodeURIComponent($("#token").text()); var enaT = $("#enableTimer"); var btnStatus; switch (action) { case "enable": btnStatus = $("#flip-status-enable"); - btnStatus.html(" "); - $.getJSON("api.lp?enable&token=" + token, function (data) { - if (data.status === "enabled") { - btnStatus.html(""); - piholeChanged("enabled"); - } - }); break; - case "disable": btnStatus = $("#flip-status-disable"); - btnStatus.html(" "); - $.getJSON("api.lp?disable=" + duration + "&token=" + token, function (data) { - if (data.status === "disabled") { - btnStatus.html(""); - piholeChanged("disabled"); - if (duration > 0) { - enaT.html(Date.now() + duration * 1000); - setTimeout(countDown, 100); - } - } - }); break; - - default: - // nothing + default: // Do nothing + break; } + + btnStatus.html(" "); + 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(""); + piholeChanged(blocking); + if (duration > 0) { + enaT.html(Date.now() + duration * 1000); + setTimeout(countDown, 100); + } + } + }) + .fail(function (data) { + apiFailure(data); + }); } function testCookies() { @@ -558,6 +571,7 @@ $("#settings-level").on("change", function () { }); $(function () { + checkBlocking(); initSettingsLevel(); }); diff --git a/scripts/pi-hole/js/groups-common.js b/scripts/pi-hole/js/groups-common.js index 90bbe765..24153fda 100644 --- a/scripts/pi-hole/js/groups-common.js +++ b/scripts/pi-hole/js/groups-common.js @@ -5,6 +5,8 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ +/* global apiFailure:false */ + // eslint-disable-next-line no-unused-vars var groups = []; @@ -17,5 +19,8 @@ function getGroups() { success: function (data) { groups = data.groups; }, + error: function (data) { + apiFailure(data); + }, }); } diff --git a/scripts/pi-hole/lua/sidebar.lp b/scripts/pi-hole/lua/sidebar.lp index b87e2d23..0bc8de30 100644 --- a/scripts/pi-hole/lua/sidebar.lp +++ b/scripts/pi-hole/lua/sidebar.lp @@ -17,7 +17,7 @@