Files
ucvl-zonghengjia-open-source/windows/build/Invoke-LiveCandidateAcceptance.ps1
T

99 lines
4.8 KiB
PowerShell
Raw Normal View History

#requires -Version 7.0
param(
[Parameter(Mandatory)][string]$Candidate,
[Parameter(Mandatory)][string]$Installed
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
$candidatePath = [IO.Path]::GetFullPath($Candidate)
$installedPath = [IO.Path]::GetFullPath($Installed)
foreach ($path in @($candidatePath, $installedPath)) {
if (-not (Test-Path -LiteralPath $path -PathType Leaf)) { throw "Missing client: $path" }
}
$candidateRoot = Split-Path $candidatePath
foreach ($relative in @('xray\xray.exe', 'xray\geoip.dat', 'xray\geosite.dat', 'wintun.dll')) {
$component = Join-Path $candidateRoot $relative
if (-not (Test-Path -LiteralPath $component -PathType Leaf)) {
throw "Candidate must be the complete formal payload; missing runtime component: $component"
}
}
$curl = (Get-Command curl.exe -ErrorAction Stop).Source
$started = Get-Date
$result = [ordered]@{
StartedAt = $started.ToString('o')
CandidateVersion = (Get-Item -LiteralPath $candidatePath).VersionInfo.ProductVersion
OldStopped = $false
CandidateStarted = $false
ProxyGoogle = $null
ProxyOpenAI = $null
ProxyCodex = $null
TunGoogle = $null
TunCodex = $null
CandidateExit = $null
OldRestored = $false
Error = $null
}
$candidateProcess = $null
try {
$shutdown = Start-Process -FilePath $installedPath -ArgumentList '--shutdown' `
-WorkingDirectory (Split-Path $installedPath) -PassThru -WindowStyle Hidden
if (-not $shutdown.WaitForExit(20000)) { throw '旧客户端关闭请求超时。' }
$deadline = (Get-Date).AddSeconds(40)
do {
Start-Sleep -Milliseconds 500
$old = @(Get-Process -Name VPNPlus -ErrorAction SilentlyContinue |
Where-Object { $_.Path -eq $installedPath })
} while ($old.Count -gt 0 -and (Get-Date) -lt $deadline)
if ($old.Count -gt 0) { throw '旧客户端未安全退出,未启动候选版。' }
$result.OldStopped = $true
$candidateProcess = Start-Process -FilePath $candidatePath -ArgumentList '--auto-connect' `
-WorkingDirectory (Split-Path $candidatePath) -PassThru -WindowStyle Hidden
$result.CandidateStarted = $true
$deadline = (Get-Date).AddSeconds(75)
do {
Start-Sleep -Seconds 2
if ($candidateProcess.HasExited) { throw "候选客户端提前退出,exit=$($candidateProcess.ExitCode)" }
$code = & $curl --proxy http://127.0.0.1:10809 --connect-timeout 5 --max-time 12 `
-sS -o NUL -w '%{http_code}' https://www.google.com/generate_204 2>$null
if ($code -eq '204') { break }
} while ((Get-Date) -lt $deadline)
$result.ProxyGoogle = [string](& $curl --proxy http://127.0.0.1:10809 --connect-timeout 8 --max-time 20 -sS -o NUL -w '%{http_code}' https://www.google.com/generate_204 2>$null)
$result.ProxyOpenAI = [string](& $curl --proxy http://127.0.0.1:10809 --connect-timeout 8 --max-time 20 -sS -o NUL -w '%{http_code}' https://api.openai.com/v1/models 2>$null)
$result.ProxyCodex = [string](& $curl --proxy http://127.0.0.1:10809 --connect-timeout 8 --max-time 20 -sS -o NUL -w '%{http_code}' https://chatgpt.com/backend-api/codex/models 2>$null)
$result.TunGoogle = [string](& $curl --noproxy '*' --connect-timeout 8 --max-time 20 -sS -o NUL -w '%{http_code}' https://www.google.com/generate_204 2>$null)
$result.TunCodex = [string](& $curl --noproxy '*' --connect-timeout 8 --max-time 20 -sS -o NUL -w '%{http_code}' https://chatgpt.com/backend-api/codex/models 2>$null)
}
catch {
$result.Error = $_.Exception.Message
}
finally {
if ($candidateProcess -and -not $candidateProcess.HasExited) {
$stop = Start-Process -FilePath $candidatePath -ArgumentList '--shutdown' `
-WorkingDirectory (Split-Path $candidatePath) -PassThru -WindowStyle Hidden
[void]$stop.WaitForExit(20000)
if (-not $candidateProcess.WaitForExit(45000)) {
$result.Error = (@($result.Error, '候选客户端未安全退出。') | Where-Object { $_ }) -join '; '
}
}
if ($candidateProcess -and $candidateProcess.HasExited) { $result.CandidateExit = $candidateProcess.ExitCode }
$running = @(Get-Process -Name VPNPlus -ErrorAction SilentlyContinue |
Where-Object { $_.Path -eq $installedPath })
if ($running.Count -eq 0) {
Start-Process -FilePath $installedPath -ArgumentList '--auto-connect' `
-WorkingDirectory (Split-Path $installedPath) -WindowStyle Hidden | Out-Null
}
$deadline = (Get-Date).AddSeconds(60)
do {
Start-Sleep -Seconds 2
$running = @(Get-Process -Name VPNPlus -ErrorAction SilentlyContinue |
Where-Object { $_.Path -eq $installedPath })
} while ($running.Count -eq 0 -and (Get-Date) -lt $deadline)
$result.OldRestored = $running.Count -gt 0
$result.FinishedAt = (Get-Date).ToString('o')
$result | ConvertTo-Json -Depth 4
}