diff --git a/server/internal/api/handlers.go b/server/internal/api/handlers.go index 1c0c953..011a9ab 100644 --- a/server/internal/api/handlers.go +++ b/server/internal/api/handlers.go @@ -21,6 +21,7 @@ func actorFromCtx(c *gin.Context) string { func RegisterRoutes(r *gin.Engine) { r.GET("/install", handleInstallScript) + r.GET("/install.ps1", handleInstallScriptWindows) r.GET("/update", handleUpdateScript) // ESO read endpoint — bearer-token auth, not session auth, so Kubernetes diff --git a/server/internal/api/install_windows.go b/server/internal/api/install_windows.go new file mode 100644 index 0000000..8890787 --- /dev/null +++ b/server/internal/api/install_windows.go @@ -0,0 +1,56 @@ +package api + +import ( + "fmt" + "net/http" + "os" + + "github.com/gin-gonic/gin" +) + +func handleInstallScriptWindows(c *gin.Context) { + serverID := c.Query("server_id") + token := c.Query("token") + + giteaHost := os.Getenv("GITEA_HOST") + if giteaHost == "" { + giteaHost = "gitea.example.com" + } + grpcHost := os.Getenv("GRPC_HOST") + if grpcHost == "" { + grpcHost = os.Getenv("PUBLIC_HOST") + } + if grpcHost == "" { + grpcHost = "vantage.example.com" + } + + script := fmt.Sprintf( + "#Requires -RunAsAdministrator\n"+ + "$ErrorActionPreference = \"Stop\"\n"+ + "\n"+ + "$ServerId = \"%s\"\n"+ + "$Token = \"%s\"\n"+ + "$GiteaHost = \"%s\"\n"+ + "$ServerUrl = \"%s\" -replace '^https?://',''\n"+ + "\n"+ + "$rel = Invoke-RestMethod -Uri \"https://$GiteaHost/api/v1/repos/mrhid6/vantage/releases?limit=10\"\n"+ + "$tag = ($rel | Where-Object { $_.tag_name -like 'agent/v*' } | Select-Object -First 1).tag_name\n"+ + "if (-not $tag) { throw \"Could not determine latest agent version\" }\n"+ + "$enc = $tag -replace '/','%%2F'\n"+ + "$base = \"https://$GiteaHost/mrhid6/vantage/releases/download/$enc\"\n"+ + "\n"+ + "$tmp = Join-Path $env:TEMP \"vantage-agent.msi\"\n"+ + "Invoke-WebRequest -Uri \"$base/vantage-agent.msi\" -OutFile $tmp\n"+ + "Invoke-WebRequest -Uri \"$base/checksums-msi.txt\" -OutFile \"$env:TEMP\\checksums-msi.txt\"\n"+ + "\n"+ + "$expected = (Get-Content \"$env:TEMP\\checksums-msi.txt\" | Select-String 'vantage-agent.msi').ToString().Split()[0]\n"+ + "$actual = (Get-FileHash $tmp -Algorithm SHA256).Hash.ToLower()\n"+ + "if ($expected -ne $actual) { throw \"Checksum mismatch\" }\n"+ + "\n"+ + "Start-Process msiexec.exe -Wait -ArgumentList \"/i `\"$tmp`\" /qn SERVERID=$ServerId TOKEN=$Token SERVERURL=$ServerUrl\"\n"+ + "Write-Host \"Vantage agent installed.\"\n", + serverID, token, giteaHost, grpcHost) + + c.Header("Content-Type", "text/plain; charset=utf-8") + c.String(http.StatusOK, script) +}