使用带有 git 的袜子代理进行 http 传输
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15227130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a socks proxy with git for the http transport
提问by Yves Blusseau
How to make git use a socks proxy for HTTP transport?
如何让git使用socks代理进行HTTP传输?
I succeed in configuring git with GIT_PROXY_COMMAND to use a socks proxy for GIT transport.
我成功地使用 GIT_PROXY_COMMAND 配置 git 以使用袜子代理进行 GIT 传输。
Also, I have configured my .curlrc file to defined the socks proxy and I can fetch information directly with curl command like:
另外,我已经配置了我的 .curlrc 文件来定义袜子代理,我可以直接使用 curl 命令获取信息,例如:
curl http://git.kernel.org/pub/scm/git/git.git/info/refs?service=git-upload-pack
But how to use a socks proxy with git to retrieve data using the HTTP transport protocol like:
但是如何使用带有 git 的袜子代理来使用 HTTP 传输协议检索数据,例如:
git clone http://git.kernel.org/pub/scm/git
回答by Yang.Y
I tested with Git 1.8.2 and SOCKS v5 proxy, following setting works for me:
我使用 Git 1.8.2 和 SOCKS v5 代理进行了测试,以下设置对我有用:
git config --global http.proxy 'socks5://127.0.0.1:7070'
git config --global http.proxy 'socks5://127.0.0.1:7070'
UPDATE 2017-3-31:
更新 2017-3-31:
According to the document, despite the name http
.proxy
, it should work for both HTTP and HTTPS repository urls. Thanks @user for pointing out this.
根据该文档,尽管名称为,但它应该适用于 HTTP 和 HTTPS 存储库 url。感谢@user 指出这一点。http
.proxy
UPDATE 2018-11-27:
更新 2018-11-27:
To disable the proxy, run command:
要禁用代理,请运行命令:
git config --global --unset http.proxy
git config --global --unset http.proxy
EDIT 2019-03-04:
编辑 2019-03-04:
If you also want the host name to be resolved using the proxy, use thuzhf's solution below, which uses socks5h
instead of socks5
如果您还希望使用代理解析主机名,请使用下面的 thuzhf 解决方案,它使用socks5h
代替socks5
回答by ohho
If you do not want to set the proxy as global config, try ALL_PROXY=
e.g.:
如果您不想将代理设置为全局配置,请尝试ALL_PROXY=
例如:
$ ALL_PROXY=socks5://127.0.0.1:8888 git clone https://github.com/some/one.git
回答by thuzhf
(Just a little reminder) If you want the hostname also be resolved by the proxy (that means passing everything through the proxy), especially when you are cloning a gist, you can use the following setting (the key is that it uses socks5hinstead of socks5):
(只是一点提醒)如果您希望主机名也由代理解析(这意味着通过代理传递所有内容),尤其是在克隆gist 时,您可以使用以下设置(关键是它使用了socks5h代替的SOCKS5):
git config --global http.proxy socks5h://127.0.0.1:1080
回答by alijandro
I use the following command to clone a specific repository from socks5 proxy. The proxy settings are specified with --config
option.
我使用以下命令从 socks5 代理克隆特定存储库。代理设置由--config
选项指定。
$ git clone https://github.com/xxxxx --config 'http.proxy=socks5://127.0.0.1:1234'
回答by patthoyts
Note: the patch here was applied to git in 2015 for version 2.4.11. Since that time you can just use socks:// urls with the http.proxy configuration settings.
注意:这里的补丁是在 2015 年的 2.4.11 版本中应用于 git 的。从那时起,您可以使用带有 http.proxy 配置设置的socks:// urls。
For the git:// protocol we have Using Git with a SOCKS proxy. However, it appears that git does not properly support socks proxies. git itself is linked to libcurl. So the .curlrc file is not used (that is just for the curl command line client). However, the following patch provides the necessary support. With this patch applied to git we can simply set the ALL_PROXY environment variable or HTTP_PROXY or HTTPS_PROXY to socks://hostname:portnum
(or socks4/socks5) or indeed the http.proxy git config setting and libcurl will now actually use the socks protocol when using the proxy.
对于 git:// 协议,我们使用带有 SOCKS 代理的 Git。但是,git 似乎没有正确支持socks 代理。git 本身链接到 libcurl。因此不使用 .curlrc 文件(仅用于 curl 命令行客户端)。但是,以下补丁提供了必要的支持。将此补丁应用于 git 后,我们可以简单地将 ALL_PROXY 环境变量或 HTTP_PROXY 或 HTTPS_PROXY 设置为socks://hostname:portnum
(或 socks4 /socks5)或实际上 http.proxy git 配置设置,libcurl 现在将在使用代理时实际使用socks 协议。
For example, an active trace:
例如,活动跟踪:
$ 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 ...
The necessary patch:
必要的补丁:
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;
回答by Paul H
Just in reference to @briankip and removing the http proxy setting as Yang.Y mentioned you can directly edit the ini file.
只是参考@briankip并删除Yang.Y提到的http代理设置,您可以直接编辑ini文件。
You can also do this on the command line using
您也可以在命令行上使用
git config --global --unset http.proxy
git config --global --unset http.proxy
To confirm it has been removed list the current configuration using
要确认它已被删除,请使用列出当前配置
git config --list
git config --list
回答by Benyamin Jafari
None of the suggested methods worked for me, so I found another manner as the following instruction:
建议的方法都不适合我,所以我找到了另一种方法,如下所示:
- Make a tunnel (dynamic port forwarding) over
SOCKS5
protocol usingssh
:ssh -ND 9994 user@YourSshServer
SOCKS5
使用ssh
以下协议建立隧道(动态端口转发):ssh -ND 9994 user@YourSshServer
- Install
proxychains
on your localhost, not the ssh server you're connected to:- Using
apt-get
:sudo apt-get install proxychains
- Using its GitHub repository:
Check Installationsection on its readme file.
How to set socks5 proxy on firefox
- Using
- 安装
proxychains
在您的本地主机上,而不是您连接到的 ssh 服务器上:- 使用
apt-get
:sudo apt-get install proxychains
- 使用其GitHub 存储库:
检查其自述文件中的安装部分。
如何在firefox上设置socks5代理
- 使用
- Edit your
proxychains
configure file:sudo nano /etc/proxychains.conf
then add the following line at the end of file:socks5 127.0.0.1 9994
- 编辑您的
proxychains
配置文件:sudo nano /etc/proxychains.conf
然后在文件末尾添加以下行:socks5 127.0.0.1 9994
- Now we are ready to do a
git
command (proxychains
must be placed before the command):proxychains git push origin develop
- 现在我们准备做一个
git
命令(proxychains
必须放在命令之前):proxychains git push origin develop