| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- [CmdletBinding()]
- param(
- [Parameter(Mandatory = $true)]
- [string]$BaseUrl,
- [Parameter(Mandatory = $true)]
- [string]$ConfigurationPath
- )
- $ErrorActionPreference = 'Stop'
- Set-StrictMode -Version Latest
- if (-not (Test-Path -LiteralPath $ConfigurationPath -PathType Leaf)) { throw "配置文件不存在:$ConfigurationPath" }
- $configuration = Get-Content -LiteralPath $ConfigurationPath -Raw | ConvertFrom-Json
- $keyId = [string]$configuration.XiaoshuSync.KeyId
- $secret = [string]$configuration.XiaoshuSync.Secret
- if ([string]::IsNullOrWhiteSpace($keyId) -or $secret.Length -lt 32) { throw '配置文件中的 KeyId 或 Secret 无效。' }
- $base = $BaseUrl.TrimEnd('/')
- $health = Invoke-RestMethod -Uri "$base/v1/health" -Method Get -TimeoutSec 15
- if ($health.status -ne 'ok') { throw '健康检查返回异常。' }
- $baseUri = [Uri]$base
- $pathBase = $baseUri.AbsolutePath.TrimEnd('/')
- $pathAndQuery = "$pathBase/v1/manifest"
- $timestamp = [DateTimeOffset]::UtcNow.ToUnixTimeSeconds().ToString()
- $nonce = [Guid]::NewGuid().ToString('N')
- $emptyBodyHash = [Convert]::ToHexString([Security.Cryptography.SHA256]::HashData([Text.Encoding]::UTF8.GetBytes(''))).ToLowerInvariant()
- $canonical = "GET`n$pathAndQuery`n$timestamp`n$nonce`n$emptyBodyHash"
- $hmac = [Security.Cryptography.HMACSHA256]::new([Text.Encoding]::UTF8.GetBytes($secret))
- try {
- $signature = [Convert]::ToHexString($hmac.ComputeHash([Text.Encoding]::UTF8.GetBytes($canonical))).ToLowerInvariant()
- } finally {
- $hmac.Dispose()
- }
- $headers = @{
- 'X-Xiaoshu-Key-Id' = $keyId
- 'X-Xiaoshu-Timestamp' = $timestamp
- 'X-Xiaoshu-Nonce' = $nonce
- 'X-Xiaoshu-Signature' = $signature
- }
- $manifest = Invoke-RestMethod -Uri "$base/v1/manifest" -Method Get -Headers $headers -TimeoutSec 120
- if (-not $manifest.manifestTime -or @($manifest.datasets).Count -lt 10 -or $manifest.consistent -ne $true) { throw 'manifest 返回结构不完整或读取期间旧库仍在变化,请稍后重试。' }
- Write-Host "健康检查:通过($($health.time))"
- Write-Host "数据水位:$($manifest.manifestTime)"
- @($manifest.datasets) | Select-Object key, label, count, watermark | Format-Table -AutoSize
|