WSL2上で起動しているアプリに外部のPCからアクセスできるようにする問題
解決方法
ポートフォワーディング設定スクリプト $ip_in_linux = bash.exe -c 'ip -4 address show eth0 | grep inet | awk ''BEGIN{FS=\" +\"}{print $3}\'' | cut -d / -f 1'
$found = $ip_in_linux -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
if (!$found) {
Write-Output "The Script Exited, the ip address of WSL 2 cannot be found";
exit;
}
$ports_in_linux = @(22, 80, 443, 8000);
$_ports_in_linux = $ports_in_linux -join ",";
Invoke-Expression "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock'";
Invoke-Expression "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $_ports_in_linux -Action Allow -Protocol TCP";
Invoke-Expression "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $_ports_in_linux -Action Allow -Protocol TCP";
for ($i = 0; $i -lt $ports_in_linux.length; $i++) {
$port = $ports_in_linux[$i];
Invoke-Expression "netsh interface portproxy delete v4tov4 listenport=$port listenaddress='0.0.0.0'";
Invoke-Expression "netsh interface portproxy add v4tov4 listenport=$port listenaddress='0.0.0.0' connectport=$port connectaddress=$ip_in_linux";
}
タスクスケジュールにスクリプトの実行を登録
参考https://github.com/microsoft/WSL/issues/4150#issuecomment-504209723 |
|