a pleasant descent into madness

Month: April 2021

Open a Windows Port to your local network

Or: “Setting a portproxy”, or: “i need an easy tool to manage my portproxies”

So, i’ve been developing a web app and wanted to use a second computer for debugging in chrome, mainly because i don’t have a second screen and i figured a tablet floating next to my display would suffice.

I broke a lot of network settings and enabled a lot of firewall rules trying to connect to a port on my host pc from the tablet, nothing worked and i really got frustrated.

A good friend of mine was kind enough to point me to “netsh interface portproxy” and the possibility to set portproxies in windows to forward connections from and to my apps.

Trying out a lot of combinations i cluttered my portproxies and had to figure out how to delete them one-by-one.

It turned out that it’s pretty easy to just set up a portproxy for the same external and internal port and the local network:

netsh interface portproxy add v4tov4 listenport=[YOUR-PORT] listenaddress=0.0.0.0 connectport=[YOUR-PORT] connectaddress=127.0.0.1

Now, i’m a pretty lazy person when it comes to tedious-typing tasks in the command-line and i really didn’t want to delete the 10 broken portproxies created while trying out different things with the netsh interface portproxy add command.

So i went ahead and wrote a small powershell script that helps me keep oversight of my setup and enables me to easily delete and create new portproxies.
It’s far from perfect, but it’s easy to use, reliable enough and you can upload it to github.

Write-Host THIS SCRIPT WILL ONLY WORK IF YOU RUN IT WITH ELEVATED PRIVILEGUES!
Write-Host List of set proxies:
Write-Host ####################
netsh interface portproxy show v4tov4
Write-Host ####################
$rord = Read-Host -Prompt 'Do you want to create a new proxy (1) or delete a proxy (2)'
if($rord -eq 1)
{
Write-Host [1] Create PortProxy
$local = Read-Host -Prompt 'Do you want to open a port to the local network? (listenaddr 0.0.0.0, connectaddr 127.0.0.1, same listen and connect port), type (yes)'
if($local -eq 'yes')
{
Write-Host [YES] Opening port to local network, set listenaddr = 0.0.0.0, connectaddr 127.0.0.1
$port = Read-Host -Prompt 'Please input the port that you want to open to the network'
netsh interface portproxy add v4tov4 listenport=$port listenaddress=0.0.0.0 connectport=$port connectaddress=127.0.0.1
Write-Host 'Done, press any key to exit'
pause
exit
}
Write-Host [NO] Opening port with full manual settings, you will need to enter the listen port, listen addr, connect port and connect addr.
$lport = Read-Host -Prompt 'Input port to listen to'
$laddr = Read-Host -Prompt 'Input address to listen to'
$cport = Read-Host -Prompt 'Input port to connect to'
$caddr = Read-Host -Prompt 'Input address to connect to'
netsh interface portproxy add v4tov4 listenport=$lport listenaddress=$laddr connectport=$cport connectaddress=$caddr
Write-Host 'Done, press any key to exit'
pause
exit
}
if($rord -eq 2)
{
Write-Host To delete a portproxy, you will need to enter the listen port and listen address
$lp = Read-Host -Prompt 'Enter the port of the proxy you want to delete, you will enter the listenaddress in the next step'
$la = Read-Host -Prompt 'Enter the listenaddress of the proxy you want to delete'
netsh interface portproxy delete v4tov4 listenport=$lp listenaddr=$la
}
Write-Host Done
 pause
 exit

You’ll probably have to create a loop outside the script if you want to do more than one thing per session, this was enough for my use-case and i left it be.

Alternatively, i found “PortProxyGUI” from “zmjack” on github. It’s nice!

Edit:
Trouble running the script because execution of powershell scripts is disabled on your system?

Enjoy and have fun!

WSL2 Docker not talking to Windows Host

Or: “WSL2 Docker is not connecting to my Host!!!! HELP!”

In another post i already talked about a setup to get both Windows Host / Windows Host Docker and WSL Docker talking to each other.
The setup broke at some point and i figured out a pretty automated solution for this… for now, let’s hope it works after the next WSL or Docker update.

My solution was following Setup:

1- Start Docker in WSL automatically on BOOT

2- Wait a minute after Logon and start Docker Desktop (this post)

Prerequisites:
– Docker Desktop WSL Integration is turned off.
Docker on WSL has been detached from Docker Desktop for the Windows Host
– Docker Desktop Autostart is disabled

Now, assuming you followed part 1.
Let’s continue and create a file and paste this powershell script provided by StackOverflow User StingyJack (Thanks m8!).

In my case, the file is running in “C:/Sources/Scripts_Autostart” and is called “docker-restart.ps1”.

Write-Output "$((Get-Date).ToString("HH:mm:ss")) - Restarting docker"

foreach($svc in (Get-Service | Where-Object {$_.name -ilike "*docker*" -and $_.Status -ieq "Running"}))
{
    $svc | Stop-Service -ErrorAction Continue -Confirm:$false -Force
    $svc.WaitForStatus('Stopped','00:00:20')
}

Get-Process | Where-Object {$_.Name -ilike "*docker*"} | Stop-Process -ErrorAction Continue -Confirm:$false -Force

foreach($svc in (Get-Service | Where-Object {$_.name -ilike "*docker*" -and $_.Status -ieq "Stopped"} ))
{
    $svc | Start-Service 
    $svc.WaitForStatus('Running','00:00:20')
}

