Private/AzStackHci.Connectivity.Helpers.ps1

# ////////////////////////////////////////////////////////////////////////////
# Function to test version of PowerShell
# This function checks the version of PowerShell and returns an error if the version is greater than 5.1.x
Function Test-PowerShellVersion5 {
    param
    (
        [Parameter(Mandatory = $false)]
        [bool]$NoVerboseOutput = $false
    )

    begin {
        # Check if the PowerShell version is 5.1.x
        $versionMaximum = [version]'5.1.99999.999'
        # Get the PowerShell version from the PSVersionTable
        $PowerShellVersion = [version]$PSVersionTable.PSVersion
    }

    process {
        # Compare the PowerShell version with the maximum version
        if($PowerShellVersion -gt $versionMaximum) {
            # PowerShell version is greater than 5.1.x, return an error
            if(-not($NoVerboseOutput)){
                Write-Verbose "PowerShell version $($PSVersionTable.PSVersion.ToString()) detected."
            }
            # $PowerShell5 = $false
            $PowerShell5 = $false
        } else {
            # PowerShell version is 5.1.x or lower, continue
            if(-not($NoVerboseOutput)){
                Write-Verbose "PowerShell version $($PSVersionTable.PSVersion.ToString()) detected."
            }
            # $PowerShell5 = $true
            $PowerShell5 = $true
        }
    } # End of process block

    end {
        # Write-Debug "Completed Test-PowerShellVersion5 function"
        return $PowerShell5
    }
}



