Skip to content

Commit eae5596

Browse files
paddybyersRuben Bridgewater
authored andcommitted
Add support for TLS connections
1 parent 918882f commit eae5596

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

index.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var net = require('net');
4+
var tls = require('tls');
45
var URL = require('url');
56
var util = require('util');
67
var utils = require('./lib/utils');
@@ -46,7 +47,10 @@ function RedisClient (options) {
4647
cnx_options.family = (!options.family && net.isIP(cnx_options.host)) || (options.family === 'IPv6' ? 6 : 4);
4748
this.address = cnx_options.host + ':' + cnx_options.port;
4849
}
49-
this.connection_option = cnx_options;
50+
for (var tls_option in options.tls) { // jshint ignore: line
51+
cnx_options[tls_option] = options.tls[tls_option];
52+
}
53+
this.connection_options = cnx_options;
5054
this.connection_id = ++connection_id;
5155
this.connected = false;
5256
this.ready = false;
@@ -95,7 +99,18 @@ util.inherits(RedisClient, events.EventEmitter);
9599
// Attention: the function name "create_stream" should not be changed, as other libraries need this to mock the stream (e.g. fakeredis)
96100
RedisClient.prototype.create_stream = function () {
97101
var self = this;
98-
this.stream = net.createConnection(this.connection_option);
102+
103+
// On a reconnect destroy the former stream and retry
104+
if (this.stream) {
105+
this.stream.removeAllListeners();
106+
this.stream.destroy();
107+
}
108+
109+
if (this.options.tls) {
110+
this.stream = tls.connect(this.connection_options);
111+
} else {
112+
this.stream = net.createConnection(this.connection_options);
113+
}
99114

100115
if (this.options.connect_timeout) {
101116
this.stream.setTimeout(this.connect_timeout, function () {
@@ -104,7 +119,8 @@ RedisClient.prototype.create_stream = function () {
104119
});
105120
}
106121

107-
this.stream.once('connect', function () {
122+
var connect_event = this.options.tls ? "secureConnect" : "connect";
123+
this.stream.on(connect_event, function () {
108124
this.removeAllListeners("timeout");
109125
self.on_connect();
110126
});
@@ -119,6 +135,10 @@ RedisClient.prototype.create_stream = function () {
119135
self.on_error(err);
120136
});
121137

138+
this.stream.on('clientError', function (err) {
139+
self.on_error(err);
140+
});
141+
122142
this.stream.once('close', function () {
123143
self.connection_gone('close');
124144
});

test/helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ module.exports = {
163163
},
164164
killConnection: function (client) {
165165
// Change the connection option to a non existing one and destroy the stream
166-
client.connection_option = {
166+
client.connection_options = {
167167
port: 65535,
168168
host: '127.0.0.1',
169169
family: 4

0 commit comments

Comments
 (0)