Write-Output "$((Get-Date).ToString("HH:mm:ss")) - Starting Docker Desktop"
& "C:\Program Files\Docker\Docker\Docker Desktop.exe"
$startTimeout = [DateTime]::Now.AddSeconds(90)
$timeoutHit = $true
while ((Get-Date) -le $startTimeout)
{

    Start-Sleep -Seconds 10
    $ErrorActionPreference = 'Continue'
    try
    {
        $info = (docker info)
        Write-Verbose "$((Get-Date).ToString("HH:mm:ss")) - `tDocker info executed. Is Error?: $($info -ilike "*error*"). Result was: $info"

        if ($info -ilike "*error*")
        {
            Write-Verbose "$((Get-Date).ToString("HH:mm:ss")) - `tDocker info had an error. throwing..."
            throw "Error running info command $info"
        }
        $timeoutHit = $false
        break
    }
    catch 
    {

        if (($_ -ilike "*error during connect*") -or ($_ -ilike "*errors pretty printing info*")  -or ($_ -ilike "*Error running info command*"))
        {
            Write-Output "$((Get-Date).ToString("HH:mm:ss")) -`t Docker Desktop startup not yet completed, waiting and checking again"
        }
        else
        {
            Write-Output "Unexpected Error: `n $_"
            return
        }
    }
    $ErrorActionPreference = 'Stop'
}
if ($timeoutHit -eq $true)
{
    throw "Timeout hit waiting for docker to startup"
}

Write-Output "$((Get-Date).ToString("HH:mm:ss")) - Docker restarted"

Next up: Open Task Scheduler and create a new Basic Task

Name it like you want, i chose “Start Docker Desktop after WSL Docker”.
Next, then select “When i log on”

Next: set “Program/Script” to Powershell
Then set argument to

-windowstyle hidden -ExecutionPolicy Bypass -file C:\Sources\Scripts_Autorun\docker-restart.ps1

Of course you will choose the folder where you put the script.
don’t forget to add quotations if your path contains spaces!

Click next and after finishing the setup open the Task back up again. (Edit properties)

In the General Tab set “Run only when user is logged on” and “Run on highest privilegues”

Open the Trigger in the “Triggers” tab and set a Delay for 1 minute.

That’s it!
Have, of course you will have to chill about a minute or two after rebooting your pc.
If you’re faster than your scripts, this won’t work.
You might also want to adjust the delay time for the script for part 1. and 2.

Enable running Powershell scripts on Windows 10

Or Fix ” Script_XYZ cannot be loaded because
running scripts is disabled on this system. For more information, see about_Execution_Policies at
https:/go.microsoft.com/fwlink/?LinkID=135170.”

Easy fix, open up an administrative powershell and enter

Set-ExecutionPolicy unrestricted

Warning:
It is out of the question that this makes your computer vulnerable for attacks via PS scripts.
Use at your own risk, know what you are doing.

If you do not want to accept the security risks of enabling scripts system-wide, you might want to check out this single-case solution which you can use to bypass restricted execution for single powershell scripts
(from an elevated powershell)

powershell.exe -noprofile -executionpolicy bypass -file .\yourScript.ps1

Enjoy!

Start WSL 2 Service on Host startup

Or: “I don’t want to manually startup everything in WSL after every boot”

For me, that’s docker.
My docker is running independantly of docker desktop on the host so i want it to startup when the PC is booted.

Following easy steps to set it up in Task Scheduler:

  • Open up Task Scheduler
  • Create new Basic Task
  • Name it like you will, mine was “Autostart of WSL Docker service on boot”
  • Next, then choose “When the Computer Starts”
  • Next, then “Start a program”
  • In “Program/script” find WSL.exe, it should be located like so:
    “C:/Windows/System32/wsl.exe”
  • In “Add arguments (optional)” enter following:
    -d Ubuntu-20.04 -u root service docker start
  • Finish the setup via “Next” then “Finish”.
  • Run it to test it.

Instead of “Ubuntu-20.04” you might want to find your correct distro name by entering
“wsl –list” into a commandline on windows, you’ll need to use the same distro name displayed for the distro of your choosing.

Edit:
if this doesn’t immediately work for you, you might want to open up the task in task-scheduler, navigate to “triggers”, open the trigger and set “Delay Task for” to 30 seconds or any amount that makes sense for your system start-up time.

That’s it.
Enjoy.
Many thanks to StackOverflow user octagon_octopus

Get Docker running in WSL 2 independently of Docker Desktop

Or: “Get Docker running in WSL without WSL Integration”

Recently, i’ve had a special case where i had to develop a project with Docker containers running on the Windows Host Docker, other containers running in WSL and some of the tools running directly on the Windows Host.
(Basically, a big confusing mess).

To get this 3-way-communication going, i actually had to install Docker in WSL independantly from Docker Desktop on my Host.
This turned out to be a little tricky, just because i didn’t really know where to look up the sources.

So if anyone of you have these special needs, feel free to follow me along.
Take these steps:

  • Install Docker Desktop normally on your host, do not enable WSL Integration but install all tools necessary for WSL Integration (just in case)

  • Open up your WSL and execute following commands:
sudo apt-get update
 sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Get the Docker GPG Key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Set up stable repository:

 echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Do another update

sudo apt-get update

Install docker engine independantly of host:

 sudo apt-get install docker-ce docker-ce-cli containerd.io

After this, i restart my WSL instance (Close and open up again) and run

sudo service docker start

And check if docker is running correctly:

sudo docker run hello-world

This is basically copied and condensed from

https://docs.docker.com/engine/install/ubuntu/ [12th of April 2021]

As i don’t know if the site content ever changes i wanted to have a copy of the steps myself.

Now you have docker only in wsl, you will have to start up the service manually if you open up your WSL instance for the first time or if WSL is stopped completely.
But i’ll offer a fix for that later.

This will also work without Docker installed on the host, so both instances don’t know about each other and Docker Desktop cannot f* with your network too much.

© 2024 Yavuz-Support Blog

Theme by Anders NorenUp ↑