From 33027bff705a7caef31d13625832ebd1f64462e2 Mon Sep 17 00:00:00 2001 From: Tynwink2000 Date: Fri, 20 Nov 2026 11:51:22 +0800 Subject: [PATCH] feat(installer): prefer compressed release downloads --- install.ps1 | 41 ++++++++++++++++------- install.sh | 59 +++++++++++++++++++++++----------- scripts/test-installer-ps51.py | 38 ++++++++++++++++++---- 3 files changed, 102 insertions(+), 36 deletions(-) diff --git a/install.ps1 b/install.ps1 index 4019c48..827f14e 100644 --- a/install.ps1 +++ b/install.ps1 @@ -11,6 +11,13 @@ function Get-Message([string]$English, [string]$Chinese) { if ($isChinese) { $Chinese } else { $English } } +function Assert-AssetChecksum([string]$Path, [string]$Expected) { + if ((Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash -ine $Expected) { + $name = Split-Path -Leaf $Path + throw (Get-Message "Checksum verification failed for $name" "$name $([char]0x6821)$([char]0x9A8C)$([char]0x5931)$([char]0x8D25)") + } +} + $heading = Get-Message "LangBot $([char]0x00B7) Production-grade platform for building agentic IM bots $([char]0x00B7) Open source" "LangBot $([char]0x00B7) $([char]0x751F)$([char]0x4EA7)$([char]0x7EA7) AI $([char]0x5373)$([char]0x65F6)$([char]0x901A)$([char]0x4FE1)$([char]0x673A)$([char]0x5668)$([char]0x4EBA)$([char]0x5F00)$([char]0x53D1)$([char]0x5E73)$([char]0x53F0) $([char]0x00B7) $([char]0x5F00)$([char]0x6E90)" if (-not [Console]::IsOutputRedirected -and $null -eq $env:NO_COLOR -and $env:TERM -ne "dumb") { Write-Host $heading -ForegroundColor Cyan @@ -61,28 +68,40 @@ try { New-Item -ItemType Directory -Path $tempDir | Out-Null [Net.ServicePointManager]::SecurityProtocol = ` [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - Write-Output (Get-Message "[1/3] Downloading $asset ..." "[1/3] $([char]0x4E0B)$([char]0x8F7D) $asset ...") - Invoke-WebRequest -UseBasicParsing -Uri "$downloadBase/$asset" -OutFile $assetPath Invoke-WebRequest -UseBasicParsing -Uri "$downloadBase/checksums.txt" -OutFile $checksumsPath - Write-Output (Get-Message "[2/3] Verifying download ..." "[2/3] $([char]0x9A8C)$([char]0x8BC1)$([char]0x4E0B)$([char]0x8F7D) ...") $escapedAsset = [regex]::Escape($asset) - $checksumPattern = "^(?[0-9a-fA-F]{64})\s+\*?$escapedAsset$" - $expected = $null + $checksumPattern = "^(?[0-9a-fA-F]{64})\s+\*?(?$escapedAsset(?:\.gz)?)$" + $checksums = @{} foreach ($line in Get-Content -LiteralPath $checksumsPath) { $match = [regex]::Match($line.Trim(), $checksumPattern) if ($match.Success) { - $expected = $match.Groups["hash"].Value - break + $checksums[$match.Groups["name"].Value] = $match.Groups["hash"].Value } } - if (-not $expected) { + if (-not $checksums.ContainsKey($asset)) { throw (Get-Message "Checksum not found for $asset" "$([char]0x672A)$([char]0x627E)$([char]0x5230) $asset $([char]0x7684)$([char]0x6821)$([char]0x9A8C)$([char]0x548C)") } - $actual = (Get-FileHash -LiteralPath $assetPath -Algorithm SHA256).Hash - if ($actual -ine $expected) { - throw (Get-Message "Checksum verification failed for $asset" "$asset $([char]0x6821)$([char]0x9A8C)$([char]0x5931)$([char]0x8D25)") + $download = if ($checksums.ContainsKey("$asset.gz")) { "$asset.gz" } else { $asset } + $downloadPath = Join-Path $tempDir $download + Write-Output (Get-Message "[1/3] Downloading $download ..." "[1/3] $([char]0x4E0B)$([char]0x8F7D) $download ...") + Invoke-WebRequest -UseBasicParsing -Uri "$downloadBase/$download" -OutFile $downloadPath + Write-Output (Get-Message "[2/3] Verifying download ..." "[2/3] $([char]0x9A8C)$([char]0x8BC1)$([char]0x4E0B)$([char]0x8F7D) ...") + Assert-AssetChecksum $downloadPath $checksums[$download] + if ($download -ne $asset) { + $archive = $decoder = $binary = $null + try { + $archive = [System.IO.File]::OpenRead($downloadPath) + $decoder = [System.IO.Compression.GZipStream]::new($archive, [System.IO.Compression.CompressionMode]::Decompress, $true) + $binary = [System.IO.File]::Create($assetPath) + $decoder.CopyTo($binary) + } finally { + if ($binary) { $binary.Dispose() } + if ($decoder) { $decoder.Dispose() } + if ($archive) { $archive.Dispose() } + } + Assert-AssetChecksum $assetPath $checksums[$asset] } $resolvedInstallDir = [System.IO.Path]::GetFullPath($installDir) diff --git a/install.sh b/install.sh index dc42477..2c7aa00 100755 --- a/install.sh +++ b/install.sh @@ -68,33 +68,56 @@ fi temp_dir="$(mktemp -d)" trap 'rm -rf "$temp_dir"' EXIT HUP INT TERM -message "[1/3] Downloading ${asset} ..." "[1/3] 下载 ${asset} ..." -if ! curl -fsSL "${download_base}/${asset}" -o "${temp_dir}/${asset}" || - ! curl -fsSL "${download_base}/checksums.txt" -o "${temp_dir}/checksums.txt"; then - message "[FAIL] Download failed; retry the installer." "[失败] 下载失败;请重试安装。" >&2 - exit 1 -fi -message "[2/3] Verifying download ..." "[2/3] 验证下载 ..." +download_asset() { + if ! curl -fsSL "${download_base}/$1" -o "${temp_dir}/$1"; then + message "[FAIL] Download failed; retry the installer." "[失败] 下载失败;请重试安装。" >&2 + exit 1 + fi +} + +asset_checksum() { + awk -v name="$1" '($2 == name || $2 == "*" name) && length($1) == 64 && $1 !~ /[^[:xdigit:]]/ { print tolower($1); exit }' "${temp_dir}/checksums.txt" +} + +verify_asset() { + if command -v sha256sum >/dev/null 2>&1; then + actual="$(sha256sum "${temp_dir}/$1" | awk '{ print $1 }')" + elif command -v shasum >/dev/null 2>&1; then + actual="$(shasum -a 256 "${temp_dir}/$1" | awk '{ print $1 }')" + else + message "[FAIL] sha256sum or shasum is required" "[失败] 需要 sha256sum 或 shasum" >&2 + exit 1 + fi + if [ "$actual" != "$2" ]; then + message "[FAIL] Checksum verification failed for $1" "[失败] $1 校验失败" >&2 + exit 1 + fi +} -expected="$(awk -v asset="$asset" '$2 == asset { print $1; exit }' "${temp_dir}/checksums.txt")" +download_asset checksums.txt +expected="$(asset_checksum "$asset")" if [ -z "$expected" ]; then message "[FAIL] Checksum not found for ${asset}" "[失败] 未找到 ${asset} 的校验和" >&2 exit 1 fi -if command -v sha256sum >/dev/null 2>&1; then - actual="$(sha256sum "${temp_dir}/${asset}" | awk '{ print $1 }')" -elif command -v shasum >/dev/null 2>&1; then - actual="$(shasum -a 256 "${temp_dir}/${asset}" | awk '{ print $1 }')" -else - message "[FAIL] sha256sum or shasum is required" "[失败] 需要 sha256sum 或 shasum" >&2 - exit 1 +download="$asset" +compressed_checksum="$(asset_checksum "${asset}.gz")" +if [ -n "$compressed_checksum" ] && command -v gzip >/dev/null 2>&1; then + download="${asset}.gz" fi -if [ "$actual" != "$expected" ]; then - message "[FAIL] Checksum verification failed for ${asset}" "[失败] ${asset} 校验失败" >&2 - exit 1 +message "[1/3] Downloading ${download} ..." "[1/3] 下载 ${download} ..." +download_asset "$download" +message "[2/3] Verifying download ..." "[2/3] 验证下载 ..." +if [ "$download" != "$asset" ]; then + verify_asset "$download" "$compressed_checksum" + if ! gzip -dc "${temp_dir}/${download}" > "${temp_dir}/${asset}"; then + message "[FAIL] Could not decompress ${download}" "[失败] 无法解压 ${download}" >&2 + exit 1 + fi fi +verify_asset "$asset" "$expected" mkdir -p "$install_dir" cp "${temp_dir}/${asset}" "${install_dir}/${binary_name}" diff --git a/scripts/test-installer-ps51.py b/scripts/test-installer-ps51.py index e9851f9..7f0d02a 100644 --- a/scripts/test-installer-ps51.py +++ b/scripts/test-installer-ps51.py @@ -1,3 +1,4 @@ +import gzip import hashlib import http.server import os @@ -29,12 +30,22 @@ def log_message(self, *args): thread.start() try: for language in ["en", "zh-Hans"]: - for fail in [False, True]: + for mode in ["raw", "gzip", "download-failure", "bad-archive-checksum", "bad-binary-checksum"]: with tempfile.TemporaryDirectory() as directory: root = Path(directory) payload = b"installer fixture" - (root / "asset").write_bytes(payload) - (root / "checksums.txt").write_text(hashlib.sha256(payload).hexdigest() + " lbctl_windows_amd64.exe\n", encoding="ascii") + asset = "lbctl_windows_amd64.exe" + (root / asset).write_bytes(payload) + digest = "0" * 64 if mode == "bad-binary-checksum" else hashlib.sha256(payload).hexdigest() + manifest = f"{digest} {asset}\n" + download = asset + if mode in ["gzip", "bad-archive-checksum", "bad-binary-checksum"]: + download += ".gz" + archive = gzip.compress(payload, mtime=0) + (root / download).write_bytes(archive) + digest = "0" * 64 if mode == "bad-archive-checksum" else hashlib.sha256(archive).hexdigest() + manifest += f"{digest} {download}\n" + (root / "checksums.txt").write_text(manifest, encoding="ascii") harness = root / "test.ps1" harness.write_text(r''' $ErrorActionPreference = 'Stop' @@ -45,27 +56,40 @@ def log_message(self, *args): function Invoke-WebRequest { param([switch]$UseBasicParsing, [string]$Uri, [string]$OutFile) if ($env:FIXTURE_FAIL -eq '1') { throw 'injected download failure' } - $name = if ($Uri.EndsWith('checksums.txt')) { 'checksums.txt' } else { 'asset' } + $name = $Uri.Split('/')[-1] + if ($name -ne 'checksums.txt') { + $script:requestedAssets += $name + } Copy-Item (Join-Path $env:FIXTURE_ROOT $name) $OutFile } +$requestedAssets = @() try { irm $env:FIXTURE_URL | iex $result = $global:LASTEXITCODE - if ($result -eq 0 -and -not (Test-Path (Join-Path $env:LBCTL_INSTALL_DIR 'lbctl.exe'))) { throw 'missing executable' } + if ($env:FIXTURE_FAIL -ne '1' -and ($requestedAssets -join ',') -cne $env:EXPECTED_DOWNLOAD) { + throw "unexpected assets: $requestedAssets" + } + $installed = Join-Path $env:LBCTL_INSTALL_DIR 'lbctl.exe' + if ($result -eq 0) { + if ([System.IO.File]::ReadAllText($installed) -cne 'installer fixture') { throw 'wrong executable content' } + } elseif (Test-Path -LiteralPath $installed) { + throw 'installed a failed download' + } } finally { [Environment]::SetEnvironmentVariable('Path', $savedPath, 'User') } exit $result ''', encoding="ascii") env = {key: value for key, value in os.environ.items() if key.upper() != "PSMODULEPATH"} - env.update(FIXTURE_ROOT=directory, FIXTURE_FAIL="1" if fail else "0", FIXTURE_URL=f"http://127.0.0.1:{server.server_port}/install.ps1", LBCTL_LANG=language, LBCTL_INSTALL_DIR=str(root / "installed"), PROCESSOR_ARCHITECTURE="AMD64", PROCESSOR_ARCHITEW6432="AMD64", NO_COLOR="1") + env.update(FIXTURE_ROOT=directory, FIXTURE_FAIL="1" if mode == "download-failure" else "0", EXPECTED_DOWNLOAD=download, FIXTURE_URL=f"http://127.0.0.1:{server.server_port}/install.ps1", LBCTL_LANG=language, LBCTL_INSTALL_DIR=str(root / "installed"), PROCESSOR_ARCHITECTURE="AMD64", PROCESSOR_ARCHITEW6432="AMD64", NO_COLOR="1") result = subprocess.run(["powershell.exe", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", str(harness)], env=env, capture_output=True, timeout=30) text = (result.stdout + result.stderr).decode("utf-8") + fail = mode not in ["raw", "gzip"] assert result.returncode == (1 if fail else 0), text expected = ("[失败]" if language == "zh-Hans" else "[FAIL]") if fail else ("已安装 lbctl.exe" if language == "zh-Hans" else "Installed lbctl.exe") assert expected in text, text assert all(marker not in text for marker in ["ParserError", "", "CategoryInfo", "At line:"]), text - print(f"PowerShell 5.1 / text/plain without charset / {language} / failure={fail}: PASS") + print(f"PowerShell 5.1 / text/plain without charset / {language} / {mode}: PASS") finally: server.shutdown() server.server_close()