# Installs the Flythrough gateway for this user and starts it when they sign # in. It needs no administrator rights. Read it before you run it. $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $Base = if ($env:FLYTHROUGH_BASE) { $env:FLYTHROUGH_BASE } else { 'https://flythrough.work/gateway/0.0.0.0' } $Archives = @{ 'AMD64' = 'flythrough-gateway-x86_64-windows.zip' } # Not the gateway's own state directory, which the installer must never replace. $AppDir = Join-Path $env:LOCALAPPDATA 'Programs\flythrough-gateway' $Task = 'work.flythrough.gateway' $User = [Security.Principal.WindowsIdentity]::GetCurrent().Name function Fail($message) { throw "install: $message" } $Machine = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE } $Archive = $Archives[$Machine] if (-not $Archive) { Fail "there is no build for $Machine" } $Tmp = Join-Path ([IO.Path]::GetTempPath()) ([IO.Path]::GetRandomFileName()) New-Item -ItemType Directory $Tmp | Out-Null try { Invoke-WebRequest "$Base/SHA256SUMS" -OutFile "$Tmp\SHA256SUMS" -UseBasicParsing $line = Get-Content "$Tmp\SHA256SUMS" | Where-Object { $_ -match " \*?$([regex]::Escape($Archive))$" } if (-not $line) { Fail 'there is no build for Windows yet' } $want = ($line -split ' ')[0] Write-Host "Downloading $Archive" Invoke-WebRequest "$Base/$Archive" -OutFile "$Tmp\$Archive" -UseBasicParsing if ((Get-FileHash "$Tmp\$Archive" -Algorithm SHA256).Hash -ne $want) { Fail "checksum mismatch for $Archive" } # A running gateway holds its files. if (Get-ScheduledTask -TaskName $Task -ErrorAction SilentlyContinue) { Stop-ScheduledTask -TaskName $Task -ErrorAction SilentlyContinue } Get-Process flythrough-gateway -ErrorAction SilentlyContinue | Where-Object { $_.Path -like "$AppDir\*" } | Stop-Process -Force Start-Sleep -Seconds 1 Expand-Archive "$Tmp\$Archive" -DestinationPath "$Tmp\unpacked" if (Test-Path $AppDir) { Remove-Item -Recurse -Force $AppDir } New-Item -ItemType Directory -Force (Split-Path $AppDir) | Out-Null Move-Item "$Tmp\unpacked\flythrough-gateway" $AppDir Write-Host "Installed $AppDir" } finally { Remove-Item -Recurse -Force $Tmp -ErrorAction SilentlyContinue } $userPath = [Environment]::GetEnvironmentVariable('Path', 'User') if (($userPath -split ';') -notcontains $AppDir) { [Environment]::SetEnvironmentVariable('Path', (($userPath, $AppDir | Where-Object { $_ }) -join ';'), 'User') $env:Path = "$env:Path;$AppDir" Write-Host 'Added it to your PATH; terminals opened from now on find flythrough-gateway.' } $exe = Join-Path $AppDir 'flythrough-gateway.exe' $action = New-ScheduledTaskAction -Execute 'powershell.exe' ` -Argument "-NoProfile -WindowStyle Hidden -Command `"& '$exe' serve`"" $trigger = New-ScheduledTaskTrigger -AtLogOn -User $User $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries ` -StartWhenAvailable -RestartCount 5 -RestartInterval (New-TimeSpan -Minutes 1) ` -ExecutionTimeLimit (New-TimeSpan -Seconds 0) $principal = New-ScheduledTaskPrincipal -UserId $User -LogonType Interactive -RunLevel Limited Register-ScheduledTask -TaskName $Task -Action $action -Trigger $trigger -Settings $settings ` -Principal $principal -Description 'Flythrough gateway: web pages ask this machine''s models' -Force | Out-Null Start-ScheduledTask -TaskName $Task Write-Host 'Gateway running. Check it with: flythrough-gateway status'