# Opens a port of this machine to the outside through a tunnel. # # tools\tunnel.ps1 [-LocalPort 3389] [-Remote 3389] [-Tool bore|chisel] # # The tunnel runs here, the far side gets a port it can connect to, and the # address to enter on the other machine is printed at the end. # # The usual use is the remote desktop: the port 3389 is forwarded and the # remote desktop client connects to the address of the tunnel. The connection # comes from the loopback, so the port of the remote desktop service does not # have to be open on the local network. # # The bore server that serves ru.zern.cc listens on 7835 and holds the secret # that the client and the server share. The old bat file on the desktop points # at zernmc.ru, which is no longer reachable; ru.zern.cc is the one that works. # # ASCII only: PowerShell 5.1 reads a .ps1 without a byte order mark using the # system ANSI code page. param( [int]$LocalPort = 3389, [int]$Remote = 3389, [string]$Tool = 'bore', [string]$Host_ = 'ru.zern.cc', [string]$Secret = 'skyzernvintageforever', [string]$Bore = "$env:USERPROFILE\Desktop\bore.exe", [string]$Chisel = "$env:USERPROFILE\Downloads\chisel.exe" ) $ErrorActionPreference = 'Stop' $Root = Split-Path -Parent $PSScriptRoot if (-not $Root) { $Root = 'G:\RustNotMC' } $LogDir = Join-Path $Root 'logs' New-Item -ItemType Directory -Force -Path $LogDir | Out-Null $Log = Join-Path $LogDir 'tunnel.log' # the tunnel that is already running goes first Get-Process bore, chisel -ErrorAction SilentlyContinue | ForEach-Object { Write-Host "stopping the old tunnel (pid $($_.Id))" Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue } Start-Sleep -Seconds 1 if ($Tool -eq 'chisel') { if (-not (Test-Path $Chisel)) { throw "no chisel at $Chisel" } $user = 'tunnel' $args = @( 'client', "--server=ssh://$user`:$Secret@$Host_`:7835", '--remote-forward', "tcp:0.0.0.0`:$Remote`:127.0.0.1`:$LocalPort", '--keepalive-interval', '10' ) } else { if (-not (Test-Path $Bore)) { throw "no bore at $Bore" } $args = @('local', "$LocalPort", '--secret', $Secret, '--to', $Host_, '-p', "$Remote") } Write-Host "$Tool : $($args -join ' ')" Remove-Item $Log, "$Log.err" -ErrorAction SilentlyContinue $proc = Start-Process -FilePath $(if ($Tool -eq 'chisel') { $Chisel } else { $Bore }) ` -ArgumentList $args -RedirectStandardOutput $Log -RedirectStandardError "$Log.err" ` -WindowStyle Hidden -PassThru Write-Host "tunnel pid $($proc.Id), log $Log" # the tunnel says when it is up $up = $false for ($i = 0; $i -lt 30; $i++) { Start-Sleep -Milliseconds 500 $text = (Get-Content $Log -Raw -ErrorAction SilentlyContinue) + (Get-Content "$Log.err" -Raw -ErrorAction SilentlyContinue) if ($text -match 'listening|Connected|connected|remote_port') { $up = $true; break } } if (-not $up) { Write-Host 'the tunnel did not say that it is up. The log:' Get-Content $Log, "$Log.err" -ErrorAction SilentlyContinue exit 1 } Write-Host '' Write-Host " the local port $LocalPort is now $Host_`:$Remote" Write-Host " connect to that address; for the remote desktop it is" Write-Host " mstsc $Host_`:$Remote" Write-Host '' Get-Content $Log -ErrorAction SilentlyContinue