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

时间:2016-04-29 10:26:07

标签: javascript node.js ssl vagrant vagrant-windows

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

我将app绑定到“0.0.0.0”,并且在securekey文件夹中有自己的server.key和cert。

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虚拟机终端上运行时,这些命令会返回成功的连接!

我需要做些什么才能让我的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无法返回数据。

2 个答案:

答案 0 :(得分:1)

经过多次挖掘,我偶然发现了这个评论: https://unix.stackexchange.com/a/255404

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

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

我希望这有助于某人。

答案 1 :(得分:0)

我怀疑你没有在你的Vagrantfile中设置端口转发设置,因为如果我没有/没有正在监听该端口,我就会得到确切的错误。您的Vagrantfile是否如下所示? forwarded_port部分是重要的部分。

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.network "forwarded_port", guest: 3001, host: 3001
end

否则,您可以发布您的Vagrant文​​件,我会修改我的答案。