I'm trying to set up a client connection to a server using node.js and TLS but it's failing.
My question is how to get more information about why the connection is failing. Ideally something similar to when using openssl (which connects fine) which shows the handshaking and certificate exchange.
At the moment all I get is { [Error: socket hang up] code: 'ECONNRESET' }.
I'm using something like the code below which was based on an internet blog.
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
});
OK after a bit of digging into the source it seems you can get some additional information by setting the NODE_DEBUG environment variable to include 'tls'. It didn't help much in my case but it might be helpful for someone else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With