From 60737f9c394c89d9728ca1e448c5e0962dfd557c Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 9 May 2023 10:06:23 +0200 Subject: [PATCH] Finish DNS, DHCP and API settings pages Signed-off-by: DL6ER --- scripts/pi-hole/js/settings-advanced.js | 62 +------------------- scripts/pi-hole/js/settings-api.js | 5 +- scripts/pi-hole/js/settings.js | 78 ++++++++++++++++++++++++- scripts/pi-hole/lua/sidebar.lp | 6 +- settings-api.lp | 17 +++--- settings-dhcp.lp | 21 ++++--- settings-dns.lp | 28 ++++----- 7 files changed, 117 insertions(+), 100 deletions(-) diff --git a/scripts/pi-hole/js/settings-advanced.js b/scripts/pi-hole/js/settings-advanced.js index 7282221f..ccc7cc01 100644 --- a/scripts/pi-hole/js/settings-advanced.js +++ b/scripts/pi-hole/js/settings-advanced.js @@ -5,7 +5,7 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -/* global utils:false, apiFailure: false, applyCheckboxRadioStyle: false */ +/* global utils:false, apiFailure: false, applyCheckboxRadioStyle: false, saveSettings:false */ /* exported createDynamicConfigTabs */ function addAllowedValues(allowed) { @@ -308,66 +308,6 @@ function createDynamicConfigTabs() { }); } -function saveSettings() { - var settings = {}; - $("[data-key]").each(function () { - var key = $(this).data("key"); - var value = $(this).val(); - if ($(this).is(":checkbox")) { - value = $(this).is(":checked"); - } - - if ($(this).is("textarea")) { - value = $(this).val(); - value = value === "" ? [] : value.split("\n"); - } - - if ($(this).data("type") === "integer") { - value = parseInt(value, 10); - } - - if ($(this).data("type") === "float") { - value = parseFloat(value); - } - - // Build deep object - // Transform "foo.bar.baz" into {foo: {bar: {baz: value}}} - var parts = key.split("."); - var obj = {}; - var tmp = obj; - for (var i = 0; i < parts.length - 1; i++) { - tmp[parts[i]] = {}; - tmp = tmp[parts[i]]; - } - - tmp[parts[parts.length - 1]] = value; - - // Merge deep object into settings - $.extend(true, settings, obj); - }); - // Apply changes - $.ajax({ - url: "/api/config", - method: "PATCH", - data: JSON.stringify({ config: settings }), - contentType: "application/json; charset=utf-8", - }) - .done(function () { - // Success - utils.showAlert( - "success", - "fa-solid fa-fw fa-floppy-disk", - "Successfully saved and applied settings", - "" - ); - // Reload page - location.reload(); - }) - .fail(function (data) { - apiFailure(data); - }); -} - $(document).ready(function () { createDynamicConfigTabs(); }); diff --git a/scripts/pi-hole/js/settings-api.js b/scripts/pi-hole/js/settings-api.js index df2253a1..1e7291ec 100644 --- a/scripts/pi-hole/js/settings-api.js +++ b/scripts/pi-hole/js/settings-api.js @@ -38,6 +38,7 @@ $(function () { { data: null, width: "22px" }, { data: "id" }, { data: "valid", render: renderBool }, + { data: "tls", render: renderBool }, { data: "login_at", render: utils.renderTimestamp }, { data: "valid_until", render: utils.renderTimestamp }, { data: "remote_addr", type: "ip-address" }, @@ -78,10 +79,10 @@ $(function () { '">' + '' + ""; - $("td:eq(7)", row).html(button); + $("td:eq(8)", row).html(button); if (data.current_session) { ownSessionID = data.id; - $("td:eq(5)", row).html( + $("td:eq(6)", row).html( '' + data.remote_addr + "" diff --git a/scripts/pi-hole/js/settings.js b/scripts/pi-hole/js/settings.js index 0e5f32d5..2a048d08 100644 --- a/scripts/pi-hole/js/settings.js +++ b/scripts/pi-hole/js/settings.js @@ -5,13 +5,20 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -// Handle hiding of alerts +/* global utils:false, apiFailure:false */ + $(function () { + // Handle hiding of alerts $("[data-hide]").on("click", function () { $(this) .closest("." + $(this).attr("data-hide")) .hide(); }); + + // Handle saving of settings + $(".save-button").on("click", function () { + saveSettings(); + }); }); // Globally available function to set config values @@ -65,3 +72,72 @@ function setConfigValues(topic, key, value) { } } } + +function saveSettings() { + var settings = {}; + $("[data-key]").each(function () { + var key = $(this).data("key"); + var value = $(this).val(); + + // If this is a checkbox, use the checked state + if ($(this).is(":checkbox")) { + value = $(this).is(":checked"); + } + + // If this is a radio button, skip all but the checked one + if ($(this).is(":radio") && !$(this).is(":checked")) return; + + // If this is a string array, split the value into an array + if ($(this).is("textarea")) { + value = $(this).val(); + value = value === "" ? [] : value.split("\n"); + } + + // If this is an integer number, parse it accordignly + if ($(this).data("type") === "integer") { + value = parseInt(value, 10); + } + + // If this is a floating point value, parse it accordignly + if ($(this).data("type") === "float") { + value = parseFloat(value); + } + + // Build deep object + // Transform "foo.bar.baz" into {foo: {bar: {baz: value}}} + var parts = key.split("."); + var obj = {}; + var tmp = obj; + for (var i = 0; i < parts.length - 1; i++) { + tmp[parts[i]] = {}; + tmp = tmp[parts[i]]; + } + + tmp[parts[parts.length - 1]] = value; + + // Merge deep object into settings + $.extend(true, settings, obj); + }); + + // Apply changes + $.ajax({ + url: "/api/config", + method: "PATCH", + data: JSON.stringify({ config: settings }), + contentType: "application/json; charset=utf-8", + }) + .done(function () { + // Success + utils.showAlert( + "success", + "fa-solid fa-fw fa-floppy-disk", + "Successfully saved and applied settings", + "" + ); + // Reload page + location.reload(); + }) + .fail(function (data) { + apiFailure(data); + }); +} diff --git a/scripts/pi-hole/lua/sidebar.lp b/scripts/pi-hole/lua/sidebar.lp index ee921263..378890bc 100644 --- a/scripts/pi-hole/lua/sidebar.lp +++ b/scripts/pi-hole/lua/sidebar.lp @@ -140,17 +140,17 @@
  • "> - DNS + DNS
  • "> - DHCP + DHCP
  • "> - Web interface / API + Web interface / API
  • "> diff --git a/settings-api.lp b/settings-api.lp index 46dc2112..99e44559 100644 --- a/settings-api.lp +++ b/settings-api.lp @@ -19,12 +19,12 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')

    Domains to be excluded from Top Domains / Ads Lists

    - +

    Domains may be described by their domain name (like example.com)

    Clients to be excluded from Top Clients List

    - +

    Clients may be described either by their IP addresses (IPv4 and IPv6 are supported), or hostnames (like laptop.lan).

    @@ -42,12 +42,12 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
    > -

    This will show all permitted domain entries in the query log.

    +

    This will show all permitted domain entries in the query log.

    > -

    This will show all blocked domain entries in the query log.

    +

    This will show all blocked domain entries in the query log.

    @@ -63,14 +63,14 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
    - +

    This will require local clients to authenticate to access the API. This is useful if you want to prevent local users from accessing the API without knowing the password.

    - +

    This will make the API output more human-readable, but will increase the size of the output and make the API a bit slower.

    @@ -81,7 +81,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
    - +
    @@ -89,7 +89,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
    -
    @@ -115,6 +115,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r') ID Valid + TLS Login at Valid until Client IP diff --git a/settings-dhcp.lp b/settings-dhcp.lp index 742db0b8..99d83168 100644 --- a/settings-dhcp.lp +++ b/settings-dhcp.lp @@ -19,7 +19,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
    -
    +

    Make sure your router's DHCP server is disabled when using the Pi-hole DHCP server!

    @@ -29,7 +29,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
    From
    -
    @@ -39,7 +39,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
    To
    -
    @@ -50,14 +50,14 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
    Router
    -
    -
     
    +
     

    Enable this option to enable IPv6 support for the Pi-hole DHCP server. This will allow the Pi-hole to hand out IPv6 addresses to clients and also provide IPv6 router advertisements (RA) to clients. This option is only useful if the Pi-hole is configured with an IPv6 address.

    @@ -76,8 +76,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
    Domain
    - +

    The DNS domains for the DHCP server. If no domain is specified, then any DHCP hostname with a domain part (i.e., with a period) will be disallowed. If a domain is specified, then hostnames with a domain parts matching the domain here are allowed. In addition, when a suffix is set then hostnames without a domain part have the suffix added as an optional domain part.

    @@ -89,7 +88,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
    Lease time
    + autocorrect="off" id="dhcp.leaseTime" data-key="dhcp.leaseTime" value="">

    The lease time can be in seconds, minutes (e.g., "45m"), hours (e.g., "1h"), days (like "2d"), or even weeks ("1w"). You may also use "infinite" as string but be aware of the drawbacks: assigned addresses are will only be made available again after the lease time has passed or when leases are manually deleted below.

    @@ -97,11 +96,11 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')
    -
     
    +
     

    The DHCPv4 rapid commit option allows the Pi-hole DHCP server to assign an IP address to a client right away. This can noteably speed up the address assignment process and you will notice, e.g., faster WiFi joins in your network. This option should only be enabled if the Pi-hole DHCP server is the only DHCP server in your network.

    -
     
    +
     

    Advertise DNS server multiple times to clients. Some devices will add their own proprietary DNS servers to the list of DNS servers, which can cause issues with Pi-hole. This option will advertise the Pi-hole DNS server multiple times to clients, which should prevent this from happening.

    @@ -149,7 +148,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')

    Specify per host parameters for the DHCP server. This allows a machine with a particular hardware address to be always allocated the same hostname, IP address and lease time. A hostname specified like this overrides any supplied by the DHCP client on the machine. It is also allowable to omit the hardware address and include the hostname, in which case the IP address and lease times will apply to any machine claiming that name.

    -
      +
     

    Each entry should be on a separate line, and should be of the form:

    [<hwaddr>][,id:<client_id>|*][,set:<tag>][,tag:<tag>][,<ipaddr>][,<hostname>][,<lease_time>][,ignore]

    Only one entry per MAC address is allowed.

    diff --git a/settings-dns.lp b/settings-dns.lp index 02f7473c..c7adc194 100644 --- a/settings-dns.lp +++ b/settings-dns.lp @@ -43,7 +43,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')

    The following list contains all DNS servers selected above. Furthermore, you can add your own custom DNS servers here. The expected format is one server per line in form of IP#port, where the port is optional. If given, it has to be separated by a hash # from the address (e.g. 127.0.0.1#5335 for a local unbound istance running on port 5335). The port defaults to 53 if omitted.

    - +
    @@ -85,7 +85,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r') when "Never forward non-FQDNs" is not enabled.

    - +
    @@ -100,13 +100,13 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r') - + - + - + @@ -130,22 +130,22 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')

    Recommended setting

    - +

    Potentially dangerous options

    Make sure your Pi-hole is properly firewalled!
    - +
    - +
    - +

    These options are dangerous on devices @@ -168,7 +168,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')

    - +

    Tells Pi-hole to never forward A or AAAA queries for plain names, without dots or domain parts, to upstream nameservers. If @@ -179,7 +179,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')


    - +

    All reverse lookups for private IP ranges (i.e., 192.168.0.x/24, etc.) which are not found in /etc/hosts or the DHCP leases are answered @@ -191,7 +191,7 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')


    - +

    Validate DNS replies and cache DNSSEC data. When forwarding DNS queries, Pi-hole requests the DNSSEC records needed to validate @@ -213,8 +213,8 @@ mg.include('scripts/pi-hole/lua/header_authenticated.lp','r')

    -

    Block clients making more than queries within - seconds.

    +

    Block clients making more than queries within + seconds.

    When a client makes too many queries in too short time, it gets rate-limited. Rate-limited queries are answered with a REFUSED reply and not further processed by FTL