-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpnconnect.sh
executable file
·154 lines (140 loc) · 4.88 KB
/
vpnconnect.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env zsh
# Copyright (c) 2023 Gijs Keij
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# based on the sample tunnelblick OSA scripts
# Temporary file to hold the previous VPN connection state
TEMP_FILE="$TMPDIR/previous_vpn.txt"
# Temporary file to hold the last established VPN connection state
LAST_VPN_FILE="$TMPDIR/current_vpn.txt"
# Desired VPN profile for connection
VPN_PROFILE="$HOME/.config/vpnconnect/vpn_profile.txt"
# Function to set a VPN profile for connection
set_vpn_profile() {
vpn_name=$1
if [[ -n "$vpn_name" ]]; then
echo "$vpn_name" >"$VPN_PROFILE"
echo "VPN profile has been set to: $vpn_name"
elif [[ -f "$VPN_PROFILE" && -n "$(cat "$VPN_PROFILE")" ]]; then
echo "Current VPN profile is: $(cat "$VPN_PROFILE")"
else
echo "No VPN profile set. Choose one from the list of available VPN profiles:"
available_profiles=$(osascript -e 'tell application "Tunnelblick"' -e 'get name of configurations' -e 'end tell')
echo "$available_profiles"
echo "Enter the name of the desired VPN profile:"
read vpn_name
echo "$vpn_name" >"$VPN_PROFILE"
echo "VPN profile has been set to: $vpn_name"
fi
}
# Function to get a VPN profile for connection
get_vpn_profile() {
if [[ -n $1 ]]; then
echo "$1"
elif [[ -f "$VPN_PROFILE" ]]; then
cat "$VPN_PROFILE"
else
echo "No VPN profile set. Choose one from the list of available VPN profiles:"
available_profiles=$(osascript -e 'tell application "Tunnelblick"' -e 'get name of configurations' -e 'end tell')
echo "$available_profiles"
echo "Enter the name of the desired VPN profile:"
read vpn_name
echo "$vpn_name" >"$VPN_PROFILE"
cat "$VPN_PROFILE"
fi
}
# Function to clear a VPN profile for connection
clear_vpn_profile() {
if [[ -f "$VPN_PROFILE" ]]; then
rm "$VPN_PROFILE"
echo "VPN profile has been cleared."
else
echo "No VPN profile is set."
fi
}
# Function to check if there are any active OPENVPN connections
check_active_connections() {
active_conn=$(osascript -e 'tell application "Tunnelblick"' -e 'get name of configurations where state is not "EXITING"' -e 'end tell')
if [[ -z "$active_conn" ]]; then
return 1
echo "$active_conn maar voor de lol"
else
echo "$active_conn"
return 0
fi
}
# Function to disconnect from an OPENVPN connection
openvpn_disconnect() {
vpn_name=$1
osascript -e 'tell application "Tunnelblick"' -e "disconnect \"$vpn_name\"" -e 'end tell'
echo "Disconnecting from $vpn_name ..."
# Wait for the connection to be disconnected
while is_vpn_connected "$vpn_name"; do
echo "Waiting for $vpn_name to disconnect..."
sleep 2
done
echo "Disconnected from $vpn_name."
}
# Function to connect to an OPENVPN connection
openvpn_connect() {
vpn_name=$1
osascript -e 'tell application "Tunnelblick"' -e "connect \"$vpn_name\"" -e 'end tell'
echo "$vpn_name" >"$LAST_VPN_FILE"
}
# Function to check if a specific VPN is connected
is_vpn_connected() {
vpn_name=$1
active_conn=$(osascript -e 'tell application "Tunnelblick"' -e "get state of first configuration where name = \"$vpn_name\"" -e 'end tell')
[[ "$active_conn" != "EXITING" ]]
}
# Configure the VPN profile
if [[ $1 == "config" ]]; then
set_vpn_profile "$2"
# Open the VPN session
elif [[ $1 == "open" ]]; then
vpn_profile=$(get_vpn_profile "$2")
active_connections=$(check_active_connections)
if [[ -n "$active_connections" ]]; then
IFS=', ' read -ra active_vpn_array <<<"$active_connections"
for active_vpn in "${active_vpn_array[@]}"; do
openvpn_disconnect "$active_vpn"
done
fi
openvpn_connect "$vpn_profile"
exit 0
# Close the VPN session
elif [[ $1 == "close" ]]; then
vpn_profile=$(get_vpn_profile "$2")
if [[ -f "$LAST_VPN_FILE" ]]; then
last_vpn=$(cat "$LAST_VPN_FILE")
if [[ "$last_vpn" == "$vpn_profile" ]]; then
openvpn_disconnect "$last_vpn"
rm "$LAST_VPN_FILE"
echo "VPN to $last_vpn is Disconnected"
else
echo "$vpn_profile is not currently connected."
exit 1
fi
else
echo "No VPN is connected, so I can't disconnect"
exit 1
fi
if [[ -f "$TEMP_FILE" ]]; then
prev_vpn=$(cat "$TEMP_FILE")
IFS=", " read -rA PREV_VPNS <<<"$prev_vpn"
for vpn in "${PREV_VPNS[@]}"; do
openvpn_connect "$vpn"
done
rm "$TEMP_FILE"
exit 0
else
echo "Not reconnecting previous connection as no previous VPN connection found."
exit 0
fi
# Clear the VPN profile
elif [[ $1 == "clear" ]]; then
clear_vpn_profile
else
echo "Invalid parameter. Use 'config', 'open', 'close' or 'clear'."
fi