首页 > 解决方案 > 如何让 HTTPS 在 Windows 10 的命令行中工作(请注意 HTTP 正在工作)?

问题描述

我已经在网上搜索了几个小时,但没有找到解决方案。我什至尝试禁用 Windows 防火墙和 Windows Defender 以查看它是否阻止了来自命令行的传出 HTTPS 请求。

我在我的 Win10 机器上安装了 Node/Npm,我试图安装一些节点模块,但我不断收到ECONNRESET错误。我的 System32 文件夹中还有一个 curl.exe,它也不适用于我尝试过的任何 HTTPS 链接。

>curl.exe --version
curl 7.55.1 (Windows) libcurl/7.55.1 WinSSL
Release-Date: [unreleased]
Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile SSPI Kerberos SPNEGO NTLM SSL

>node --version
v10.16.3

>npm --version
6.12.0

(注:NPM 版本原为 6.3.x,稍后详解)

当我尝试以下操作时,出现握手错误:

>curl.exe -vvv https://www.google.com
* Rebuilt URL to: https://www.google.com/
*   Trying 172.217.25.4...
* TCP_NODELAY set
* Connected to www.google.com (172.217.25.4) port 443 (#0)
* schannel: SSL/TLS connection with www.google.com port 443 (step 1/3)
* schannel: checking server certificate revocation
* schannel: sending initial handshake data: sending 179 bytes...
* schannel: sent initial handshake data: sent 179 bytes
* schannel: SSL/TLS connection with www.google.com port 443 (step 2/3)
* schannel: failed to receive handshake, need more data
* schannel: SSL/TLS connection with www.google.com port 443 (step 2/3)
* schannel: encrypted data got 2536
* schannel: encrypted data buffer: offset 2536 length 4096
* schannel: sending next handshake data: sending 93 bytes...
* schannel: SSL/TLS connection with www.google.com port 443 (step 2/3)
* schannel: failed to receive handshake, SSL/TLS connection failed
* Closing connection 0
* schannel: shutting down SSL/TLS connection with www.google.com port 443
* schannel: clear security context handle
curl: (35) schannel: failed to receive handshake, SSL/TLS connection failed
>curl.exe -vvv https://registry.npmjs.org
* Rebuilt URL to: https://registry.npmjs.org/
*   Trying 104.16.27.35...
* TCP_NODELAY set
* Connected to registry.npmjs.org (104.16.27.35) port 443 (#0)
* schannel: SSL/TLS connection with registry.npmjs.org port 443 (step 1/3)
* schannel: checking server certificate revocation
* schannel: sending initial handshake data: sending 183 bytes...
* schannel: sent initial handshake data: sent 183 bytes
* schannel: SSL/TLS connection with registry.npmjs.org port 443 (step 2/3)
* schannel: failed to receive handshake, need more data
* schannel: SSL/TLS connection with registry.npmjs.org port 443 (step 2/3)
* schannel: encrypted data got 3706
* schannel: encrypted data buffer: offset 3706 length 4096
* schannel: sending next handshake data: sending 93 bytes...
* schannel: SSL/TLS connection with registry.npmjs.org port 443 (step 2/3)
* schannel: failed to receive handshake, SSL/TLS connection failed
* Closing connection 0
* schannel: shutting down SSL/TLS connection with registry.npmjs.org port 443
* schannel: clear security context handle
curl: (35) schannel: failed to receive handshake, SSL/TLS connection failed

但是将HTTPS更改为HTTP有效...

>curl.exe -vvv http://www.google.com
* Rebuilt URL to: http://www.google.com/
*   Trying 172.217.24.68...
* TCP_NODELAY set
* Connected to www.google.com (172.217.24.68) port 80 (#0)
> GET / HTTP/1.1
> Host: www.google.com
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Sat, 19 Oct 2019 01:20:34 GMT
< Expires: -1
< Cache-Control: private, max-age=0
[RESPOSNE WAS TOO LONG TO INCLUDE IN POST, NO SENSE IN PASTING THE WHOLE THING]

如您所见,HTTPS 给出错误,但 HTTP 没有。我没有配置任何代理,我的网络没有限制,我什至尝试禁用 Windows 防火墙和 Windows Defender。我在同一台机器上有一个 Ubuntu WSL,一个 Ubuntu VM(通过 vmware),它们中的 HTTPS 没有问题。

为了测试这一点,我将 npm 注册表从https 更改为 http

NPM 注册表设置为HTTPS

>npm install --global --production npm-windows-upgrade --verbose
npm info it worked if it ends with ok
npm verb cli [ 'C:\\Program Files\\nodejs\\node.exe',
npm verb cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
npm verb cli   'install',
npm verb cli   '--global',
npm verb cli   '--production',
npm verb cli   'npm-windows-upgrade',
npm verb cli   '--verbose' ]
npm info using npm@6.12.0
npm info using node@v10.16.3
npm verb npm-session 8f54e2561f964acb
npm timing stage:rollbackFailedOptional Completed in 0ms
npm timing stage:runTopLevelLifecycles Completed in 115092ms
npm verb type system
npm verb stack FetchError: request to https://registry.npmjs.org/npm-windows-upgrade failed, reason: Client network socket disconnected before secure TLS connection was established
npm verb stack     at ClientRequest.req.on.err (C:\Program Files\nodejs\node_modules\npm\node_modules\node-fetch-npm\src\index.js:68:14)
npm verb stack     at ClientRequest.emit (events.js:198:13)
npm verb stack     at TLSSocket.socketErrorListener (_http_client.js:392:9)
npm verb stack     at TLSSocket.emit (events.js:198:13)
npm verb stack     at emitErrorNT (internal/streams/destroy.js:91:8)
npm verb stack     at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
npm verb stack     at process._tickCallback (internal/process/next_tick.js:63:19)
npm verb cwd C:\WINDOWS\system32
npm verb Windows_NT 10.0.18362
npm verb argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "--global" "--production" "npm-windows-upgrade" "--verbose"
npm verb node v10.16.3
npm verb npm  v6.12.0
npm ERR! code ECONNRESET
npm ERR! errno ECONNRESET
npm ERR! network request to https://registry.npmjs.org/npm-windows-upgrade failed, reason: Client network socket disconnected before secure TLS connection was established
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'
npm verb exit [ 1, true ]
npm timing npm Completed in 116077ms

NPM 注册表设置为HTTP

>npm install --global --production npm-windows-upgrade --verbose
npm info it worked if it ends with ok
npm verb cli [ 'C:\\Program Files\\nodejs\\node.exe',
npm verb cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
npm verb cli   'install',
npm verb cli   '--global',
npm verb cli   '--production',
npm verb cli   'npm-windows-upgrade',
npm verb cli   '--verbose' ]
npm info using npm@6.12.0
npm info using node@v10.16.3
npm verb npm-session 4cde14186baae336
npm http fetch GET 304 http://registry.npmjs.org/npm-windows-upgrade 451ms (from cache)
npm timing stage:loadCurrentTree Completed in 1439ms
npm timing stage:loadIdealTree:cloneCurrentTree Completed in 1ms
npm timing stage:loadIdealTree:loadShrinkwrap Completed in 20ms
npm http fetch GET 304 http://registry.npmjs.org/chalk 137ms (from cache)
npm http fetch GET 304 http://registry.npmjs.org/debug 139ms (from cache)
npm http fetch GET 304 http://registry.npmjs.org/commander 146ms (from cache)
npm http fetch GET 304 http://registry.npmjs.org/cli-spinner 154ms (from cache)
npm http fetch GET 304 http://registry.npmjs.org/inquirer 155ms (from cache)
npm http fetch GET 304 http://registry.npmjs.org/prompt 446ms (from cache)
npm http fetch GET 304 http://registry.npmjs.org/escape-string-regexp 36ms (from cache)
npm http fetch GET 304 http://registry.npmjs.org/supports-color 117ms (from cache)
npm http fetch GET 304 http://registry.npmjs.org/ansi-styles 139ms (from cache)

从上面可以看出,如果它的 HTTP 没有问题。我特别记得,几个月前 HTTPS 可以正常工作,所以我不确定最新更新的 Windows 10 中发生了什么变化。下面是我的Win10版本。

版本:Windows 10 Home Single License 版本:1903 OS Build:18362.418

有谁能够帮我?

标签: node.jssslnpmcmdhttps

解决方案


推荐阅读