Skip to content

Commit 47de917

Browse files
authored
Merge pull request #219 from r0p0s3c/iptables
move iptables/nat functionality to a function
2 parents f4351bb + cbf9cbf commit 47de917

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

bin/ovpn_run

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ function addArg {
3535
fi
3636
}
3737

38+
# set up iptables rules and routing
39+
# this allows rules/routing to be altered by supplying this function
40+
# in an included file, such as ovpn_env.sh
41+
function setupIptablesAndRouting {
42+
iptables -t nat -C POSTROUTING -s $OVPN_SERVER -o $OVPN_NATDEVICE -j MASQUERADE || {
43+
iptables -t nat -A POSTROUTING -s $OVPN_SERVER -o $OVPN_NATDEVICE -j MASQUERADE
44+
}
45+
for i in "${OVPN_ROUTES[@]}"; do
46+
iptables -t nat -C POSTROUTING -s "$i" -o $OVPN_NATDEVICE -j MASQUERADE || {
47+
iptables -t nat -A POSTROUTING -s "$i" -o $OVPN_NATDEVICE -j MASQUERADE
48+
}
49+
done
50+
}
51+
52+
3853
addArg "--config" "$OPENVPN/openvpn.conf"
3954

4055
source "$OPENVPN/ovpn_env.sh"
@@ -53,14 +68,10 @@ fi
5368

5469
# Setup NAT forwarding if requested
5570
if [ "$OVPN_DEFROUTE" != "0" ] || [ "$OVPN_NAT" == "1" ] ; then
56-
iptables -t nat -C POSTROUTING -s $OVPN_SERVER -o $OVPN_NATDEVICE -j MASQUERADE || {
57-
iptables -t nat -A POSTROUTING -s $OVPN_SERVER -o $OVPN_NATDEVICE -j MASQUERADE
58-
}
59-
for i in "${OVPN_ROUTES[@]}"; do
60-
iptables -t nat -C POSTROUTING -s "$i" -o $OVPN_NATDEVICE -j MASQUERADE || {
61-
iptables -t nat -A POSTROUTING -s "$i" -o $OVPN_NATDEVICE -j MASQUERADE
62-
}
63-
done
71+
# call function to setup iptables rules and routing
72+
# this allows rules to be customized by supplying
73+
# a replacement function in, for example, ovpn_env.sh
74+
setupIptablesAndRouting
6475
fi
6576

6677
# Use a hacky hardlink as the CRL Needs to be readable by the user/group
@@ -85,4 +96,3 @@ fi
8596

8697
echo "Running 'openvpn ${ARGS[@]} ${USER_ARGS[@]}'"
8798
exec openvpn ${ARGS[@]} ${USER_ARGS[@]}
88-

test/config.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ imageTests+=(
1212
basic
1313
dual-proto
1414
otp
15+
iptables
1516
'
1617
)

test/tests/iptables/run.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
set -e
3+
4+
[ -n "${DEBUG+x}" ] && set -x
5+
OVPN_DATA=basic-data
6+
IMG="kylemanna/openvpn"
7+
NAME="ovpn-test"
8+
SERV_IP=$(ip -4 -o addr show scope global | awk '{print $4}' | sed -e 's:/.*::' | head -n1)
9+
10+
# generate server config including iptables nat-ing
11+
docker volume create --name $OVPN_DATA
12+
docker run --rm -v $OVPN_DATA:/etc/openvpn $IMG ovpn_genconfig -u udp://$SERV_IP -N
13+
docker run -v $OVPN_DATA:/etc/openvpn --rm -it -e "EASYRSA_BATCH=1" -e "EASYRSA_REQ_CN=Travis-CI Test CA" $IMG ovpn_initpki nopass
14+
15+
# Fire up the server
16+
docker run -d --name $NAME -v $OVPN_DATA:/etc/openvpn --cap-add=NET_ADMIN $IMG
17+
18+
# check default iptables rules
19+
docker exec -ti $NAME bash -c 'source /etc/openvpn/ovpn_env.sh; eval iptables -t nat -C POSTROUTING -s $OVPN_SERVER -o eth0 -j MASQUERADE'
20+
21+
# append new setupIptablesAndRouting function to config
22+
docker exec -ti $NAME bash -c 'echo function setupIptablesAndRouting { iptables -t nat -A POSTROUTING -m comment --comment "test"\;} >> /etc/openvpn/ovpn_env.sh'
23+
24+
# kill server in preparation to modify config
25+
docker kill $NAME
26+
docker rm $NAME
27+
28+
# check that overridden function exists and that test iptables rules is active
29+
docker run -d --name $NAME -v $OVPN_DATA:/etc/openvpn --cap-add=NET_ADMIN $IMG
30+
docker exec -ti $NAME bash -c 'source /etc/openvpn/ovpn_env.sh; type -t setupIptablesAndRouting && iptables -t nat -C POSTROUTING -m comment --comment "test"'
31+
32+
#
33+
# kill server
34+
#
35+
36+
docker kill $NAME
37+
docker rm $NAME
38+
docker volume rm $OVPN_DATA

0 commit comments

Comments
 (0)