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

参考https://blog.youkuaiyun.com/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 "\b(?:(?: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]?)\b") {
                $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:SystemRoot\System32\drivers\etc\hosts"
    $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")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值