diff options
Diffstat (limited to 'keyboard/vpn')
| -rwxr-xr-x | keyboard/vpn | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/keyboard/vpn b/keyboard/vpn new file mode 100755 index 0000000..ca60942 --- /dev/null +++ b/keyboard/vpn @@ -0,0 +1,39 @@ +#!/bin/sh +set -e + +die() { + ec=$1 + [ -n "$1" ] && shift + [ -n "$1" ] && notify-send "$@" + exit $ec +} + +command -v systemctl > /dev/null || die 1 "VPN Error" "this script is designed for systemd based systems" + +vpnunits() { + # TODO currently this only lists wg-quick-* which is generated on nixos + for f in $(systemctl list-unit-files 'wg-quick-*.service' --no-legend | cut -d' ' -f1); do + systemctl show "$f" -p Id -p Description -p ActiveState --value | paste -sd '\t' - + done | sed 's/\.service//;s/\t/ (/;s/\t/) (/;s/$/)/' +} + +# get unit +s="$(vpnunits | dmenu -l 8 -p "Toggle VPN")" +[ -z "$s" ] && die +unit=$(cut -d' ' -f1 <<< "$s") + +# check if unit exits +systemctl cat "$unit" > /dev/null || die 1 "VPN Error" "Unit $unit.service does not exist" + +# toggle state +if [ "$(systemctl show "$unit" -p ActiveState --value)" = "active" ]; then + [ "$(printf '%s\n' 'yes' 'no' | dmenu -p "Disable VPN $unit?")" != "yes" ] && die # confirm + + pkexec systemctl stop "$unit" \ + && die 0 "VPN Disabled" "$unit stopped successfully" \ + || die 1 "VPN Error" "$unit threw an error when stopping" +else + pkexec systemctl start "$unit" \ + && die 0 "VPN Enabled" "$unit started successfully" \ + || die 1 "VPN Error" "$unit threw an error when starting" +fi |
