From 12b77f08e52ddb4a60fafa845514d0cbfd190b04 Mon Sep 17 00:00:00 2001 From: Naresh NK <36729469+Noddy20@users.noreply.github.com> Date: Mon, 10 Apr 2023 11:39:18 +0530 Subject: [PATCH] Support for mac OS ventura https://github.com/thecasualcoder/lazy-connect/pull/32 --- lazy-connect | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 2 deletions(-) diff --git a/lazy-connect b/lazy-connect index 81b40b0..c509f3b 100755 --- a/lazy-connect +++ b/lazy-connect @@ -170,8 +170,8 @@ function _lazy_connect_mojave() { tell application "System Events" tell process "System Preferences" tell window 1 + delay 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 @@ -206,6 +206,103 @@ function _lazy_connect_mojave() { EOF } +function _lazy_connect_ventura(){ + vpn_name=$1 + osx_vpn_name="${vpn_name/Connect /}" + + _lazy_connect_get_totp $2 + local autofill=$3 + + if [ -z "$password" ]; then + case $TOTP_MODE in + oathtool) + echo "Error: Unable to generate otp using oathtool." + return 1 + ;; + yubikey) + echo "Error: No YubiKey found." + return 1 + ;; + esac + elif [ "$autofill" == "false" ]; then + echo -n "$password" | pbcopy + fi + + osascript -l JavaScript > /dev/null <<-EOF + function findGroupIndex(vpnName, areas) { + let idx = -1 + for (let i = 0; i< areas.groups.length; i++) { + let g = areas.groups[i] + if (g.staticTexts[0].value() === vpnName) { + idx = i + break + } + } + return idx + } + function connectVpn(vpnName, password, autofill) { + // Reveal System Settings + const app = Application("System Settings") + app.reveal() + const settings = Application("System Events").applicationProcesses.byName("System Settings") + + // Focus on it + settings.frontmost = true + + // Wait for System Settings window appearing + delay(0.5) + + // Reveal VPN panel + settings.menuBars[0].menuBarItems["View"].menus["View"].menuItems["VPN"].click() + + // Wait for panel switching + delay(0.5) + + // check the vpn name + const areas = settings.windows[0].groups[0].splitterGroups[0].groups[1].groups[0].scrollAreas[0] + let idx = findGroupIndex(vpnName, areas) + + let checkbox = areas.groups[idx].checkboxes[0] + if (idx > -1 && checkbox.value() === 0) { + const button = areas.groups[idx].buttons[0] + if (button !== null) { + button.click() + } + + delay(0.5) + + const sheet = settings.windows[0].sheets[0] + const dialogArea = sheet.groups[0].splitterGroups[0].groups[1] + const authArea = dialogArea.scrollAreas[0].groups[2] + + const passwordField = authArea.textFields[1] + passwordField.focused = true + passwordField.value = password + passwordField.focused = false + + try { + let ok = dialogArea.buttons[2] + if (ok.enabled()) { + ok.click() + } else { + throw "Could not OK" + } + } catch (e) { + let cancel = dialogArea.buttons[1] + if (cancel.enabled()) { + cancel.click() + } + } + + delay(0.5) + checkbox.click() + } + app.quit() + } + connectVpn("$osx_vpn_name", "$password", "$autofill") +EOF +} + version_lte() { [ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ] } @@ -261,11 +358,14 @@ function lazy-connect() { fzf --height=10 --ansi --reverse --query "$*" --select-1) mac_version=$(sw_vers -productVersion) + is_less_than_ventura=$(version_lt $mac_version 13.0 && echo "yes" || echo "no") is_less_than_mojave=$(version_lt $mac_version 10.14 && echo "yes" || echo "no") if [ $is_less_than_mojave = "yes" ]; then [ -z "$vpn_name" ] || _lazy_connect "$vpn_name" "$secret" "$autofill" - else + elif [ $is_less_than_ventura = "yes" ]; then [ -z "$vpn_name" ] || _lazy_connect_mojave "$vpn_name" "$secret" "$autofill" + else + [ -z "$vpn_name" ] || _lazy_connect_ventura "$vpn_name" "$secret" "$autofill" fi }