always-on-openconnect-vpn/bitbar-openconnect.6s.sh

97 lines
3.1 KiB
Bash
Raw Normal View History

2023-07-06 06:44:38 +00:00
#!/bin/bash
# Get current status of a VPN connection with options to connect/disconnect.
# Working with OpenConnect, but can work with any executable VPN. Commands
# that require admin permissions should be whitelisted with 'visudo', e.g.:
#
#joesmith ALL=(ALL) NOPASSWD: /path/to/always-on-openconnect-vpn/run-vpn.sh
#joesmith ALL=(ALL) NOPASSWD: /usr/bin/killall -2 openconnect
# <xbar.title>VPN Status</xbar.title>
# <xbar.version>v1.0</xbar.version>
# <xbar.author>Jesse Jarzynka</xbar.author>
# <xbar.author.github>jessejoe</xbar.author.github>
# <xbar.desc>Displays status of a VPN interface with option to connect/disconnect.</xbar.desc>
# <xbar.image>http://i.imgur.com/RkmptwO.png</xbar.image>
2023-08-22 11:53:37 +00:00
# <swiftbar.hideAbout>true</swiftbar.hideAbout>
# <swiftbar.hideRunInTerminal>true</swiftbar.hideRunInTerminal>
# <swiftbar.hideLastUpdated>true</swiftbar.hideLastUpdated>
# <swiftbar.hideDisablePlugin>true</swiftbar.hideDisablePlugin>
# <swiftbar.hideSwiftBar>false</swiftbar.hideSwiftBar>
2023-07-06 06:44:38 +00:00
2023-06-16 00:35:15 +00:00
VPN_CONNECTED=""
if [[ "$OSTYPE" == "darwin"* ]]; then
2024-01-12 14:35:21 +00:00
VPN_CONNECTED="ifconfig | egrep -A1 utun | grep 'inet 10.140.'"
2023-06-16 00:35:15 +00:00
fi
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
VPN_CONNECTED="ifconfig | grep -E -A1 tun | grep inet"
fi
2023-07-06 06:44:38 +00:00
function notify(){
if [[ "$OSTYPE" == "darwin"* ]]; then
terminal-notifier -title "VPN" -message "${1}" -sender "SwiftBar"
fi
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
notify-send "VPN" "${1}"
fi
}
# Get location to this script from symlink
SCRIPT_LOCATION=$(dirname $([ -L $0 ] && readlink -f $0 || echo $0))
2023-08-22 11:53:37 +00:00
SCRIPT_NAME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
2023-07-06 06:44:38 +00:00
LOG_FILE=/tmp/vpn.log
case "$1" in
connect)
notify "Connecting..."
cd $SCRIPT_LOCATION
2023-06-16 00:35:15 +00:00
if [[ "$OSTYPE" == "darwin"* ]]; then
sudo $SCRIPT_LOCATION/run-vpn.sh &> $LOG_FILE &
fi
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# For Linux desktop use DBUS to use keychain
export DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS
sudo --preserve-env $SCRIPT_LOCATION/run-vpn.sh &> $LOG_FILE &
fi
2023-07-06 06:44:38 +00:00
until eval "$VPN_CONNECTED"; do sleep 1; done
notify "Connected!"
;;
disconnect)
eval "sudo killall -2 openconnect"
until [ -z "$(eval "$VPN_CONNECTED")" ]; do sleep 1; done
notify "Disconnected"
;;
2023-08-22 11:53:37 +00:00
logs)
tail -n 200 -f $LOG_FILE
exit
;;
2023-07-06 06:44:38 +00:00
esac
if [ -n "$(eval "$VPN_CONNECTED")" ]; then
echo "VPN ✔"
echo '---'
echo "Disconnect VPN | bash='$0' param1=disconnect terminal=false refresh=true"
else
echo "VPN ✘"
echo '---'
echo "Connect VPN | bash='$0' param1=connect terminal=false refresh=true"
fi
echo '---'
echo "Edit routes | iconName=folder-symbolic href='file://$SCRIPT_LOCATION/routes.txt' refresh=false"
2023-12-14 14:57:39 +00:00
echo "Edit domains | iconName=accessories-dictionary-symbolic href='file://$SCRIPT_LOCATION/domains.txt' refresh=false"
2023-08-22 11:53:37 +00:00
LOG_CMD="$SCRIPT_LOCATION/$SCRIPT_NAME"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
LOG_CMD="$LOG_CMD logs"
fi
echo "Tail VPN log file | bash='$LOG_CMD' params='logs' terminal=true refresh=false"
2023-07-06 06:44:38 +00:00
exit