dotfiles/.local/bin/vpn
2025-09-02 05:44:14 +00:00

39 lines
753 B
Bash
Executable File

#!/bin/bash -e
DMENU=dmenu
SUDO=pkexec
if tty -s; then
DMENU=fzf
SUDO=sudo
fi
if [ "$EUID" = 0 ]; then
SUDO=
fi
d="$(mktemp -d)"
# shellcheck disable=SC2064
trap "rm -rf '$d'" EXIT
touch "$d/"{1..2}
CMD=()
while read -r i; do
CMD+=("wg-quick down '$i'")
done < <(ip -br link show type wireguard | awk '{print $1}')
if [ "$1" != off ]; then
CMD=(
"basename -s.conf '/etc/wireguard/$1'*.conf > '$d/1'"
"inotifywait -e close_write '$d/2'"
"up=\"\$(cat '$d/2')\""
"[ -z \"\$up\" ] && exit"
"${CMD[@]}"
"wg-quick up \"\$up\""
)
fi
(inotifywait -e close_write "$d/1"; "$DMENU" < "$d/1" > "$d/2") &
$SUDO bash -c "${CMD[*]/%/;}"
touch "${XDG_RUNTIME_DIR:-/run/user/1000}/sbar/vpn"