$ pacman -S openvpn easy-rsa
$ cp -a /etc/easy-rsa/. /etc/easy-rsa-clientnet/
$ cd /etc/easy-rsa-clientnet/
$ export EASYRSA=$(pwd)
$ easyrsa init-pki
$ easyrsa build-ca nopass
$ easyrsa gen-req server nopass
$ easyrsa sign-req server server
$ cat pki/index.txt
$ mkdir -p /etc/openvpn/server/{tcp,udp}
$ cp /etc/easy-rsa-clientnet/pki/ca.crt /etc/openvpn/server/tcp/
$ cp /etc/easy-rsa-clientnet/pki/private/server.key /etc/openvpn/server/tcp/
$ cp /etc/easy-rsa-clientnet/pki/issued/server.crt /etc/openvpn/server/tcp/
$ openssl dhparam -out /etc/openvpn/server/tcp/dh.pem 2048
$ openvpn --genkey secret /etc/openvpn/server/tcp/ta.key
$ cp -a /etc/openvpn/server/tcp/. /etc/openvpn/server/udp/
/etc/sysctl.d/30-ipforward.conf
-
net.ipv4.ip_forward=1
/etc/iptables/iptables.rules
-
...
*filter
:TCP - [0:0]
:UDP - [0:0]
-A INPUT -i tun+ -j ACCEPT
-A TCP -p tcp --dport 5060 -j ACCEPT
-A UDP -p udp --dport 5060 -j ACCEPT
COMMIT
*nat
-A POSTROUTING -o tun1 -j MASQUERADE
-A POSTROUTING -o tun2 -j MASQUERADE
-A POSTROUTING -s 10.8.0.0/24 -o eth0 -j SNAT --to-source 203.0.113.2
-A POSTROUTING -s 10.9.0.0/24 -o eth0 -j SNAT --to-source 203.0.113.2
COMMIT
/etc/openvpn/server/clientnet-udp.conf
-
sndbuf 0
rcvbuf 0
reneg-sec 0
mute-replay-warnings
management 127.0.0.1 5061
port 5060
proto udp
dev tun1
topology subnet
ca /etc/openvpn/server/udp/ca.crt
cert /etc/openvpn/server/udp/server.crt
key /etc/openvpn/server/udp/server.key
dh /etc/openvpn/server/udp/dh.pem
tls-auth /etc/openvpn/server/udp/ta.key 0
tls-exit
server 10.8.0.0 255.255.0.0 # 65K IP's
keepalive 10 120
persist-key
persist-tun
cipher AES-256-GCM
data-ciphers "AES-256-GCM"
replay-persist /etc/openvpn/server/udp/replay-persist.txt
verb 2
explicit-exit-notify 5
config /etc/openvpn/server/routes.conf
/etc/openvpn/server/clientnet-tcp.conf
-
sndbuf 0
rcvbuf 0
reneg-sec 0
mute-replay-warnings
management 127.0.0.1 5062
port 5060
proto tcp
dev tun2
topology subnet
ca /etc/openvpn/server/tcp/ca.crt
cert /etc/openvpn/server/tcp/server.crt
key /etc/openvpn/server/tcp/server.key
dh /etc/openvpn/server/tcp/dh.pem
tls-auth /etc/openvpn/server/tcp/ta.key 0
tls-exit
server 10.9.0.0 255.255.0.0 # 65K IP's
keepalive 10 120
persist-key
persist-tun
cipher AES-256-GCM
data-ciphers "AES-256-GCM"
replay-persist /etc/openvpn/server/tcp/replay-persist.txt
verb 2
config /etc/openvpn/server/routes.conf
$ chown -R openvpn:network /etc/openvpn/server
/etc/openvpn/server/routes.conf
-
push "block-outside-dns"
push "redirect-gateway def1 bypass-dhcp"
push "route 8.8.8.8 255.255.255.255 vpn_gateway"
push "route 8.8.4.4 255.255.255.255 vpn_gateway"
push "route 208.67.222.222 255.255.255.255 vpn_gateway"
push "route 208.67.220.220 255.255.255.255 vpn_gateway"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"
push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.220.220"
$ systemctl enable openvpn-server@clientnet-udp \
&& systemctl start openvpn-server@clientnet-udp \
&& systemctl status openvpn-server@clientnet-udp
$ systemctl enable openvpn-server@clientnet-tcp \
&& systemctl start openvpn-server@clientnet-tcp \
&& systemctl status openvpn-server@clientnet-tcp
$ mkdir /etc/openvpn/client
/etc/openvpn/client/client-udp.conf
-
client
dev tun
remote fqdn1.example 5060 udp
remote 203.0.113.2 5060 udp
remote fqdn2.example 5060 udp
remote 203.0.113.3 5060 udp
resolv-retry 10
nobind
#user nobody
#group nobody
persist-key
persist-tun
key-direction 1
remote-cert-tls server
verb 2
cipher AES-256-GCM
explicit-exit-notify 5
connect-retry-max 2
tls-exit
push-peer-info
#socks-proxy localhost 9150 socks-proxy-retry
/etc/openvpn/client/client-tcp.conf
-
client
dev tun
remote fqdn1.example 5060 tcp
remote 203.0.113.2 5060 tcp
remote fqdn2.example 5060 tcp
remote 203.0.113.3 5060 tcp
resolv-retry 10
nobind
#user nobody
#group nobody
persist-key
persist-tun
key-direction 1
remote-cert-tls server
verb 2
cipher AES-256-GCM
connect-retry-max 2
tls-exit
push-peer-info
#socks-proxy localhost 9150 socks-proxy-retry
/etc/openvpn/client/generate.php
-
(see generate.php file)
$ cd /etc/openvpn/client
$ php generate.php client1 tcp
$ php generate.php client1 udp
Management
$ telnet localhost 5061 # clientnet-udp
> status
$ telnet localhost 5062 # clientnet-tcp
> status
Optional, setting up forawrd of traffic form second vps (203.0.113.3) to first (203.0.113.2)
/etc/iptables/iptables.rules
...
*filter
:TCP - [0:0]
-A TCP -p tcp --dport 5060 -j ACCEPT
COMMIT
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
-A PREROUTING -p udp -m udp --dport 5060 -j DNAT --to-destination 203.0.113.2:5060
-A PREROUTING -p tcp -m tcp --dport 5060 -j DNAT --to-destination 203.0.113.2:5060
-A POSTROUTING -j MASQUERADE
COMMIT
Optional, setting up SSH tunnel
# on local machine
$ cd somedirtostorekeys
$ ssh-keygen -t ed25519 -f ./id_ed25519 -C "forward"
# on remote
$ useradd -m forward
$ su forward
$ mkdir ~/.ssh && chmod 700 ~/.ssh
$ touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys
~/.ssh/authorized_keys
-
restrict,port-forwarding,command="echo 'This account can only be used for forwarding, e.g. run: ssh -D 5060 -N [email protected]'" ssh-ed25519 ...# id_ed25519.pub content
tcp ovpn profile changes
client
...
socks-proxy localhost 5060
socks-proxy-retry
route 203.0.113.2 255.255.255.255 net_gateway
route 203.0.113.3 255.255.255.255 net_gateway
run tunnel
$ cd somedirtostorekeys
$ ssh -i id_ed25519 -D 5060 -N [email protected]