Advertisement
tracins

auto port WSL forwarding

Apr 6th, 2025
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Get WSL2 IP
  2. $wslIp = wsl.exe -e "ip addr show eth0 | grep 'inet ' | awk '{print $2}' | cut -d '/' -f 1"
  3.  
  4. # List of containers and their ports to forward
  5. $containerPorts = @(
  6.     @{ ContainerName = "meshcentral"; Ports = @(3000, 3001, 3002) },
  7.     @{ ContainerName = "plex"; Ports = @(32400, 3005, 8324, 32469) },
  8.     @{ ContainerName = "sonarr"; Ports = @(8989) },
  9.     @{ ContainerName = "radarr"; Ports = @(7878) },
  10.     @{ ContainerName = "prowlarr"; Ports = @(9696) },
  11.     @{ ContainerName = "debridav"; Ports = @(8081) },
  12.     @{ ContainerName = "jellyfin"; Ports = @(8096) },
  13.     @{ ContainerName = "overseerr"; Ports = @(5055) },
  14.     @{ ContainerName = "tautulli"; Ports = @(8181) },
  15.     @{ ContainerName = "seerrbridge"; Ports = @(8082) },
  16.     @{ ContainerName = "traefik"; Ports = @(80, 443, 8080) }
  17. )
  18.  
  19. # Function to forward a single port
  20. function Forward-Port {
  21.     param (
  22.         [string]$containerName,
  23.         [int]$port,
  24.         [string]$wslIp
  25.     )
  26.  
  27.     # Remove existing rule
  28.     try {
  29.       netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=$port -ErrorAction Stop
  30.       Write-Host "Removed existing port proxy for $containerName:$port"
  31.     } catch {
  32.       Write-Host "No existing port proxy found for $containerName:$port. Continuing"
  33.     }
  34.  
  35.     # Add new rule with updated IP
  36.     try {
  37.         netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=$port connectaddress=$wslIp connectport=$port -ErrorAction Stop
  38.         Write-Host "Port $port forwarded for $containerName"
  39.     } catch {
  40.         Write-Error "Failed to forward port $port for $containerName: $_"
  41.     }
  42. }
  43.  
  44. # Forward ports for each container
  45. foreach ($container in $containerPorts) {
  46.     foreach ($port in $container.Ports) {
  47.         Forward-Port -containerName $container.ContainerName -port $port -wslIp $wslIp
  48.     }
  49. }
  50.  
  51. Write-Host "Port forwarding completed."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement