$ErrorActionPreference = "Stop" $ProgressPreference = "SilentlyContinue" [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 $baseUrl = "https://headlessjkh.pages.dev" $manifestUrl = "$baseUrl/latest.json" $installDir = Join-Path $env:LOCALAPPDATA "Programs\headless-native" $installedExe = Join-Path $installDir "headless-native.exe" $wc = New-Object System.Net.WebClient $wc.Headers.Add("Cache-Control", "no-cache") $wc.Encoding = [System.Text.Encoding]::UTF8 $manifest = $null try { $raw = $wc.DownloadString($manifestUrl) $manifest = $raw | ConvertFrom-Json } catch { $shaLine = ($wc.DownloadString("$baseUrl/headless-compact.exe.sha256")).Trim() $sha = ($shaLine -split '\s+')[0] $manifest = [PSCustomObject]@{ build_stamp = "" exe_sha256 = "" setup_url = "$baseUrl/headless-compact.exe" setup_sha256 = $sha } } if (-not $manifest.setup_url -or -not ($manifest.setup_url -like "https://*")) { throw "Invalid setup_url in manifest." } # Fast path: already installed and matches latest build -> launch directly with zero downloads. if ((Test-Path $installedExe) -and $manifest.exe_sha256) { $localExeSha = (Get-FileHash -Path $installedExe -Algorithm SHA256).Hash $regStamp = $null try { $regStamp = (Get-ItemProperty -Path "HKCU:\Software\headless-native" -Name "BuildStamp" -ErrorAction SilentlyContinue).BuildStamp } catch {} $exeMatches = ($localExeSha.ToUpperInvariant() -eq $manifest.exe_sha256.Trim().ToUpperInvariant()) $stampMatches = (-not $manifest.build_stamp) -or ($regStamp -and ($regStamp.ToLowerInvariant() -eq $manifest.build_stamp.Trim().ToLowerInvariant())) if ($exeMatches -and $stampMatches) { $psi = New-Object System.Diagnostics.ProcessStartInfo $psi.FileName = $installedExe $psi.WorkingDirectory = $installDir $psi.UseShellExecute = $true [void][System.Diagnostics.Process]::Start($psi) Write-Host "[headless-native] Already up to date. Launched from $installDir" return } } # Download setup ONLY into %TEMP% (never touches Downloads), verify SHA-256, silent install, then wipe %TEMP%. $tmpDir = Join-Path $env:TEMP ("headless-install-" + [Guid]::NewGuid().ToString("N")) New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null $setupPath = Join-Path $tmpDir "setup.exe" try { $wc.Headers.Set("Cache-Control", "no-cache") $wc.DownloadFile($manifest.setup_url, $setupPath) if ($manifest.setup_sha256) { $actualSha = (Get-FileHash -Path $setupPath -Algorithm SHA256).Hash if ($actualSha.ToUpperInvariant() -ne $manifest.setup_sha256.Trim().ToUpperInvariant()) { throw "SHA-256 mismatch for setup.exe (expected $($manifest.setup_sha256), got $actualSha)" } } $psi = New-Object System.Diagnostics.ProcessStartInfo $psi.FileName = $setupPath $psi.Arguments = "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /NOCANCEL /REINSTALL" $psi.UseShellExecute = $false $psi.CreateNoWindow = $true $proc = [System.Diagnostics.Process]::Start($psi) $deadline = [DateTime]::UtcNow.AddSeconds(90) while (-not $proc.HasExited -and [DateTime]::UtcNow -lt $deadline) { Start-Sleep -Milliseconds 250 } if (-not $proc.HasExited) { throw "Installer did not finish within 90 seconds." } if ($proc.ExitCode -ne 0) { throw "Installer exited with code $($proc.ExitCode)." } Write-Host "[headless-native] Installed and started in $installDir" } finally { $wc.Dispose() for ($i = 0; $i -lt 15; $i++) { if (-not (Test-Path $tmpDir)) { break } try { Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction Stop break } catch { Start-Sleep -Milliseconds 300 } } }