25 lines
1.2 KiB
PowerShell
25 lines
1.2 KiB
PowerShell
param(
|
|
[Parameter(Mandatory=$true)][string]$WslAddress,
|
|
[Parameter(Mandatory=$true)][string]$ListenAddress
|
|
)
|
|
$ErrorActionPreference = 'Stop'
|
|
$log = Join-Path $env:TEMP 'r2c-network-setup.log'
|
|
try {
|
|
$wslIp = [System.Net.IPAddress]::Parse($WslAddress)
|
|
$listenIp = [System.Net.IPAddress]::Parse($ListenAddress)
|
|
if ($wslIp.AddressFamily -ne 'InterNetwork' -or $listenIp.AddressFamily -ne 'InterNetwork') {
|
|
throw 'IPv4 addresses are required.'
|
|
}
|
|
& netsh.exe interface portproxy add v4tov4 listenaddress=$ListenAddress listenport=8000 connectaddress=$WslAddress connectport=8000
|
|
if ($LASTEXITCODE -ne 0) { throw 'Could not configure port forwarding.' }
|
|
$ruleName = 'R2C-Viewer-8000-Private10'
|
|
if (Get-NetFirewallRule -Name $ruleName -ErrorAction SilentlyContinue) {
|
|
Remove-NetFirewallRule -Name $ruleName
|
|
}
|
|
New-NetFirewallRule -Name $ruleName -DisplayName 'R2C viewer (10.0.0.0/8)' -Direction Inbound -Action Allow -Protocol TCP -LocalPort 8000 -LocalAddress $ListenAddress -RemoteAddress '10.0.0.0/8' -Profile Any | Out-Null
|
|
"OK: http://${ListenAddress}:8000/ -> ${WslAddress}:8000; allowed clients: 10.0.0.0/8" | Set-Content $log
|
|
} catch {
|
|
$_.Exception.Message | Set-Content $log
|
|
exit 1
|
|
}
|