-
Notifications
You must be signed in to change notification settings - Fork 57
/
watch-portmapping.sh
executable file
·42 lines (35 loc) · 1.59 KB
/
watch-portmapping.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
#!/bin/bash
[ $DEBUG ] && set -x
iptables -t nat -N KUBEOPENVPNPORTFORWARD
iptables -t nat -A PREROUTING -j KUBEOPENVPNPORTFORWARD
while true; do
if [ -d $OVPN_CCD ]; then
if [ -d $OVPN_PORTMAPPING ]; then
# Flush any old NAT rules.
iptables -t nat -F KUBEOPENVPNPORTFORWARD
for port in $(ls -1 ${OVPN_PORTMAPPING}); do
dest_cname=$(cut -d':' -f1 ${OVPN_PORTMAPPING}/${port})
dest_port=$(cut -d':' -f2 ${OVPN_PORTMAPPING}/${port})
if [ -f ${OVPN_CCD}/${dest_cname} ]; then
dest_ip=$(grep 'ifconfig-push' $OVPN_CCD/${dest_cname} | cut -d' ' -f2)
echo "$(date "+%a %b %d %H:%M:%S %Y") Routing ${PODIPADDR}:${port} to ${dest_ip}:${dest_port} (${dest_cname})"
iptables -t nat -A KUBEOPENVPNPORTFORWARD -p tcp -d $PODIPADDR --dport ${port} -j DNAT --to ${dest_ip}:${dest_port}
else
echo "$(date "+%a %b %d %H:%M:%S %Y") ERROR: client configuration for ${dest_cname} not found"
fi
done
# Done. Block for updates to configmap.
inotifywait -qq -e modify -e create -e delete $OVPN_PORTMAPPING
else
# Watch $OPENVPN directory for portmapping directory creation
inotifywait -qq -e create $OPENVPN
# Give it time to settle
sleep 60
fi
else
# Watch $OPENVPN directory for client configuration directory creation
inotifywait -qq -e create $OPENVPN
# Give it time to settle
sleep 60
fi
done