From d75cf87695fa0be7638d48c29db91ecd1ac24325 Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Tue, 28 Jan 2025 01:15:11 -0500 Subject: [PATCH] Update TGBeta.ps1 --- TGBeta.ps1 | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/TGBeta.ps1 b/TGBeta.ps1 index 4b10dfd..17cb169 100644 --- a/TGBeta.ps1 +++ b/TGBeta.ps1 @@ -410,9 +410,29 @@ function Install-SVSMSP { # - "/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() +### Listener Initialization with Error Handling +try { + # Create a new HttpListener object + $listener = New-Object System.Net.HttpListener + + # Check if the object was successfully created + if (-not $listener) { + throw "Failed to initialize HttpListener." + } + + # Add prefix for the listener + $listener.Prefixes.Add("http://localhost:8081/") + Write-LogHybrid -Message "Listener initialized with prefix http://localhost:8081/" -Level "Info" + + # Start the listener + $listener.Start() + Write-LogHybrid -Message "Listener started successfully." -Level "Info" +} catch { + # Log the error and rethrow it for visibility + Write-LogHybrid -Message "Critical error initializing listener: $($_.Exception.Message)" -Level "Error" + throw $_ +} + function Get-N8nWebhookData { @@ -1420,7 +1440,7 @@ try { # - Each route corresponds to a specific action or task. switch ($request.Url.AbsolutePath) { - # ---------------------------------------------------------------- + # ---------------------------------------------------------------- # ROOT ROUTE ("/") # Serves the main HTML GUI to the client. # ---------------------------------------------------------------- @@ -1950,6 +1970,17 @@ catch { } finally { - $listener.Stop() - $listener.Close() + if ($listener -ne $null) { + try { + Write-LogHybrid -Message "Stopping the listener." -Level "Info" + $listener.Stop() + $listener.Close() + Write-LogHybrid -Message "Listener stopped successfully." -Level "Info" + } catch { + Write-LogHybrid -Message "Error stopping the listener: $($_.Exception.Message)" -Level "Error" + } + } else { + Write-LogHybrid -Message "Listener object is null; nothing to stop." -Level "Warning" + } } +