From d29b3d62d03c9fa20110b2d599b4ed0898cae8d0 Mon Sep 17 00:00:00 2001 From: Ivan Fraixedes Date: Thu, 14 Nov 2013 18:13:26 +0000 Subject: [PATCH] ca option and reporting errors no ECONNREFUSED I added the ```ca``` options to provide an array with the CAs that winston-logstash secure socket accept, so it is provided to node client secure socket connection. I also added a checking in socket ```error``` listener to report to transport eventEmitter when socket received an error different from ECONNREFUSED --- lib/winston-logstash.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/winston-logstash.js b/lib/winston-logstash.js index 8b5de6f..090b6b9 100644 --- a/lib/winston-logstash.js +++ b/lib/winston-logstash.js @@ -13,6 +13,8 @@ var net = require('net'), winston = require('winston'), common = require('winston/lib/winston/common'); +var ECONNREFUSED_REGEXP = /ECONNREFUSED/; + var Logstash = exports.Logstash = function (options) { winston.Transport.call(this, options); options = options || {}; @@ -27,6 +29,7 @@ var Logstash = exports.Logstash = function (options) { this.ssl_enable = options.ssl_enable || false; this.ssl_key = options.ssl_key || ''; this.ssl_cert = options.ssl_cert || ''; + this.ca = options.ca || ''; this.ssl_passphrase = options.ssl_passphrase || ''; // Connection state @@ -84,6 +87,7 @@ Logstash.prototype.log = function (level, msg, meta, callback) { }; Logstash.prototype.connect = function () { + var tryReconnect = true; var options = {}; var self = this; this.retries++; @@ -92,7 +96,16 @@ Logstash.prototype.connect = function () { options = { key: this.ssl_key ? fs.readFileSync(this.ssl_key) : null, cert: this.ssl_cert ? fs.readFileSync(this.ssl_cert) : null, - passphrase: this.ssl_passphrase ? this.ssl_passphrase : null + passphrase: this.ssl_passphrase ? this.ssl_passphrase : null, + ca: this.ca ? (function (caList) { + var caFilesList = []; + + caList.forEach(function (filePath) { + caFilesList.push(fs.readFileSync(filePath)); + }); + + return caFilesList; + }(this.ca)) : null } this.socket = new tls.connect(this.port, this.host, options, function() { self.socket.setEncoding('UTF-8'); @@ -108,6 +121,11 @@ Logstash.prototype.connect = function () { self.connected = false; self.socket.destroy(); self.socket = null; + + if (!ECONNREFUSED_REGEXP.test(err.message)) { + tryReconnect = false; + self.emit('error', err); + } }); this.socket.on('timeout', function() { @@ -119,7 +137,7 @@ Logstash.prototype.connect = function () { this.socket.on('close', function (had_error) { self.connected = false; - if (self.max_connect_retries === -1 || self.retries < self.max_connect_retries) { + if ((tryReconnect) && (self.max_connect_retries === -1 || self.retries < self.max_connect_retries)) { if (!self.connecting) { setTimeout(function () { self.connect();