Last active
November 14, 2024 12:14
-
-
Save shigenobuokamoto/b565d468541fc8be7d7d76a0434496a0 to your computer and use it in GitHub Desktop.
/etc/systemd/system/network-mirrored.service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 0;\ | |
echo "\ | |
add chain ip nat WSLPREROUTING { type nat hook prerouting priority dstnat - 1; policy accept; };\ | |
insert rule ip nat WSLPREROUTING iif loopback0 ip daddr 127.0.0.1 counter dnat to 127.0.0.1 comment mirrored;\ | |
"|nft -f -\ | |
' | |
ExecStop=/bin/sh -ec '\ | |
[ -x /usr/bin/wslinfo ] && [ "$(/usr/bin/wslinfo --networking-mode)" = "mirrored" ] || exit 0;\ | |
for chain in "ip nat WSLPREROUTING";\ | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this script is no longer needed.
please use Docker 27.3.0 or later.