git 如何通过苛刻的代理使用 GitHub?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1040089/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:44:22  来源:igfitidea点击:

How do I use GitHub through harsh proxies?

gitgithub

提问by Greg Bacon

Given the following constraints, how can I use GitHub from Windows and Unix?

鉴于以下限制,我如何从 Windows 和 Unix 使用 GitHub?

  • All access to the internet is restricted to a proxy
  • The proxy only allows connections out on port 80 and 443
  • CONNECT method is only enabled for 443
  • Proxy Authentication is required (NTLM or Basic)
  • 所有对互联网的访问仅限于代理
  • 代理仅允许在端口 80 和 443 上进行连接
  • CONNECT 方法仅对 443 启用
  • 需要代理身份验证(NTLM 或基本)

采纳答案by Greg Bacon

See a “Using Github Through Draconian Proxies (Windows And Unix)”by Jeff Tchang (formerly available from another location), which includes instructions for both Windows and Unix platforms, summarized below.

请参阅Jeff Tchang(以前可从其他位置获得)的“通过 Draconian 代理(Windows 和 Unix)使用 Github”,其中包括 Windows 和 Unix 平台的说明,总结如下。

Unix

Unix

  1. Download Git.
  2. Download and install corkscrew.
  3. Edit or create the file ~/.ssh/configand put the following:

    ProxyCommand /usr/bin/corkscrew proxy.example.com 443 %h %p ~/.ssh/myauth
    
    Host github.com
    User git
    Port 22
    Hostname github.com
    IdentityFile "/media/truecrypt1/Keys/GitHubKey.private"
    TCPKeepAlive yes
    IdentitiesOnly yes
    
    Host ssh.github.com
    User git
    Port 443
    Hostname ssh.github.com
    IdentityFile "/media/truecrypt1/Keys/GitHubKey.private"
    TCPKeepAlive yes
    IdentitiesOnly yes
    
  4. If everything is setup correctly you should be able to run ssh github.comand see

    Hi user! You've successfully authenticated, but GitHub does not provide shell access.
    Connection to github.com closed.

    If this doesn't work you can run ssh ssh.github.comand get the exact same thing. If the first command didn't work it means you are using a proxy that blocks CONNECT on port 22. Almost no proxies block CONNECT on port 443 because you need that for SSL.

  1. 下载 Git。
  2. 下载并安装开瓶器
  3. 编辑或创建文件~/.ssh/config并输入以下内容:

    ProxyCommand /usr/bin/corkscrew proxy.example.com 443 %h %p ~/.ssh/myauth
    
    Host github.com
    User git
    Port 22
    Hostname github.com
    IdentityFile "/media/truecrypt1/Keys/GitHubKey.private"
    TCPKeepAlive yes
    IdentitiesOnly yes
    
    Host ssh.github.com
    User git
    Port 443
    Hostname ssh.github.com
    IdentityFile "/media/truecrypt1/Keys/GitHubKey.private"
    TCPKeepAlive yes
    IdentitiesOnly yes
    
  4. 如果一切设置正确,您应该能够运行ssh github.com并查看

    用户好!您已成功通过身份验证,但 GitHub 不提供 shell 访问。
    与 github.com 的连接已关闭。

    如果这不起作用,您可以运行ssh ssh.github.com并获得完全相同的东西。如果第一个命令不起作用,则意味着您使用的代理会阻止端口 22 上的 CONNECT。几乎没有代理会阻止端口 443 上的 CONNECT,因为 SSL 需要它。

Windows

