我的用户名是以“domain\username”的形式命名的——在代理配置中包括斜杠会导致出现前向斜杠。因此,输入以下内容:
npm config set proxy "http://domain\username:password@servername:port/"
我还必须对我的domain\user
字符串进行URL编码,但是我的用户名中有一个空格,因此我放了一个+
来编码空格的URL编码,但它会被双重编码为%2B
(这是加号的URL编码,但空格的URL编码是%20
),因此我不得不改为执行以下操作:
npm命令
// option one
// it works for some packages
npm config set http_proxy "http://DOMAIN%5Cuser+name:password@x.x.x.x:port"
npm config set proxy "http://DOMAIN%5Cuser+name:password@x.x.x.x:port"
// option two
// it works best for me
// please notice that I actually used a space
// instead of URL encode it with '+', '%20 ' OR %2B (plus url encoded)
npm config set http_proxy "http://DOMAIN%5Cuser name:password@x.x.x.x:port"
npm config set proxy "http://DOMAIN%5Cuser name:password@x.x.x.x:port"
// option two (B) as of 2019-06-01
// no DOMAIN
// instead of URL encode it with '+', '%20 ' OR %2B (plus url encoded)
npm config set http_proxy "http://user name:password@x.x.x.x:port"
npm config set proxy "http://user name:password@x.x.x.x:port"
故障排除npm配置
我使用npm config list
获取我之前设置的解析值,这就是我发现双重编码问题的方式。很奇怪。
实质上,您必须确定以下要求:
- 是否需要进行身份验证的
DOMAIN
字符串
- 您是否需要对特殊字符进行编码?
祝好。
WINDOWS环境变量(CMD提示符)
更新
事实证明,即使有了上述配置,我仍然在使用Request - Simplified HTTP client下载一些东西时,仍然存在一些包/脚本的问题。因此,如上面的自述文件所解释的那样,我们可以指定环境变量在命令行上设置代理,而请求将尊重这些值。
然后,在经过(我不愿意承认这一点)若干次尝试(更像是几天时间)之后,我终于成功地按照以下准则设置了环境变量:
rem notice that the value after the = has no quotations
rem - I believe that if quotations are placed after it, they become
rem part of the value, you do not want that
rem notice that there is no space before or after the = sign
rem - if you leave a space before it, you will be declaring a variable
rem name that includes such space, you do not want to do that
rem - if you leave a space after it, you will be including the space
rem as part of the value, you do not want that either
rem looks like there is no need to URL encode stuff in there
SET HTTP_PROXY=http://DOMAIN\user name:password@x.x.x.x:port
SET HTTPS_PROXY=http://DOMAIN\user name:password@x.x.x.x:port
cntlm
I used this method for a few weeks, but then realized it was too cumbersome to update my password across all the tools that needed the proxy setup. Besides npm, I also use bower, vagrant (with virtual box running Linux), git, vscode, brackets, atom, and tsd.
Steps to Set Up Cntlm
To resolve these issues, I installed cntlm. Setting up cntlm is straightforward: simply locate the ini file at C:\Program Files\Cntlm\cntlm.ini
.
- Open the
C:\Program Files\Cntlm\cntlm.ini
file (you may need admin rights).
- Locate the
Username
and Domain
lines (probably lines 8-9).
- Add your username.
- Add your domain.
- In the command prompt, run:
cd C:\Program Files\Cntlm\
cntlm -M
cntlm -H
cygwin warning:
MS-DOS style path detected: C:\Program Files\Cntlm\cntlm.ini
Preferred POSIX equivalent is: /Cntlm/cntlm.ini
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
Password:
cntlm -H
命令的输出结果大致如下:
proxy.pac
文件以从中获取代理地址很有帮助。(在我的情况下,我只需要将端口切换到 8080)。 - sakura-bloom