<# .SYNOPSIS Synthia Terminal installer for Windows - by AI-SNS.IO .DESCRIPTION Downloads synthia-terminal--windows-x64.zip and SHA256SUMS from the download base URL (https only), refuses anything whose SHA-256 does not match, installs to %LOCALAPPDATA%\Programs\Synthia Terminal and appends that folder to the user PATH. `synthia` (synthia.exe) and `synthia-terminal` (synthia-terminal.cmd) both work. Integrity, not authenticity: SHA256SUMS comes from the same host as the archive, so the check catches a damaged or swapped download on the way, not a compromised host. TODO(signing): verify a signature over SHA256SUMS and an Authenticode signature once the AI-SNS.IO certificates exist. .EXAMPLE irm /install.ps1 | iex .NOTES Environment: SYNTHIA_TERMINAL_BASE_URL where the archives and SHA256SUMS live (https) SYNTHIA_TERMINAL_VERSION version to install (default below) SYNTHIA_TERMINAL_INSTALL_DIR default %LOCALAPPDATA%\Programs\Synthia Terminal SYNTHIA_TERMINAL_NO_PATH=1 do not change the user PATH SYNTHIA_TERMINAL_TEST_LOCAL_HTTP=1 tests only: allow http:// on 127.0.0.1 / localhost Errors are thrown, never `exit`: under `irm | iex` an exit would close the user's PowerShell window. Run as a file, an error ends with exit code 1. #> # Japanese text is kept as \u escapes so this file stays ASCII: Windows # PowerShell 5.1 reads a BOM-less script file as ANSI, not UTF-8. function U([string]$s) { return [regex]::Unescape($s) } function Install-SynthiaTerminal { $ErrorActionPreference = "Stop" $ProgressPreference = "SilentlyContinue" try { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 } catch { } try { [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 } catch { } # Served by the Synthia Platform (/admin/downloads). Override with SYNTHIA_TERMINAL_BASE_URL. $DefaultBaseUrl = "https://platform.ai-sns.io/dl/terminal" $DefaultVersion = "0.1.0" $Marker = ".synthia-terminal-install" function Fail($msg) { throw ((U "\u30a8\u30e9\u30fc") + " / error: " + $msg) } $BaseUrl = if ($env:SYNTHIA_TERMINAL_BASE_URL) { $env:SYNTHIA_TERMINAL_BASE_URL } else { $DefaultBaseUrl } $BaseUrl = $BaseUrl.TrimEnd("/") $Version = if ($env:SYNTHIA_TERMINAL_VERSION) { $env:SYNTHIA_TERMINAL_VERSION } else { $DefaultVersion } $localTest = ($env:SYNTHIA_TERMINAL_TEST_LOCAL_HTTP -eq "1") -and ($BaseUrl -match '^http://(127\.0\.0\.1|localhost)(:\d+)?(/|$)') if (-not ($BaseUrl -match '^https://' -or $localTest)) { Fail "SYNTHIA_TERMINAL_BASE_URL must be https://: $BaseUrl" } if ($Version -notmatch '^[0-9A-Za-z.-]+$') { Fail "invalid SYNTHIA_TERMINAL_VERSION: $Version" } $arch = $env:PROCESSOR_ARCHITECTURE if ($env:PROCESSOR_ARCHITEW6432) { $arch = $env:PROCESSOR_ARCHITEW6432 } if ($arch -ne "AMD64") { Fail "this build supports Windows x64 only (found $arch)" } $Asset = "synthia-terminal-$Version-windows-x64.zip" $InstallDir = if ($env:SYNTHIA_TERMINAL_INSTALL_DIR) { $env:SYNTHIA_TERMINAL_INSTALL_DIR } else { Join-Path $env:LOCALAPPDATA "Programs\Synthia Terminal" } # Only ever replace a folder this installer made (marker file). if (Test-Path -LiteralPath $InstallDir) { $hasMarker = Test-Path -LiteralPath (Join-Path $InstallDir $Marker) $isEmpty = -not (Get-ChildItem -LiteralPath $InstallDir -Force | Select-Object -First 1) if (-not $hasMarker -and -not $isEmpty) { Fail "$InstallDir exists and is not a Synthia Terminal install (no $Marker); refusing to replace it" } } # Lowercase hex SHA-256 without relying on Get-FileHash (absent on trimmed images). function Get-Sha256([string]$path) { $sha = [System.Security.Cryptography.SHA256]::Create() $stream = [System.IO.File]::OpenRead($path) try { $bytes = $sha.ComputeHash($stream) } finally { $stream.Dispose(); $sha.Dispose() } return (($bytes | ForEach-Object { $_.ToString("x2") }) -join "") } $Work = Join-Path ([System.IO.Path]::GetTempPath()) ("synthia-terminal-install-" + [guid]::NewGuid().ToString("N")) New-Item -ItemType Directory -Path $Work | Out-Null try { Write-Host ("Synthia Terminal $Version (windows-x64) " + (U "\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u3044\u307e\u3059\u2026")) $sums = Join-Path $Work "SHA256SUMS" $zip = Join-Path $Work $Asset try { Invoke-WebRequest -UseBasicParsing -Uri "$BaseUrl/SHA256SUMS" -OutFile $sums } catch { Fail "could not download $BaseUrl/SHA256SUMS" } try { Invoke-WebRequest -UseBasicParsing -Uri "$BaseUrl/$Asset" -OutFile $zip } catch { Fail "could not download $BaseUrl/$Asset" } $expected = $null foreach ($line in Get-Content -LiteralPath $sums) { if ($line -match '^([0-9a-fA-F]{64})\s+\*?(.+?)\s*$' -and $Matches[2] -eq $Asset) { $expected = $Matches[1].ToLowerInvariant() } } if (-not $expected) { Fail "$Asset is not listed in SHA256SUMS" } $actual = Get-Sha256 $zip if ($expected -ne $actual) { Write-Host "SHA-256 mismatch for $Asset`n expected: $expected`n actual: $actual" Fail "the download did not match SHA256SUMS; nothing was installed" } Write-Host "SHA-256 OK ($Asset)" $extract = Join-Path $Work "x" Expand-Archive -LiteralPath $zip -DestinationPath $extract -Force $stage = Get-ChildItem -LiteralPath $extract -Directory | Select-Object -First 1 if (-not $stage -or -not (Test-Path -LiteralPath (Join-Path $stage.FullName "synthia.exe"))) { Fail "unexpected archive layout (no synthia.exe)" } Set-Content -LiteralPath (Join-Path $stage.FullName $Marker) -Value "Synthia Terminal $Version" -Encoding ASCII # `synthia-terminal` alias: a shim next to the binary. Set-Content -LiteralPath (Join-Path $stage.FullName "synthia-terminal.cmd") -Value "@echo off`r`n`"%~dp0synthia.exe`" %*" -Encoding ASCII # Move the old install aside first, so a running synthia.exe (locked) # never blocks the new copy. $parent = Split-Path -Parent $InstallDir New-Item -ItemType Directory -Force -Path $parent | Out-Null $old = "$InstallDir.old" if (Test-Path -LiteralPath $old) { Remove-Item -LiteralPath $old -Recurse -Force -ErrorAction SilentlyContinue } if (Test-Path -LiteralPath $InstallDir) { Rename-Item -LiteralPath $InstallDir -NewName (Split-Path -Leaf $old) } Move-Item -LiteralPath $stage.FullName -Destination $InstallDir if (Test-Path -LiteralPath $old) { Remove-Item -LiteralPath $old -Recurse -Force -ErrorAction SilentlyContinue } } finally { Remove-Item -LiteralPath $Work -Recurse -Force -ErrorAction SilentlyContinue } # User PATH: read and write HKCU\Environment\Path raw, keeping its type # (REG_EXPAND_SZ) and unexpanded %VARS%, and only ever append. $pathNote = "" $key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Environment", $true) try { $raw = [string]$key.GetValue("Path", "", [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames) $parts = @($raw.Split(";") | Where-Object { $_ -ne "" }) $expanded = @($parts | ForEach-Object { [Environment]::ExpandEnvironmentVariables($_).TrimEnd("\") }) if ($expanded -notcontains $InstallDir.TrimEnd("\")) { if ($env:SYNTHIA_TERMINAL_NO_PATH -eq "1") { $pathNote = (U "PATH \u306b\u8ffd\u52a0\u3057\u3066\u304f\u3060\u3055\u3044 / add to PATH: ") + $InstallDir } else { $kind = [Microsoft.Win32.RegistryValueKind]::ExpandString if ($key.GetValueNames() -contains "Path") { $kind = $key.GetValueKind("Path") } if ($kind -ne [Microsoft.Win32.RegistryValueKind]::String) { $kind = [Microsoft.Win32.RegistryValueKind]::ExpandString } $key.SetValue("Path", ((@($parts) + @($InstallDir)) -join ";"), $kind) # Tell running programs (Explorer, new terminals) that the environment changed. [Environment]::SetEnvironmentVariable("SYNTHIA_TERMINAL_PATH_REFRESH", "1", "User") [Environment]::SetEnvironmentVariable("SYNTHIA_TERMINAL_PATH_REFRESH", $null, "User") $pathNote = (U "\u65b0\u3057\u3044\u30bf\u30fc\u30df\u30ca\u30eb\u3092\u958b\u304f\u3068 synthia \u304c\u4f7f\u3048\u307e\u3059 / open a new terminal to use synthia") } } } finally { $key.Close() } $env:Path = "$env:Path;$InstallDir" Write-Host "" Write-Host (U "Synthia Terminal \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3057\u305f\u3002``synthia login`` \u3067\u6709\u52b9\u5316\u3057\u3066\u304f\u3060\u3055\u3044\u3002") Write-Host " $InstallDir\synthia.exe (alias: synthia-terminal)" if ($pathNote) { Write-Host " $pathNote" } Write-Host (U " (Windows: \u672a\u7f72\u540d\u306e\u30d3\u30eb\u30c9\u3067\u3059\u3002SmartScreen \u304c\u8868\u793a\u3055\u308c\u305f\u3089\u300c\u8a73\u7d30\u60c5\u5831\u300d\u2192\u300c\u5b9f\u884c\u300d\u3092\u9078\u3093\u3067\u304f\u3060\u3055\u3044\u3002)") } Install-SynthiaTerminal