From fa9fe33df76c3ce17648cbe3d562af21bf000197 Mon Sep 17 00:00:00 2001
From: Dinesh Kumar <dinesh.kumar@go-jek.com>
Date: Sat, 13 Oct 2018 13:37:42 +0530
Subject: [PATCH 1/4] Enable manual mode for entering password

---
 lazy-connect.sh | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/lazy-connect.sh b/lazy-connect.sh
index e1d8531..cb4ad37 100755
--- a/lazy-connect.sh
+++ b/lazy-connect.sh
@@ -105,8 +105,10 @@ 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")
@@ -114,8 +116,11 @@ function _lazy_connect() {
           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 +130,7 @@ function _lazy_connect() {
       end tell
     end connectVpn
 
-    connectVpn("$vpn_name", "$password")
+    connectVpn("$vpn_name", "$password", "$MANUAL_MODE")
 EOF
 }
 

From 4b5a74998ce665f09f98f9647c98716391414d9f Mon Sep 17 00:00:00 2001
From: Dinesh Kumar <dinesh.kumar@go-jek.com>
Date: Sun, 14 Oct 2018 14:18:32 +0530
Subject: [PATCH 2/4] osascript to clik given vpn

---
 click_vpn.scpt | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
 create mode 100644 click_vpn.scpt

diff --git a/click_vpn.scpt b/click_vpn.scpt
new file mode 100644
index 0000000..aa3f8f0
--- /dev/null
+++ b/click_vpn.scpt
@@ -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

From a3107f1b9c27136b41f376b5168208d73f28214a Mon Sep 17 00:00:00 2001
From: Dinesh Kumar <dinesh.kumar@go-jek.com>
Date: Sun, 14 Oct 2018 14:18:48 +0530
Subject: [PATCH 3/4] osascript to get connected vpns

---
 connected_vpns.scpt | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 connected_vpns.scpt

diff --git a/connected_vpns.scpt b/connected_vpns.scpt
new file mode 100644
index 0000000..c14fcc4
--- /dev/null
+++ b/connected_vpns.scpt
@@ -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

From 64e5a5d50a729fa14f95ac79785b7adac2c8e337 Mon Sep 17 00:00:00 2001
From: Dinesh Kumar <dinesh.kumar@go-jek.com>
Date: Sun, 14 Oct 2018 14:19:08 +0530
Subject: [PATCH 4/4] disconnect all connected vpns

---
 lazy-connect.sh | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/lazy-connect.sh b/lazy-connect.sh
index cb4ad37..82fccff 100755
--- a/lazy-connect.sh
+++ b/lazy-connect.sh
@@ -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
@@ -144,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
@@ -163,6 +170,11 @@ function lazy-connect() {
         _lazy_connect_update
         return 0
         ;;
+      q)
+        _lazy_disconnect_all
+        return 0
+        ;;
+
       \?)
         echo "Invalid Option: -$OPTARG."
         _lazy_connect_usage