forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson-github-dns-443-block-hosts-workaround.json
More file actions
16 lines (16 loc) · 2.51 KB
/
Copy pathlesson-github-dns-443-block-hosts-workaround.json
File metadata and controls
16 lines (16 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"task_id": "lesson-github-dns-443-block-hosts-workaround",
"title": "GitHub DNS 污染/443端口不通 — hosts 备用 IP 方案",
"domain": "devops",
"tags": [
"git",
"github",
"TLS",
"network",
"DNS"
],
"problem": "`git push` / `git fetch` 持续超时或报 TLS 握手错误:\n\n\n\n重试无效,非瞬时问题。",
"solution": "### 1. 验证当前 DNS 解析的 IP 是否可达\n\n```bash\nGITHUB_IP=$(getent hosts github.com | awk '{print $1}')\ntimeout 3 bash -c \"echo > /dev/tcp/$GITHUB_IP/443\" && echo \"✅ 可达\" || echo \"❌ 不可达\"\n```\n\n### 2. 扫描 GitHub 备用 IP 的 443 端口\n\nGitHub 官方 IP 范围(部分):\n\n```\n140.82.112.0/20 # 主要服务\n185.199.108.0/22 # Pages/CDN\n192.30.252.0/22 # 旧范围\n```\n\n扫描脚本:\n\n```bash\nfor ip in 140.82.112.3 140.82.112.4 140.82.113.3 140.82.114.3 \\\n 140.82.121.3 140.82.121.4 \\\n 185.199.108.153 185.199.109.153 185.199.110.153; do\n timeout 3 bash -c \"echo > /dev/tcp/$ip/443\" 2>/dev/null \\\n && echo \"✅ $ip\" || echo \"❌ $ip\"\ndone\n```\n\n### 3. 写入 hosts — ⚠️ 只加 github.com,不加 api.github.com\n\n**关键陷阱:`api.github.com` 的真实 IP 不同于 `github.com`。**\n\n| 域名 | 真实 IP | 说明 |\n|------|---------|------|\n| `github.com` | 140.82.112.x/20 | Web/Git 服务 |\n| `api.github.com` | **20.205.243.168** | REST API 服务(独立 IP 段) |\n\n如果 hosts 里把 `api.github.com` 也指向 `github.com` 的 IP,API 请求会返回 **301 重定向**(TLS SNI 路由将 API 请求误识别为 Web 请求),导致 `curl -X POST https://api.github.com/user/repos` 等 API 调用全部失败。\n\n**正确写法:**\n\n```bash\n# ✅ 只写 github.com\necho \"<可达IP> github.com\" | sudo tee -a /etc/hosts\n\n# ❌ 不要这样写——api.github.com 有自己的 IP\necho \"<可达IP> github.com api.github.com\" | sudo tee -a /etc/hosts\n\n# 如果必须调 API,用 --resolve 绕过 hosts:\ncurl --resolve \"api.github.com:443:20.205.243.168\" \\\n -H \"Authorization: token $TOKEN\" \\\n https://api.github.com/user\n```\n\n**如果 hosts 已经写错了,修复:**\n\n```bash\n# 从 /etc/hosts 中移除 api.github.com\nsudo sed -i 's/ github.com api.github.com/ github.com/' /etc/hosts\n# 或者直接删掉整行重新写\n```\n\n### 4. 验证\n\n```bash\ngit fetch origin main\n# 正常返回分支信息 → 修复成功\n```",
"source": "lessons/contrib/github-dns-443-block-hosts-workaround.md",
"test_cmd": ""
}