as

mail@pastecode.io avatar
unknown
plain_text
5 months ago
2.7 kB
2
Indexable
# Function to check if a port is open using Test-NetConnection
function Check-Port {
    param (
        [string]$domain,
        [int]$port,
        [string]$protocol
    )
    
    if ($protocol -eq "UDP") {
        $result = Test-NetConnection -ComputerName $domain -Port $port -InformationLevel Detailed -WarningAction SilentlyContinue
    } else {
        $result = Test-NetConnection -ComputerName $domain -Port $port -WarningAction SilentlyContinue
    }

    if ($result.TcpTestSucceeded -eq $false -and $protocol -eq "TCP") {
        $script:blocked_ports += "$domain ($protocol): $port"
    } elseif ($result.UdpTestSucceeded -eq $false -and $protocol -eq "UDP") {
        $script:blocked_ports += "$domain ($protocol): $port"
    }
}

# Initialize blocked_ports array
$script:blocked_ports = @()

# Total number of ports to check
$total_ports = 1 + 2 + (65535 - 49152 + 1) # STUN + 2 signaling + UDP range
$current_port = 0

# Set the maximum number of concurrent jobs
$MAX_JOBS = 10
$jobs = @()

# Function to manage concurrent jobs
function Run-With-Limit {
    param (
        [scriptblock]$scriptblock
    )
    
    while ($jobs.Count -ge $MAX_JOBS) {
        $completedJob = Wait-Job -Any $jobs
        $jobs = $jobs | Where-Object { $_.Id -ne $completedJob.Id }
    }
    $job = Start-Job -ScriptBlock $scriptblock
    $jobs += $job
}

# Check STUN/TURN Servers
$stun_ports = @(3478)
foreach ($port in $stun_ports) {
    $current_port++
    Write-Host "[$current_port/$total_ports] Checking STUN port $port..." -NoNewline
    Run-With-Limit { Check-Port -domain "remoteviewer.esper.cloud" -port $using:port -protocol "TCP" }
}

# Check Signaling Ports
$signaling_ports = @(80, 443)
foreach ($port in $signaling_ports) {
    $current_port++
    Write-Host "[$current_port/$total_ports] Checking signaling port $port..." -NoNewline
    Run-With-Limit { Check-Port -domain "status.esper.cloud" -port $using:port -protocol "TCP" }
}

# Check Media Ports (Dynamic Range)
foreach ($port in 49152..65535) {
    $current_port++
    Write-Host "[$current_port/$total_ports] Checking UDP port $port..." -NoNewline
    Run-With-Limit { Check-Port -domain "remoteviewer.esper.cloud" -port $using:port -protocol "UDP" }
}

# Wait for all background tasks to finish
$jobs | Wait-Job | Out-Null

# Final output
Write-Host

# Display blocked ports if any
if ($script:blocked_ports.Count -eq 0) {
    Write-Host "Port checking complete: all ports are open. 🎉"
} else {
    Write-Host "Port checking complete: Blocked or unreachable ports:"
    foreach ($port in $script:blocked_ports) {
        Write-Host "❌ $port"
    }
}

# Clean up jobs
$jobs | Remove-Job
Leave a Comment