summaryrefslogtreecommitdiff
path: root/polybar/scripts/wg-pc-status.sh
diff options
context:
space:
mode:
Diffstat (limited to 'polybar/scripts/wg-pc-status.sh')
-rwxr-xr-xpolybar/scripts/wg-pc-status.sh32
1 files changed, 32 insertions, 0 deletions
diff --git a/polybar/scripts/wg-pc-status.sh b/polybar/scripts/wg-pc-status.sh
new file mode 100755
index 0000000..b708695
--- /dev/null
+++ b/polybar/scripts/wg-pc-status.sh
@@ -0,0 +1,32 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+iface="${WG_QUICK_PROFILE:-PC}"
+
+age_label() {
+ local latest now age
+
+ latest="$(wg show "$iface" latest-handshakes 2>/dev/null | awk 'NF { if ($2 > newest) newest = $2 } END { print newest + 0 }')"
+ if [ -z "$latest" ] || [ "$latest" -le 0 ] 2>/dev/null; then
+ return 0
+ fi
+
+ now="$(date +%s)"
+ age=$((now - latest))
+
+ if [ "$age" -lt 60 ]; then
+ printf ' %ss' "$age"
+ elif [ "$age" -lt 3600 ]; then
+ printf ' %sm' "$((age / 60))"
+ elif [ "$age" -lt 86400 ]; then
+ printf ' %sh' "$((age / 3600))"
+ else
+ printf ' %sd' "$((age / 86400))"
+ fi
+}
+
+if [ -d "/sys/class/net/$iface" ]; then
+ echo "%{F#98971a}up$(age_label)%{F-}"
+else
+ echo "%{F#cc241d}down%{F-}"
+fi