#requires -Version 5.1 [CmdletBinding()] param( [string]$ManifestUrl = 'https://remote.b6-team.com/releases/windows-x86_64/latest.json', [string]$InstallRoot = (Join-Path $env:LOCALAPPDATA 'B6Remote'), [switch]$NoPath, [switch]$SkipWebView2 ) & { param($ManifestUrl, $InstallRoot, $NoPath, $SkipWebView2) $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' function Assert-DownloadUri([Uri]$Uri) { if ($Uri.Scheme -ne 'https' -and -not ($Uri.Scheme -eq 'http' -and $Uri.IsLoopback)) { throw 'Installer downloads require HTTPS (localhost is allowed for development).' } if ($Uri.UserInfo -or $Uri.Fragment) { throw 'Invalid download URL.' } } function Join-UserPath([string]$Existing, [string]$Bin) { foreach ($entry in ($Existing -split ';')) { if ([Environment]::ExpandEnvironmentVariables($entry.Trim('"')).TrimEnd('\') -ieq $Bin.TrimEnd('\')) { return $Existing } } if ([string]::IsNullOrEmpty($Existing)) { return $Bin } return $Existing.TrimEnd(';') + ';' + $Bin } function Test-WebView2 { $guid = '{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}' foreach ($key in @("HKLM:\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\$guid", "HKLM:\SOFTWARE\Microsoft\EdgeUpdate\Clients\$guid", "HKCU:\Software\Microsoft\EdgeUpdate\Clients\$guid")) { $value = Get-ItemProperty -LiteralPath $key -Name pv -ErrorAction SilentlyContinue if ($value -and $value.pv -and $value.pv -ne '0.0.0.0') { return $true } } return $false } function Write-AtomicText([string]$Path, [string]$Text) { $temp = $Path + '.' + [Guid]::NewGuid().ToString('N') + '.tmp' try { [IO.File]::WriteAllText($temp, $Text, [Text.UTF8Encoding]::new($false)) if ([IO.File]::Exists($Path)) { [IO.File]::Replace($temp, $Path, [NullString]::Value) } else { [IO.File]::Move($temp, $Path) } } finally { if ([IO.File]::Exists($temp)) { [IO.File]::Delete($temp) } } } function Assert-PackageFiles([string]$Directory, $Files) { foreach ($name in @('remote.exe','b6-remote.exe','b6-remote-desktop.exe')) { $hash = [string]$Files.$name if ($hash -notmatch '^[a-fA-F0-9]{64}$') { throw "Missing checksum for $name." } $file = Join-Path $Directory $name if (-not (Test-Path -LiteralPath $file -PathType Leaf)) { throw "Package missing $name." } if ((Get-FileHash -LiteralPath $file -Algorithm SHA256).Hash -ine $hash) { throw "Checksum mismatch: $name." } } } if (-not [Environment]::Is64BitOperatingSystem -or $env:PROCESSOR_ARCHITECTURE -eq 'ARM64' -or $env:PROCESSOR_ARCHITEW6432 -eq 'ARM64') { throw 'This release requires Windows x64.' } [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 $manifestUri = [Uri]$ManifestUrl Assert-DownloadUri $manifestUri Write-Host 'Downloading B6 Remote release information...' $manifest = Invoke-RestMethod -Uri $manifestUri -UseBasicParsing -TimeoutSec 30 if ($manifest.schema_version -ne 1 -or $manifest.platform -ne 'windows-x86_64') { throw 'Unsupported release manifest.' } $version = [string]$manifest.version if ($version -notmatch '^[0-9][a-zA-Z0-9._-]{0,79}$') { throw 'Invalid release version.' } if ([string]$manifest.sha256 -notmatch '^[a-fA-F0-9]{64}$') { throw 'Invalid archive checksum.' } $archiveUri = [Uri]::new($manifestUri, [string]$manifest.archive) Assert-DownloadUri $archiveUri if ($archiveUri.Authority -ne $manifestUri.Authority -or $archiveUri.Scheme -ne $manifestUri.Scheme) { throw 'Archive must be hosted on the manifest origin.' } $root = [IO.Path]::GetFullPath($InstallRoot).TrimEnd('\') if ($root -eq [IO.Path]::GetPathRoot($root).TrimEnd('\')) { throw 'InstallRoot must be a dedicated application directory.' } New-Item -ItemType Directory -Path $root -Force | Out-Null $lock = $null $stage = Join-Path $root ('.stage-' + [Guid]::NewGuid().ToString('N')) try { $lock = [IO.File]::Open((Join-Path $root '.install.lock'), [IO.FileMode]::OpenOrCreate, [IO.FileAccess]::ReadWrite, [IO.FileShare]::None) New-Item -ItemType Directory -Path $stage | Out-Null $archive = Join-Path $stage 'package.zip' Write-Host "Downloading B6 Remote $version..." Invoke-WebRequest -Uri $archiveUri -UseBasicParsing -OutFile $archive -TimeoutSec 300 if ((Get-FileHash -LiteralPath $archive -Algorithm SHA256).Hash -ine [string]$manifest.sha256) { throw 'Archive checksum mismatch. Existing installation was not changed.' } Add-Type -AssemblyName System.IO.Compression.FileSystem $zip = [IO.Compression.ZipFile]::OpenRead($archive) $payload = Join-Path $stage 'payload' New-Item -ItemType Directory -Path $payload | Out-Null try { # Flat allowlist also prevents zip traversal, symlinks and unexpected executables. $seen = @{} $size = 0L foreach ($entry in $zip.Entries) { if ($entry.FullName -notin @('remote.exe','b6-remote.exe','b6-remote-desktop.exe','RELEASE.txt') -or $seen.ContainsKey($entry.FullName)) { throw 'Unexpected or duplicate archive entry.' } $seen[$entry.FullName] = $true $size += $entry.Length if ($size -gt 536870912) { throw 'Archive exceeds the 512 MiB extracted size limit.' } [IO.Compression.ZipFileExtensions]::ExtractToFile($entry, (Join-Path $payload $entry.FullName), $false) } } finally { $zip.Dispose() } Assert-PackageFiles $payload $manifest.files if (-not $SkipWebView2 -and -not (Test-WebView2)) { Write-Host 'Installing Microsoft WebView2 Runtime...' $runtime = Join-Path $stage 'WebView2Setup.exe' Invoke-WebRequest -Uri 'https://go.microsoft.com/fwlink/p/?LinkId=2124703' -UseBasicParsing -OutFile $runtime -TimeoutSec 180 $signature = Get-AuthenticodeSignature -LiteralPath $runtime if ($signature.Status -ne 'Valid' -or $signature.SignerCertificate.Subject -notmatch 'O=Microsoft Corporation(?:,|$)') { throw 'Invalid Microsoft WebView2 installer signature.' } $process = Start-Process -FilePath $runtime -ArgumentList '/silent','/install' -WindowStyle Hidden -PassThru -Wait if ($process.ExitCode -notin @(0,3010) -or -not (Test-WebView2)) { throw 'WebView2 installation did not complete. Run the installer again after installing WebView2.' } } $versions = Join-Path $root 'versions' $bin = Join-Path $root 'bin' New-Item -ItemType Directory -Path $versions,$bin -Force | Out-Null $release = Join-Path $versions $version if (Test-Path -LiteralPath $release) { Assert-PackageFiles $release $manifest.files } else { [IO.Directory]::Move($payload, $release) } $launcher = Join-Path $bin 'remote.exe' $newLauncher = Join-Path $release 'remote.exe' if (-not (Test-Path -LiteralPath $launcher) -or (Get-FileHash -LiteralPath $launcher).Hash -ine [string]$manifest.files.'remote.exe') { $nextLauncher = Join-Path $bin ('remote-' + [Guid]::NewGuid().ToString('N') + '.tmp') try { [IO.File]::Copy($newLauncher, $nextLauncher) if (Test-Path -LiteralPath $launcher) { [IO.File]::Replace($nextLauncher, $launcher, [NullString]::Value) } else { [IO.File]::Move($nextLauncher, $launcher) } } finally { if (Test-Path -LiteralPath $nextLauncher) { [IO.File]::Delete($nextLauncher) } } } Write-AtomicText (Join-Path $root 'current.json') (@{version=$version; platform='windows-x86_64'; schema_version=1} | ConvertTo-Json -Compress) if (-not $NoPath) { $key = [Microsoft.Win32.Registry]::CurrentUser.CreateSubKey('Environment') try { $old = [string]$key.GetValue('Path','',[Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames) $next = Join-UserPath $old $bin if ($old -cne $next) { $key.SetValue('Path',$next,[Microsoft.Win32.RegistryValueKind]::ExpandString) } } finally { $key.Dispose() } $env:Path = Join-UserPath $env:Path $bin if (-not ('B6Remote.EnvironmentBroadcast' -as [type])) { Add-Type -TypeDefinition 'namespace B6Remote { public static class EnvironmentBroadcast { [System.Runtime.InteropServices.DllImport("user32.dll", CharSet=System.Runtime.InteropServices.CharSet.Unicode, SetLastError=true)] public static extern System.IntPtr SendMessageTimeout(System.IntPtr hwnd, uint msg, System.UIntPtr wParam, string lParam, uint flags, uint timeout, out System.UIntPtr result); } }' } $result = [UIntPtr]::Zero [void][B6Remote.EnvironmentBroadcast]::SendMessageTimeout([IntPtr]65535,0x1a,[UIntPtr]::Zero,'Environment',2,2000,[ref]$result) } Write-Host "Installed B6 Remote $version to $root" Write-Host 'Open a NEW terminal and type: remote' Write-Host 'Agent integration: remote mcp' Write-Host 'Run this installer again to update. Windows admin mode: remote --admin (approve UAC, then create a new session).' } finally { if ($lock) { $lock.Dispose() } if (Test-Path -LiteralPath $stage) { $checked = [IO.Path]::GetFullPath($stage) if ($checked.StartsWith($root + '\',[StringComparison]::OrdinalIgnoreCase) -and [IO.Path]::GetFileName($checked).StartsWith('.stage-')) { Remove-Item -LiteralPath $checked -Recurse -Force } } } } $ManifestUrl $InstallRoot $NoPath $SkipWebView2