問題現象
- GitHub 訪問過慢。
- GitHub clone 過慢。
- GitHub 訪問頁面加載失敗。
問題原因
由於以下三個域名遭到 DNS 污染,導致我們我發適用 GitHub 加速服務。
github.com
assets-cdn.github.com
github.global.ssl.fastly.net
所以我們只需要跳過 DNS 解析,直接訪問對應的 IP 地址即可。
解決方法
- 訪問 https://www.ipaddress.com/
- 分別輸入:
github.com
,assets-cdn.github.com
,github.global.ssl.fastly.net
三個域名進行查詢。 - 以“ip 域名”的格式寫入 hosts 文件。windows 位置:
c:\windows\system32\drivers\etc\hosts
;linux 位置:/etc/hosts
。 - 更新 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()]))