Skip to content

Commit

Permalink
ca option and reporting errors no ECONNREFUSED
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ifraixedes committed Nov 14, 2013
1 parent 78dad5b commit d29b3d6
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/winston-logstash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
Expand All @@ -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
Expand Down Expand Up @@ -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++;
Expand All @@ -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');
Expand All @@ -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() {
Expand All @@ -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();
Expand Down

0 comments on commit d29b3d6

Please sign in to comment.