| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- # Local static JSON HTTP servers for AccountServer config fetch
- # Run 静态服务启动.bat from repo root (admin recommended for port 80)
- $root = Split-Path -Parent $PSScriptRoot
- $port80Dir = Join-Path $root "local_static\port80"
- $port8000Dir = Join-Path $root "local_static\port8000"
- $logDir = Join-Path $root "local_static\logs"
- $pidDir = Join-Path $root "local_static\pids"
- New-Item -ItemType Directory -Force -Path $logDir, $pidDir, $port80Dir, $port8000Dir | Out-Null
- function Get-PythonExe {
- $cmd = Get-Command python -ErrorAction SilentlyContinue
- if ($cmd) { return $cmd.Source }
- throw "python not found in PATH"
- }
- function Test-PortListening([int]$Port) {
- $conn = Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue
- return $null -ne $conn
- }
- function Stop-StaticServer([int]$Port) {
- $pidFile = Join-Path $pidDir "http_$Port.pid"
- if (-not (Test-Path $pidFile)) { return }
- $oldPid = [int](Get-Content $pidFile -ErrorAction SilentlyContinue)
- if ($oldPid -gt 0) {
- $proc = Get-Process -Id $oldPid -ErrorAction SilentlyContinue
- if ($proc) {
- Write-Host "[stop] port $Port old pid $oldPid"
- Stop-Process -Id $oldPid -Force -ErrorAction SilentlyContinue
- Start-Sleep -Milliseconds 500
- }
- }
- Remove-Item $pidFile -Force -ErrorAction SilentlyContinue
- }
- function Start-StaticServer([int]$Port, [string]$Directory) {
- $python = Get-PythonExe
- $logFile = Join-Path $logDir "http_$Port.log"
- $errFile = Join-Path $logDir "http_$Port.err.log"
- $pidFile = Join-Path $pidDir "http_$Port.pid"
- if (Test-PortListening $Port) {
- if (Test-Path $pidFile) {
- $existingPid = [int](Get-Content $pidFile -ErrorAction SilentlyContinue)
- $proc = Get-Process -Id $existingPid -ErrorAction SilentlyContinue
- if ($proc) {
- Write-Host "[skip] port $Port already running (pid $existingPid)"
- return $true
- }
- }
- Write-Host "[warn] port $Port is used by another process, trying to restart managed service"
- }
- Stop-StaticServer $Port
- $argList = "-m http.server $Port --bind 127.0.0.1 --directory `"$Directory`""
- Write-Host "[start] port $Port -> $Directory"
- try {
- $proc = Start-Process -FilePath $python `
- -ArgumentList $argList `
- -WindowStyle Hidden `
- -RedirectStandardOutput $logFile `
- -RedirectStandardError $errFile `
- -PassThru
- } catch {
- Write-Host "[fail] port $Port start error: $($_.Exception.Message)"
- return $false
- }
- $proc.Id | Out-File -FilePath $pidFile -Encoding ascii -NoNewline
- Start-Sleep -Seconds 1
- if ($proc.HasExited) {
- Write-Host "[fail] port $Port exited immediately, see $logFile"
- return $false
- }
- if (-not (Test-PortListening $Port)) {
- Write-Host "[fail] port $Port not listening, see $logFile"
- return $false
- }
- Write-Host "[ok] port $Port pid $($proc.Id)"
- return $true
- }
- $ok80 = Start-StaticServer -Port 80 -Directory $port80Dir
- $ok8000 = Start-StaticServer -Port 8000 -Directory $port8000Dir
- Write-Host ""
- Write-Host "verify:"
- foreach ($item in @(
- @{ Url = "http://127.0.0.1/cjw_60000_stop_server.json"; Ok = $ok80 },
- @{ Url = "http://127.0.0.1:8000/serverlist.json"; Ok = $ok8000 }
- )) {
- if (-not $item.Ok) {
- Write-Host " FAIL $($item.Url) (service not started)"
- continue
- }
- try {
- $r = Invoke-WebRequest -Uri $item.Url -UseBasicParsing -TimeoutSec 5
- Write-Host " OK $($item.Url) HTTP $($r.StatusCode)"
- } catch {
- Write-Host " FAIL $($item.Url) $($_.Exception.Message)"
- }
- }
- if (-not $ok80 -or -not $ok8000) {
- Write-Host ""
- Write-Host "tips:"
- Write-Host " - run bat as Administrator if port 80 fails"
- Write-Host " - check logs in local_static\logs\"
- Write-Host " - run 静态服务停止.bat then start again"
- exit 1
- }
|