1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-07 13:22:56 +03:00

Add support for TLS connections

This commit is contained in:
Paddy Byers
2015-10-19 17:16:41 +01:00
committed by Ruben Bridgewater
parent 918882f0bf
commit eae5596a3c
2 changed files with 24 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
'use strict'; 'use strict';
var net = require('net'); var net = require('net');
var tls = require('tls');
var URL = require('url'); var URL = require('url');
var util = require('util'); var util = require('util');
var utils = require('./lib/utils'); var utils = require('./lib/utils');
@@ -46,7 +47,10 @@ function RedisClient (options) {
cnx_options.family = (!options.family && net.isIP(cnx_options.host)) || (options.family === 'IPv6' ? 6 : 4); cnx_options.family = (!options.family && net.isIP(cnx_options.host)) || (options.family === 'IPv6' ? 6 : 4);
this.address = cnx_options.host + ':' + cnx_options.port; this.address = cnx_options.host + ':' + cnx_options.port;
} }
this.connection_option = cnx_options; for (var tls_option in options.tls) { // jshint ignore: line
cnx_options[tls_option] = options.tls[tls_option];
}
this.connection_options = cnx_options;
this.connection_id = ++connection_id; this.connection_id = ++connection_id;
this.connected = false; this.connected = false;
this.ready = false; this.ready = false;
@@ -95,7 +99,18 @@ util.inherits(RedisClient, events.EventEmitter);
// Attention: the function name "create_stream" should not be changed, as other libraries need this to mock the stream (e.g. fakeredis) // Attention: the function name "create_stream" should not be changed, as other libraries need this to mock the stream (e.g. fakeredis)
RedisClient.prototype.create_stream = function () { RedisClient.prototype.create_stream = function () {
var self = this; var self = this;
this.stream = net.createConnection(this.connection_option);
// On a reconnect destroy the former stream and retry
if (this.stream) {
this.stream.removeAllListeners();
this.stream.destroy();
}
if (this.options.tls) {
this.stream = tls.connect(this.connection_options);
} else {
this.stream = net.createConnection(this.connection_options);
}
if (this.options.connect_timeout) { if (this.options.connect_timeout) {
this.stream.setTimeout(this.connect_timeout, function () { this.stream.setTimeout(this.connect_timeout, function () {
@@ -104,7 +119,8 @@ RedisClient.prototype.create_stream = function () {
}); });
} }
this.stream.once('connect', function () { var connect_event = this.options.tls ? "secureConnect" : "connect";
this.stream.on(connect_event, function () {
this.removeAllListeners("timeout"); this.removeAllListeners("timeout");
self.on_connect(); self.on_connect();
}); });
@@ -119,6 +135,10 @@ RedisClient.prototype.create_stream = function () {
self.on_error(err); self.on_error(err);
}); });
this.stream.on('clientError', function (err) {
self.on_error(err);
});
this.stream.once('close', function () { this.stream.once('close', function () {
self.connection_gone('close'); self.connection_gone('close');
}); });

View File

@@ -163,7 +163,7 @@ module.exports = {
}, },
killConnection: function (client) { killConnection: function (client) {
// Change the connection option to a non existing one and destroy the stream // Change the connection option to a non existing one and destroy the stream
client.connection_option = { client.connection_options = {
port: 65535, port: 65535,
host: '127.0.0.1', host: '127.0.0.1',
family: 4 family: 4