Manage Windows firewall rules and port forwardings to expose a port in your WSL2 machine.
Tested in Windows 10
Firewall Rules and Port Forwardings for WSL2
--------------------------------------------------
Firewall Rule: Allowing LAN connections to port 5000
0.0.0.0:5000 to localhost:5000
1) Create new rule and port forwarding
2) Delete an existing rule
3) Display Firewall Rule for port
4) Display all Firewall rules "Allowing LAN connections to port *"
5) Display all port forwardings
0) Exit
Choose an option:
To add or remove rules administrative privileges are required. Adding or deleting rules persist over reboots and can imply a security risk or cause applications to stop working. Use this at your own risk.
netsh advfirewall export "firewall_rules_backup.wfw"
netsh advfirewall import "firewall_rules_backup.wfw"
netsh advfirewall firewall show rule name=all
netsh advfirewall firewall show rule name=all | find "Allowing LAN connections to port"
netsh advfirewall firewall show rule name="Allowing LAN connections to port 5000"
netsh advfirewall firewall add rule name="Allowing LAN connections to port 5000" dir=in action=allow protocol=TCP localport=5000
netsh advfirewall firewall add rule name="Allowing LAN connections to port 5000" dir=in action=allow protocol=TCP localport=5000 remoteip=192.168.1.83/32
netsh advfirewall firewall delete rule name="Allowing LAN connections to port 5000"
netsh interface portproxy show all
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=5000 connectaddress=localhost connectport=5000
netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=5000
Thanks to ChatGPT :)