dotfiles/.local/bin/scripts/vpn
2025-05-05 20:56:59 +02:00

34 lines
1.2 KiB
Bash
Executable file

#!/bin/sh -e
test -f "$1" || cd "$XDG_DATA_HOME/openvpn"
case "$1" in
("") tmux new-session -s "vpn" "tmux ls; $SHELL";;
(forti)
if ip route | grep -q ppp0
then printf "Device ppp0 already in use - press Enter to kill"
read -r _
sudo killall openfortivpn
fi
sudo tmux new-session -s "$1" '
set -x
route="$(ip route | grep default | sort | head -1)"
# Start openfortivpn in the background
openfortivpn &
VPN_PID=$!
# Wait a little for the VPN to establish the connection
sleep 5
# TODO Loop
# Delete the default route set by openfortivpn (ppp0)
ip route del default dev ppp0
# Add the default route via the local network
eval "ip route add $route"
ip route add 134.245.0.0/16 dev ppp0;
ip route
wait $VPN_PID
echo "VPN has disconnected."
ip dev del ppp0;
'
;;
(*) test -f "$1" || file=$(find $1* -name '*.ovpn')
cd "$(dirname "$file")" && file="$(basename "$file")"
tmux new-session -s "$@" "sudo openvpn ${file:-$@} || read"
esac