From 7d514cdfa2ab4d3a8246c87a140e06e3ccce4cbe Mon Sep 17 00:00:00 2001 From: Charles Crete Date: Wed, 16 Oct 2024 00:51:02 -0400 Subject: [PATCH 1/2] Add pihole-status --- README.md | 2 + polybar-scripts/pihole-status/README.md | 25 ++++++++ .../pihole-status/pihole-status.sh | 58 ++++++++++++++++++ .../pihole-status/screenshots/1.png | Bin 0 -> 846 bytes .../pihole-status/screenshots/2.png | Bin 0 -> 472 bytes 5 files changed, 85 insertions(+) create mode 100644 polybar-scripts/pihole-status/README.md create mode 100755 polybar-scripts/pihole-status/pihole-status.sh create mode 100644 polybar-scripts/pihole-status/screenshots/1.png create mode 100644 polybar-scripts/pihole-status/screenshots/2.png diff --git a/README.md b/README.md index e882f382..3cea6855 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,8 @@ Is this your first time here? You should definitely take a look at these scripts [![info-healthchecks.io](polybar-scripts/info-healthchecks.io/screenshots/1.png)](polybar-scripts/info-healthchecks.io/) [![network-huawei-modem](polybar-scripts/network-huawei-modem/screenshots/1.png)](polybar-scripts/network-huawei-modem/) [![info-timezone](polybar-scripts/info-timezone/screenshots/1.gif)](polybar-scripts/info-timezone/) +[![pihole-status](polybar-scripts/pihole-status/screenshots/1.png)](polybar-scripts/pihole-status/) +[![pihole-status](polybar-scripts/pihole-status/screenshots/2.png)](polybar-scripts/pihole-status/) ## See also these other user repositories: diff --git a/polybar-scripts/pihole-status/README.md b/polybar-scripts/pihole-status/README.md new file mode 100644 index 00000000..f99cebe8 --- /dev/null +++ b/polybar-scripts/pihole-status/README.md @@ -0,0 +1,25 @@ +# Script: pihole-status + +A script to remotely disable/enable Pi-Hole's blocking. Displays the current status and toggle status when clicked. + +Depends on `curl` and `jq`. + +## Module + +Update the `pihole-status.sh` with your Pi-Hole instance's hostname and API key. You may also customize the on/off label, refresh time, as well as the amount of time to disable Pi-Hole for (by default, 300 seconds). + +```ini +[module/pihole-status] +type = custom/script +exec = ~/polybar-scripts/pihole-status.sh +tail = true +click-left = kill -USR1 %pid% +``` + +## How this script works + +This script uses `tail` to keep a long-running instance of the application, which Polybar uses the last outputted line as label. It fetches the new status with curl every 10 seconds (configurable) and outputs the on or off label based on the current status read without the JSON response with jq. + +When the script is clicked, a `USR1` signal is sent to it with `kill`, and it runs the `toggle`. This updates the status with curl and prints the current status, and restarts the refresh loop. + +This design was required to have both auto-refreshing and updating on click. diff --git a/polybar-scripts/pihole-status/pihole-status.sh b/polybar-scripts/pihole-status/pihole-status.sh new file mode 100755 index 00000000..76b54b9d --- /dev/null +++ b/polybar-scripts/pihole-status/pihole-status.sh @@ -0,0 +1,58 @@ +#!/bin/sh + +ENDPOINT="http://pi.hole/admin/api.php" # Pi-Hole API endpoint. Change host +API_TOKEN="" # Get from /admin/settings.php?tab=api + +DISABLE_TIME="300" # In seconds. Set to 0 to disable Pi-Hole forever +REFRESH_INTERVAL="10" # In seconds. Set to "infinity" to disable refreshing + +# Uses Nerd Fonts for icon and lemonbar tags for coloring +ON_LABEL="󰷱" +OFF_LABEL="%{F#f00}󰷱%{F-}" + + +get_status() { + curl -s "$ENDPOINT?status&auth=$API_TOKEN" | jq -r '.status' +} + +sleep_pid=0 + +toggle() { + status="$(get_status)" + + case "$status" in + enabled) + curl -s "$ENDPOINT?disable=$DISABLE_TIME&auth=$API_TOKEN" > /dev/null + echo "$OFF_LABEL" + sleep 0.1 # some time for write to finish before refetch + ;; + disabled) + curl -s "$ENDPOINT?enable&auth=$API_TOKEN" > /dev/null + echo "$ON_LABEL" + sleep 0.1 # some time for write to finish before refetch + ;; + esac + + if [ "$sleep_pid" -ne 0 ]; then + kill $sleep_pid >/dev/null 2>&1 + fi +} + +trap "toggle" USR1 + +while true; do + status="$(get_status)" + + case "$status" in + enabled) + echo "$ON_LABEL" + ;; + disabled) + echo "$OFF_LABEL" + ;; + esac + + sleep "$REFRESH_INTERVAL" & + sleep_pid=$! + wait +done diff --git a/polybar-scripts/pihole-status/screenshots/1.png b/polybar-scripts/pihole-status/screenshots/1.png new file mode 100644 index 0000000000000000000000000000000000000000..84d84cbf5b0daaa5bc5c75cf58d3bad87c9f9f62 GIT binary patch literal 846 zcmV-U1F`&xP)s!Vv}{gU{-iE-GJi^0*)T2C+zRXm zBBWiIKatT@k&#`g;9Y2-C@V_z!i&%liu_5;fj}S-2m}J*@8P0-&XJrisV=;tlm}2NUzj<&3y;-*fI3@Y zU>KUF8RldAqs&O^eFH*D55fq!h;8R^uk=7|*+0Inz|BP%cKLXf4~ zFwbIfMFV=P$_FORjA>l=w(r9vtt(BUAWxd9)tMNx+0=G72DKKyL65R#9-Tgd0zuuUL~EL^r=^>NGAnv4zJ9|M11qrlHS_nV}8s zRe5py@s$B1f(Xr%J`-c!W^%0Yjslx2o{2B!B)Lx9X$gM z(LY%N09>o~HDO4iO5xPE49@*TOoA-R55HPjSDY$#SZ7VfSytpDbb|l@cEi%Ao0Ww6 z@jU`gq{ln@TZX>`=K1wPy z(cM5VFV9Um)`j;ag(m8pXn!-cvE5JC^2qR%ZTsjhvH zB>J(i$PtUFT}x_tr0RN3*#N>E0RZsqQOEp9YmY5jogx?WAl&JR%MxI_nKl;2+PZD> z^yCmv&Xy@gOWX*;EdVRRjWoKp+qZ1j3%+ Y7uN?Yp>E|*asU7T07*qoM6N<$g5>9f&;S4c literal 0 HcmV?d00001 diff --git a/polybar-scripts/pihole-status/screenshots/2.png b/polybar-scripts/pihole-status/screenshots/2.png new file mode 100644 index 0000000000000000000000000000000000000000..e8300c9bbbfe664a579a35e5d11b43befe42274e GIT binary patch literal 472 zcmV;}0Vn>6P)2yfgW&Aspd#)LE>fY0 z5AX$o-~*R#E(IrtPU6zdB8Z!tiy*o Date: Thu, 20 Feb 2025 17:49:50 -0500 Subject: [PATCH 2/2] Updated for Pi-Hole 6 For Pi-Hole 5 and lower, use https://github.com/polybar/polybar-scripts/blob/7d514cdfa2ab4d3a8246c87a140e06e3ccce4cbe/polybar-scripts/pihole-status/pihole-status.sh --- .../pihole-status/pihole-status.sh | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/polybar-scripts/pihole-status/pihole-status.sh b/polybar-scripts/pihole-status/pihole-status.sh index 76b54b9d..1a90e09a 100755 --- a/polybar-scripts/pihole-status/pihole-status.sh +++ b/polybar-scripts/pihole-status/pihole-status.sh @@ -1,18 +1,30 @@ #!/bin/sh ENDPOINT="http://pi.hole/admin/api.php" # Pi-Hole API endpoint. Change host -API_TOKEN="" # Get from /admin/settings.php?tab=api +API_TOKEN="" # Get from /admin/settings/api under "Expert" -> "Advanced Settings" -> "Configure app password" +# NOTE: You may want to increase webserver.api.max_sessions (in /admin/settings/all) DISABLE_TIME="300" # In seconds. Set to 0 to disable Pi-Hole forever REFRESH_INTERVAL="10" # In seconds. Set to "infinity" to disable refreshing -# Uses Nerd Fonts for icon and lemonbar tags for coloring ON_LABEL="󰷱" OFF_LABEL="%{F#f00}󰷱%{F-}" +get_sid() { + if [ -n "$session_validity" ] && [ "$session_validity" -gt "$(date +%s)" ]; then + echo "$session_sid" + return + fi + + local response=$(curl -k -s -X POST "$ENDPOINT/auth" --data "{\"password\":\"$API_TOKEN\"}") + session_sid=$(echo "$response" | jq -r '.session.sid') + session_validity=$(($(date +%s) + $(echo "$response" | jq -r '.session.validity'))) + echo "$session_sid" +} + get_status() { - curl -s "$ENDPOINT?status&auth=$API_TOKEN" | jq -r '.status' + curl -k -s -H "X-FTL-SID: $(get_sid)" "$ENDPOINT/dns/blocking" | jq -r '.blocking' } sleep_pid=0 @@ -22,12 +34,12 @@ toggle() { case "$status" in enabled) - curl -s "$ENDPOINT?disable=$DISABLE_TIME&auth=$API_TOKEN" > /dev/null + curl -k -s -H "X-FTL-SID: $(get_sid)" -X POST "$ENDPOINT/dns/blocking" --data "{\"blocking\":false,\"timer\":$DISABLE_TIME}" > /dev/null echo "$OFF_LABEL" sleep 0.1 # some time for write to finish before refetch ;; disabled) - curl -s "$ENDPOINT?enable&auth=$API_TOKEN" > /dev/null + curl -k -s -H "X-FTL-SID: $(get_sid)" -X POST "$ENDPOINT/dns/blocking" --data "{\"blocking\":true}" > /dev/null echo "$ON_LABEL" sleep 0.1 # some time for write to finish before refetch ;;