Here is my setup with Wireguard and VPS for two nodes on ports 28867 & 28868
It works in general but has a small issue when the container starts, it got stuck when trying to connect to github while the VPN is active.
Workaround:
- Stop & Delete container
- Stop VPN
- Start container
- Wait for finished startup and downloded binaries
- Start VPN
From Google research
https://www.cyberciti.biz/faq/debian-10-set-up-wireguard-vpn-server/
https://www.cyberciti.biz/faq/how-to-set-up-wireguard-firewall-rules-in-linux/
https://golb.hplar.ch/2019/01/expose-server-vpn.html
Client Config
File: /etc/wireguard/wg0.conf
[Interface]
## This Desktop/client's private key ##
PrivateKey = <Key>
## Client private IP address ##
Address = <Client private VPN IP>/24
#DNS = 1.1.1.1
#DNS = 8.8.8.8
DNS = <my local DNS server>
[Peer]
## server public key ##
PublicKey = <Server public key>
## Preshared Key from server ##
PresharedKey = <Preshared key>
## set ACL ##
#################################################
## Allow remote server as gateway
## Otherwise client won't show server's IP
#################################################
AllowedIPs = 0.0.0.0/0
## server's public IPv4/IPv6 address and port ##
Endpoint = <Server public IP>:51820
## Key connection alive ##
PersistentKeepalive = 20
Server Config
UFW Firewall Entries (Not sure if they are really needed after the iptables entries were made in the script for startup VPN)
# Node 1
ufw route allow proto tcp to <Client private VPN IP> port 28867
ufw route allow proto udp to <Client private VPN IP> port 28867
# Node 2
ufw route allow proto tcp to <Client private VPN IP> port 28868
ufw route allow proto udp to <Client private VPN IP> port 28868
Wireguard Config
File: /etc/wireguard/wg0.conf
[Interface]
Address = <Server private VPN IP>/24
SaveConfig = true
PostUp = /etc/wireguard/helper/add-nat-routing.sh
PostDown = /etc/wireguard/helper/remove-nat-routing.sh
ListenPort = 51820
PrivateKey = <Server private key>
[Peer]
PublicKey = <Client public key>
PresharedKey = <Preshared key>
AllowedIPs = <Client private VPN IP>/32
File: /etc/wireguard/helper/add-nat-routing.sh
#!/bin/bash
IPT="/sbin/iptables"
IPT6="/sbin/ip6tables"
IN_IF="eth0" # NIC connected to the internet
WG_IF="wg0" # WG NIC
WG_SUBNET="<Private Subnet>.0/24" # WG IPv4 sub/net aka CIDR
WG_GW="<Server private VPN IP>" # WG IPv4 Gateway, Server private VPN IP
WG_PORT="51820" # WG udp port
#WG_SUBNET_6="fd42:42:42:42::/112" # WG IPv6 sub/net
HOST01="<Client private VPN IP>" # Host pi-vpn IPv4
HOST01_PORT01="28867" # Host pi-vpn Storj Port 1
HOST01_PORT02="28868" # Host pi-vpn Storj Port 2
## IPv4 Rules ##
$IPT -t nat -I POSTROUTING 1 -s $WG_SUBNET -o $IN_IF -j MASQUERADE
$IPT -I INPUT 1 -i $WG_IF -j ACCEPT
$IPT -I FORWARD 1 -i $IN_IF -o $WG_IF -j ACCEPT
$IPT -I FORWARD 1 -i $WG_IF -o $IN_IF -j ACCEPT
$IPT -I INPUT 1 -i $IN_IF -p udp --dport $WG_PORT -j ACCEPT
# Node 1
$IPT -A FORWARD -i $IN_IF -o $WG_IF -p tcp --syn --dport $HOST01_PORT01 -m conntrack --ctstate NEW -j ACCEPT
$IPT -A FORWARD -i $IN_IF -o $WG_IF -p udp --dport $HOST01_PORT01 -m conntrack --ctstate NEW -j ACCEPT
# Node 2
$IPT -A FORWARD -i $IN_IF -o $WG_IF -p tcp --syn --dport $HOST01_PORT02 -m conntrack --ctstate NEW -j ACCEPT
$IPT -A FORWARD -i $IN_IF -o $WG_IF -p udp --dport $HOST01_PORT02 -m conntrack --ctstate NEW -j ACCEPT
$IPT -A FORWARD -i $IN_IF -o $WG_IF -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
$IPT -A FORWARD -i $WG_IF -o $IN_IF -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# Node 1
$IPT -t nat -A PREROUTING -i $IN_IF -p tcp --dport $HOST01_PORT01 -j DNAT --to-destination $HOST01
$IPT -t nat -A PREROUTING -i $IN_IF -p udp --dport $HOST01_PORT01 -j DNAT --to-destination $HOST01
# Node 2
$IPT -t nat -A PREROUTING -i $IN_IF -p tcp --dport $HOST01_PORT02 -j DNAT --to-destination $HOST01
$IPT -t nat -A PREROUTING -i $IN_IF -p udp --dport $HOST01_PORT02 -j DNAT --to-destination $HOST01
# Node 1
$IPT -t nat -A POSTROUTING -o $WG_IF -p tcp --dport $HOST01_PORT01 -d $HOST01 -j SNAT --to-source $WG_GW
$IPT -t nat -A POSTROUTING -o $WG_IF -p udp --dport $HOST01_PORT01 -d $HOST01 -j SNAT --to-source $WG_GW
# Node 2
$IPT -t nat -A POSTROUTING -o $WG_IF -p tcp --dport $HOST01_PORT02 -d $HOST01 -j SNAT --to-source $WG_GW
$IPT -t nat -A POSTROUTING -o $WG_IF -p udp --dport $HOST01_PORT02 -d $HOST01 -j SNAT --to-source $WG_GW
## IPv6 (Uncomment) ##
## $IPT6 -t nat -I POSTROUTING 1 -s $WG_SUBNET_6 -o $IN_IF -j MASQUERADE
## $IPT6 -I INPUT 1 -i $WG_IF -j ACCEPT
## $IPT6 -I FORWARD 1 -i $IN_IF -o $WG_IF -j ACCEPT
## $IPT6 -I FORWARD 1 -i $WG_IF -o $IN_IF -j ACCEPT
File: /etc/wireguard/helper/remove-nat-routing.sh
#!/bin/bash
IPT="/sbin/iptables"
IPT6="/sbin/ip6tables"
IN_IF="eth0" # NIC connected to the internet
WG_IF="wg0" # WG NIC
WG_SUBNET="<Private Subnet>.0/24" # WG IPv4 sub/net aka CIDR
WG_GW="<Server private VPN IP>" # WG IPv4 Gateway, Server private VPN IP
WG_PORT="51820" # WG udp port
#WG_SUBNET_6="fd42:42:42:42::/112" # WG IPv6 sub/net
HOST01="<Client private VPN IP>" # Host pi-vpn IPv4
HOST01_PORT01="28867" # Host pi-vpn Storj Port 1
HOST01_PORT02="28868" # Host pi-vpn Storj Port 2
## IPv4 Rules ##
$IPT -t nat -D POSTROUTING -s $WG_SUBNET -o $IN_IF -j MASQUERADE
$IPT -D INPUT -i $WG_IF -j ACCEPT
$IPT -D FORWARD -i $IN_IF -o $WG_IF -j ACCEPT
$IPT -D FORWARD -i $WG_IF -o $IN_IF -j ACCEPT
$IPT -D INPUT -i $IN_IF -p udp --dport $WG_PORT -j ACCEPT
# Node 1
$IPT -D FORWARD -i $IN_IF -o $WG_IF -p tcp --syn --dport $HOST01_PORT01 -m conntrack --ctstate NEW -j ACCEPT
$IPT -D FORWARD -i $IN_IF -o $WG_IF -p udp --dport $HOST01_PORT01 -m conntrack --ctstate NEW -j ACCEPT
# Node 2
$IPT -D FORWARD -i $IN_IF -o $WG_IF -p tcp --syn --dport $HOST01_PORT02 -m conntrack --ctstate NEW -j ACCEPT
$IPT -D FORWARD -i $IN_IF -o $WG_IF -p udp --dport $HOST01_PORT02 -m conntrack --ctstate NEW -j ACCEPT
$IPT -D FORWARD -i $IN_IF -o $WG_IF -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
$IPT -D FORWARD -i $WG_IF -o $IN_IF -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# Node 1
$IPT -t nat -D PREROUTING -i $IN_IF -p tcp --dport $HOST01_PORT01 -j DNAT --to-destination $HOST01
$IPT -t nat -D PREROUTING -i $IN_IF -p udp --dport $HOST01_PORT01 -j DNAT --to-destination $HOST01
# Node 2
$IPT -t nat -D PREROUTING -i $IN_IF -p tcp --dport $HOST01_PORT02 -j DNAT --to-destination $HOST01
$IPT -t nat -D PREROUTING -i $IN_IF -p udp --dport $HOST01_PORT02 -j DNAT --to-destination $HOST01
# Node 1
$IPT -t nat -D POSTROUTING -o $WG_IF -p tcp --dport $HOST01_PORT01 -d $HOST01 -j SNAT --to-source $WG_GW
$IPT -t nat -D POSTROUTING -o $WG_IF -p udp --dport $HOST01_PORT01 -d $HOST01 -j SNAT --to-source $WG_GW
# Node 2
$IPT -t nat -D POSTROUTING -o $WG_IF -p tcp --dport $HOST01_PORT02 -d $HOST01 -j SNAT --to-source $WG_GW
$IPT -t nat -D POSTROUTING -o $WG_IF -p udp --dport $HOST01_PORT02 -d $HOST01 -j SNAT --to-source $WG_GW
# IPv6 rules (uncomment) #
## $IPT6 -t nat -D POSTROUTING -s $WG_SUBNET_6 -o $IN_IF -j MASQUERADE
## $IPT6 -D INPUT -i $WG_IF -j ACCEPT
## $IPT6 -D FORWARD -i $IN_IF -o $WG_IF -j ACCEPT
## $IPT6 -D FORWARD -i $WG_IF -o $IN_IF -j ACCEPT
Commands
systemctl start wg-quick@wg0
systemctl stop wg-quick@wg0
wg show
=>Shows connection information
Perhaps needed if name resolution don’t work:
ln -s /usr/bin/resolvectl /usr/local/bin/resolvconf
systemctl enable systemd-resolved.service
systemctl start systemd-resolved.service