The image shows a complete picture of what I'm building. But for now I want to implement a small part with one home server and 2 LXC, 2 ipv4 on a remote server.
I want to use 2 different Wireguard tunnels for 2 LXC containers.
It's easy to solve when I have only one Wireguard tunnel - just "AllowedIPs = 0.0.0.0/0" and all (host and guests) traffic will go thought Wireguard tunnel. But how to solve it in case of many different Wireguard tunnels?
Home server 1
WireGuard config:
[Interface]
PrivateKey = my_private_key
Address = 192.168.7.2/24
[Peer]
PublicKey = my_public_key
AllowedIPs = 0.0.0.0/0 <- ok for 1 LXC, but how to do it for 2?
Endpoint = 11.11.11.1:51194
PersistentKeepalive = 15
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 enp2s0
10.7.56.0 0.0.0.0 255.255.255.0 U 0 0 0 lxdbr0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 enp2s0
192.168.1.1 0.0.0.0 255.255.255.255 UH 100 0 0 enp2s0
192.168.7.0 0.0.0.0 255.255.255.0 U 0 0 0 wg1
Remote server
/etc/wireguard/helper/add-nat-routing.sh
#!/bin/bash
IPT="/sbin/iptables"
IN_FACE="eth0"
WG_FACE="wg1"
SUB_NET="192.168.7.0/24"
WG_PORT="51194"
## IPv4 ##
$IPT -t nat -I POSTROUTING 1 -s $SUB_NET -o $IN_FACE -j MASQUERADE
$IPT -I INPUT 1 -i $WG_FACE -j ACCEPT
$IPT -I FORWARD 1 -i $IN_FACE -o $WG_FACE -j ACCEPT
$IPT -I FORWARD 1 -i $WG_FACE -o $IN_FACE -j ACCEPT
$IPT -I INPUT 1 -i $IN_FACE -p udp --dport $WG_PORT -j ACCEPT
$IPT -t nat -A PREROUTING -p tcp -d 11.11.11.1 --jump DNAT --to-destination 192.168.7.2
/etc/wireguard/wg1.conf
[Interface]
Address = 192.168.7.1/24
ListenPort = 51194
PrivateKey = private_key
PostUp = /etc/wireguard/helper/add-nat-routing.sh
[Peer]
PublicKey = public_key
AllowedIPs = 192.168.7.2/32