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 #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.Prefixes.Add("http://localhost:8081/")
$listener.Start() $listener.Start()
@@ -1399,12 +1407,23 @@ Start-Process "msedge.exe" -ArgumentList "--app=http://localhost:8081/"
try { try {
while ($listener.IsListening) { 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() $context = $listener.GetContext()
$request = $context.Request $request = $context.Request
$response = $context.Response $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) { switch ($request.Url.AbsolutePath) {
# ----------------------------------------------------------------
# ROOT ROUTE ("/")
# Serves the main HTML GUI to the client.
# ----------------------------------------------------------------
"/" { "/" {
$htmlContent = GetHtmlContent $htmlContent = GetHtmlContent
$buffer = [System.Text.Encoding]::UTF8.GetBytes($htmlContent) $buffer = [System.Text.Encoding]::UTF8.GetBytes($htmlContent)
@@ -1414,29 +1433,46 @@ try {
$response.OutputStream.Close() $response.OutputStream.Close()
} }
# ----------------------------------------------------------------
# ROUTE: /getn8npw
# Fetches the n8n password and retrieves DattoRMM site details.
# ----------------------------------------------------------------
"/getn8npw" { "/getn8npw" {
if ($request.HttpMethod -eq "POST") { if ($request.HttpMethod -eq "POST") {
$bodyStream = New-Object IO.StreamReader $request.InputStream try {
$body = $bodyStream.ReadToEnd() # Parse the JSON body to extract the password.
$data = ConvertFrom-Json $body $bodyStream = New-Object IO.StreamReader $request.InputStream
$password = $data.password $body = $bodyStream.ReadToEnd()
$data = ConvertFrom-Json $body
$password = $data.password
Get-N8nWebhookData -AuthHeaderValue $password # Call the webhook to fetch site details.
$sites = Install-DattoRMM-Helper -ApiUrl $ApiUrl -ApiKey $ApiKey -ApiSecretKey $ApiSecretKey -FetchSitesOnly Get-N8nWebhookData -AuthHeaderValue $password
if (-not $sites) { $sites = Install-DattoRMM-Helper -ApiUrl $ApiUrl -ApiKey $ApiKey -ApiSecretKey $ApiSecretKey -FetchSitesOnly
Write-Host "No sites returned. Please check the API." -ForegroundColor Red
$response.StatusCode = 500 if (-not $sites) {
$buffer = [System.Text.Encoding]::UTF8.GetBytes("No sites found") 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.Write($buffer, 0, $buffer.Length)
$response.OutputStream.Close() $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()
} }
} }