#!/bin/sh # toggle VPN down_not_mydefault () { for v in wg_studio wg_ia wg_aa wg_ca; do nmcli connection down "$v" done } wg_aa_restore_default () { # case seems faster than pipe to grep maybe because case is shell built-in, so I use case # if nmcli -t --fields device,state device | grep -q '^wg_aa:connected$'; then ...; fi case "$(nmcli -t --fields device,state device)" in # stop aa wireguard+swgp server that listen from insp wiregurad+swgp client, start aa wg+swgp connect to ca # use /usr/bin/alacritty instead of /home/xyz/.local/bin/alacritty so script can wait until alacritty exit to run other codes following *wg_aa:connected*) /usr/bin/alacritty -T aa --hold -e sh -c 'echo "Wait for aa sudo prompt and enter password, then manully close this window:"; ssh -t aa.flylightning.xyz "sudo systemctl stop wg-quick@wg0 && sudo systemctl start wg-quick@wg_ca"';; esac } # no toggle wg_mydefault, I want wg_mydefault to always on by default if ! [ "$1" ] || [ "$1" = mydefault ]; then wg_aa_restore_default down_not_mydefault nmcli connection up wg_mydefault notify-send -u critical 'VPN disabled' exit fi if nmcli -t --fields device,state device | grep -q "^wg_$1:connected$"; then wg_aa_restore_default down_not_mydefault nmcli connection up wg_mydefault notify-send -u critical "$1 VPN disabled" else if [ "$1" = aa ]; then /usr/bin/alacritty -T aa --hold -e sh -c 'echo "Wait for aa sudo prompt and enter password, then manully close this window"; ssh -t aa.flylightning.xyz "sudo systemctl stop wg-quick@wg_ca && sudo systemctl start wg-quick@wg0"' fi down_not_mydefault nmcli connection down wg_mydefault nmcli connection up "wg_$1" notify-send "$1 VPN enabled" fi