From c1eb16632e51645df81cf6961df6e4805ca78a88 Mon Sep 17 00:00:00 2001 From: Matheus Albino Date: Mon, 6 Nov 2023 03:16:47 -0300 Subject: [PATCH] Update powershell scripts and add port forwarding wsl scripts --- .../pshell/AddOpenTerminalHereContextMenu.txt | 14 ++++++++-- .../Bridge-WslPorts-AddTaskToScheduler.ps1 | 5 ++++ .../Bridge-WslPorts-RunTaskOnDemand.ps1 | 1 + scripts/pshell/Bridge-WslPorts.ps1 | 28 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100755 scripts/pshell/Bridge-WslPorts-AddTaskToScheduler.ps1 create mode 100755 scripts/pshell/Bridge-WslPorts-RunTaskOnDemand.ps1 create mode 100755 scripts/pshell/Bridge-WslPorts.ps1 diff --git a/scripts/pshell/AddOpenTerminalHereContextMenu.txt b/scripts/pshell/AddOpenTerminalHereContextMenu.txt index 44e5bcb..7fa61dc 100644 --- a/scripts/pshell/AddOpenTerminalHereContextMenu.txt +++ b/scripts/pshell/AddOpenTerminalHereContextMenu.txt @@ -1,3 +1,13 @@ -# https://github.com/lextm/windowsterminal-shell.git +1. install PowerShell 7 + winget install --id Microsoft.Powershell --source winget + winget install --id Microsoft.Powershell.Preview --source winget -Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; .\install.ps1 mini +2. clone repo + git clone https://github.com/lextm/windowsterminal-shell.git + +3. run script + Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; .\install.ps1 mini + +4. remove keys + HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdminMin + HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdminMin diff --git a/scripts/pshell/Bridge-WslPorts-AddTaskToScheduler.ps1 b/scripts/pshell/Bridge-WslPorts-AddTaskToScheduler.ps1 new file mode 100755 index 0000000..f1473f4 --- /dev/null +++ b/scripts/pshell/Bridge-WslPorts-AddTaskToScheduler.ps1 @@ -0,0 +1,5 @@ +$a = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File `"/home/cloud/repos/personal-devboot/scripts/pshell/Bridge-WslPorts.ps1`" -WindowStyle Hidden -ExecutionPolicy Bypass" +$t = New-ScheduledTaskTrigger -AtLogon +$s = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries +$p = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Administrators" -RunLevel Highest +Register-ScheduledTask -TaskName "WSL2PortsBridge" -Action $a -Trigger $t -Settings $s -Principal $p diff --git a/scripts/pshell/Bridge-WslPorts-RunTaskOnDemand.ps1 b/scripts/pshell/Bridge-WslPorts-RunTaskOnDemand.ps1 new file mode 100755 index 0000000..b2de78a --- /dev/null +++ b/scripts/pshell/Bridge-WslPorts-RunTaskOnDemand.ps1 @@ -0,0 +1 @@ +powershell.exe -ExecutionPolicy Bypass -File "/home/cloud/repos/personal-devboot/scripts/pshell/Bridge-WslPorts.ps1" diff --git a/scripts/pshell/Bridge-WslPorts.ps1 b/scripts/pshell/Bridge-WslPorts.ps1 new file mode 100755 index 0000000..15ca00c --- /dev/null +++ b/scripts/pshell/Bridge-WslPorts.ps1 @@ -0,0 +1,28 @@ +# https://jwstanly.com/blog/article/Port+Forwarding+WSL+2+to+Your+LAN/ + +$ports = @(80, 443, 25565); + +$wslAddress = bash.exe -c "ifconfig eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'" + +if ($wslAddress -match '^(\d{1,3}\.){3}\d{1,3}$') { + Write-Host "WSL IP address: $wslAddress" -ForegroundColor Green + Write-Host "Ports: $ports" -ForegroundColor Green +} +else { + Write-Host "Error: Could not find WSL IP address." -ForegroundColor Red + exit +} + +$listenAddress = '0.0.0.0'; + +foreach ($port in $ports) { + Invoke-Expression "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$listenAddress"; + Invoke-Expression "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$listenAddress connectport=$port connectaddress=$wslAddress"; +} + +# $fireWallDisplayName = 'WSL Port Forwarding'; +# $portsStr = $ports -join ","; + +# Invoke-Expression "Remove-NetFireWallRule -DisplayName $fireWallDisplayName"; +# Invoke-Expression "New-NetFireWallRule -DisplayName $fireWallDisplayName -Direction Outbound -LocalPort $portsStr -Action Allow -Protocol TCP"; +# Invoke-Expression "New-NetFireWallRule -DisplayName $fireWallDisplayName -Direction Inbound -LocalPort $portsStr -Action Allow -Protocol TCP";