218 lines
11 KiB
PowerShell
218 lines
11 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Lab capture step (b): install the pinned Sunshine bound to 127.0.0.2 only (docs/LAB_CAPTURE.md section 2).
|
|
|
|
.DESCRIPTION
|
|
Runs ELEVATED, once, started by the operator with:
|
|
Start-Process powershell -Verb RunAs -ArgumentList '-NoProfile','-ExecutionPolicy','Bypass','-File',
|
|
'<repo>\tools\lab-relay\scripts\lab-install.ps1'
|
|
That is the one UAC prompt of the lab capture.
|
|
|
|
What it does, in order, and nothing else:
|
|
1. Refuses unless elevated, unless the MSI's sha256 matches the pin, and unless no Sunshine is installed.
|
|
2. Creates keys\, secrets\, captures\ under the lab root, readable only by this account and SYSTEM.
|
|
3. msiexec /i (quiet, logged to captures\msiexec.log), then stops the service the MSI started.
|
|
4. Writes sunshine.conf: bind_address 127.0.0.2, web UI for this PC only, no UPnP, lab cert/key under keys\.
|
|
5. Sets throwaway web-UI credentials with `sunshine --creds` (they exist only in that one process's argv and
|
|
in a DPAPI-protected file; lab-api.ps1 -Action Rotate replaces them).
|
|
6. Starts the service and checks that every Sunshine listener is on 127.0.0.2. Anything else: stop, fail.
|
|
It does not touch Windows Defender, firewall rules (it only lists the ones the MSI added), or any other app.
|
|
It never prints, logs or writes a credential in plain text.
|
|
#>
|
|
[CmdletBinding()]
|
|
param(
|
|
[string] $LabRoot = 'E:\build\play-remotely\lab',
|
|
[string] $Msi = 'E:\build\play-remotely\lab\downloads\Sunshine-Windows-AMD64-installer.msi'
|
|
)
|
|
|
|
Set-StrictMode -Version 2.0
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
# --- Pins (release API, docs/LAB_CAPTURE.md section 2) ------------------------------------------------------------
|
|
$PinnedSha256 = '1d7fed8beecd5889dc7ff14cf9f42d6d38f37c3066c13c6c2a5f4e91847e0ccf'
|
|
$PinnedSize = 33710080
|
|
$BindAddress = '127.0.0.2'
|
|
$Ports = @(47984, 47989, 47990, 48010)
|
|
|
|
function Fail([string] $Why) {
|
|
Write-Host "lab-install: STOPPED: $Why"
|
|
exit 1
|
|
}
|
|
|
|
function Say([string] $What) {
|
|
Write-Host "lab-install: $What"
|
|
}
|
|
|
|
# --- 1. Preconditions --------------------------------------------------------------------------------------------
|
|
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
|
|
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
|
Fail 'not elevated; start it with Start-Process -Verb RunAs'
|
|
}
|
|
if (-not (Test-Path -LiteralPath $Msi -PathType Leaf)) { Fail "MSI not found: $Msi" }
|
|
$msiItem = Get-Item -LiteralPath $Msi
|
|
if ($msiItem.Length -ne $PinnedSize) { Fail "MSI size $($msiItem.Length) is not the pinned $PinnedSize" }
|
|
$hash = (Get-FileHash -LiteralPath $Msi -Algorithm SHA256).Hash.ToLowerInvariant()
|
|
if ($hash -ne $PinnedSha256) { Fail "MSI sha256 $hash does not match the pin; delete the file" }
|
|
Say 'MSI matches the pinned sha256'
|
|
|
|
if (Get-Service -Name '*sunshine*' -ErrorAction SilentlyContinue) { Fail 'a Sunshine service already exists' }
|
|
if (Test-Path -LiteralPath 'C:\Program Files\Sunshine\sunshine.exe') { Fail 'C:\Program Files\Sunshine\sunshine.exe already exists' }
|
|
$busy = Get-NetTCPConnection -State Listen -ErrorAction SilentlyContinue | Where-Object { $Ports -contains $_.LocalPort }
|
|
if ($busy) { Fail 'a GameStream port is already in use' }
|
|
|
|
# --- 2. Lab folders, this account + SYSTEM only ------------------------------------------------------------------
|
|
$account = [Security.Principal.WindowsIdentity]::GetCurrent().Name
|
|
$keys = Join-Path $LabRoot 'keys'
|
|
$secrets = Join-Path $LabRoot 'secrets'
|
|
$captures = Join-Path $LabRoot 'captures'
|
|
foreach ($dir in @($keys, $secrets, $captures)) {
|
|
New-Item -ItemType Directory -Force -Path $dir | Out-Null
|
|
& icacls.exe $dir /inheritance:r /grant:r "${account}:(OI)(CI)F" '*S-1-5-18:(OI)(CI)F' | Out-Null
|
|
if ($LASTEXITCODE -ne 0) { Fail "icacls failed on $dir" }
|
|
}
|
|
$hostKey = Join-Path $keys 'sunshine-lab-key.pem'
|
|
$hostCert = Join-Path $keys 'sunshine-lab-cert.pem'
|
|
foreach ($f in @($hostKey, $hostCert)) { if (Test-Path -LiteralPath $f) { Fail "$f already exists; clean up first" } }
|
|
Say "lab folders ready under $LabRoot"
|
|
|
|
# --- 3. Install ----------------------------------------------------------------------------------------------------
|
|
$msiLog = Join-Path $captures 'msiexec.log'
|
|
$p = Start-Process -FilePath 'msiexec.exe' -ArgumentList @('/i', "`"$Msi`"", '/qn', '/norestart', '/l*v', "`"$msiLog`"") -Wait -PassThru
|
|
if ($p.ExitCode -ne 0 -and $p.ExitCode -ne 3010) { Fail "msiexec exit code $($p.ExitCode); see $msiLog" }
|
|
Say "MSI installed (exit code $($p.ExitCode))"
|
|
|
|
$service = Get-CimInstance Win32_Service | Where-Object { $_.PathName -like '*Sunshine*' } | Select-Object -First 1
|
|
if (-not $service) { Fail 'no Sunshine service after the install' }
|
|
Stop-Service -Name $service.Name -Force
|
|
Say "service $($service.Name) stopped until the config is in"
|
|
|
|
$exe = 'C:\Program Files\Sunshine\sunshine.exe'
|
|
if (-not (Test-Path -LiteralPath $exe)) { Fail "$exe not found after the install" }
|
|
$installDir = Split-Path -Parent $exe
|
|
$configDir = Join-Path $installDir 'config'
|
|
New-Item -ItemType Directory -Force -Path $configDir | Out-Null
|
|
$conf = Join-Path $configDir 'sunshine.conf'
|
|
if (Test-Path -LiteralPath $conf) { Copy-Item -LiteralPath $conf -Destination (Join-Path $captures 'sunshine.conf.as-installed') -Force }
|
|
|
|
# --- 4. Config: loopback only ------------------------------------------------------------------------------------
|
|
function Write-Conf([bool] $WithLabKeys) {
|
|
$lines = @(
|
|
"bind_address = $BindAddress",
|
|
'address_family = ipv4',
|
|
'origin_web_ui_allowed = pc',
|
|
'upnp = disabled',
|
|
"external_ip = $BindAddress",
|
|
'sunshine_name = DomiStream-lab',
|
|
'lan_encryption_mode = 1',
|
|
'wan_encryption_mode = 1',
|
|
'min_log_level = debug'
|
|
)
|
|
if ($WithLabKeys) {
|
|
# Forward slashes: how the config parser treats backslashes is not documented.
|
|
$lines += 'pkey = ' + ($hostKey -replace '\\', '/')
|
|
$lines += 'cert = ' + ($hostCert -replace '\\', '/')
|
|
}
|
|
[IO.File]::WriteAllLines($conf, [string[]] $lines, (New-Object Text.UTF8Encoding($false)))
|
|
}
|
|
Write-Conf $true
|
|
Say 'sunshine.conf written (bind_address 127.0.0.2)'
|
|
|
|
# --- 5. Throwaway credentials ------------------------------------------------------------------------------------
|
|
function New-Secret {
|
|
$bytes = New-Object byte[] 32
|
|
$rng = [Security.Cryptography.RandomNumberGenerator]::Create()
|
|
$rng.GetBytes($bytes)
|
|
$rng.Dispose()
|
|
# base64url: only [A-Za-z0-9_-], so it needs no quoting on a command line.
|
|
return ([Convert]::ToBase64String($bytes)).TrimEnd('=').Replace('+', '-').Replace('/', '_')
|
|
}
|
|
$user = 'lab' + (New-Secret).Substring(0, 12)
|
|
$pass = New-Secret
|
|
|
|
# One child process; its argv dies with it. Output is read and discarded, never written anywhere.
|
|
$psi = New-Object Diagnostics.ProcessStartInfo
|
|
$psi.FileName = $exe
|
|
$psi.Arguments = "--creds $user $pass"
|
|
$psi.WorkingDirectory = $installDir
|
|
$psi.UseShellExecute = $false
|
|
$psi.CreateNoWindow = $true
|
|
$psi.RedirectStandardOutput = $true
|
|
$psi.RedirectStandardError = $true
|
|
$proc = [Diagnostics.Process]::Start($psi)
|
|
$null = $proc.StandardOutput.ReadToEnd()
|
|
$null = $proc.StandardError.ReadToEnd()
|
|
if (-not $proc.WaitForExit(60000)) { $proc.Kill(); Fail 'sunshine --creds did not finish' }
|
|
$credsExit = $proc.ExitCode
|
|
$psi = $null
|
|
$proc.Dispose()
|
|
if ($credsExit -ne 0) { Fail "sunshine --creds exit code $credsExit" }
|
|
|
|
# DPAPI (this account): readable only by this account on this PC; lab-api.ps1 -Action Rotate replaces it.
|
|
$throwaway = Join-Path $secrets 'sunshine-throwaway.cred'
|
|
@{
|
|
user = (ConvertTo-SecureString -String $user -AsPlainText -Force | ConvertFrom-SecureString)
|
|
password = (ConvertTo-SecureString -String $pass -AsPlainText -Force | ConvertFrom-SecureString)
|
|
} | ConvertTo-Json | Set-Content -LiteralPath $throwaway -Encoding ASCII
|
|
Remove-Variable -Name user, pass -ErrorAction SilentlyContinue
|
|
Say 'throwaway credentials set and stored (DPAPI)'
|
|
|
|
# --- 6. Start and check the binding ------------------------------------------------------------------------------
|
|
function Wait-Listeners([int] $Seconds) {
|
|
$deadline = (Get-Date).AddSeconds($Seconds)
|
|
while ((Get-Date) -lt $deadline) {
|
|
$l = Get-NetTCPConnection -State Listen -ErrorAction SilentlyContinue | Where-Object { $Ports -contains $_.LocalPort }
|
|
if ($l -and (@($l | Select-Object -ExpandProperty LocalPort -Unique).Count -eq $Ports.Count)) { return $l }
|
|
Start-Sleep -Milliseconds 500
|
|
}
|
|
return $null
|
|
}
|
|
function Assert-Loopback($Listeners) {
|
|
$wrong = @($Listeners | Where-Object { $_.LocalAddress -ne $BindAddress })
|
|
if ($wrong.Count -gt 0) {
|
|
Stop-Service -Name $service.Name -Force
|
|
Fail ('Sunshine listened on ' + (($wrong | ForEach-Object { "$($_.LocalAddress):$($_.LocalPort)" }) -join ', ') + '; service stopped')
|
|
}
|
|
}
|
|
|
|
Start-Service -Name $service.Name
|
|
$listeners = Wait-Listeners 60
|
|
if (-not $listeners) {
|
|
Stop-Service -Name $service.Name -Force
|
|
Fail 'Sunshine did not listen on all four TCP ports; service stopped (bind_address 127.0.0.2 refused? see LAB_CAPTURE.md section 4 fallback)'
|
|
}
|
|
Assert-Loopback $listeners
|
|
|
|
# Did Sunshine create its pair at the lab path? If not, fall back to its default pair and copy it for the relay.
|
|
$deadline = (Get-Date).AddSeconds(30)
|
|
while (-not ((Test-Path -LiteralPath $hostKey) -and (Test-Path -LiteralPath $hostCert)) -and (Get-Date) -lt $deadline) {
|
|
Start-Sleep -Milliseconds 500
|
|
}
|
|
if (-not ((Test-Path -LiteralPath $hostKey) -and (Test-Path -LiteralPath $hostCert))) {
|
|
Say 'Sunshine did not create the lab pair at keys\; using its default pair'
|
|
Stop-Service -Name $service.Name -Force
|
|
Write-Conf $false
|
|
Start-Service -Name $service.Name
|
|
$listeners = Wait-Listeners 60
|
|
if (-not $listeners) { Stop-Service -Name $service.Name -Force; Fail 'Sunshine did not come back after the fallback' }
|
|
Assert-Loopback $listeners
|
|
$defKey = Join-Path $configDir 'credentials\cakey.pem'
|
|
$defCert = Join-Path $configDir 'credentials\cacert.pem'
|
|
if (-not ((Test-Path -LiteralPath $defKey) -and (Test-Path -LiteralPath $defCert))) { Fail 'default pair not found either' }
|
|
Copy-Item -LiteralPath $defKey -Destination $hostKey
|
|
Copy-Item -LiteralPath $defCert -Destination $hostCert
|
|
}
|
|
Say 'Sunshine listens on 127.0.0.2 only; lab certificate pair in keys\'
|
|
|
|
# --- Report what the MSI did to the firewall (read only, nothing changed) ----------------------------------------
|
|
$rules = @(Get-NetFirewallRule -ErrorAction SilentlyContinue | Where-Object { $_.DisplayName -like '*Sunshine*' })
|
|
$notes = Join-Path $captures 'install-notes.txt'
|
|
@(
|
|
"installed: $(Get-Date -Format o)",
|
|
"service: $($service.Name)",
|
|
"msi sha256: $hash",
|
|
"listeners: " + (($listeners | ForEach-Object { "$($_.LocalAddress):$($_.LocalPort)" }) -join ', '),
|
|
"firewall rules named like Sunshine (not changed): " + (($rules | ForEach-Object { "$($_.DisplayName) [$($_.Direction) $($_.Action) $($_.Enabled)]" }) -join '; ')
|
|
) | Set-Content -LiteralPath $notes -Encoding ASCII
|
|
Say "done; notes in $notes. Next, not elevated: lab-api.ps1 -Action Rotate"
|
|
exit 0
|