Skip to content

Instantly share code, notes, and snippets.

@CypherpunkSamurai
Created April 7, 2025 07:45
Show Gist options
  • Save CypherpunkSamurai/a64297a77d0781153a289c8750b005bc to your computer and use it in GitHub Desktop.
Save CypherpunkSamurai/a64297a77d0781153a289c8750b005bc to your computer and use it in GitHub Desktop.
Powershell Script to Rank Mirrors from the Debian Website on Windows
<#
.SYNOPSIS
Tests latency of Debian mirrors and sorts them by response time.
.DESCRIPTION
This script fetches the Debian mirrors list from the official Debian website,
extracts all mirror URLs, tests the latency of each mirror by sending HTTP requests,
and then displays them sorted by response time (fastest first).
.NOTES
Author: CypherpunkSamurai
Date: 2025-04-07
#>
function Test-WebsiteLatency {
param (
[Parameter(Mandatory = $true)]
[string]$Url,
[string]$Country
)
try {
# Create web request
$request = [System.Net.WebRequest]::Create($Url)
$request.Method = "HEAD"
$request.Timeout = 5000 # 5 second timeout
# Measure time taken
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$response = $request.GetResponse()
$stopwatch.Stop()
# Create result object
$result = [PSCustomObject]@{
Url = $Url
Country = $Country
ResponseTime = $stopwatch.ElapsedMilliseconds
Status = "Success"
}
# Dispose response
$response.Close()
return $result
}
catch {
return [PSCustomObject]@{
Url = $Url
Country = $Country
ResponseTime = [int]::MaxValue
Status = "Failed: $($_.Exception.Message)"
}
}
}
# Main script
Write-Host "Fetching Debian mirrors list from https://www.debian.org/CD/http-ftp/" -ForegroundColor Cyan
try {
# Download the webpage
$webClient = New-Object System.Net.WebClient
$content = $webClient.DownloadString("https://www.debian.org/CD/http-ftp/")
# Parse the content to extract mirrors
Write-Host "Parsing mirrors list..." -ForegroundColor Cyan
$mirrors = @()
$pattern = '<li>([^:]+): [^<]*<a rel="nofollow" href="(http://[^"]+)"'
$matches = [regex]::Matches($content, $pattern)
foreach ($match in $matches) {
$country = $match.Groups[1].Value.Trim()
$url = $match.Groups[2].Value.Trim()
$mirrors += [PSCustomObject]@{
Country = $country
Url = $url
}
}
# Display number of mirrors found
Write-Host "Found $($mirrors.Count) mirrors." -ForegroundColor Green
# Test latency for each mirror
Write-Host "`nTesting latency of each mirror (this may take a while)..." -ForegroundColor Cyan
$results = @()
$index = 1
$totalMirrors = $mirrors.Count
foreach ($mirror in $mirrors) {
Write-Progress -Activity "Testing mirrors" -Status "$index of $totalMirrors - $($mirror.Url)" -PercentComplete (($index / $totalMirrors) * 100)
$result = Test-WebsiteLatency -Url $mirror.Url -Country $mirror.Country
$results += $result
$index++
}
# Sort and display results
Write-Host "`nResults sorted by response time (fastest first):" -ForegroundColor Green
$results | Sort-Object -Property ResponseTime | Format-Table -Property Country, Url, @{Name="Response Time (ms)"; Expression={$_.ResponseTime}}, Status -AutoSize
# Export results to CSV file (optional)
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$outputFile = "DebianMirrors_$timestamp.csv"
$results | Sort-Object -Property ResponseTime | Export-Csv -Path $outputFile -NoTypeInformation
Write-Host "Results exported to $outputFile" -ForegroundColor Green
}
catch {
Write-Host "Error: $_" -ForegroundColor Red
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment