配置 DNS 解決訪問github過慢


問題現象

  1. GitHub 訪問過慢。
  2. GitHub clone 過慢。
  3. GitHub 訪問頁面加載失敗。

問題原因

由於以下三個域名遭到 DNS 污染,導致我們我發適用 GitHub 加速服務。

github.com
assets-cdn.github.com
github.global.ssl.fastly.net 

所以我們只需要跳過 DNS 解析,直接訪問對應的 IP 地址即可。

解決方法

  1. 訪問 https://www.ipaddress.com/
  2. 分別輸入: github.comassets-cdn.github.comgithub.global.ssl.fastly.net 三個域名進行查詢。
  3. 以“ip 域名”的格式寫入 hosts 文件。windows 位置:c:\windows\system32\drivers\etc\hosts;linux 位置:/etc/hosts
  4. 更新 DNS 配置。windows 命令:ipconfig /flushdns;linux 命令:sudo /etc/init.d/networking restart

獲取域名對應的 IP 腳本

import re
import requests
import os

domain_dict = {

    "github.com": "https://github.com.ipaddress.com/",

    "assets-cdn.github.com": "https://github.com.ipaddress.com/assets-cdn.github.com",

    "github.global.ssl.fastly.net": "https://fastly.net.ipaddress.com/github.global.ssl.fastly.net"
}


hosts_dict = {}


for domain, url in domain_dict.items():

    method = "GET"

    req = requests.request(method=method, url=url)
    pattern = r'<th>IPv4 Addresses</th><td><ul class="comma-separated"><li>(.+?)</li>'
    com = re.compile(pattern)
    ip = com.findall(req.content.decode())[0]
    hosts_dict[ip] = domain

hosts_path_dict = {
    "nt": "c:\\windows\\system32\\drivers\\etc\\hosts",
    "posix": "/etc/hosts"
}

hosts_path = hosts_path_dict.get(os.name)

print("\nhosts文件路徑:", hosts_path, "\n")

print("\n".join([f"{k} {v}" for k, v in hosts_dict.items()])) 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM