您现在的位置是:首页 >学无止境 >解决raw.githubusercontent.com访问问题的ps1脚本网站首页学无止境

解决raw.githubusercontent.com访问问题的ps1脚本

m0_52111823 2025-03-23 12:01:02
简介github 打开缓慢

参考https://blog.csdn.net/Naisu_kun/article/details/118957940,还有https://zhuanlan.zhihu.com/p/438447914
使用deepseek完成编写和测试
前提:本地代理服务器127.0.0.1:7890打开,以管理员身份运行powershell

思路:通过国内的DNS服务商,查询域名的IP,并curl+代理的方式测试能否连通,能连通就保存。最后将结果除重,并写入hosts文件。

问题中的默认样例就是raw.githubusercontent.com,但是允许用户输入新的域名进行解析,添加到hosts中。

# Define a list of public DNS servers
$dnsServers = @(
    "114.114.115.115",
    "223.5.5.5",
    "223.6.6.6",
    "180.76.76.76",
    "8.8.8.8",
    "114.114.114.114"
)

$proxy = "http://127.0.0.1:7890"

# Set the default domain to raw.githubusercontent.com
$defaultDomain = "raw.githubusercontent.com"

# Prompt the user to enter a domain name
$domain = Read-Host "Please enter a domain name (e.g., $defaultDomain)"

# If the user does not enter anything, use the default domain
if ([string]::IsNullOrWhiteSpace($domain)) {
    $domain = $defaultDomain
}

# Function to resolve a domain using a specific DNS server
function Resolve-Domain {
    param (
        [string]$domain,
        [string]$dnsServer
    )
    try {
        # Use nslookup to resolve the domain with the specified DNS server
        $resultLines = (nslookup $domain $dnsServer 2>&1) -split "`n"
        $ipAddresses = @()
        foreach ($line in $resultLines) {
            if ($line -match "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)") {
                $ipAddresses += $matches[0]
            }
        }

        if ($ipAddresses.Count -ge 4) {
            $lastFourIPs = $ipAddresses[-4..-1]
        } else {
            $lastFourIPs = $ipAddresses
        }
        return $lastFourIPs
    } catch {
        Write-Host "Failed to run instruction: nslookup $domain $dnsServer"
    }
    return $null
}

# Function to test connectivity to an IP address
function Test-Connectivity {
    param (
        [string]$ipAddress
    )
    try {
        # Ping the IP address to test connectivity
        $pingResult = Test-Connection -ComputerName $ipAddress -Count 2 -ErrorAction Stop
        return $pingResult.Status -eq "Success"
    } catch {
        return $false
    }
}

# Iterate through each DNS server and resolve the domain
$resolvedIPs = @()
if (-not [string]::IsNullOrWhiteSpace($domain)) {
    foreach ($dnsServer in $dnsServers) {
        Write-Host "Resolving $domain using DNS server $dnsServer..."
        $ipAddresses = Resolve-Domain -domain $domain -dnsServer $dnsServer
        if ($ipAddresses) {
            Write-Host "Resolved IPs: $($ipAddresses -join ', ')"
            # Test connectivity to each resolved IP
            foreach ($ipAddress in $ipAddresses) {
                $curlCommand = "curl.exe -x $proxy -I -m 5 http://$ipAddress 2>&1"
                $result = & ([ScriptBlock]::Create($curlCommand))
                if ($result) {
                    Write-Host "IP $ipAddress is reachable."
                    $resolvedIPs += $ipAddress
                } else {
                    Write-Host "IP $ipAddress is not reachable."
                }
            }
        }
    }
} else {
    Write-Host "No domain entered. Exiting..."
    exit
}

# If at least one reachable IP is found, update the hosts file
if ($resolvedIPs.Count -gt 0) {
    $hostsFilePath = "$env:SystemRootSystem32driversetchosts"
    $uniqueIPs = $resolvedIPs | Select-Object -Unique
    foreach ($ipAddress in $uniqueIPs){
    	$entry = "$ipAddress $domain"
        # Add the domain-IP mapping to the hosts file
        Add-Content -Path $hostsFilePath -Value $entry
        Write-Host "Successfully added the following entry to the hosts file:"
        Write-Host $entry
    }
} else {
    Write-Host "Failed to resolve a reachable IP for $domain."
}

# Keep the command window open after execution
Write-Host "Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。