rekabping.psm1

function rekabping {
    param(
        [string]$Target = "8.8.8.8",

        [int]$Interval = 1
    )

    # Statistics
    $PacketsSent = 0
    $PacketsReceived = 0
    $PacketsLost = 0
    $Latencies = @()

    Write-Host ""
    Write-Host "RekabPing started for $Target"
    Write-Host "Press Ctrl+C to stop and view the final analysis"
    Write-Host ""

    try {
        while ($true) {

            $Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
            $PacketsSent++

            # Run one ping and keep the normal Windows ping reply
            $PingResult = ping.exe -n 1 $Target |
                Select-String "Reply from|Request timed out|Destination host unreachable|General failure"

            if ($PingResult) {

                # Display the original Windows ping reply with timestamp
                Write-Host "$Timestamp | $PingResult"

                if ($PingResult -match "Reply from") {

                    $PacketsReceived++

                    # Extract latency for final analysis
                    if ($PingResult -match "time[=<](\d+)ms") {

                        $Latency = [int]$Matches[1]
                        $Latencies += $Latency
                    }
                }
                else {

                    $PacketsLost++
                }
            }
            else {

                $PacketsLost++

                Write-Host "$Timestamp | No response"
            }

            Start-Sleep -Seconds $Interval
        }
    }
    finally {

        # Calculate packet-loss percentage
        if ($PacketsSent -gt 0) {

            $LossPercentage = [math]::Round(
                ($PacketsLost / $PacketsSent) * 100,
                2
            )
        }
        else {

            $LossPercentage = 0
        }

        # Calculate latency statistics
        if ($Latencies.Count -gt 0) {

            $MinimumLatency = (
                $Latencies |
                Measure-Object -Minimum
            ).Minimum

            $MaximumLatency = (
                $Latencies |
                Measure-Object -Maximum
            ).Maximum

            $AverageLatency = [math]::Round(
                (
                    $Latencies |
                    Measure-Object -Average
                ).Average,
                2
            )
        }
        else {

            $MinimumLatency = "N/A"
            $MaximumLatency = "N/A"
            $AverageLatency = "N/A"
        }

        # Overall connection analysis
        if ($PacketsSent -eq 0) {

            $Status = "NO DATA"
        }
        elseif ($PacketsReceived -eq 0) {

            $Status = "NO RESPONSE - All packets failed"
        }
        elseif ($LossPercentage -ge 15) {

            $Status = "POOR - Severe packet loss ($LossPercentage%)"
        }
        elseif ($LossPercentage -ge 8) {

            $Status = "UNSTABLE - Noticeable packet loss ($LossPercentage%)"
        }
        elseif ($LossPercentage -ge 3) {

            $Status = "FAIR - Occasional packet loss ($LossPercentage%)"
        }
        elseif ($LossPercentage -gt 0) {

            $Status = "GOOD - Minor packet loss ($LossPercentage%)"
        }
        elseif ($AverageLatency -le 30) {

            $Status = "EXCELLENT"
        }
        elseif ($AverageLatency -le 80) {

            $Status = "GOOD"
        }
        elseif ($AverageLatency -le 150) {

            $Status = "FAIR - High latency"
        }
        else {

            $Status = "POOR - Very high latency"
        }

        # Final analysis
        Write-Host ""
        Write-Host "========================================"
        Write-Host " REKABPING FINAL ANALYSIS"
        Write-Host "========================================"
        Write-Host "Target: $Target"
        Write-Host "Packets Sent: $PacketsSent"
        Write-Host "Packets Received: $PacketsReceived"
        Write-Host "Packets Lost: $PacketsLost ($LossPercentage%)"
        Write-Host "Min Latency: $MinimumLatency ms"
        Write-Host "Max Latency: $MaximumLatency ms"
        Write-Host "Average Latency: $AverageLatency ms"
        Write-Host "Status: $Status"
        Write-Host "========================================"
        Write-Host ""
    }
}

Export-ModuleMember -Function rekabping