blob: ca60942daaa714d5f21985493588aed806b35341 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
|