Skip to content

Commit 4a705a1

Browse files
author
Dinesh Kumar
committed
adding -n flag to autofill the password, and making vars local to functions
1 parent 81ae7b2 commit 4a705a1

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lazy-connect.sh

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ USAGE:
5858
lazy-connect - Shell function to fuzzy search an IPSec VPN by name
5959
and connect to it automatically.
6060
61+
-n - Connect to VPN by autofilling password
6162
-i - Initialize lazy-connect.
6263
Stores the secret and VPN list to ~/.config/lazy-connect/
6364
-u - Update lazy-connect
@@ -67,7 +68,7 @@ EOF
6768
}
6869

6970
function _lazy_connect_get_totp() {
70-
secret_key=$1
71+
local secret_key=$1
7172
case $TOTP_MODE in
7273
oathtool)
7374
password=$(oathtool --totp --base32 $secret_key)
@@ -89,7 +90,7 @@ function _lazy_connect_get_totp() {
8990
}
9091

9192
function _lazy_connect() {
92-
vpn_name=$1
93+
local vpn_name=$1
9394
_lazy_connect_get_totp $2
9495

9596
if [ -z "$password" ]; then
@@ -105,7 +106,11 @@ function _lazy_connect() {
105106
esac
106107
fi
107108

108-
[[ "$LAZY_CONNECT_AUTOFILL" -eq "true" ]] && echo -n "$password" | pbcopy
109+
if [ "$3" = "true" ]; then
110+
local _lazy_connect_auto_fill_pwd="true"
111+
else
112+
echo "$password" | pbcopy
113+
fi
109114

110115
osascript <<EOF
111116
on connectVpn(vpnName, password, autofill)
@@ -130,7 +135,7 @@ function _lazy_connect() {
130135
end tell
131136
end connectVpn
132137
133-
connectVpn("$vpn_name", "$password", "$LAZY_CONNECT_AUTOFILL")
138+
connectVpn("$vpn_name", "$password", "$_lazy_connect_auto_fill_pwd")
134139
EOF
135140
}
136141

@@ -144,7 +149,7 @@ function lazy-connect() {
144149
local OPTIND
145150
mkdir -p $_lazy_connect_config_dir
146151

147-
while getopts "iruh" opt; do
152+
while getopts "iruhn" opt; do
148153
case $opt in
149154
h)
150155
_lazy_connect_usage
@@ -163,6 +168,12 @@ function lazy-connect() {
163168
_lazy_connect_update
164169
return 0
165170
;;
171+
n)
172+
local _secret=$(cat $_lazy_connect_config_dir/secret)
173+
local _vpn_name=$(cat $_lazy_connect_config_dir/vpns | fzf --height=10 --ansi --reverse --select-1)
174+
_lazy_connect "$_vpn_name" "$_secret" true
175+
return 0
176+
;;
166177
\?)
167178
echo "Invalid Option: -$OPTARG."
168179
_lazy_connect_usage

0 commit comments

Comments
 (0)