前言
使用 GitHub 的时候,经常会出现网络连接不稳定的情况,偏偏很着急的时候就非常耽误事儿。所以就想着有没有什么办法能尽可能保证访问 GitHub 的连通性。
一般解决加速问题,最常用的手段就是使用 http proxy, 但是大部分的 GitHub 用户都会使用 SSH keys.
设置 Http Proxy
git config --global http.proxy socks5://127.0.0.1:7890
因为 git 底层使用 libcurl 发送 http 请求,而 libcurl 的代理使用 socks5:// 时会在本地解析 DNS,实际使用中我们希望 DNS 也在远程解析,所以使用 socks5h ,即
git config --global http.proxy socks5h://127.0.0.1:7890
推荐使用 socks5 代理,因为 socks5 包含 http(s)。而且 socks5 代理工作在 osi 七层模型中的会话层(第五层),https/http 代理工作在 osi 七层模型的应用层(第七层), socks 代理更加底层。所以就没必要配置 git config --global http.proxy http://127.0.0.1:7890
了。
但这样配置的话会使本机所有的 git 服务都走了代理,假如你在良心云上部署了自己的 gitea,服务地址 https://gitea.example.com,那么可以只配置 GitHub 的 http proxy,即
git config --global http.https://github.com.proxy socks5://127.0.0.1:7890
这样做实际上是修改了 ~/.gitconfig
文件,添加了如下内容
[http "https://github.com"]
proxy = socks5://127.0.0.1:7890
设置 SSH Proxy
配置文件在用户家目录下的 .ssh/config
其中 nc 程序位于 /usr/bin/nc
$ cat ~/.ssh/config
Host github.com
Hostname ssh.github.com
IdentityFile /xxx/.ssh/github_id_rsa
User git
Port 443
ProxyCommand nc -v -x 127.0.0.1:7890 %h %p
说明
为什么 hostname 是 ssh.github.com,为什么要用 443 端口,ssh 默认不是 22 端口么?
因为有些 T 子 对于 22 端口做了限制,要么禁止了,要么有些抽风,这时经常会遇到如下错误:kex_exchange_identification: Connection closed by remote host
所以如果 22 端口不畅就使用 443,安全稳定可靠。(ps: 22 端口时 hostname 请填 github.com。
如果代理设置了用户名和密码基础认证呢?比如 clash 的 config.yaml 中就可以添加如下配置以增加 http 基础认证
authentication:
- "USERNAME:PASSWORD"