Created
April 29, 2024 20:47
-
-
Save ijortengab/8d73c54408976809eec9a9d3ade5ca46 to your computer and use it in GitHub Desktop.
wsl2 port forwarding host to guest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @filename: C:\ProgramData\bin\wsl.ps1 | |
# @created: IjorTengab <http://ijortengab.my.id> | |
# @last-modified: 20240111 Kamis | |
# @reference: https://jwstanly.com/blog/article/Port+Forwarding+WSL+2+to+Your+LAN/ | |
# @reference: https://www.askvg.com/fix-users-must-enter-a-user-name-and-password-to-use-this-computer-checkbox-missing-in-windows-10/ | |
# | |
# | |
# | |
# Script untuk Menjalankan WSL2 secara otomatis | |
# plus autorun service | |
# plus port forwarding untuk diakses LAN. | |
# | |
# WSL2 yang digunakan adalah versi bawaan OS `C:\Windows\System32\wsl.exe` | |
# bukan versi dari aplikasi yang didownload dari Microsoft Store. | |
# Versi aplikasi masih ada issue sbb: https://github.com/microsoft/WSL/issues/9231 | |
# Untuk mengecek versi, kita bisa menggunakan Command Prompt dengan perintah | |
# `where wsl`. | |
# | |
# Jika script ini dijalankan menggunakan Task Scheduler dengan opsi | |
# Run whether user is logged on or not, maka dapat menyebabkan | |
# WSL2 menjadi hang. (Studi kasus pada winver: 22H2 build:19045.3803) | |
# | |
# Solusinya adalah dengan menggunakan dua item Task Scheduler. | |
# | |
# Jalankan RUN, ketik `taskschd.msc`, enter. | |
# | |
# Create Basic Task. | |
# | |
# - Name: wsl2-cron | |
# - Trigger: When the computer starts | |
# - Action: Start a program | |
# - Program: wsl | |
# - Add arguments: -u root /etc/init.d/cron start | |
# - [x] Open the Properties dialog for this task when I click Finish | |
# - Security options: | |
# - [x] Run whether user is logged on or not | |
# | |
# Create Basic Task. | |
# | |
# - Name: wsl2-port | |
# - Trigger: When I log on | |
# - Action: Start a program | |
# - Program: powershell.exe | |
# - Add arguments: -ExecutionPolicy Bypass -file C:\ProgramData\bin\wsl.ps1 | |
# - [x] Open the Properties dialog for this task when I click Finish | |
# - Security options: | |
# - [x] Run with highest privileges | |
# | |
# Instalasi: | |
# | |
# Taruh file ini di folder `%ALLUSERSPROFILE%\bin` atau di `C:\ProgramData\bin`. | |
# | |
# Agar bisa dijalankan dengan trigger `When I log on`, maka kita perlu autologin. | |
# | |
# Jalankan RUN, ketik `control userpasswords2`, enter. ATAU | |
# Jalankan RUN, ketik `netplwiz`, enter. | |
# | |
# Jika checkbox hilang, maka edit registry. | |
# | |
# Jalankan RUN, ketik `regedit`, enter. Copy Paste pada alamat: | |
# ``` | |
# HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\PasswordLess\Device | |
# ``` | |
# Edit value pada key `DevicePasswordLessBuildVersion` dari 2 menjadi 0. | |
# | |
# atau via Command Prompt dengan Run as Admin: | |
# ``` | |
# reg ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\PasswordLess\Device" /v DevicePasswordLessBuildVersion /t REG_DWORD /d 0 /f | |
# ``` | |
# | |
# Jika checkbox dengan label "Users must enter a username and password to use this computer." | |
# sudah muncul, maka lakukan uncheck, tekan OK, dan masukkan password username. | |
# | |
# Jika sudah autologin, maka kita perlu autolock lagi PC, agar sempurna kita | |
# jadikan PC Windows sebagai server. | |
# | |
# Jalankan RUN, ketik `shell:startup`, enter. Akan muncul direktori/folder | |
# dengan alamat: C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup | |
# | |
# Buat file bernama `lock.bat` dan tempatkan pada folder/direktori tersebut | |
# diatas. Isi file adalah sebagai berikut: | |
# ``` | |
# rundll32 user32.dll,LockWorkStation | |
# ``` | |
# powershell.exe -ExecutionPolicy Bypass -file C:\ProgramData\bin\wsl.ps1 | |
# nssm install wsl-port powershell.exe -ExecutionPolicy Bypass -file C:\ProgramData\bin\wsl.ps1 | |
$ports = @(22, 80, 443, 3306); | |
$wslAddress = wsl -u root bash -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" > $null | |
Invoke-Expression "netsh advfirewall firewall delete rule name=$port" > $null | |
Invoke-Expression "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$listenAddress connectport=$port connectaddress=$wslAddress" > $null | |
Invoke-Expression "netsh advfirewall firewall add rule name=$port dir=in action=allow protocol=TCP localport=$port" > $null | |
} | |
# Invoke-Expression "netsh interface portproxy show v4tov4" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment