在公司代理 Node.js 后面访问 Github

Accessing Github behind corporate proxy Node.js

我在从 cmd 调用以下命令安装 PhoneGap 时遇到问题:

npm install -g phonegap

返回以下错误:

Failed to connect to github.com port 443: Timed out

有很多关于这个主题的问题,它们似乎都提供了相同的答案 - 确保配置 gitnodejs 的代理设置。我像这样设置 node 代理设置:

npm config set proxy http://proxyname:8080
npm config set https-proxy http://proxyname:8080

git

git config --global http.proxy http://proxyname:8080
git config --global https.proxy http://proxyname:8080

git config --listnpm config list 均确认这些代理设置已到位。

我还通过转到 Windows Firewall -> Allow a program or feature through windows firewall 允许程序通过防火墙。然后我为 Node.js 选择了 node.exe,为 git 选择了 git.exe

但是我仍然遇到无法连接到 Github 的问题。还有什么我遗漏或忘记设置的吗? Node.js 和 Git 都是第一次为此任务安装。

除了上述设置之外,如果某些下载的库使用 git:// 协议而不是 https:// 声明其依赖项,则可能会出现此错误。这些依赖项通常会因上述错误而失败。

要解决此问题,您可以运行以下操作:

git config --global url."https://".insteadOf git://

这将为 Git 添加一个配置选项,要求 Git 在 URL 使用 git:// 协议时使用 https

此设置为我解决了很多代理问题。

您需要为您的命令应用程序添加代理身份验证。我假设你在 windows 工作,以下对我有用(我不需要将代理添加到 npm 或 git,除非 运行 git 命令)。

在您的活动命令 window 中,您需要为每个会话使用其中一个或两个,包括初始安装添加设备或插件以及在第一次应用程序构建时:

设置https_proxy=http://username:password@proxy:port

设置http_proxy=http://username:password@proxy:port

所以我的看起来像这样:

设置https_proxy=http://john.doe:1234@proxy.det.nsw.edu.au:8080

使用 git 2.8(2016 年 3 月),您不必在 url.

中以明文形式嵌入您的密码

参见 commit 372370f, commit ef97639 (26 Jan 2016) by Knut Franke (``)
帮助:Junio C Hamano (gitster), Eric Sunshine (sunshinebell28), and Elia Pinto (devzero2000).
(由 Junio C Hamano -- gitster -- in commit 30f302f 合并,2016 年 2 月 3 日)

http: use credential API to handle proxy authentication

Currently, the only way to pass proxy credentials to curl is by including them in the proxy URL. Usually, this means they will end up on disk unencrypted, one way or another (by inclusion in ~/.gitconfig, shell profile or history).
Since proxy authentication often uses a domain user, credentials can be security sensitive; therefore, a safer way of passing credentials is desirable.

If the configured proxy contains a username but not a password, query the credential API for one. Also, make sure we approve/reject proxy credentials properly. So:

In addition to the syntax understood by curl, it is possible to specify a proxy string with a user name but no password, in which case git will attempt to acquire one in the same way it does for other credentials.
See gitcredentials for more information.
The syntax thus is:

[protocol://][user[:password]@]proxyhost[:port]

This can be overridden on a per-remote basis; see remote.<name>.proxy.


http: allow selection of proxy authentication method

CURLAUTH_ANY does not work with proxies which answer unauthenticated requests with a 307 redirect to an error page instead of a 407 listing supported authentication methods.
Therefore, allow the authentication method to be set using the environment variable GIT_HTTP_PROXY_AUTHMETHOD or configuration variables http.proxyAuthmethod and remote.<name>.proxyAuthmethod (in analogy to http.proxy and remote.<name>.proxy).

The following values are supported:

  • anyauth (default)
  • basic
  • digest
  • negotiate
  • ntlm