This commit is contained in:
2025-05-26 23:38:52 -04:00
parent dc8cbe1cae
commit 085078c808

View File

@@ -213,14 +213,17 @@ function Handle-FetchSites {
$body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
$pw = (ConvertFrom-Json $body).password
# Securely get Datto API credentials
$creds = Get-DattoApiCredentials -Password $pw
if (-not $creds) {
Write-LogHybrid "Credential fetch failed." "Error"
$Context.Response.StatusCode = 403
Respond-JSON $Context @()
Write-LogHybrid "Failed to fetch credentials." "Error" "Server"
$Context.Response.StatusCode = 500
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
return
}
# Fetch the site list using the helper
$sites = Install-DattoRMM-Helper `
-ApiUrl $creds.ApiUrl `
-ApiKey $creds.ApiKey `
@@ -229,13 +232,23 @@ function Handle-FetchSites {
if (-not $sites) {
$Context.Response.StatusCode = 500
$sites = @()
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
return
}
Respond-JSON $Context $sites
# Serialize and respond
$json = $sites | ConvertTo-Json -Depth 2
$bytes = [Text.Encoding]::UTF8.GetBytes($json)
$Context.Response.ContentType = "application/json"
$Context.Response.ContentLength64 = $bytes.Length
$Context.Response.OutputStream.Write($bytes, 0, $bytes.Length)
} catch {
Write-LogHybrid "Handle-FetchSites failed: $($_.Exception.Message)" "Error" "Server"
$Context.Response.StatusCode = 500
Respond-JSON $Context @()
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
} finally {
$Context.Response.OutputStream.Close()
}
}