-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpn
executable file
·227 lines (197 loc) · 6.22 KB
/
vpn
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/bin/bash
# nmcli-vpn 3.0.0
# An nmcli wrapper for managing VPN connections and .ovpn configuration files
# https://github.com/chrisdenman/nmcli-vpn
# Commands:
#
# vpn config list - List all known .ovpn configuration files
# vpn config update - Updates the .ovpn configuration files
# vpn connection import FILES... - Import connections from the specified .ovpn configuration files
# vpn connection delete - Delete all connections
# vpn connection delete NAMES - Delete the named connections (as they appear in nmcli)
# vpn connection list - List all connections
# vpn connection list --active - List active connections
# vpn connection up - Bring up the sole connection else displays a console menu
# vpn connection up NAMES... - Bring up the connections by name (as they appear in nmcli).
# If any name has the value 'random', a random connection will be
# considered.
# vpn connection down - Take down all connections
# vpn connection down NAMES... - Take down the named connection (as they appear in nmcli)
#
### User-Implemented Section
OVPN_CONFIGS_DIR="$HOME"/.ovpn # The location of your .ovpn config files
_on_connection() { # Tasks to perform when a vpn connected
return 0
}
_on_disconnecting() { # Tasks to perform when a vpn is disconnected
return 0
}
_patch_config() { # Post downloading config updates
sed -i '/keysize.*/d' "${OVPN_CONFIGS_DIR}"/*.ovpn
sed -i '/comp-.*/d' "${OVPN_CONFIGS_DIR}"/*.ovpn
}
_update_config() { # Tasks to perform to update the open vpn configuration files
return 0
}
_vpn_un() { # Return the VPN provider's username for the configuration file passed as argument 1
echo "username"
}
_vpn_pwd() { # Return the VPN provider's password for the connection name passed as argument 1
echo "password"
}
ME=$(basename "$0") && readonly ME
### Resource: Config
_configs() {
find "${OVPN_CONFIGS_DIR}" -name "*${OVPN_EXTENSION}" -print0 -type f
}
_configs_basename() {
_configs | xargs -0 -n 1 basename | sort
}
_config_file_basename() {
local configBaseName
configBaseName=$(basename "$1")
configBaseName=${configBaseName%"$OVPN_EXTENSION"}
echo "${configBaseName}"
}
_config_update() {
_update_config
_patch_config
}
_config_list() {
local i
for i in $(find "${OVPN_CONFIGS_DIR}" -name "*${OVPN_EXTENSION}" -type f | sort); do
echo "$i"
done
}
_config() {
case "$1" in
list) shift 1; _config_list;; # list the known configurations and their locations
update) shift 1; _config_update;; # delete, and re-download the .ovpn files, refresh password files
-*) _i_o "$1";;
*) _i_a "$1";;
esac
}
### Resource: Connection
_connection_list() {
local activeOption=""
if [ "$#" -gt 0 ]; then
case "$1" in
-a|--active) activeOption="--active";;
-*) _i_o "$1";;
*) _i_a "$1";;
esac
fi
nmcli --fields NAME,TYPE con show ${activeOption} | grep "vpn" | awk '{print $1}' ORS="${ORS}"
}
_connection_name_from_config_filename() {
_config_file_basename "$1"
}
_connection_import() {
local connectionName configFilePath
for configFilePath in "$@"; do
connectionName=$(_connection_name_from_config_filename "${configFilePath}")
nmcli con import type openvpn file "${configFilePath}"
nmcli con mod "${connectionName}" vpn.user-name "$(_vpn_un "${configFilePath}")"
done
}
_connection_up() {
local connectionName
if [ "$#" -gt 0 ]; then
for connectionName in "$@"; do
if [ "${connectionName}" == "random" ]; then
_connection_up_random
else
_connection_up_named "${connectionName}"
fi
done
else
_connection_up_interactively
fi
}
_connection_up_random() {
local CONNECTION_NAMES NUM INDEX
read -ra CONNECTION_NAMES <<< "$(_connection_list)"
NUM=${#CONNECTION_NAMES[@]}
if [ "$NUM" -gt 0 ]; then
INDEX=$(shuf -i 0-$(("$NUM" - 1)) -n 1)
_connection_up_named "${CONNECTION_NAMES[$INDEX]}"
else
echo "no connections" >&2; exit 1
fi
}
_connection_up_interactively() {
local connection i INDEX MENU
MENU=()
i=0
for connection in $(_connection_list); do
MENU[i]="$((i/2))"
MENU[i+1]="${connection}"
((i+=2))
done
case "$i" in
0) ;;
2) _connection_up_named "$(_connection_list)";;
*)
INDEX=$(whiptail --menu "Connections" \
$(($(tput lines) - 0)) \
$(($(tput cols) - 0)) \
$(($(tput lines) - 5)) \
"${MENU[@]}" 2>&1 >/dev/tty)
# shellcheck disable=SC2181
[[ $? == 0 ]] && _connection_up_named "${MENU[$INDEX * 2 + 1]}"
;;
esac
}
_connection_up_named() {
local OVPN_SECRETS_FILE
OVPN_SECRETS_FILE=$(mktemp)
echo "vpn.secrets.password:$(_vpn_pwd "$1")" > "${OVPN_SECRETS_FILE}"
chmod u+r "${OVPN_SECRETS_FILE}"
nmcli con up "$1" passwd-file "${OVPN_SECRETS_FILE}" && _on_connection "$1"
if ! rm -f "${OVPN_SECRETS_FILE}"; then
echo "Critical failure to delete temporary credentials file ${OVPN_SECRETS_FILE}" >&2; exit 1
fi
}
_connection_going() {
_connection_list active | grep -qo "\b$1\b" && _on_disconnecting; nmcli con "$2" "$1"
}
_connection_down() {
local connectionName args
if [ $# == 0 ]; then args="$(_connection_list --active)"; else args=$*; fi
for connectionName in ${args}; do _connection_going "$connectionName" "down"; done
}
_connection_delete() {
local connectionName args
if [ $# == 0 ]; then args="$(_connection_list)"; else args=$*; fi
for connectionName in ${args}; do _connection_going "$connectionName" "delete"; done
}
_connection() {
case "$1" in
import) shift 1; _connection_import "$@";;
delete) shift 1; _connection_delete "$@";;
list) shift 1; ORS="\n"; _connection_list "$@";ORS=" ";;
up) shift 1; _connection_up "$@";;
down) shift 1; _connection_down "$@";;
-*) _i_o "$1";;
*) _i_a "$1";;
esac
}
_i_o() {
echo "${ME}: invalid option -- '$1'"
exit 1
}
_i_a() {
echo "${ME}: invalid argument -- '$1'"
exit 1
}
### Entrypoint
OVPN_EXTENSION=".ovpn"
ORS=' '
while [ "$#" -gt 0 ]; do
case "$1" in
config) _config "$2"; break;;
connection) shift 1; _connection "$@"; break;;
-*) _i_o "$1";;
*) _i_a "$1";;
esac
done