Files
RustNotMC/tools/tunnel-game.ps1
T

84 lines
3.1 KiB
PowerShell

# Opens the game server to the internet through a tunnel.
#
# tools\tunnel-game.ps1 [-Port 25599] [-Remote 9000] [-Tool bore|chisel]
#
# The tunnel runs on this machine, the game server listens on the loopback,
# and the far side gets a port it can connect to. The address to put into the
# launcher is printed at the end.
#
# 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]$Port = 25599,
[int]$Remote = 9000,
[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" }
# chisel client --server ssh://user:pass@host:port --remote-forward ...
$user = 'tunnel'
$args = @(
'client',
"--server=ssh://$user`:$Secret@$Host_`:7835",
'--remote-forward', "tcp:0.0.0.0`:$Remote`:127.0.0.1`:$Port",
'--keepalive-interval', '10'
)
Write-Host "chisel: $($args -join ' ')"
} else {
if (-not (Test-Path $Bore)) { throw "no bore at $Bore" }
$args = @('local', "$Port", '--secret', $Secret, '--to', $Host_, '-p', "$Remote")
Write-Host "bore: $($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 game is at $Host_`:$Remote"
Write-Host " the local port is $Port, the server must listen on 127.0.0.1"
Write-Host " the game client asks for the host and the port; the token must be"
Write-Host " for the audience the server expects (by default 'default')"
Write-Host ''
Get-Content $Log -ErrorAction SilentlyContinue