github 加速访问的方法参考

目录
[隐藏]

近期经常遇到这种问题:github 打不开无法正常解析,或者主站虽然可以正常解析和访问,但涉及静态资源的 CDN 域名格式如 xxx.githubusercontent.com 则无法解析。这种大部分都是静态的图片等资源,访问不到也就算了,关键是其域名解析不正常导致页面加载异常缓慢。

1 绑定 Host 方式解决域名无法访问的方法

由于 GitHub 的这类资源使用的是 fastly CDN(Fastly, Inc),其在全球不同地区会有许多不同的 IP 解析,对 fastly CDN 这种大厂商的所有 IP 地址都进行屏蔽是不现实的。我们可以通过查找其域名 CDN IP,选取一个可以访问的 IP 进行本地绑定即可解决该问题。

1.1 实时更新 Hosts

1.1.1 定时更新 GitHub host 解析的项目与接口

开源项目 GitHub520 提供了脚本自动实时更新 github host 映射的支持。

GitHub Hosts 是一个与 GitHub520 类似的项目,通过定时任务搜集和验证最新可用的 github host 并更新提交至 Github 仓库。

1.1.2 安装 hosts 快速切换工具自动更新 GitHub hosts

开源项目 SwitchHosts 是一个 hosts 快速切换的辅助工具。它提供了根据指定的 URL 地址按策略更新 hosts 的能力。那么正好可以结合 GitHub520GitHub Hosts 项目提供的 API 实现定时更新 GitHub 最新的 host 解析能力。

1.1.3 安装FastGithub

通过修改 host 绑定的方法,其可靠性取决于 GitHub520 项目提供的 hosts 是否稳定,实际使用过程中仍可能会抽风无法访问。
此时你可能需要 FastGithub 这个工具。该工具自动收集并判断 GitHub 最新可用并选取速度最快的 ip,并以本地代理的方式实现自定义 DNS 解析。

1.2 手动查找 CDN IP 并绑定 Host 解决域名无法访问

如果你不想使用以上开源辅助工具,只想手动去分析和获取 Github 可用的 ip 进行绑定,以下步骤可为你提供一定的参考。

首先选择一个 IP 查询网站,如 ipaddressip138,打开网站后,输入域名 camo.githubusercontent.com 查询其 IP 地址。

域名 DNS 解析 IP 查询网站参考:

笔者本次通过 ipaddress 查询得到的 IP 地址为: 199.232.96.133

然后我们编辑本机的 hosts 配置文件,记得要以管理员权限打开。其 windows 位置为 c:\windows\System32\drivers\etc\hosts,Mac 和 linux 上的位置默认为 /etc/hosts

windows 下可以进入该目录,选择该文件并右键选择以管理员权限用记事本打开即可编辑。

Mac/Linux 上执行如下命令编辑它:

sudo vi /etc/hosts

然后在末尾添加 hosts 绑定信息,示例:

# New!欢迎使用基于DNS的新方案
# https://gitlab.com/ineo6/hosts/-/raw/master/next-hosts
# 地址可能会变动,请务必关注GitHub、Gitlab获取最新消息
# 也可以关注公众号:湖中剑,保证不迷路
# GitHub Host Start

185.199.109.154              github.githubassets.com
140.82.113.22                central.github.com
185.199.111.133              desktop.githubusercontent.com
185.199.110.153              assets-cdn.github.com
185.199.110.133              camo.githubusercontent.com
185.199.108.133              github.map.fastly.net
151.101.65.194               github.global.ssl.fastly.net
140.82.112.3                 gist.github.com
185.199.109.153              github.io
140.82.112.3                 github.com
140.82.112.5                 api.github.com
185.199.110.133              raw.githubusercontent.com
185.199.110.133              user-images.githubusercontent.com
185.199.109.133              favicons.githubusercontent.com
185.199.110.133              avatars5.githubusercontent.com
185.199.108.133              avatars4.githubusercontent.com
185.199.110.133              avatars3.githubusercontent.com
185.199.109.133              avatars2.githubusercontent.com
185.199.110.133              avatars1.githubusercontent.com
185.199.109.133              avatars0.githubusercontent.com
185.199.111.133              avatars.githubusercontent.com
140.82.114.9                 codeload.github.com
54.231.130.201               github-cloud.s3.amazonaws.com
52.217.16.84                 github-com.s3.amazonaws.com
54.231.139.121               github-production-release-asset-2e65be.s3.amazonaws.com
52.216.105.27                github-production-user-asset-6210df.s3.amazonaws.com
52.217.207.33                github-production-repository-file-5c1aeb.s3.amazonaws.com
185.199.111.153              githubstatus.com
140.82.113.18                github.community
185.199.108.133              media.githubusercontent.com
185.199.111.133              objects.githubusercontent.com
185.199.111.133              raw.github.com
20.80.64.28                  copilot-proxy.githubusercontent.com

# Please Star : https://github.com/ineo6/hosts
# Mirror Repo : https://gitlab.com/ineo6/hosts

# Update at: 2023-04-17 08:54:50

# GitHub Host End

最后按 ESC 键并输入 :wq 保存退出即可。

1.3 相关扩展:域名解析分析

