#!/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