-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautovpn
executable file
·47 lines (42 loc) · 1.51 KB
/
autovpn
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
#!/bin/bash
# check for properties file
if [ -f "${PROPS_FILE}" ] ; then
source ${PROPS_FILE}
fi
# replace empty values with defaults
default_filename_pattern='^.+\.ovpn$'
filename_pattern=${filename_pattern:-$default_filename_pattern}
default_exclude_files=".*\.crdownload|\.org\.chromium\.Chromium\..*"
exclude_files="${exclude_files:-$default_exclude_files}"
default_download_dir="${USER_HOME}/Downloads"
download_dir="${download_dir:-$default_download_dir}"
# Watch the Downloads directory for new VPN config files
inotifywait -mqe create --exclude "${exclude_files}" "${default_download_dir}" |
while read path event file
do
# Check if the file looks like VPN config we want
if [ "${event}" = "CREATE" ] && [[ "${file}" =~ ${filename_pattern} ]]
then
# Get the VPN name
if [ -n "${vpn_name_suffix}" ] ; then
vpn=`echo ${file} | sed -E "s/${vpn_name_suffix}//"`
fi
vpn=${vpn:-$file}
# Check if an old connection already exists
cons=`pgrep --list-full openvpn | grep "${vpn}" | awk '{print $1}'`
if [ -n "${cons}" ]
then
echo "Closing old connection: '${vpn}'..."
kill ${cons}
fi
# Connect to the VPN
echo "Connecting to: '${vpn}'..."
openvpn --config "${path}${file}" &
# Wait a mo then remove the VPN config file
if [ "${auto_remove}" != "no" ] || [ "${auto_remove}" != "false" ] ; then
sleep 2
echo "Removing old configuration file: ${file}"
rm "${path}${file}"
fi
fi
done