Set-StrictMode -Version 2.0 $ErrorActionPreference = "Stop" $sshCommand = Get-Command ssh.exe -ErrorAction SilentlyContinue if (-not $sshCommand) { Write-Error "OpenSSH Client is not installed. Add it from Windows Optional Features." return } $rawPort = Read-Host "Local port" $localPort = 0 if (-not [int]::TryParse($rawPort, [ref]$localPort) -or $localPort -lt 1 -or $localPort -gt 65535) { Write-Error "The local port must be a number from 1 to 65535." return } $portIsOpen = $false foreach ($address in @("127.0.0.1", "::1")) { $client = New-Object System.Net.Sockets.TcpClient try { $connectTask = $client.ConnectAsync($address, $localPort) if ($connectTask.Wait(1500) -and $client.Connected) { $portIsOpen = $true break } } catch { # Try the other loopback address before reporting failure. } finally { $client.Dispose() } } if (-not $portIsOpen) { Write-Error "Nothing is listening on localhost:$localPort. Start the local development server first." return } $knownHostLine = "[terminal.sudorgin.com]:22222 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAdWHOoQ80tqid08wdBMh+q5pRz31lULb+Hg6wIQVczN" $knownHostsPath = Join-Path ([System.IO.Path]::GetTempPath()) ("sudorgin-dev-tunnel-" + [guid]::NewGuid().ToString("N") + ".known_hosts") $utf8NoBom = New-Object System.Text.UTF8Encoding($false) [System.IO.File]::WriteAllText($knownHostsPath, $knownHostLine + [Environment]::NewLine, $utf8NoBom) $sshArguments = @( "-p", "22222", "-R", "9999:localhost:$localPort", "-o", "ExitOnForwardFailure=yes", "-o", "ServerAliveInterval=30", "-o", "ServerAliveCountMax=3", "-o", "TCPKeepAlive=yes", "-o", "PubkeyAuthentication=no", "-o", "PreferredAuthentications=keyboard-interactive,password", "-o", "StrictHostKeyChecking=yes", "-o", "UserKnownHostsFile=$knownHostsPath", "-N", "creator@terminal.sudorgin.com" ) Write-Host "Publishing localhost:$localPort as https://dev.sudorgin.com" -ForegroundColor Cyan Write-Host "SSH will ask for the server password. Keep this window open; press Ctrl+C to stop." -ForegroundColor Yellow try { & $sshCommand.Source @sshArguments if ($LASTEXITCODE -ne 0) { Write-Error "SSH exited with code $LASTEXITCODE. Another tunnel may already be using the public endpoint." } } finally { Remove-Item -LiteralPath $knownHostsPath -Force -ErrorAction SilentlyContinue }