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

Accessing Node.js app on Vagrant via SSL/TLS connection

本文关键字:上的 Node js 应用程序 Vagrant 访问 SSL TLS 连接 通过      更新时间:2023-09-26

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

我有绑定到"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在我的Windows(Vagrant正在我的Windows PC上运行)浏览器上访问它https://localhost:3001

但我在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.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

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

对于CentOS 7,要在firewalld上打开端口:centos 7-打开防火墙端口

我希望这能帮助到别人。

我怀疑您的Vagrantfile中没有端口转发设置,因为如果我没有/如果没有任何东西在该端口上侦听,我会得到确切的错误。您的Vagartfile看起来像下面这样吗?转发部分是重要的部分。

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

否则,请你张贴你的流浪者档案,我会修改我的答案。