summaryrefslogtreecommitdiffstats
path: root/keyboard
diff options
context:
space:
mode:
authorTim Keller <tjk@tjkeller.xyz>2026-10-05 14:58:51 -0400
committerTim Keller <tjk@tjkeller.xyz>2026-10-05 14:58:51 -0400
commiteebf8dda5f940bff97a0a06ac3fa06d6a710355a (patch)
treef30dca9e9850a12aac09786f881f27fb56128306 /keyboard
parentff2d831980ee777ed85a0309e08d0b6fad3a6ef7 (diff)
downloadscripts-eebf8dda5f940bff97a0a06ac3fa06d6a710355a.tar.xz
scripts-eebf8dda5f940bff97a0a06ac3fa06d6a710355a.zip
add vpn script to start vpn units with systemd
Diffstat (limited to 'keyboard')
-rwxr-xr-xkeyboard/vpn39
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