Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support to connect or disconnect VPN for >= mohave #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```

Expand Down
108 changes: 68 additions & 40 deletions lazy-connect
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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 "$@"