当发现网站域名无法正常访问时,可以借助以下命令对域名解析的具体细节进行分析诊断。

  • 使用 ping 命令可以查看域名当前的解析 IP 地址。示例: ping lzw.me

  • 使用 dignslookup 命令查看域名的 DNS 解析细节。示例:

# linux/Mac 下
dig +trace lzw.me

# windows 下
nslookup lzw.me
  • 使用 traceroutetracert 命令可以追踪路由转发的细节,以协助诊测网络情况。示例:
# linux/Mac 下
traceroute lzw.me

# windows 下
tracert lzw.me

2 SNI(Server Name Indication) 阻断与伪装

在实际的实践过程中,通过修改 hosts 绑定 IP 的方式,在使用效果上并不是太理想。
实际上 GTW 并没有封禁 github 的 IP,而是使用了 SNI 阻断的技术。

SNI 伪装可以加速 Github 访问原理

  1. SNI(Server Name Indication):一项允许一台服务器部署多个域名网站的技术
  2. 在 https 连接握手时,客户端会先发一个带有域名的 client Hello,告诉服务器我要访问哪个域名的网站,给我那个域名的证书
  3. 这个域名是明文的,GFW 检测到这个域名是 github.com 就会根据客户端 ip 和端口实施阻断(并没有直接封杀 Github 的 ip 地址)
  4. 重点:实际上 Github 的服务器的 443 端口默认证书就是 github.com,即使你给错误的 sni 或者不给 sni,它还是会返回 github.com 的证书
  5. 所以通过修改 sni 的域名,就可以规避 GFW 的拦截,实现有效的 github 访问

基于 SNI 阻断的原理,可以实现一个本地代理,在请求被 GFW 以 SNI 方式阻断的域名时,使用伪造的域名发送请求,即可实现绕过拦截。

3 通过镜像地址加速 github 仓库的拉取

如果只是为了访问和 clone 仓库,通过镜像网址来访问。当前已知有如下好用的 github 镜像:

  • git@git.zhlh6.cn
  • https://gh-proxy.com/github.com
  • https://ghproxy.net/github.com
  • https://gitclone.com/github.com
  • https://hub.fgit.ml

3.1 镜像地址替代单个仓库

对于 clone 单个仓库,可以直接使用以上任一地址替换 github 实际仓库的地址,例如:

# github 原始仓库(http格式)
git clone https://github.com/lzwme/scoop-proxy-cn.git
# github 原始仓库(ssh格式)
git clone git@github.com:lzwme/scoop-proxy-cn.git

# 可修改为使用以下任一格式实现加速拉取:

# gh-proxy
git clone https://gh-proxy.com/github.com/lzwme/scoop-proxy-cn.git
# zhlh6(支持 ssh 推送)
git clone git@git.zhlh6.cn:lzwme/scoop-proxy-cn.git
# fgit
git clone https://hub.fgit.ml/lzwme/scoop-proxy-cn.git

3.2 设置 git 全局默认重定向 github 仓库地址

如果希望全局默认设置自动替换,可以设置 git insteadOf 配置以重定向对 github 仓库的拉取。示例:

# 全局设置将 github.com 的仓库(http格式)重定向到 gh-proxy.com
git config --global url."https://gh-proxy.com/github.com".insteadOf https://github.com

# 全局设置将 github.com 的仓库(ssh格式)重定向到 zhlh6.cn
git config --global url."git@github.com".insteadOf git@git.zhlh6.cn

执行以上代码的实际效果是会在 ~/.gitconfig 文件中添加如下内容(所以也可以直接手动编辑该文件):

[url "https://gh-proxy.com/github.com"]
         insteadOf = https://github.com
[url "git@github.com"]
         insteadOf = git@git.zhlh6.cn

查看 git 全局所有的配置信息:

git config --global --list

取消上述设置:

git config --global --unset url."https://mirror.ghproxy.com/github.com.insteadof"
git config --global --unset url."git@github.com.insteadof"

4 使用 Chrome 插件辅助 Github 的加速访问

开源项目 https://github.com/gauseen/faster-hosts 是一个加快 GitHub 资源访问速度的 Chrome 插件,自动根据 GitHub520 项目提供的最新 hosts 加速 GitHub 解析。另外还参考了开源项目 ReplaceGoogleCDN 提供的能力,自动将 Google CDN 替换为国内可访问的替代地址。所以我们可以安装该 chrome 插件来实现加速访问 GitHub。

https://github.com/lzwme/vm-gh-proxy-cn 是一个适合中国大陆用户使用的浏览器油猴脚本。它通过修改 Github 部分源文件、附件等内容的链接为国内可用的镜像地址,以加速对相关资源的访问与下载体验。

5 相关参考

点赞 (4)
  1. php粉丝网说道:
    Google Chrome 78.0.3904.108 Google Chrome 78.0.3904.108 Windows 7 x64 Edition Windows 7 x64 Edition

    github 都不能正常打开了。

  2. 王光卫博客说道:
    Google Chrome 88.0.4324.96 Google Chrome 88.0.4324.96 Mac OS X  11.2.0 Mac OS X 11.2.0

    通常IP段都被屏蔽了,这是个尴尬的事情,还好有gitee

回复 php粉丝网 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code