WSL, VPN und Docker

Gespeichert von Erik Wegner am/um
Body

Die WSL2 bietet den Netzwerk-Modus "gespiegelt". Damit wird die Zusammenarbeit mit VPN-Verbindungen verbessert und das Ändern der Schnittstellen-Metriken ist nicht mehr erforderlich.

In dieser Situation tritt aktuell der unter 10926 gemeldete Fehler auf: vom Docker-Dämon veröffentlichte Container-Ports sind von Windows aus nicht erreichbar.

Dagegen hilft es, die WSL2-Umgebung auf systemd umzustellen und anschließend die Konfiguration des IP-FIlters anzupassen:

/etc/systemd/system/network-mirrored.service
[Unit]
Wants=network-pre.target
Before=network-pre.target shutdown.target

[Service]
User=root
ExecStart=/bin/sh -ec '\
[ -x /usr/bin/wslinfo ] && [ "$(/usr/bin/wslinfo --networking-mode)" = "mirrored" ] || exit;\
echo "\
add chain ip nat PREROUTING { type nat hook prerouting priority dstnat; policy accept; };\
insert rule ip nat PREROUTING iif loopback0 counter accept comment mirrored;\
add table ip6 filter;\
add chain ip6 filter WSLOUTPUT {type filter hook output priority filter; policy accept;};\
add rule ip6 filter WSLOUTPUT counter meta mark set 0x00000001 comment mirrored;\
add table ip6 nat;\
add chain ip6 nat WSLPOSTROUTING {type nat hook postrouting priority srcnat - 1; policy accept;};\
add rule ip6 nat WSLPOSTROUTING oif eth0 udp sport 1-65535 meta mark != 0x00000001 counter masquerade to :60600-60900 comment mirrored;\
add rule ip6 nat WSLPOSTROUTING oif eth0 tcp sport 1-65535 meta mark != 0x00000001 counter masquerade to :60600-60900 comment mirrored;\
"|nft -f -\
'
ExecStop=/bin/sh -ec '\
[ -x /usr/bin/wslinfo ] && [ "$(/usr/bin/wslinfo --networking-mode)" = "mirrored" ] || exit;\
for chain in "ip nat PREROUTING" "ip6 filter WSLOUTPUT" "ip6 nat WSLPOSTROUTING";\
do\
handle=$(nft -a list chain $chain | sed -En "s/^.*comment \\"mirrored\\" # handle ([0-9]+)$/\\1/p");\
for n in $handle; do echo "delete rule $chain handle $n"; done;\
done|nft -f -\
'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

Anschließend kann der automatische Start aktiviert werden:

sudo systemctl --now enable network-mirrored