Skip to content

Commit 998f977

Browse files
committed
Added basic_routed example
Added example were routing is used instead of NAT-ing
1 parent d5a3624 commit 998f977

File tree

8 files changed

+261
-0
lines changed

8 files changed

+261
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# basic_routed
2+
3+
Features:
4+
5+
- Has configuration wizard
6+
- Prepared for using routing (so you will have access to LANs directly without usign NAT)
7+
8+
## Configure
9+
10+
``` bash
11+
ovpn_enconf basic_routed
12+
#Protocol udp, tcp, udp6, tcp6 [udp]:
13+
#VPN network [10.0.0.0]:
14+
#Port [1194]:
15+
#Public IP or domain of server: <PUBLIC IP>
16+
#DNS1 [8.8.8.8]:
17+
#DNS2 [8.8.4.4]:
18+
```
19+
20+
### Network configuration
21+
22+
1. If you are using **bridge** networking mode else skip this step:
23+
24+
- Assign static IP to this container
25+
- see [docker compose networks](https://docs.docker.com/compose/compose-file/compose-file-v2/#networks), you can also check current IP of container
26+
with `docker exec -it CONTAINERNAME ifconfig`
27+
- Add static route on host to the container with network
28+
29+
``` bash
30+
route add -net NETWORK netmask MASK gw CONTAINER_IP
31+
```
32+
33+
2. Add route to the network on your router
34+
35+
- Destination IP Address: NETWORK
36+
- Subnet mask: MASK
37+
- Gateway: SERVERIP_OR_CONTAINERIP (IP where your OpenVPN server is running: server ip when bridge mode, container ip on host mode)
38+
39+
![Sample interface](img/img1.png)
40+
41+
3. If you have Mikrotik or Cisco router make sure you have NAT correctly configured
42+
4. Make sure you have firewall rules correctly configured on your router
43+
5. Add additional routes in *server config* if nessesary (see [--route option](https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage)), default route is set as default gateway
44+
45+
``` OpenVPN
46+
route network/IP [netmask] [gateway] [metric]
47+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#
2+
# Basic OpenVPN server configuration
3+
# @author Martin Dagarin
4+
# @version 2
5+
# @since 12/03/2019
6+
#
7+
8+
# Basic info
9+
client
10+
dev tun0
11+
proto $PROTO
12+
nobind
13+
pull
14+
15+
16+
# Remote info
17+
remote $SERVER_IP $PORT
18+
19+
# Connection settings
20+
resolv-retry infinite
21+
persist-key
22+
persist-tun
23+
24+
# Encryption settings
25+
cipher AES-256-GCM
26+
27+
# Additional settings
28+
compress lzo
29+
verb 3
30+
ping 10 120
31+
32+
# Permissions
33+
user nobody
34+
group nogroup
35+
36+
# CA
37+
remote-cert-tls server
38+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
# Allow ICMP ping request
19+
ovpn-iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
20+
21+
# Drop all forwarded traffic
22+
ovpn-iptables -P FORWARD DROP
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
Loading
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#
2+
# Basic OpenVPN server configuration
3+
# @author Martin Dagarin
4+
# @version 3
5+
# @since 12/03/2019
6+
#
7+
8+
# Basic info
9+
proto $PROTO
10+
port $PORT
11+
12+
# Network info (local VPN network)
13+
topology subnet
14+
server $NETWORK_ADDRESS 255.255.255.0
15+
16+
push "redirect-gateway def1 bypass-dhcp"
17+
push "dhcp-option $DNS1"
18+
push "dhcp-option $DNS2"
19+
20+
ifconfig-pool-persist tmp/ipp.txt
21+
22+
# CA files
23+
ca pki/ca.crt
24+
cert pki/issued/server.crt
25+
key pki/private/server.key
26+
dh pki/dh.pem
27+
tls-crypt pki/ta.key
28+
remote-cert-tls client
29+
30+
# Connection settings
31+
persist-key
32+
persist-tun
33+
34+
# Encryption settings
35+
cipher AES-256-GCM
36+
37+
# Verify client certificate
38+
verify-client-cert require
39+
40+
# Additional settings
41+
client-to-client
42+
keepalive 10 120
43+
compress lzo
44+
explicit-exit-notify 1
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_routed 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 [1194]:")
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)