Skip to content

Commit

Permalink
Merge pull request #150 from luin/tls
Browse files Browse the repository at this point in the history
Add support for TLS
  • Loading branch information
luin committed Sep 16, 2015
2 parents bdd9636 + 2d3677e commit 85080e0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Master Branch

* Support TLS.
* Support reconnecting on the specified error.

### v1.8.0 - September 9, 2015
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ used in the world's biggest online commerce company [Alibaba](http://www.alibaba
0. Transparent key prefixing.
0. Abstraction for Lua scripting, allowing you to define custom commands.
0. Support for binary data.
0. Support for both TCP/IP and UNIX domain sockets.
0. Support for TLS.
0. Support for offline queue and ready checking.
0. Support for ES6 types, such as `Map` and `Set`.
0. Support for GEO commands (Redis 3.2 Unstable).
Expand Down Expand Up @@ -568,6 +568,21 @@ function (times) {
}
```

## TLS Options
Redis doesn't support TLS natively, however if the redis server you want to connect to is hosted behind a TLS proxy (e.g. [stunnel](https://www.stunnel.org/)) or is offered by a PaaS service that supports TLS connection (e.g. [Redis Labs](https://redislabs.com/)), you can set the `tls` option:

```javascript
var redis = new Redis({
host: 'localhost',
tls: {
// Refer to `tls.connect()` section in
// https://nodejs.org/api/tls.html
// for all supported options
ca: fs.readFileSync('cert.pem')
}
});
```

## Cluster
Redis Cluster provides a way to run a Redis installation where data is automatically sharded across multiple Redis nodes.
You can connect to a Redis Cluster like this:
Expand Down
12 changes: 11 additions & 1 deletion lib/connectors/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var _ = require('lodash');
var net = require('net');
var tls = require('tls');

function Connector(options) {
this.options = options;
Expand All @@ -26,14 +27,23 @@ Connector.prototype.connect = function (callback) {
} else {
connectionOptions = _.pick(this.options, ['port', 'host', 'family']);
}
if (this.options.tls) {
_.assign(connectionOptions, this.options.tls);
}

var _this = this;
process.nextTick(function () {
if (!_this.connecting) {
callback(new Error('Connection is closed.'));
return;
}
var stream = _this.stream = net.createConnection(connectionOptions);
var stream;
if (_this.options.tls) {
stream = tls.connect(connectionOptions);
} else {
stream = net.createConnection(connectionOptions);
}
_this.stream = stream;
callback(null, stream);
});
};
Expand Down

0 comments on commit 85080e0

Please sign in to comment.