dotfiles/.bin/dexec_vpn
2023-11-28 21:34:09 +01:00

35 lines
814 B
Bash
Executable File

#!/bin/sh
. ~/.bin/_config
LIST="$(cat /etc/wireguard/vpnlist.txt)"
SEL="$(print "$LIST\ndeactivate" | cut -d' ' -f1 | $DMENU_CMD -l 20 -p "VPN")"
[ -z "$SEL" ] && exit 0
if [ "$SEL" == "deactivate" ]
then
ACTIVE=$(ifconfig | grep wg-quick | cut -d: -f3 | tr -d " ")
if [ ! -z "$ACTIVE" ]
then
doas wg-quick down $ACTIVE && $NOTIFY_CMD "VPN: disconnected"
sleep 1
fi
exit 0
fi
HOST="$(echo "$LIST" | fgrep "$SEL" | cut -d' ' -f2)"
cat /etc/wireguard/template.conf | sed "s/XXXXXX/$HOST/g" > /tmp/vpn.conf
doas mv /tmp/vpn.conf /etc/wireguard/vpn.conf
ACTIVE=$(ifconfig | grep wg-quick | cut -d: -f3 | tr -d " ")
if [ ! -z "$ACTIVE" ]
then
doas wg-quick down $ACTIVE && $NOTIFY_CMD "VPN: disconnected"
sleep 1
fi
doas wg-quick up vpn && $NOTIFY_CMD "VPN: $HOST"