使用带有git的socks代理进行http传输

Yve*_*eau 66 git ssh proxy socks

如何让git使用socks代理进行http传输?

我成功地使用GIT_PROXY_COMMAND配置git以使用socks代理进行GIT传输.

此外,我已经配置我的.curlrc文件来定义socks代理,我可以直接用curl命令获取信息,如:

curl http://git.kernel.org/pub/scm/git/git.git/info/refs?service=git-upload-pack
Run Code Online (Sandbox Code Playgroud)

但是如何使用带git的socks代理来使用http传输协议检索数据,如:

git clone http://git.kernel.org/pub/scm/git
Run Code Online (Sandbox Code Playgroud)

Yan*_*g.Y 116

我使用Git 1.8.2和SOCKS v5代理进行测试,以下设置为我工作:

git config --global http.proxy 'socks5://127.0.0.1:7070'

更新2017-3-31:

根据该文档,尽管名称,它应该适用于HTTP和HTTPS存储库URL.感谢@user指出这一点.http.proxy

更新2018-11-27:

要禁用代理,请运行命令:

git config --global --unset http.proxy

  • brantyoung,谢谢!我忘记支付互联网连接,没有外部连接.我使用了另一台服务器作为网关:ssh -D 4000 <user> @server -p <port>,并成功将项目上传到github:git config http.proxy'socks5:// localhost:4000'.谢谢. (3认同)
  • @briankip打开`〜/ .gitconfig`并删除`[https.proxy]`和`[http.proxy]`部分,它是一个`ini`文件,随时可以编辑它. (2认同)
  • @briankip,我想你可以把它放在命令行而不是永久保存在你的配置中,例如`git -c http.proxy = socks5://127.0.0.1:7070`,或类似的东西.也许你可以为它做一个别名`gitsocks`,这样你就可以根据需要轻松地调用带有袜子的git. (2认同)
  • 我只能在将协议更改为“socks5**h**://&lt;address&gt;”后才能完成这项工作(“h”代表主机名解析;请参阅“man 1 curl”。“使其相当于--socks5-主机名") (2认同)

ohh*_*hho 41

如果您不想将代理设置为全局配置,请尝试以下操作ALL_PROXY=:

$ ALL_PROXY=socks5://127.0.0.1:8888 git clone https://github.com/some/one.git
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。对我来说稍作修改:ALL_PROXY=socks5h:...(注意“h”——在socks服务器中进行地址解析) (6认同)
  • 谢谢你!真的有效! (2认同)

thu*_*zhf 30

(只是一点提醒)如果你想让主机名也被代理解析(这意味着通过代理传递一切),特别是当你克隆一个要点时,你可以使用以下设置(关键是它使用socks5h代替of socks5):

git config --global http.proxy socks5h://127.0.0.1:1080
Run Code Online (Sandbox Code Playgroud)


ali*_*dro 8

我使用以下命令从socks5代理克隆特定的存储库.代理设置使用--config选项指定.

$ git clone https://github.com/xxxxx --config 'http.proxy=socks5://127.0.0.1:1234'
Run Code Online (Sandbox Code Playgroud)


pat*_*yts 7

对于git://协议,我们使用Git和SOCKS代理.但是,似乎git不能正确支持socks代理.git本身与libcurl相关联.因此不使用.curlrc文件(仅适用于curl命令行客户端).但是,以下补丁提供了必要的支持.将此补丁应用于git,我们可以简单地将ALL_PROXY环境变量或HTTP_PROXY或HTTPS_PROXY设置为socks://hostname:portnum(或socks4/socks5),或者实际上http.proxy git config设置和libcurl现在实际上在使用代理时使用socks协议.

例如,活动跟踪:

$ GIT_CURL_VERBOSE=1 bin-wrappers/git -c "http.proxy=socks://localhost:1080" ls-remote http://github.com/patthoyts/tclftd2xx.git
* Couldn't find host github.com in the _netrc file; using defaults
* About to connect() to proxy localhost port 1080 (#0)
*   Trying 127.0.0.1...
* connected
* SOCKS4 request granted.
* Connected to localhost (127.0.0.1) port 1080 (#0)
> GET /patthoyts/tclftd2xx.git/info/refs?service=git-upload-pack HTTP/1.1
User-Agent: git/1.8.1.msysgit.1.dirty
... and on to a successful request ...
Run Code Online (Sandbox Code Playgroud)

必要的补丁:

diff --git a/http.c b/http.c
index 3b312a8..f34cc75 100644
--- a/http.c
+++ b/http.c
@@ -322,6 +322,14 @@ static CURL *get_curl_handle(void)
        if (curl_http_proxy) {
                curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
                curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
+#if LIBCURL_VERSION_NUM >= 0x071800
+               if (!strncmp("socks5", curl_http_proxy, 6))
+                       curl_easy_setopt(result, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
+               else if (!strncmp("socks4a", curl_http_proxy, 7))
+                       curl_easy_setopt(result, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
+               else if (!strncmp("socks", curl_http_proxy, 5))
+                       curl_easy_setopt(result, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
+#endif
        }

        return result;
Run Code Online (Sandbox Code Playgroud)