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..1a90e09a --- /dev/null +++ b/polybar-scripts/pihole-status/pihole-status.sh @@ -0,0 +1,70 @@ +#!/bin/sh + +ENDPOINT="http://pi.hole/admin/api.php" # Pi-Hole API endpoint. Change host +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 + +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 -k -s -H "X-FTL-SID: $(get_sid)" "$ENDPOINT/dns/blocking" | jq -r '.blocking' +} + +sleep_pid=0 + +toggle() { + status="$(get_status)" + + case "$status" in + enabled) + 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 -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 + ;; + 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 00000000..84d84cbf Binary files /dev/null and b/polybar-scripts/pihole-status/screenshots/1.png differ diff --git a/polybar-scripts/pihole-status/screenshots/2.png b/polybar-scripts/pihole-status/screenshots/2.png new file mode 100644 index 00000000..e8300c9b Binary files /dev/null and b/polybar-scripts/pihole-status/screenshots/2.png differ