我正在尝试使用 node.js 和 TLS 建立与服务器的客户端连接,但它失败了。
我的问题是如何获取有关连接失败原因的更多信息。理想情况下,类似于使用 openssl(连接良好)时的情况,显示握手和证书交换。
目前我得到的只是{ [错误:套接字挂断]代码:'ECONNRESET'}。
我正在使用类似于下面基于互联网博客的代码。
var tls = require('tls'),
fs = require('fs');
// callback for when secure connection established
function connected(stream) {
if (stream) {
stream.write('Hello');
} else {
console.log("Connection failed");
}
}
// needed to keep socket variable in scope
var dummy = this;
// try to connect to the server
dummy.socket = tls.connect(
{
port: 8000,
host: 'dest.server.com',
ca: [
fs.readFileSync('C:\\Tools\\Certificates\\TestRoot.pem', 'utf8')
],
cert: fs.readFileSync('C:\\Tools\\Certificates\\source.server.com.cert.pem', 'utf8'),
key: fs.readFileSync('C:\\Tools\\Certificates\\source.server.com.key.pem', 'utf8'),
passphrase: 'password',
}
, function() {
// callback called only after successful socket connection
dummy.connected = true;
if (dummy.socket.authorized) {
// authorization successful
console.log('auth ok');
dummy.socket.setEncoding('utf-8');
connected(dummy.socket);
} else {
// authorization failed
console.log('auth fail');
console.log(dummy.socket.authorizationError);
connected(null);
}
});
dummy.socket.addListener('data', function(data) {
// received data
console.log(data);
});
dummy.socket.addListener('error', function(error) {
if (!dummy.connected) {
// socket was not connected, notify callback
connected(null);
}
console.log("FAIL");
console.log(error);
});
dummy.socket.addListener('close', function() {
// do something
});