Update TGBeta.ps1

This commit is contained in:
2025-01-28 01:02:37 -05:00
parent aea67d4727
commit 7e97860bf2

View File

@@ -400,9 +400,17 @@ function Install-SVSMSP {
#endregion SVS Module
# ----------------------------------------------------------------------------------
# START THE LISTENER
# START THE HTTP LISTENER
# ----------------------------------------------------------------------------------
$listener = New-Object System.Net.HttpListener
# This listener serves as the backend for handling requests from the GUI.
# It supports multiple routes, such as:
# - "/" (Root): Serves the HTML GUI.
# - "/getn8npw": Fetches n8n password and site information.
# - "/installSVSMSPModule": Triggers the installation of SVSMSP modules.
# - "/installrmm": Handles RMM installation with dynamic parameters.
# - Additional routes for tweaks and other tasks.
# Listener initialization$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add("http://localhost:8081/")
$listener.Start()
@@ -1399,12 +1407,23 @@ Start-Process "msedge.exe" -ArgumentList "--app=http://localhost:8081/"
try {
while ($listener.IsListening) {
### Process Incoming Requests
# - `$context`: Contains the request and response objects.
# - `$request`: Represents the HTTP request.
# - `$response`: Represents the HTTP response.
$context = $listener.GetContext()
$request = $context.Request
$response = $context.Response
### Route Handling
# - Routes are matched based on the `AbsolutePath` of the request URL.
# - Each route corresponds to a specific action or task.
switch ($request.Url.AbsolutePath) {
# ----------------------------------------------------------------
# ROOT ROUTE ("/")
# Serves the main HTML GUI to the client.
# ----------------------------------------------------------------
"/" {
$htmlContent = GetHtmlContent
$buffer = [System.Text.Encoding]::UTF8.GetBytes($htmlContent)
@@ -1414,29 +1433,46 @@ try {
$response.OutputStream.Close()
}
# ----------------------------------------------------------------
# ROUTE: /getn8npw
# Fetches the n8n password and retrieves DattoRMM site details.
# ----------------------------------------------------------------
"/getn8npw" {
if ($request.HttpMethod -eq "POST") {
$bodyStream = New-Object IO.StreamReader $request.InputStream
$body = $bodyStream.ReadToEnd()
$data = ConvertFrom-Json $body
$password = $data.password
try {
# Parse the JSON body to extract the password.
$bodyStream = New-Object IO.StreamReader $request.InputStream
$body = $bodyStream.ReadToEnd()
$data = ConvertFrom-Json $body
$password = $data.password
Get-N8nWebhookData -AuthHeaderValue $password
$sites = Install-DattoRMM-Helper -ApiUrl $ApiUrl -ApiKey $ApiKey -ApiSecretKey $ApiSecretKey -FetchSitesOnly
if (-not $sites) {
Write-Host "No sites returned. Please check the API." -ForegroundColor Red
$response.StatusCode = 500
$buffer = [System.Text.Encoding]::UTF8.GetBytes("No sites found")
# Call the webhook to fetch site details.
Get-N8nWebhookData -AuthHeaderValue $password
$sites = Install-DattoRMM-Helper -ApiUrl $ApiUrl -ApiKey $ApiKey -ApiSecretKey $ApiSecretKey -FetchSitesOnly
if (-not $sites) {
Write-Host "No sites returned. Please check the API." -ForegroundColor Red
$response.StatusCode = 500
$buffer = [System.Text.Encoding]::UTF8.GetBytes("No sites found")
$response.OutputStream.Write($buffer, 0, $buffer.Length)
$response.OutputStream.Close()
continue
}
# Return site details as JSON.
$responseData = $sites | ConvertTo-Json
$buffer = [System.Text.Encoding]::UTF8.GetBytes($responseData)
$response.ContentType = "application/json"
$response.ContentLength64 = $buffer.Length
$response.OutputStream.Write($buffer, 0, $buffer.Length)
$response.OutputStream.Close()
}catch {
Write-LogHybrid -Message "Error processing /getn8npw: $($_.Exception.Message)" -Level "Error"
$response.StatusCode = 500
$buffer = [System.Text.Encoding]::UTF8.GetBytes("Error: Failed to process the request.")
$response.OutputStream.Write($buffer, 0, $buffer.Length)
$response.OutputStream.Close()
continue
}
$responseData = $sites | ConvertTo-Json
$buffer = [System.Text.Encoding]::UTF8.GetBytes($responseData)
$response.ContentType = "application/json"
$response.ContentLength64 = $buffer.Length
$response.OutputStream.Write($buffer, 0, $buffer.Length)
$response.OutputStream.Close()
}
}