forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpnutil-toggle.sh
executable file
·70 lines (57 loc) · 1.68 KB
/
vpnutil-toggle.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
# Based on https://github.com/raycast/script-commands/tree/master/commands/system/vpn
# Dependency: vpnutil https://github.com/Timac/VPNStatus/releases/latest
# Installation:
# 1. Download vpnutil.zip
# 2. Uncompress
# 3. Move vpnutil to /usr/local/bin
# 4. Run manually once to bypass GateKeeper protection:
# - Right-click the app in the Finder
# - Select Open
# - Click on the Open button
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Toggle VPN
# @raycast.mode compact
# Optional parameters:
# @raycast.icon 📡
# @Documentation:
# @raycast.packageName VPN
# @raycast.description Toggle VPN connection
# @raycast.author Sergey Fuksman
# @raycast.authorURL https://github.com/fuksman
CONFIG=vpnutil-config.sh
if [ ! -f $CONFIG ]; then
echo "Create a vpn-config.sh from template!"
exit 1
fi
# shellcheck source=/dev/null
source $CONFIG
VPN=$VPN_NAME
# Source: https://superuser.com/a/736859
function isnt_connected () {
vpnutil status "$VPN" | sed -n 1p | grep -qv Connected
}
function poll_until_connected () {
(( loops=0 ))
(( max_loops=200 )) # 200 * 0.1 is 20 seconds. Bash doesn't support floats
while isnt_connected "$VPN"; do
sleep 0.1 # can't use a variable here, bash doesn't have floats
(( loops=loops+1 ))
[ "$loops" -gt "$max_loops" ] && break
done
[ "$loops" -le "$max_loops" ]
}
if isnt_connected "$VPN"; then
vpnutil start "$VPN"
if poll_until_connected "$VPN"; then
echo "Connected to $VPN!"
else
echo "Couldn't connect to $VPN"
vpnutil stop "$VPN"
exit 1
fi
else
vpnutil stop "$VPN"
echo "Disconnected from $VPN"
fi