Update TGBeta.ps1
This commit is contained in:
40
TGBeta.ps1
40
TGBeta.ps1
@@ -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,15 +1433,23 @@ 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") {
|
||||||
|
try {
|
||||||
|
# Parse the JSON body to extract the password.
|
||||||
$bodyStream = New-Object IO.StreamReader $request.InputStream
|
$bodyStream = New-Object IO.StreamReader $request.InputStream
|
||||||
$body = $bodyStream.ReadToEnd()
|
$body = $bodyStream.ReadToEnd()
|
||||||
$data = ConvertFrom-Json $body
|
$data = ConvertFrom-Json $body
|
||||||
$password = $data.password
|
$password = $data.password
|
||||||
|
|
||||||
|
# Call the webhook to fetch site details.
|
||||||
Get-N8nWebhookData -AuthHeaderValue $password
|
Get-N8nWebhookData -AuthHeaderValue $password
|
||||||
$sites = Install-DattoRMM-Helper -ApiUrl $ApiUrl -ApiKey $ApiKey -ApiSecretKey $ApiSecretKey -FetchSitesOnly
|
$sites = Install-DattoRMM-Helper -ApiUrl $ApiUrl -ApiKey $ApiKey -ApiSecretKey $ApiSecretKey -FetchSitesOnly
|
||||||
|
|
||||||
if (-not $sites) {
|
if (-not $sites) {
|
||||||
Write-Host "No sites returned. Please check the API." -ForegroundColor Red
|
Write-Host "No sites returned. Please check the API." -ForegroundColor Red
|
||||||
$response.StatusCode = 500
|
$response.StatusCode = 500
|
||||||
@@ -1431,12 +1458,21 @@ try {
|
|||||||
$response.OutputStream.Close()
|
$response.OutputStream.Close()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Return site details as JSON.
|
||||||
$responseData = $sites | ConvertTo-Json
|
$responseData = $sites | ConvertTo-Json
|
||||||
$buffer = [System.Text.Encoding]::UTF8.GetBytes($responseData)
|
$buffer = [System.Text.Encoding]::UTF8.GetBytes($responseData)
|
||||||
$response.ContentType = "application/json"
|
$response.ContentType = "application/json"
|
||||||
$response.ContentLength64 = $buffer.Length
|
$response.ContentLength64 = $buffer.Length
|
||||||
$response.OutputStream.Write($buffer, 0, $buffer.Length)
|
$response.OutputStream.Write($buffer, 0, $buffer.Length)
|
||||||
$response.OutputStream.Close()
|
$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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user