使用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 config失败:错误:02001005:系统库:fopen:输入/输出错误
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配置文件中可以修改什么?
答案 0 :(得分:0)
通过在环境变量中添加openssl.cnf的路径解决openssl config failed: error:02001005:system library:fopen:Input/output error
- >系统变量
OPENSSL_CONF=C:\OpenSSL-Win64\bin\openssl.cnf
要验证它,您可以输入外壳:
echo %OPENSSL_CONF%
但我仍然遇到 TLSServer.js
的错误cmd>node TLSServer.js
module.js:544
throw err;
^
Error: Cannot find module 'C:\Users\user\Desktop\TLSServer.js'
at Function.Module._resolveFilename (module.js:542:15)
at Function.Module._load (module.js:472:25)
at Function.Module.runMain (module.js:682:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:613:3