Skip to content

Instantly share code, notes, and snippets.

@HallexCosta
Last active December 12, 2021 01:57
Show Gist options
  • Save HallexCosta/c090c1dfe5e6ff26333ad8d29bb6aed9 to your computer and use it in GitHub Desktop.
Save HallexCosta/c090c1dfe5e6ff26333ad8d29bb6aed9 to your computer and use it in GitHub Desktop.
WSL2 Bridge Mode and XLaunch VcXsrv

Automate WSL Bridge Mode and XLaunch VcXsrv

Step 1

Select Task Schedule Library and click in Import Task...

import-task

Step 2

Select file wsl2-bridge-task-schedule.xml and open.

select-xml-file-and-open

Step 3

Click in OK to finish.

click-in-ok-to-finish

Step 4

Congratulations, now the task was imported with successfully :D

success-to-import-task


Attention

If task schedule not running the scripts on system start, download the binaries for execute manually the files .bat contain in folder scripts/bin after extract scripts.zip

Click to here for view relases of bianries.
or
Click to here for download binaries.

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2021-05-19T16:34:57.018115</Date>
<Author>LAPTOP-SCMHQMMM\hallexcosta</Author>
<URI>\wsl2-bridge-task-schedule</URI>
</RegistrationInfo>
<Triggers>
<BootTrigger>
<Enabled>true</Enabled>
</BootTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-21-1875673865-2973482659-973363300-1001</UserId>
<LogonType>InteractiveToken</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>powershell.exe</Command>
<Arguments>-ExecutionPolicy Bypass -File "C:\scripts\wsl2bridge.ps1"</Arguments>
<WorkingDirectory>C:\Windows\System32\WindowsPowerShell\v1.0</WorkingDirectory>
</Exec>
</Actions>
</Task>
# c:\scripts\wslbridge.ps1
$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
if ( $found )
{
$remoteport = $matches[0];
}
else
{
echo "The Script Exited, the ip address of WSL 2 cannot be found";
exit;
}
# [Ports]
# All the ports you want to forward separated by coma
$ports=@(80,3333,8080,8000,19000,19001);
# [Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";
# Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
# Adding Exception Rules for inbound and outbound Rules
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";
for ( $i = 0; $i -lt $ports.length; $i++ )
{
$port = $ports[$i];
iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}
echo "Done!!"
[Console]::ReadKey()
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2021-05-25T14:38:22.7179782</Date>
<Author>LAPTOP-SCMHQMMM\hallexcosta</Author>
<URI>\xlaunch-auto-run-task-schedule</URI>
</RegistrationInfo>
<Triggers>
<BootTrigger>
<Enabled>true</Enabled>
</BootTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-21-1875673865-2973482659-973363300-1001</UserId>
<LogonType>InteractiveToken</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>false</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>powershell.exe</Command>
<Arguments>-File "C:\scripts\xlaunch-auto-run.ps1"</Arguments>
</Exec>
</Actions>
</Task>
Start-Process -FilePath "C:\Program Files\VcXsrv\xlaunch.exe"-ArgumentList "-run C:\scripts\config.xlaunch"
@HallexCosta
Copy link
Author

In $ports is where they define their ports that will be opened.

Examples:

If I want to add MySQL/MariaDB port 3306

# before
$ports=@(80,3333,8080,8000,19000,19001);
# after
$ports=@(80,3333,8080,8000,19000,19001,3306);

I can also delete ports I don't use (I'll use the same example from MySQL / MariaDB)

# after
$ports=@(3306);

@HallexCosta
Copy link
Author

HallexCosta commented May 25, 2021

You can import file wsl2-bridge-task-schedule.xml

@HallexCosta
Copy link
Author

You can import file xlaunch-auto-run.xml in the same way, that import the file wsl2-bridge-task-schedule.xml

@HallexCosta
Copy link
Author

HallexCosta commented May 26, 2021

If you don't know much about bash, shell script, or directories, I recommend that you keep the files inside a scripts folder in the following directory C:\scripts so that they can work successfully

@HallanCosta
Copy link

Download XLaunch VCXSrv click for here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment