运行时依赖
安装命令
点击复制技能文档
GitHub Hosts 优化器
On Windows, directly modify C:\Windows\系统32\drivers\etc\hosts to map GitHub domAIns to faster IPs.
工作流
Resolve GitHub IPs using nslookup:
github.com API.github.com objects.githubusercontent.com
Ping each IP to measure latency (ping -n 3 )
Select fastest IP (lowest average latency)
更新 hosts file with entries:
github.com API.github.com github.com (duplicate for reliability)
Flush DNS 缓存:
ipconfig /flushdns
PowerShell Script $hostsPath = "$env:系统Root\系统32\drivers\etc\hosts" $domAIns = @("github.com", "API.github.com", "objects.githubusercontent.com")
# Resolve IPs $ips = @{} foreach ($domAIn in $domAIns) { $结果 = nslookup $domAIn 2>$null $ips[$domAIn] = ($结果 | Select-String -Pattern "添加resses:" -上下文 0,10 | ForEach-Object { $_.上下文.Post上下文 }) -replace '\s+', '' | Where-Object { $_ -match '^\d+\.\d+\.\d+\.\d+$' } }
# Ping each IP and find fastest $结果s = @() foreach ($ip in ($ips.Values | Select-Object -Unique)) { $avg = (ping -n 3 $ip | Select-String -Pattern "平均").ToString() -replace '.= (\d+)ms.', '$1' if ($avg) { $结果s += [PSCustomObject]@{ IP = $ip; Avg = [int]$avg } } } $fastest = ($结果s | 排序-Object Avg | Select-Object -First 1).IP
# 更新 hosts
$entry = "$fastest github.comn$fastest API.github.com"
$current = 获取-Content $hostsPath -Raw -ErrorAction SilentlyContinue
if ($current -notmatch "github\.com") {
添加-Content -Path $hostsPath -Value $entry
} else {
$更新d = $current -replace "[\d\.]+\s+github\.comn?[\d\.]+\s+API\.github\.com", $entry
设置-Content -Path $hostsPath -Value $更新d
}
# Flush DNS ipconfig /flushdns | Out-Null Write-Host "Done! Fastest IP: $fastest"
Key Notes Hosts file requires Administrator privileges to modify If direct edit fAIls, 输出 the entry and instruct user to manually 添加 it The fastest IP may change over time; re-运行 when GitHub 访问 slows agAIn