如何强制 Git 通过其 ssh 连接使用socks 代理?

dar*_*ush 3 git ssh proxy ssh-tunnel

互联网上有大量相同的解决方案,用于定义 git 下载的代理隧道,就像这样,这一切都是通过设置 git 的https.proxy& http.proxyconfig.js 来实现的。但是当您尝试通过协议clone/ push/pull等时,这些答案不起作用ssh

例如,通过设置git config --global https.proxy socks5://127.0.0.1:9999当您尝试克隆时git clone git@github.org:user/repo.git它不通过定义的 sock5 隧道!

我尝试了各种方法,但都没有奏效!

题:

如何设置 git127.0.0.1:9999在使用ssh连接时使用本地 socks5 代理(例如)?

She*_*don 31

克隆 git 有 2 种类型:HTTP 和 ssh。有两种常见的代理类型:HTTP 和socks。

这是处理 2 * 2 条件的方法:

# Method 1. git http + proxy http
git config --global http.proxy "http://127.0.0.1:1080"
git config --global https.proxy "http://127.0.0.1:1080"

# Method 2. git http + proxy shocks
git config --global http.proxy "socks5://127.0.0.1:1080"
git config --global https.proxy "socks5://127.0.0.1:1080"

# to unset
git config --global --unset http.proxy
git config --global --unset https.proxy

# Method 3. git ssh + proxy http
vim ~/.ssh/config
Host github.com
HostName github.com
User git
ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=1087

# Method 4. git ssh + proxy socks
vim ~/.ssh/config
Host github.com
HostName github.com
User git
ProxyCommand nc -v -x 127.0.0.1:1080 %h %p

Run Code Online (Sandbox Code Playgroud)

  • 我知道这个,因为这对中国男人来说是必要的,呵呵。https://www.notion.so/Accelerate-Git-in-China-d1b66e3a51bd4051b79f8ff435aafb59 (2认同)
  • 对于所有 Windows 平台的人。请改用 ProxyCommand connect -S localhost:1081 %h %p </code>。 (2认同)

dar*_*ush 13

经过浏览了这么多页面,我终于找到了我的问题的解决方案:

# [step 1] create a ssh-proxy
  ssh -D 9999 -qCN user@server.net

# [step 2] make git connect through the ssh-proxy
  # [current script only]
  export GIT_SSH_COMMAND='ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"'
  # OR [git global setting] 
  git config --global core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"'
  # OR [one-time only use]
  git clone -c=core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"' git@github.com:user/repo.git
  # OR [current repository use only]
  git config core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"'
Run Code Online (Sandbox Code Playgroud)

connect在 Ubuntu 上安装:

sudo apt install connect-proxy
Run Code Online (Sandbox Code Playgroud)


Jon*_*mar 5

git自己git --config 'http.proxy=socks5://127.0.0.1:4444'ssh_configproxycommand使用socat和技术成功地通过 SOCKS 代理nc重新路由命令。git

但是,问题可能是 DNS 查找失败,因为查找是在本地完成的,而不是通过 SOCKS 代理远程完成的。例如,在内联网的情况下,服务器可能没有互联网上的域名(只有内网 DNS 知道)。

有两种方法可以解决这个问题。第一个我已经多次看到推荐,但它做出了一个雄心勃勃的假设,即您控制远程服务器。

  1. 将本地 53 流量 (DNS) 重新路由到端口 X,将其转发到端口 Y 处的服务器,将端口 Y 转发到服务器上的 53。为简单起见,通常 X=Y。

  2. 拦截检索名称的 IP 地址的本地系统调用。

我认为 2. 更好,因为它从源头上捕获了问题,并且只假设您控制本地计算机,这比控制远程服务器更有可能发生。

proxychains-ng在访问本地 nss 之前拦截 getaddrinfo 系统调用。

脚步

打开 SOCKS5 端口

ssh -v -NT -D 127.0.0.1:4444 intranethost
Run Code Online (Sandbox Code Playgroud)

设置proxychains使用该端口。

~/.proxychains/proxychains.conf

strict_chain
proxy_dns
tcp_read_time_out 150000
tcp_connect_time_out 80000

[ProxyList]
socks5 127.0.0.1 4444
Run Code Online (Sandbox Code Playgroud)

使用proxychains来封装git。

alias gitproxy='proxychains git'
gitproxy clone intranethost:path/to/repo.git
Run Code Online (Sandbox Code Playgroud)

采取这条路线的美妙之处在于,这对于 git 及其遥控器来说都是透明的。

  • 如果是 DNS 问题,只需使用 `socks5h` 来使用远程 dns 解析:`git --config 'http.proxy=socks5h://127.0.0.1:4444'` (2认同)

Tel*_*her 5

以前的答案可能有效,但我发现它们过于复杂。

最常见的情况应该是通过 SOCKS5 代理访问公司 git 服务器。在这个例子中:

  • Git 服务器:git.evilcorp.com
  • SOCKS5 代理:本地主机:11080

在这种情况下配置 git 的最简单方法是为 git 服务器 (~/.ssh/config) 设置一个很好的 SSH 配置:

Host git.evilcorp.com
  # Identity file specifies wich SSH key used to access the git server.
  Identityfile ~/.ssh/id_rsa
  # ProxyCommand does the magic to access the proxy server.
  ProxyCommand /bin/nc -X 5 -x 127.0.0.1:11080 %h %p
Run Code Online (Sandbox Code Playgroud)

很酷的细节:DNS 解析由代理完成,因此您的机器不需要知道公司 DNS 服务器。