视窗

  1. Download msysgit. Some settings:
    • “Run Git from the Windows Command Prompt”
    • “Use OpenSSH” (this one is very important)
    • Pick your line endings
  2. Download connect.c. This tool deserves its own post mostly because of its utter simplicity. It mirrors the open source tool corkscrew and is used for tunneling through proxies. Yes the tool's name is really called “connect.c.” For Windows users, a pre-compiled binary is available. I put my connect.exein C:\Windows\connect.exe.
  3. Decide whether you like to use the Windows cmd.exeto do stuff or the Cygwin style shell. Or both.
  4. Set up the Cygwin Git bash shell.

    For the Cygwin style shell start up the Git icon and edit the file ~/.ssh/configand make sure the file has no extension. Put the following in that file, and note how the paths are specified.

    ProxyCommand /c/windows/connect.exe -H [email protected]:443 %h %p
    
    Host github.com
    User git
    Port 22
    Hostname github.com
    IdentityFile "/c/Keys/GitHubKey.private"
    TCPKeepAlive yes
    IdentitiesOnly yes
    
    Host ssh.github.com
    User git
    Port 443
    Hostname ssh.github.com
    IdentityFile "/c/Keys/GitHubKey.private"
    TCPKeepAlive yes
    IdentitiesOnly yes
    
  5. Set up the Windows cmd.exeshell.

    Suppose you don't like the Git Bash shell. You prefer the cmd.exe interpreter.

    • Go to your config file at C:\Documents and Settings\.ssh\config
    • Make a copy of it or make a new one. I called mine config-windows

    Put the following in the file, again paying careful attention to path separators and styles.

    ProxyCommand C:/Windows/connect.exe -H [email protected]:443 %h %p
    
    Host github.com
    User git
    Port 22
    Hostname github.com
    IdentityFile "C:\Keys\GitHubKey.private"
    TCPKeepAlive yes
    IdentitiesOnly yes
    
    Host ssh.github.com
    User git
    Port 443
    Hostname ssh.github.com
    IdentityFile "C:\Keys\GitHubKey.private"
    TCPKeepAlive yes
    IdentitiesOnly yes
    
  1. 下载msysgit。一些设置:
    • “从 Windows 命令提示符运行 Git”
    • “使用OpenSSH”(这个很重要)
    • 选择你的行尾
  2. 下载connect.c。这个工具值得拥有它自己的帖子,主要是因为它非常简单。它反映了开源工具 corkscrew,用于通过代理进行隧道传输。是的,该工具的名称实际上称为“connect.c”。对于 Windows 用户,可以使用预编译的二进制文件。我把我connect.exeC:\Windows\connect.exe
  3. 决定您是喜欢使用 Windowscmd.exe来做事还是喜欢Cygwin 风格的 shell。或两者。
  4. 设置 Cygwin Git bash shell。

    对于 Cygwin 风格的 shell,启动 Git 图标并编辑文件~/.ssh/config并确保文件没有扩展名。将以下内容放入该文件中,并注意路径的指定方式。

    ProxyCommand /c/windows/connect.exe -H [email protected]:443 %h %p
    
    Host github.com
    User git
    Port 22
    Hostname github.com
    IdentityFile "/c/Keys/GitHubKey.private"
    TCPKeepAlive yes
    IdentitiesOnly yes
    
    Host ssh.github.com
    User git
    Port 443
    Hostname ssh.github.com
    IdentityFile "/c/Keys/GitHubKey.private"
    TCPKeepAlive yes
    IdentitiesOnly yes
    
  5. 设置 Windowscmd.exe外壳程序。

    假设您不喜欢 Git Bash shell。您更喜欢 cmd.exe 解释器。

    • 转到您的配置文件 C:\Documents and Settings\.ssh\config
    • 制作一份副本或制作一个新的。我叫我的config-windows

    将以下内容放入文件中,再次注意路径分隔符和样式。

    ProxyCommand C:/Windows/connect.exe -H [email protected]:443 %h %p
    
    Host github.com
    User git
    Port 22
    Hostname github.com
    IdentityFile "C:\Keys\GitHubKey.private"
    TCPKeepAlive yes
    IdentitiesOnly yes
    
    Host ssh.github.com
    User git
    Port 443
    Hostname ssh.github.com
    IdentityFile "C:\Keys\GitHubKey.private"
    TCPKeepAlive yes
    IdentitiesOnly yes
    

For full details, see the full blog post.

有关完整的详细信息,请参阅完整的博客文章

回答by dvo

[Since my addition to the first answer given above did not get approved within four days, I put it here.]

[由于我对上面给出的第一个答案的补充在四天内没有得到批准,所以我把它放在这里。]

