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

Disconnect All Connected VPNS #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions click_vpn.scpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env osascript

on click(name)
log "selection: " & {name}
tell application "System Events"
tell process "SystemUIServer"
set vpnMenu to (menu bar item 1 of menu bar 1 where description is "VPN")
tell vpnMenu to click
try
click menu item name of menu 1 of vpnMenu
on error errorStr
return "false"
log "error: " & errorStr
end try
end tell
end tell
return "true"
end click

on run argv
set arg to (item 1 of argv)
set res to click(arg)
if res equal to "false" then
-- ESCAPE KEY
tell application "System Events" to key code 53
end if
end run
33 changes: 33 additions & 0 deletions connected_vpns.scpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env osascript

-- could replace refresh with this, passing regex
on getConnectedVpns()
log "search for connected vpns"
tell application "System Events"
tell process "SystemUIServer"
set vpnMenu to (menu bar item 1 of menu bar 1 where description is "VPN")
tell vpnMenu to click
set vpnMenuItems to (menu items of menu 1 of vpnMenu)
set connectedVpns to {}
repeat with vpnMenuItem in vpnMenuItems
set vpnName to name of vpnMenuItem
if vpnName starts with "Disconnect" then
set connectedVpns to connectedVpns & {vpnName}
else if vpnName starts with "Connect" then
exit repeat
end if
end repeat
get connectedVpns
end tell
end tell
end getConnectedVpns

on run argv
set res to getConnectedVpns()
if res equal to {} then
log "No vpn is connected."
-- ESCAPE
end if
tell application "System Events" to key code 53
return res
end run
27 changes: 22 additions & 5 deletions lazy-connect.sh
Original file line number Diff line number Diff line change
@@ -88,6 +88,13 @@ function _lazy_connect_get_totp() {
esac
}

function _lazy_disconnect_all(){
echo "Disconnecting..."
connected_vpns=$(osascript ${_lazy_connect_project_dir}/connected_vpns.scpt)
vpns=$(echo $connected_vpns | tr ',' '\n' | sed 's/^[[:space:]]//g')
echo -n $vpns | xargs -L 1 -I {} /bin/bash -c "osascript ${_lazy_connect_project_dir}/click_vpn.scpt '{}'"
}

function _lazy_connect() {
vpn_name=$1
_lazy_connect_get_totp $2
@@ -105,17 +112,22 @@ function _lazy_connect() {
esac
fi

[[ "$MANUAL_MODE" -eq "true" ]] && echo -n "$password" | pbcopy

osascript <<EOF
on connectVpn(vpnName, password)
on connectVpn(vpnName, password, manual)
tell application "System Events"
tell process "SystemUIServer"
set vpnMenu to (menu bar item 1 of menu bar 1 where description is "VPN")
tell vpnMenu to click
try
click menu item vpnName of menu 1 of vpnMenu
delay 1
keystroke password
keystroke return
if (manual is not equal to "true")
keystroke $password
keystroke return
end if
return "true"
on error errorStr
if errorStr does not contain "Can’t get menu item" and errorStr does not contain vpnName then
display dialog errorStr
@@ -125,7 +137,7 @@ function _lazy_connect() {
end tell
end connectVpn

connectVpn("$vpn_name", "$password")
connectVpn("$vpn_name", "$password", "$MANUAL_MODE")
EOF
}

@@ -139,7 +151,7 @@ function lazy-connect() {
local OPTIND
mkdir -p $_lazy_connect_config_dir

while getopts "iruh" opt; do
while getopts "iqruh" opt; do
case $opt in
h)
_lazy_connect_usage
@@ -158,6 +170,11 @@ function lazy-connect() {
_lazy_connect_update
return 0
;;
q)
_lazy_disconnect_all
return 0
;;

\?)
echo "Invalid Option: -$OPTARG."
_lazy_connect_usage