【发布时间】:2018-06-14 01:10:57
【问题描述】:
使用 OpenSSL 生成自签名证书,并将证书和私钥复制到所需的目标文件夹。
要创建 HTTPS 服务器,我们需要两件事:SSL 证书和 Node 内置的 https 模块。
安装 Node.js 后,我尝试从命令行运行以下 JavaScript
TLSServer.js
var tls = require('tls');
var fs = require('fs');
var port = 8081; //3000;
var host = '127.0.0.1'; //192.168.1.135
var options = {
key: fs.readFileSync('private-key.pem'), // /path/to/private-key.pem
cert: fs.readFileSync('certificate.pem') // /path/to/certificate.pem
};
TLSClient.js
var client = tls.connect(port, host, options, function() {
console.log('connected');
if (client.authorized) {
console.log('authorized: ' + client.authorized);
client.on('data', function(data) {
client.write(data); // Just send data back to server
});
} else {
console.log('connection not authorized: ' + client.authorizationError);
}
});
实际输出:
cmd>node TLSServer.js
openssl config failed: error:02001005:system library:fopen:Input/output error
cmd>node TLSClient.js
openssl config failed: error:02001005:system library:fopen:Input/output error
events.js:193
throw er; // Unhandled 'error' event
^
Error: connect ETIMEDOUT 127.0.0.1:8081
at Object._errnoException (util.js:1031:13)
at _exceptionWithHostPort (util.js:1052:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1195:14)
出现此问题的原因可能是什么:
openssl 配置失败:错误:02001005:系统库:fopen:输入/输出错误
httpserver.js
var fs = require('fs');
var https = require('https');
var options = {
key: fs.readFileSync('private-key.pem'),
cert: fs.readFileSync('certificate.pem')
};
https.createServer(options, function (req, res) {
res.writeHead(200);
res.write("You are connected to https server");
res.end("\n hello world \n");
}).listen(8080)
我曾经从浏览器获得以下输出:
You are connected to https server
hello world
但不适用于 TLS 客户端/服务器。但是 OpenSSL 配置文件中可能需要修改什么?
【问题讨论】:
-
可能权限不足;您的 cmd 是否使用管理员帐户启动?
-
您可能需要安装库以支持 openssl
-
@EugèneAdell 是的
-
@Malice 运行需要什么命令
-
另见"OPENSSL_CONF" variable。它通常是由于带有 PHP 和 Windows 系统的 WAMP 而出现的。
标签: node.js ssl cmd openssl tls1.2