From 5fb19f10389b70ee2389755106092a9ef6c64598 Mon Sep 17 00:00:00 2001 From: David Moc Date: Sun, 24 May 2026 11:48:07 +0200 Subject: Go --- polybar/scripts/wg-pc-toggle.sh | 82 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 polybar/scripts/wg-pc-toggle.sh (limited to 'polybar/scripts/wg-pc-toggle.sh') diff --git a/polybar/scripts/wg-pc-toggle.sh b/polybar/scripts/wg-pc-toggle.sh new file mode 100755 index 0000000..5cd102f --- /dev/null +++ b/polybar/scripts/wg-pc-toggle.sh @@ -0,0 +1,82 @@ +#!/usr/bin/env bash +set -euo pipefail + +profile="${WG_QUICK_PROFILE:-PC}" +iface="$profile" +requested="${1:-toggle}" + +export DISPLAY="${DISPLAY:-:0}" +if [ -z "${XAUTHORITY:-}" ] && [ -r "$HOME/.Xauthority" ]; then + export XAUTHORITY="$HOME/.Xauthority" +fi + +case "$profile" in + *[!A-Za-z0-9_.@-]*|"") + echo "Invalid WireGuard profile: $profile" >&2 + exit 2 + ;; +esac + +log_file="${XDG_RUNTIME_DIR:-/tmp}/polybar-wg-$profile.log" + +notify() { + if command -v dunstify >/dev/null 2>&1; then + dunstify -a polybar -u low "WireGuard" "$1" + fi +} + +polkit_agent_running() { + pgrep -u "$UID" -f 'polkit-gnome-authentication-agent-1|polkit-kde-authentication-agent-1|lxqt-policykit-agent|polkit-mate-authentication-agent-1|xfce-polkit' >/dev/null 2>&1 +} + +ensure_polkit_agent() { + if polkit_agent_running; then + return 0 + fi + + if [ -x /home/aag/.config/polybar/scripts/polkit-agent.sh ]; then + /usr/bin/setsid -f /home/aag/.config/polybar/scripts/polkit-agent.sh >>"$log_file" 2>&1 + sleep 0.5 + fi + + polkit_agent_running +} + +case "$requested" in + up|down) + action="$requested" + ;; + toggle) + if [ -d "/sys/class/net/$iface" ]; then + action="down" + else + action="up" + fi + ;; + *) + notify "Unknown action: $requested" + exit 2 + ;; +esac + +if [ "$(id -u)" -eq 0 ]; then + auth=() +elif command -v pkexec >/dev/null 2>&1; then + if ! ensure_polkit_agent; then + notify "No polkit agent running" + echo "No polkit authentication agent is running." >"$log_file" + echo "Install/start polkit-gnome, then restart i3 or run ~/.config/polybar/scripts/polkit-agent.sh." >>"$log_file" + exit 1 + fi + auth=(pkexec --disable-internal-agent) +else + notify "pkexec not found" + exit 1 +fi + +if "${auth[@]}" /usr/bin/wg-quick "$action" "$profile" >"$log_file" 2>&1; then + notify "$profile $action complete" +else + notify "$profile $action failed: $log_file" + exit 1 +fi -- cgit v1.2.3