diff --git a/gravity.php b/gravity.php deleted file mode 100644 index 38b0b5ef..00000000 --- a/gravity.php +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - - - - - - - - - diff --git a/network.php b/network.lp similarity index 83% rename from network.php rename to network.lp index 5f75e8f7..5c26559f 100644 --- a/network.php +++ b/network.lp @@ -1,11 +1,11 @@ - @@ -22,10 +22,9 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r') ID - IP address + IP address (hostname) Hardware address Interface - Hostname First seen Last Query Number of queries @@ -36,10 +35,9 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r') ID - IP address + IP address (hostname) Hardware address Interface - Hostname First seen Last Query Number of queries diff --git a/scripts/pi-hole/js/gravity.js b/scripts/pi-hole/js/gravity.js deleted file mode 100644 index 993dddba..00000000 --- a/scripts/pi-hole/js/gravity.js +++ /dev/null @@ -1,81 +0,0 @@ -/* 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. */ - -function eventsource() { - var alInfo = $("#alInfo"); - var alSuccess = $("#alSuccess"); - var ta = $("#output"); - - // IE does not support EventSource - exit early - if (typeof EventSource !== "function") { - ta.show(); - ta.html("Updating lists of ad-serving domains is not supported with this browser!"); - return; - } - - var source = new EventSource("scripts/pi-hole/php/gravity.sh.php"); - - ta.html(""); - ta.show(); - alInfo.show(); - alSuccess.hide(); - - source.addEventListener( - "message", - function (e) { - if (e.data.indexOf("Pi-hole blocking is") !== -1) { - alSuccess.show(); - } - - // Detect ${OVER} - var newString = "<------"; - - if (e.data.indexOf(newString) !== -1) { - ta.text(ta.text().substring(0, ta.text().lastIndexOf("\n")) + "\n"); - ta.append(e.data.replace(newString, "")); - } else { - ta.append(e.data); - } - }, - false - ); - - // Will be called when script has finished - source.addEventListener( - "error", - function () { - alInfo.delay(1000).fadeOut(2000, function () { - alInfo.hide(); - }); - source.close(); - $("#gravityBtn").prop("disabled", false); - }, - false - ); -} - -$("#gravityBtn").on("click", function () { - $("#gravityBtn").prop("disabled", true); - eventsource(); -}); - -// Handle hiding of alerts -$(function () { - $("[data-hide]").on("click", function () { - $(this) - .closest("." + $(this).attr("data-hide")) - .hide(); - }); - - // Do we want to start updating immediately? - // gravity.php?go - var searchString = window.location.search.substring(1); - if (searchString.indexOf("go") !== -1) { - $("#gravityBtn").prop("disabled", true); - eventsource(); - } -}); diff --git a/scripts/pi-hole/js/network.js b/scripts/pi-hole/js/network.js index 7899374b..6df74927 100644 --- a/scripts/pi-hole/js/network.js +++ b/scripts/pi-hole/js/network.js @@ -5,17 +5,13 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -/* global utils:false */ +/* global utils:false, apiFailure:false */ var tableApi; -var token = $("#token").text(); - -var API_STRING = "api_db.php?network"; // How many IPs do we show at most per device? -var MAXIPDISPLAY = 3; - -var DAY_IN_SECONDS = 24 * 60 * 60; +const MAXIPDISPLAY = 3; +const DAY_IN_SECONDS = 24 * 60 * 60; function handleAjaxError(xhr, textStatus) { if (textStatus === "timeout") { @@ -61,37 +57,33 @@ function parseColor(input) { } function deleteNetworkEntry() { - var tr = $(this).closest("tr"); - var id = tr.attr("data-id"); + const tr = $(this).closest("tr"); + const id = tr.attr("data-id"); + const hwaddr = tr.attr("data-hwaddr"); utils.disableAll(); utils.showAlert("info", "", "Deleting network table entry..."); $.ajax({ - url: "scripts/pi-hole/php/network.php", - method: "post", - dataType: "json", - data: { action: "delete_network_entry", id: id, token: token }, - success: function (response) { + url: "/api/network/devices/" + id, + method: "DELETE", + success: function () { utils.enableAll(); - if (response.success) { - utils.showAlert("success", "far fa-trash-alt", "Successfully deleted network table entry"); - tableApi.row(tr).remove().draw(false).ajax.reload(null, false); - } else { - utils.showAlert( - "error", - "", - "Error while network table entry with ID " + id, - response.message - ); - } + utils.showAlert( + "success", + "far fa-trash-alt", + "Successfully deleted network table entry", + hwaddr + ); + tableApi.row(tr).remove().draw(false).ajax.reload(null, false); }, - error: function (jqXHR, exception) { + error: function (data, exception) { + apiFailure(data); utils.enableAll(); utils.showAlert( "error", "", "Error while deleting network table entry with ID " + id, - jqXHR.responseText + data.responseText ); console.log(exception); // eslint-disable-line no-console }, @@ -133,63 +125,29 @@ $(function () { // Set determined background color $(row).css("background-color", color); - $("td:eq(7)", row).html(''); + $("td:eq(6)", row).html(''); // Insert "Never" into Last Query field when we have // never seen a query from this device if (data.lastQuery === 0) { - $("td:eq(5)", row).html("Never"); - } - - // Set hostname to "unknown" if not available - if (!data.name || data.name.length === 0) { - $("td:eq(3)", row).html("unknown"); - } else { - var names = []; - var name = ""; - maxiter = Math.min(data.name.length, MAXIPDISPLAY); - index = 0; - for (index = 0; index < maxiter; index++) { - name = data.name[index]; - if (name.length === 0) continue; - names.push('' + name + ""); - } - - if (data.name.length > MAXIPDISPLAY) { - // We hit the maximum above, add "..." to symbolize we would - // have more to show here - names.push("..."); - } - - maxiter = Math.min(data.ip.length, data.name.length); - var allnames = []; - for (index = 0; index < maxiter; index++) { - name = data.name[index]; - if (name.length > 0) { - allnames.push(name + " (" + data.ip[index] + ")"); - } else { - allnames.push("No host name for " + data.ip[index] + " known"); - } - } - - $("td:eq(3)", row).html(names.join("
")); - $("td:eq(3)", row).on("hover", function () { - this.title = allnames.join("\n"); - }); + $("td:eq(4)", row).html("Never"); } // Set number of queries to localized string (add thousand separators) - $("td:eq(6)", row).html(data.numQueries.toLocaleString()); + $("td:eq(5)", row).html(data.numQueries.toLocaleString()); var ips = []; - maxiter = Math.min(data.ip.length, MAXIPDISPLAY); - index = 0; + maxiter = Math.min(data.ips.length, MAXIPDISPLAY); for (index = 0; index < maxiter; index++) { - var ip = data.ip[index]; - ips.push('' + ip + ""); + var ip = data.ips[index]; + if (ip.name !== null && ip.name.length > 0) + ips.push( + '' + ip.ip + " (" + ip.name + ")" + ); + else ips.push('' + ip.ip + ""); } - if (data.ip.length > MAXIPDISPLAY) { + if (data.ips.length > MAXIPDISPLAY) { // We hit the maximum above, add "..." to symbolize we would // have more to show here ips.push("..."); @@ -197,7 +155,7 @@ $(function () { $("td:eq(0)", row).html(ips.join("
")); $("td:eq(0)", row).on("hover", function () { - this.title = data.ip.join("\n"); + this.title = data.ips.join("\n"); }); // MAC + Vendor field if available @@ -212,29 +170,39 @@ $(function () { // Add delete button $(row).attr("data-id", data.id); + $(row).attr("data-hwaddr", data.hwaddr); var button = '"; - $("td:eq(8)", row).html(button); + $("td:eq(7)", row).html(button); }, dom: "<'row'<'col-sm-12'f>>" + "<'row'<'col-sm-4'l><'col-sm-8'p>>" + "<'row'<'col-sm-12'<'table-responsive'tr>>>" + "<'row'<'col-sm-5'i><'col-sm-7'p>>", - ajax: { url: API_STRING, error: handleAjaxError, dataSrc: "network" }, + ajax: { + url: "/api/network/devices", + type: "GET", + dataType: "json", + data: { + max_devices: 999, + max_addresses: 25, + }, + error: handleAjaxError, + dataSrc: "devices", + }, autoWidth: false, processing: true, order: [[6, "desc"]], columns: [ { data: "id", visible: false }, - { data: "ip", type: "ip-address", width: "10%" }, + { data: "ip", type: "ip-address", width: "25%" }, { data: "hwaddr", width: "10%" }, { data: "interface", width: "4%" }, - { data: "name", width: "15%" }, { data: "firstSeen", width: "8%", diff --git a/scripts/pi-hole/lua/header.lp b/scripts/pi-hole/lua/header.lp index 4de8e029..ccc50f8c 100644 --- a/scripts/pi-hole/lua/header.lp +++ b/scripts/pi-hole/lua/header.lp @@ -27,6 +27,18 @@ function GET(name) return mg.request_info.query_string:match(name.."=([^&]*)") -- mg.get_var(mg.request_info.query_string, "REQUEST_URI") end + +-- Function checking if val is in tab +function in_array (val, tab) + for index, value in ipairs(tab) do + if value == val then + return true + end + end + + return false +end + ?> diff --git a/scripts/pi-hole/lua/sidebar.lp b/scripts/pi-hole/lua/sidebar.lp index 0bc8de30..168ca262 100644 --- a/scripts/pi-hole/lua/sidebar.lp +++ b/scripts/pi-hole/lua/sidebar.lp @@ -176,7 +176,7 @@ - - -
  • "> - - Update Gravity - -
  • "> @@ -236,7 +230,7 @@
  • "> - Network + Network