Skip to content

Commit b52932a

Browse files
committed
Persistent interface option (#1), finish hook
Added option to make interface persistent on container restarts and firewall rules are called once (no iptables mess). Added finish hooks to examples, added more debug output to scripts added a note on how to access container environment variables.
1 parent 77ad4ff commit b52932a

File tree

20 files changed

+261
-14
lines changed

20 files changed

+261
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Changelog
22

3-
### 1.0.5 - Bugfix
3+
### 1.0.5 - Bugfix, finish hook
44

55
- Fixed bug when running hooks (#3)
6+
- Added **finish** hook (which runs just before container exit)
7+
- Added **persistent interface** option, so interface is persistently present on device (if using host networking mode) and firewall setup rules are executed **only once** (no ip tables mess) (#1)
68

79
### 1.0.4 - IPv6 docs, improved wizards
810

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ services:
7070
|:-----------:|:----------:|
7171
|`-e PUID=1000`|for UserID - see below for explanation|
7272
|`-e PGID=1000`|for GroupID - see below for explanation|
73+
|`-o OVPN_PERINT=false`|Disable persistent TUN interface|
7374
|`-v /config`|All the config files including OpenVPNs reside here|
7475

7576
See also: [EasyRSA](https://github.com/OpenVPN/easy-rsa/blob/master/doc/EasyRSA-Advanced.md)

root/app/lib/settings

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/with-contenv bash
2+
3+
#
4+
# Settings functions
5+
#
6+
7+
#
8+
# Checks if TUN interface is supposed to be persistant
9+
# @return 1 if persistant, 0 if not
10+
#
11+
function intPersistant() {
12+
if [ ! -n "$OVPN_PERINT" ] || ([ "$OVPN_PERINT" != "true" ] && [ "$OVPN_PERINT" != "1" ]); then
13+
return 0 # Not persistant by default
14+
else
15+
return 1 # Persistant
16+
fi
17+
}
18+
19+
#
20+
# Checks if TUN interface exists already
21+
# @return 0 if found, 1 if not found
22+
#
23+
function intTunExists() {
24+
RES=`cat /proc/net/dev | grep tun0`
25+
if [ -n "$RES" ]; then
26+
return 0 # Found
27+
else
28+
return 1 # Not found
29+
fi
30+
}

root/app/lib/utils

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/with-contenv bash
22

33
#
44
# Additional functions
@@ -45,4 +45,16 @@ function arrayContains() {
4545

4646
# Element not found
4747
return 0
48+
}
49+
50+
#
51+
# Function that makes sure, that script runs only once
52+
# @param $1 ID
53+
#
54+
function run_once() {
55+
if [ -f "$1" ]; then # Check if file (as flag) exists
56+
exit 0
57+
fi
58+
touch $1 # Create flag
59+
chown abc:abc $1 # Change permission
4860
}

root/defaults/example/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ config
4141
- **DO NOT** use any script running directives, because they are probably already set in `system.conf` (except `auth-user-pass-verify` is commented out), but use hooks directory.
4242
- **DO NOT** use log directives, because they are already set for `log` directory.
4343
- Please name your hooks as `<number>-<name>` to ensure order of execution.
44+
- If your hooks need access to container environment variables add `#!/usr/bin/with-contenv bash` at the top of the file.
4445

4546
### Wizard
4647

root/defaults/example/config/basic_nat/hooks/down/10-network.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
#!/bin/bash
1+
#!/usr/bin/with-contenv bash
2+
3+
source /app/lib/settings
4+
source /app/lib/utils
5+
6+
# Don't run if interface persistent
7+
intPersistant
8+
if [ $? -eq 1 ]; then
9+
exit 0
10+
fi
211

312
#
413
# Network clear
514
#
15+
echo "Clearing OpenVPN releated firewall rules"
616

717
# Close OpenVPN port to outside
818
ovpn-iptables -D INPUT -p udp -m udp --dport $PORT -j ACCEPT -m comment --comment "Open OpenVPN port"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/with-contenv bash
2+
3+
source /app/lib/settings
4+
source /app/lib/utils
5+
6+
# Don't run if interface persistent
7+
intPersistant
8+
if [ $? -eq 1 ]; then
9+
exit 0
10+
fi
11+
12+
#
13+
# Network clear
14+
#
15+
echo "Clearing up basic firewall rules"
16+
17+
# Accept everything from input
18+
ovpn-iptables -P INPUT ACCEPT
19+
20+
# Delete: Allow established connection
21+
ovpn-iptables -D INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "Accept traffic from established connections"
22+
23+
# Delete: Allow ICMP ping request
24+
ovpn-iptables -D INPUT -p icmp --icmp-type 8 -j ACCEPT
25+
26+
# Accept all forwarded traffic
27+
ovpn-iptables -P FORWARD ACCEPT

root/defaults/example/config/basic_nat/hooks/init/10-network.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
#!/bin/bash
1+
#!/usr/bin/with-contenv bash
2+
3+
source /app/lib/settings
4+
source /app/lib/utils
5+
6+
# Run only once if interface persistent
7+
intPersistant
8+
if [ $? -eq 1 ]; then
9+
run_once "/config/hooks/init/10-network"
10+
fi
211

312
#
413
# Network initialization
514
#
15+
echo "Setting up basic firewall rules"
616

717
#
818
# Because default iptables rules are set to ACCEPT all connection, we need to put some

root/defaults/example/config/basic_nat/hooks/up/10-network.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
#!/bin/bash
1+
#!/usr/bin/with-contenv bash
2+
3+
source /app/lib/settings
4+
source /app/lib/utils
5+
6+
# Run only once if interface persistent
7+
intPersistant
8+
if [ $? -eq 1 ]; then
9+
run_once "/config/hooks/up/10-network"
10+
fi
211

312
#
413
# Network initialization
514
#
15+
echo "Setting up OpenVPN related firewall rules"
616

717
# Open OpenVPN port to outside
818
ovpn-iptables -A INPUT -p udp -m udp --dport $PORT -j ACCEPT -m comment --comment "Open OpenVPN port"

root/defaults/example/config/basic_nat_wlp/hooks/down/10-network.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
#!/bin/bash
1+
#!/usr/bin/with-contenv bash
2+
3+
source /app/lib/settings
4+
source /app/lib/utils
5+
6+
# Don't run if interface persistent
7+
intPersistant
8+
if [ $? -eq 1 ]; then
9+
exit 0
10+
fi
211

312
#
413
# Network clear
514
#
15+
echo "Clearing OpenVPN releated firewall rules"
616

717
# Close OpenVPN port to outside
818
ovpn-iptables -D INPUT -p udp -m udp --dport $PORT -j ACCEPT -m comment --comment "Open OpenVPN port"

0 commit comments

Comments
 (0)