diff --git a/README.md b/README.md index f9fb84b..0bb7f6e 100644 --- a/README.md +++ b/README.md @@ -31,11 +31,12 @@ sudo ln -s ~/.lazy-connect/lazy-connect /usr/local/bin/lazy-connect ``` lazy-connect - Shell function to fuzzy search an IPSec VPN by name - and connect to it automatically. + and connect/disconnect to it automatically. -i - Initialize lazy-connect. Stores the TOTP secret and VPN list. -r - Refresh vpn list in ~/.config/lazy-connect . -n - Do not fill the password automatically. Instead copy the password to clipboard. +-y - Connect/disconnect automatically to the last connected VPN without prompt. -h - Show this help. ``` diff --git a/lazy-connect b/lazy-connect index 81b40b0..8733516 100755 --- a/lazy-connect +++ b/lazy-connect @@ -45,7 +45,7 @@ function _lazy_connect_vpn_refresh() { end tell end tell EOF - tr ',' '\n' | sed 's/^[[:space:]]//g' >$_lazy_connect_config_dir/vpns + tr ',' '\n' | sed 's/^[[:space:]]//g' | cut -d' ' -f2- >$_lazy_connect_config_dir/vpns echo "Storing the VPN list..." if [ -f $backup_file ]; then @@ -66,6 +66,7 @@ lazy-connect - Shell function to fuzzy search an IPSec VPN by name -i - Initialize lazy-connect. Stores the TOTP secret and VPN list. -r - Refresh vpn list in ~/.config/lazy-connect . +-y - Select the last selected VPN. -n - Do not fill the password automatically. Instead copy the password to clipboard. -h - Show this help. EOF @@ -134,14 +135,15 @@ function _lazy_connect() { end tell end tell end connectVpn - connectVpn("$vpn_name", "$password", "$autofill") EOF } function _lazy_connect_mojave() { vpn_name=$1 - osx_vpn_name="${vpn_name/Connect /}, Not Connected" + osx_vpn_name_not_connected="${vpn_name/Connect /}, Not Connected" + osx_vpn_name_connected="${vpn_name/Connect /}, Connected" + osx_vpn_name="${vpn_name/Connect /}" _lazy_connect_get_totp $2 local autofill=$3 @@ -162,47 +164,52 @@ function _lazy_connect_mojave() { fi osascript <<EOF - on connectVpn(vpnName, password, autofill) + on connectOrDisconnectVpn(vpnName, disconnectedVpnName, connectedVpnName, password) tell application "System Preferences" reveal pane "Network" activate - tell application "System Events" - tell process "System Preferences" - tell window 1 - repeat with r in rows of table 1 of scroll area 1 - - if (value of attribute "AXValue" of static text 1 of r as string) is equal to "$osx_vpn_name" then - select r - end if - end repeat - - tell group 1 - click button "Authentication Settingsā¦" - end tell - - tell sheet 1 - set focused of text field 2 to true - set value of text field 2 to "$password" - click button "Ok" - end tell - - click button "Apply" - - delay 1 - tell group 1 - click button "Connect" - end tell - + tell process "System Preferences" + tell window 1 + repeat with r in rows of table 1 of scroll area 1 + if (value of attribute "AXValue" of static text 1 of r as string) contains vpnName then + select r + exit repeat + end if + end repeat + if (value of attribute "AXValue" of static text 1 of r as string) is equal to disconnectedVpnName then + log "Connecting to vpn " & vpnName + tell group 1 + click button "Authentication Settingsā¦" + end tell + tell sheet 1 + set focused of text field 2 to true + set value of text field 2 to password + click button "Ok" end tell + click button "Apply" + delay 1 + tell group 1 + click button "Connect" + end tell + log "Connected" + else if (value of attribute "AXValue" of static text 1 of r as string) is equal to connectedVpnName then + log "Disconnecting from vpn " & vpnName + tell group 1 + click button "Disconnect" + end tell + log "Disconnected" + else + log "Couldn't connect or disconnect the VPN, didn't find any exact match. The network found was: " + log (value of attribute "AXValue" of static text 1 of r as string) + end if end tell + end tell end tell - quit - end tell - end connectVpn - - connectVpn("$osx_vpn_name", "$password", "$autofill") + end tell + end connectOrDisconnectVpn + connectOrDisconnectVpn("$osx_vpn_name", "$osx_vpn_name_not_connected", "$osx_vpn_name_connected", "$password") EOF } @@ -214,12 +221,24 @@ version_lt() { [ "$1" = "$2" ] && return 1 || version_lte $1 $2 } +_get_last_selected_vpn() { + if [ -f $_lazy_connect_config_dir/last_vpn ]; then + last_vpn=$(cat $_lazy_connect_config_dir/last_vpn) + fi + if [ -z "$last_vpn" ]; then + echo "0" + else + echo $last_vpn + fi +} + function lazy-connect() { local OPTIND local autofill="true" + local choose_last="false" mkdir -p $_lazy_connect_config_dir - while getopts "irnh" opt; do + while getopts "irnhy" opt; do case $opt in h) _lazy_connect_usage @@ -238,6 +257,10 @@ function lazy-connect() { autofill="false" shift $((OPTIND - 1)) ;; + y) + choose_last="true" + shift $((OPTIND - 1)) + ;; \?) echo "Invalid Option: -$OPTARG." _lazy_connect_usage @@ -257,8 +280,13 @@ function lazy-connect() { return 1 fi - vpn_name=$(cat $_lazy_connect_config_dir/vpns | - fzf --height=10 --ansi --reverse --query "$*" --select-1) + last_vpn=$(_get_last_selected_vpn) + if [ "true" == "$choose_last" -a "0" != "$last_vpn" ]; then + vpn_name=$last_vpn + else + vpn_name=$(cat $_lazy_connect_config_dir/vpns | + fzf --height=10 --ansi --reverse --query "$*" --select-1) + fi mac_version=$(sw_vers -productVersion) is_less_than_mojave=$(version_lt $mac_version 10.14 && echo "yes" || echo "no") @@ -267,7 +295,7 @@ function lazy-connect() { else [ -z "$vpn_name" ] || _lazy_connect_mojave "$vpn_name" "$secret" "$autofill" fi - + echo -n $vpn_name > $_lazy_connect_config_dir/last_vpn } lazy-connect "$@"