Skip to content

Commit ccffc57

Browse files
committed
basic_nat: Added Firewall & wizard
Added basic firewall rules & NAT rules to basic_nat example. Wizard was also added to make usage easier.
1 parent 6d4df9c commit ccffc57

File tree

6 files changed

+141
-6
lines changed

6 files changed

+141
-6
lines changed

root/defaults/example/config/basic_nat/client/client.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pull
1414

1515

1616
# Remote info
17-
remote <SERVER ADDRESS> <PORT>
17+
remote $SERVER_IP $PORT
1818

1919
# Connection settings
2020
resolv-retry infinite
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
#
4+
# Network clear
5+
#
6+
7+
# Close OpenVPN port to outside
8+
ovpn-iptables -D INPUT -p udp -m udp --dport $PORT -j ACCEPT -m comment --comment "Open OpenVPN port"
9+
10+
# Disable Routing Internet <--> VPN network
11+
ovpn-iptables -D FORWARD -i tun0 -s $NETWORK_ADDRESS/24 -o eth0 -j ACCEPT -m comment --comment "Allow traffic VPN --> Internet"
12+
ovpn-iptables -D FORWARD -i eth0 -d $NETWORK_ADDRESS/24 -o tun0 -j ACCEPT -m comment --comment "Allow traffic Internet --> VPN"
13+
14+
# Disable NAT for VPN traffic
15+
ovpn-iptables -t nat -D POSTROUTING -s $NETWORK_ADDRESS/24 -o eth0 -j MASQUERADE -m comment --comment "NAT traffic VPN --> Internet"
16+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
#
4+
# Network initialization
5+
#
6+
7+
#
8+
# Because default iptables rules are set to ACCEPT all connection, we need to put some
9+
# security settings in place
10+
#
11+
12+
# Drop everything from input
13+
ovpn-iptables -P INPUT DROP
14+
15+
# Allow established connection
16+
ovpn-iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -m comment --comment "Accept traffic from established connections"
17+
18+
# Drop all forwarded traffic
19+
ovpn-iptables -P FORWARD DROP
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
#
4+
# Network initialization
5+
#
6+
7+
# Open OpenVPN port to outside
8+
ovpn-iptables -A INPUT -p udp -m udp --dport $PORT -j ACCEPT -m comment --comment "Open OpenVPN port"
9+
10+
# Allow Routing Internet <--> VPN network
11+
ovpn-iptables -A FORWARD -i tun0 -s $NETWORK_ADDRESS/24 -o eth0 -j ACCEPT -m comment --comment "Allow traffic VPN --> Internet"
12+
ovpn-iptables -A FORWARD -i eth0 -d $NETWORK_ADDRESS/24 -o tun0 -j ACCEPT -m comment --comment "Allow traffic Internet --> VPN"
13+
14+
# Preform NAT for VPN traffic
15+
ovpn-iptables -t nat -A POSTROUTING -s $NETWORK_ADDRESS/24 -o eth0 -j MASQUERADE -m comment --comment "NAT traffic VPN --> Internet"
16+

root/defaults/example/config/basic_nat/server/server.conf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
#
77

88
# Basic info
9-
proto udp
10-
port 1194
9+
proto $PROTO
10+
port $PORT
1111

1212
# Network info (local VPN network)
1313
topology subnet
14-
server <NETWORK ADDRESS> <MASK>
14+
server $NETWORK_ADDRESS 255.255.255.0
1515

1616
push "redirect-gateway def1 bypass-dhcp"
17-
push "dhcp-option <DNS 1>"
18-
push "dhcp-option <DNS 2>"
17+
push "dhcp-option $DNS1"
18+
push "dhcp-option $DNS2"
1919

2020
ifconfig-pool-persist tmp/ipp.txt
2121

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/python
2+
3+
#
4+
# Config wizard for basic_nat example
5+
# @author Martin Dagarin
6+
# @version 1
7+
# @since 19/03/2019
8+
#
9+
10+
# Defaults:
11+
# Protocol: udp
12+
# Network: 10.0.0.0
13+
# Port: 1194
14+
# DNS: 8.8.8.8, 8.8.4.4
15+
#
16+
17+
import sys, os
18+
19+
# Import libraries included in this docker
20+
sys.path.insert(0, '/app/lib')
21+
import libovpn
22+
23+
# Check if temporary path was passed to this script
24+
if len(sys.argv) < 2:
25+
print("Temporary path was not passed to wizard")
26+
sys.exit(1)
27+
TEMP_PATH = sys.argv[1]
28+
if not os.path.isdir(TEMP_PATH):
29+
print("Specified directory does not exist")
30+
sys.exit(2)
31+
32+
# Select protocol
33+
protocol = input("Protocol udp, tcp, udp6, tcp6 [udp]:")
34+
AVAILABLE_PROTOCOLS = ["udp", "tcp", "udp6", "tcp6"]
35+
if len(protocol) != 0 and protocol not in AVAILABLE_PROTOCOLS:
36+
print("Invalid protocol")
37+
sys.exit(3)
38+
if len(protocol) == 0:
39+
protocol = "udp"
40+
41+
# Select network
42+
network = input("VPN network [10.0.0.0]:")
43+
if len(network) == 0:
44+
network = "10.0.0.0"
45+
46+
# Select port
47+
port = input("Port [1104]:")
48+
if len(port) == 0:
49+
port="1194"
50+
51+
# Select Public IP or domain
52+
public = input("Public IP or domain of server:")
53+
if len(public) == 0:
54+
print("Invalid Public IP")
55+
sys.exit(4)
56+
57+
# DNS servers
58+
dns1 = input("DNS1 [8.8.8.8]:")
59+
if len(dns1) == 0:
60+
dns1 = "8.8.8.8"
61+
dns2 = input("DNS2 [8.8.4.4]:")
62+
if len(dns2) == 0:
63+
dns2 = "8.8.4.4"
64+
65+
66+
# Write to server config
67+
vars = [
68+
("$PROTO", protocol),
69+
("$PORT", port),
70+
("$NETWORK_ADDRESS", network),
71+
("$SERVER_IP", public),
72+
("$DNS1", dns1),
73+
("$DNS2", dns2)
74+
]
75+
76+
# Process config files
77+
confs = [
78+
"/server/server.conf",
79+
"/client/client.conf",
80+
"/hooks/down/10-network.sh",
81+
"/hooks/up/10-network.sh"
82+
]
83+
for config_file in confs:
84+
libovpn.conf_envsubst(TEMP_PATH + config_file, vars)

0 commit comments

Comments
 (0)