-
Notifications
You must be signed in to change notification settings - Fork 5
/
check-vpn.sh
executable file
·58 lines (51 loc) · 1.91 KB
/
check-vpn.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
#!/usr/bin/env bash
ROOT_DIRECTORY=$(cd $(dirname "$0"); pwd -P)
BOM_DIRECTORY="${PWD}"
VPN_REQUIRED=$(grep "vpn/required" "${BOM_DIRECTORY}/bom.yaml" | sed -E "s~[^:]+: [\"'](.*)[\"']~\1~g")
USER=$(whoami)
RUNNING_PROCESSES=$(ps -ef)
VPN_RUNNING=$(echo "${RUNNING_PROCESSES}" | grep "openvpn --config")
if [[ "${VPN_REQUIRED}" == "true" ]]; then
if [[ -n "${VPN_RUNNING}" ]]; then
echo "VPN required but it is already running"
elif command -v openvpn 1> /dev/null 2> /dev/null; then
OVPN_FILE=$(find "${ROOT_DIRECTORY}" -name "*.ovpn" | head -1)
if [[ -z "${OVPN_FILE}" ]]; then
echo "VPN profile not found."
exit 1
fi
echo "Connecting to vpn with profile: ${OVPN_FILE}"
if [[ "${UID}" -eq 0 ]]; then
exec 1<&-
exec 2<&-
openvpn --config "${OVPN_FILE}" || true &
elif [[ "${USER}" == "runner" ]]; then # Caters for self hosted runner image
exec 1<&-
exec 2<&-
openvpn --config "${OVPN_FILE}" || true &
else
exec 1<&-
exec 2<&-
sudo openvpn --config "${OVPN_FILE}" || true &
fi
else
echo "VPN connection required but unable to create the connection automatically. Please connect to your vpn instance using the .ovpn profile within the 101-azure-vnet-std directory and re-run apply-all.sh."
exit 1
fi
else
if [[ -n "${VPN_RUNNING}" ]]; then
echo "VPN not required but it is already running, shutting down"
if [[ "${UID}" -eq 0 ]]; then
VPN_PID=$(ps xua | grep "openvpn --config" | grep -v grep | awk '{print$1}')
kill "${VPN_PID}"
elif [[ "${USER}" == "runner" ]]; then # Caters for self hosted runner image
VPN_PID=$(ps xua | grep "openvpn --config" | grep -v grep | awk '{print$2}')
kill "${VPN_PID}"
else
VPN_PID=$(ps xua | grep "openvpn --config" | grep -v grep | awk '{print$1}')
sudo kill "${VPN_PID}"
fi
else
echo "VPN not required"
fi
fi