Note that corkscrewand connect, as well as the standard Unix command nconly support basic authentication (which insecurely transmits the password).
tunnel-authversion 0.04in addition supports digest authentication.

请注意,corkscrewconnect,以及标准 Unix 命令nc仅支持基本身份验证(不安全地传输密码)。
tunnel-auth0.04 版还支持摘要认证。

In case your proxy requires NTLM authentication, all these commands may be combined very nicely with cntlmas follows:

如果您的代理需要 NTLM 身份验证,所有这些命令可以很好地与cntlm如下组合:

Choose a local port (e.g, 8080 as in the examples below) on which cntlmwill listen (performing the user authentication with the proxy and forwarding any further packages to/from the proxy), set the port etc. (e.g., in /etc/cntlm.conf), and use instead of the ProxyCommand given above (with the respective port number inserted):

选择一个本地端口(例如,下面示例中的 8080),在该端口上进行cntlm侦听(使用代理执行用户身份验证并将任何进一步的包转发到/从代理转发),设置端口等(例如,在 /etc/cntlm.conf),以及使用代替上面给出的 ProxyCommand(插入相应的端口号):

ProxyCommand corkscrew 127.0.0.1 8080 %h %p

ProxyCommand 开瓶器 127.0.0.1 8080 %h %p

or

或者

ProxyCommand connect -H 127.0.0.1:8080 %h %p

ProxyCommand 连接 -H 127.0.0.1:8080 %h %p

or

或者

ProxyCommand nc -X connect -x 127.0.0.1:8080 %h %p

代理命令 nc -X 连接 -x 127.0.0.1:8080 %h %p

or

或者

ProxyCommand tunnel-auth -p 127.0.0.1:8080 -r %h:%p

ProxyCommand 隧道认证 -p 127.0.0.1:8080 -r %h:%p

回答by Enda Farrell

My scenario was a little different to Jeff Tchang's (but based on his post) but might be helpful here.

我的场景与 Jeff Tchang 的(但基于他的帖子)略有不同,但在这里可能会有所帮助。

All of our workplace/corporate internet access is via a non-authenticating proxy. I was able to clone frombut not push togithub: running

我们所有的工作场所/公司互联网访问都是通过非身份验证代理进行的。我能克隆,但不能推GitHub上:运行

git push -u origin master

would return

会回来

ssh: connect to host github.com port 22: Operation timed out
fatal: The remote end hung up unexpectedly

Based on http://returnbooleantrue.blogspot.com/2009/06/using-github-through-draconian-proxies.htmland http://meinit.nl/ssh-through-a-proxy-from-your-apple-mac-os-xand http://www.mtu.net/~engstrom/ssh-proxy.phpI was able to download/install corkscrew and add the following to my ~/.ssh/config:

基于http://returnbooleantrue.blogspot.com/2009/06/using-github-through-draconian-proxies.htmlhttp://meinit.nl/ssh-through-a-proxy-from-your-apple- mac-os-xhttp://www.mtu.net/~engstrom/ssh-proxy.php我能够下载/安装开瓶器并将以下内容添加到我的 ~/.ssh/config 中:

Host github.com
  User git
  Port 22
  Hostname github.com
  TCPKeepAlive yes
  IdentitiesOnly yes
  ProxyCommand /usr/local/bin/corkscrew proxy.<my-workplace>.com 8080 %h %p 

Some points to note:

需要注意的几点:

  • I use my workplace/corporate private key also with GitHub: if you don't you will need to add an "IdentityFile" line

  • Unlike with Jeff Tchang (and thanks to mtu.net) I did not need to have the "~/.ssh/myauth" at the end of the ProxyCommand line

  • I did not need to set up a ssh.github.com Host section.

  • 我也在 GitHub 上使用我的工作场所/公司私钥:如果不这样做,则需要添加“IdentityFile”行

  • 与 Jeff Tchang 不同(感谢 mtu.net),我不需要在 ProxyCommand 行的末尾添加“~/.ssh/myauth”

  • 我不需要设置 ssh.github.com Host 部分。

Hope these help.

希望这些有帮助。