javascript – 通过SSL / TLS连接在Vagrant上访问Node.js应用程序

所以我继承了一个Nodes.js应用程序,我在Vagrant盒子上运行.

我有app绑定到“0.0.0.0”,它在securekey文件夹中有自己的server.key和certs.

var https = require('https');
var fs = require('fs');
var ssl_options = {
    key: fs.readFileSync('./securekey/server.key'),
    cert: fs.readFileSync('./securekey/server.crt'),
    ca: fs.readFileSync('./securekey/ca.crt')
     };

https.createServer(ssl_options, app).listen(3001, '0.0.0.0');

当我运行应用程序时,我希望能够通过URL https://localhost:3001在我的Windows(Vagrant在我的Windows PC上运行)浏览器上访问它

但是我在Mozilla上获得了“安全连接失败”.

我在使用Cygwin的Windows PC上尝试过这个:

$openssl s_client -host 127.0.0.1 -port 3001
CONNECTED(00000003)
write:errno=104
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 0 bytes and written 316 bytes
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : 0000
    Session-ID:
    Session-ID-ctx:
    Master-Key:
    Key-Arg   : None
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    Start Time: 1461923745
    Timeout   : 300 (sec)
    Verify return code: 0 (ok)
---

$curl -v -k 'https://localhost:3001'
* STATE: INIT => CONNECT handle 0x6000574a0; line 1103 (connection #-5000)
* Rebuilt URL to: https://localhost:3001/
* Added connection 0. The cache now contains 1 members
*   Trying 127.0.0.1...
* STATE: CONNECT => WAITCONNECT handle 0x6000574a0; line 1156 (connection #0)
* Connected to localhost (127.0.0.1) port 3001 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x6000574a0; line 1253 (connection #0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* STATE: SENDPROTOCONNECT => PROTOCONNECT handle 0x6000574a0; line 1267 (connection #0)
* Unknown SSL protocol error in connection to localhost:3001
* Curl_done
* Closing connection 0
* The cache now contains 0 members
curl: (35) Unknown SSL protocol error in connection to localhost:3001

但是这些命令在Vagrant vm终端上运行时返回成功的连接!

我需要做什么才能让我的Windows PC /浏览器接受应用程序的证书,以便我可以从Mozilla Firefox访问该应用程序?既然它已经有了server.key和certs,我肯定不需要为应用程序再次生成自己的密钥了吗?

编辑:
这是我的Vagrant文​​件:

Vagrant.configure(2) do |config|
  config.vm.box = "centos7"
  config.vm.network "forwarded_port", guest: 3000, host: 3000, auto_correct: true
  config.vm.network "forwarded_port", guest: 3001, host: 3001, auto_correct: true
end

我只有端口转发配置..其余的是默认的.

当应用程序在Vagrant上运行时,netstat会显示端口正在侦听连接

$netstat -an | grep 3001
  TCP    0.0.0.0:3001           0.0.0.0:0              LISTENING

当我在浏览器上访问https://localhost:3001时,我看到了这个:

 netstat -an | grep 3001
  TCP    0.0.0.0:3001           0.0.0.0:0              LISTENING
  TCP    127.0.0.1:3001         127.0.0.1:49651        ESTABLISHED
  TCP    127.0.0.1:49651        127.0.0.1:3001         ESTABLISHED

好像端口连接很好,但是vm无法返回数据.

最佳答案 经过多次挖掘,我偶然发现了这个评论:

https://unix.stackexchange.com/a/255404

因为我在CentOS 7上,禁用firewalld对我来说很有用.我没有意识到这种变化.从某种意义上说,joelnb在他的回答评论中检查iptables的说明是正确的方向(谢谢!).检查操作系统的防火墙并尝试禁用它以查看它是否有助于解决问题.如果是,则可以根据需要继续为端口配置规则.

对于CentOS 7,在firewalld上打开一个端口:
centos 7 – open firewall port

我希望这有助于某人.

点赞