# ////////////////////////////////////////////////////////////////////////////
# This function converts a hashtable to a string representation
Function Convert-HashTableToString {

    param
    (
        [Parameter(Mandatory = $true)]
        [System.Collections.Hashtable]
        $HashTable
    )

    begin {
        # Write-Verbose "Starting Convert-HashTableToString function"
    }

    process {
        $HashString = "@{"
        $Keys = $HashTable.keys
        foreach ($Key in $Keys)
        {
            $v = $HashTable[$key]
            if ($key -match "\s")
            {
                $HashString += "`"$key`"" + "=" + "`"$v`"" + ";"
            }
            else
            {
                $HashString += $key + "=" + "`"$v`"" + ";"
            }
        }
        $HashString += "}"

    } # End of process block

    end {
        # Write-Debug "Completed Convert-HashTableToString function"
        return $HashString
    }
}



# ////////////////////////////////////////////////////////////////////////////
# Function to extract domains from URLs
Function Get-DomainFromURL {
    param (
        [string]$url
    )

    begin {
        # Write-Verbose "Starting Get-DomainFromURL function"
    }

    process {
        # Remove the protocol (http:// or https://) and extract the domain and port
        $url = $url -replace "^https?://", ""
        # Check if the URL contains a port number
        # If the URL contains a port number, extract it
        if ($url -match ":(\d+)$") {
            $port = [int]($url -replace ".*:(\d+)$", '$1')
            $url = $url -replace ":(\d+)$", ""
        } else {
            $port = $null
        }
        $domain = $url -split '/' | Select-Object -First 1
        return @{ Domain = $domain; Port = $port }
    }

    end {
        # Write-Debug "Completed Get-DomainFromURL function"
    }
}


# ////////////////////////////////////////////////////////////////////////////
# Helper function to normalize URL with protocol based on port
# This is a private helper function for Test-Layer7Connectivity
Function Initialize-WebRequestUrl {
    param (
        [Parameter(Mandatory=$true)]
        [string]$url,
        [Parameter(Mandatory=$true)]
        [int]$port
    )

    # Check if the port is 80 for HTTP or port 443, 8084 or 8443 for HTTPS and update the URL accordingly
    if ($port -eq $script:PORT_HTTPS -or $port -eq $script:PORT_HTTPS_ALT1 -or $port -eq $script:PORT_HTTPS_ALT2) {
        if($url -notmatch "^https://") {
            # If the URL does not start with https://, add it
            $url = "https://$url"
        }
    } elseif($port -eq $script:PORT_HTTP) {
        if($url -notmatch "^http://") {
            # If the URL does not start with http://, add it
            $url = "http://$url"
        }
    } else {
        # For custom ports, use http as the default
        if($url -notmatch "^http") {
            Write-Verbose "Custom port $port detected, using http as the default"
            $url = "http://$url"
        }
    }

    return $url
}


# ////////////////////////////////////////////////////////////////////////////
# Helper function to register a redirected URL into the RedirectedResults array
# Prevents duplicates by checking both RedirectedResults and Results arrays.
# Called from the redirect-handling section of Test-Layer7Connectivity.
Function Add-RedirectedUrlToResults {
    param (
        [Parameter(Mandatory=$true)]
        [string]$RedirectedURL,

        [Parameter(Mandatory=$true)]
        [string]$FullRedirectUrl,

        [Parameter(Mandatory=$true)]
        [int]$DestinationPort,

        [Parameter(Mandatory=$true)]
        [string]$OriginalURL,

        [Parameter(Mandatory=$true)]
        [int]$OriginalPort
    )

    Write-HostAzS "Redirected to '$FullRedirectUrl'" -ForegroundColor Yellow

    # Update the Note on the original result to show the redirect target (only if not already set)
    $ResultsToUpdate = $script:Results | Where-Object { $_.url -eq $OriginalURL -and ($_.Port -eq $OriginalPort) }
    ForEach ($ResultToUpdate in $ResultsToUpdate) {
        if ($ResultToUpdate.Note -and -not($ResultToUpdate.Note -like "Redirects to *")) {
            $ResultToUpdate.Note = "Redirects to '$($RedirectedURL)' - $($ResultToUpdate.Note)"
        }
    }

    if ([string]::IsNullOrWhiteSpace($RedirectedURL)) {
        Write-Warning "Redirected URL is null for $FullRedirectUrl, skipping"
        return
    }

    # Check if the URL already exists in the Results array
    [bool]$RedirectExistsInResults = $false
    ForEach ($result in $script:Results) {
        if ((Get-DomainFromURL -url $result.url).Domain -eq $RedirectedURL -and ($result.Port -eq $DestinationPort)) {
            Write-Debug "Redirected URL already exists in Results array, skipping"
            $RedirectExistsInResults = $true
            break
        }
    }

    # Add to RedirectedResults if it doesn't already exist in either array
    # Note: -notcontains works correctly on an empty @() array, no special first-entry handling needed
    if (($script:RedirectedResults.url -notcontains $RedirectedURL) -and (-not($RedirectExistsInResults))) {
        $OriginalResult = $script:Results | Where-Object { ((Get-DomainFromURL -url $_.URL).Domain -eq (Get-DomainFromURL -url $OriginalURL).Domain) -and ($_.Port -eq $OriginalPort) } | Select-Object -First 1
        $script:RedirectedResults.Add([PSCustomObject]@{
            RowID = 0
            url = $RedirectedURL
            redirect = $FullRedirectUrl
            Port = $DestinationPort
            ArcGateway = $false
            IsWildcard = $false
            Source = "Redirect for $($OriginalResult.Source)"
            Note = "Redirected URL from $($OriginalURL) - $($OriginalResult.Note)"
            TCPStatus = ""
            IPAddress = ""
            Layer7Status = ""
            Layer7Response = ""
            Layer7ResponseTime = ""
            CertificateSubject = ""
            CertificateIssuer = ""
            CertificateThumbprint = ""
            IntermediateCertificateIssuer = ""
            IntermediateCertificateSubject = ""
            IntermediateCertificateThumbprint = ""
            RootCertificateIssuer = ""
            RootCertificateSubject = ""
            RootCertificateThumbprint = ""
        }) | Out-Null
        Write-Debug "Added $($script:RedirectedResults[-1]) to RedirectedResults array"
    } else {
        Write-Debug "Redirected URL $RedirectedURL already exists in RedirectedResults or Results array, skipping"
    }
}


# ////////////////////////////////////////////////////////////////////////////
# Helper function to initialize ServerCertificateValidationCallback for SSL bypass
# This is a private helper function for Test-Layer7Connectivity
Function Initialize-CertificateCallback {
    # IMPORTANT: This sets a PROCESS-WIDE callback on ServicePointManager.ServerCertificateValidationCallback
    # that accepts all certificates. This is intentional — the module needs to connect to endpoints even when
    # SSL inspection replaces certificates, so it can detect and report SSL inspection to the user.
    # The callback is reset to $null in the end block of Test-Layer7Connectivity and Test-AzureLocalConnectivity.

    # Check if ServerCertificateValidationCallback class is already defined
    if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type) {
        # Define the ServerCertificateValidationCallback class
        # This class is used to bypass SSL certificate validation, to allow for SSL inspection detection
        $CertCallback = @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
    public static void Ignore()
    {
        if(ServicePointManager.ServerCertificateValidationCallback == null)
        {
            ServicePointManager.ServerCertificateValidationCallback +=
                delegate
                (
                    Object obj,
                    X509Certificate certificate,
                    X509Chain chain,
                    SslPolicyErrors errors
                )
                {
                    return true;
                };
        }
    }
}
"@

        Add-Type $CertCallback
    }
    # Call the Ignore method to bypass SSL certificate validation
    [ServerCertificateValidationCallback]::Ignore()
}


# ////////////////////////////////////////////////////////////////////////////
# Helper to set all connectivity result fields on a URL object to skip/N/A values.
# Used when a URL is skipped, wildcarded, or DNS resolution fails.
Function Set-UrlObjectSkipFields {
    param(
        [Parameter(Mandatory)][PSCustomObject]$UrlObject,
        [Parameter(Mandatory)][string]$TCPStatus,
        [Parameter(Mandatory)][string]$Layer7Status,
        [string]$IPAddress = "N/A",
        [string]$Layer7Response = "N/A",
        [string]$Layer7ResponseTime = "N/A"
    )
    $UrlObject.TCPStatus = $TCPStatus
    $UrlObject.IPAddress = $IPAddress
    $UrlObject.Layer7Status = $Layer7Status
    $UrlObject.Layer7Response = $Layer7Response
    $UrlObject.Layer7ResponseTime = $Layer7ResponseTime
    $UrlObject.CertificateIssuer = "N/A"
    $UrlObject.CertificateSubject = "N/A"
    $UrlObject.CertificateThumbprint = "N/A"
    $UrlObject.IntermediateCertificateIssuer = "N/A"
    $UrlObject.IntermediateCertificateSubject = "N/A"
    $UrlObject.IntermediateCertificateThumbprint = "N/A"
    $UrlObject.RootCertificateIssuer = "N/A"
    $UrlObject.RootCertificateSubject = "N/A"
    $UrlObject.RootCertificateThumbprint = "N/A"
}


# ////////////////////////////////////////////////////////////////////////////
# Helper to copy connectivity test results from an Invoke-EndpointConnectivityTest
# result hashtable to a URL result object.
Function Copy-ConnectivityResultsToUrlObject {
    param(
        [Parameter(Mandatory)][PSCustomObject]$UrlObject,
        [Parameter(Mandatory)][hashtable]$TestResult
    )
    $UrlObject.TCPStatus = $TestResult.TCPStatus
    $UrlObject.IPAddress = $TestResult.IPAddress
    $UrlObject.Layer7Status = $TestResult.Layer7Status
    $UrlObject.Layer7Response = $TestResult.Layer7Response
    $UrlObject.Layer7ResponseTime = $TestResult.Layer7ResponseTime
    $UrlObject.CertificateIssuer = $TestResult.CertificateIssuer
    $UrlObject.CertificateSubject = $TestResult.CertificateSubject
    $UrlObject.CertificateThumbprint = $TestResult.CertificateThumbprint
    $UrlObject.IntermediateCertificateIssuer = $TestResult.CertificateIntermediateIssuer
    $UrlObject.IntermediateCertificateSubject = $TestResult.CertificateIntermediateSubject
    $UrlObject.IntermediateCertificateThumbprint = $TestResult.CertificateIntermediateThumbprint
    $UrlObject.RootCertificateIssuer = $TestResult.CertificateRootIssuer
    $UrlObject.RootCertificateSubject = $TestResult.CertificateRootSubject
    $UrlObject.RootCertificateThumbprint = $TestResult.CertificateRootThumbprint
}