Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tls protocol #165

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 42 additions & 18 deletions lib/sockets/sock.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var Parser = require('amp').Stream;
var url = require('url');
var net = require('net');
var fs = require('fs');

var tls = require('tls');
/**
* Errors to ignore.
*/
Expand Down Expand Up @@ -144,7 +144,7 @@ Socket.prototype.closeServer = function(fn){
Socket.prototype.address = function(){
if (!this.server) return;
var addr = this.server.address();
addr.string = 'tcp://' + addr.address + ':' + addr.port;
addr.string = (this.get('tls') ? 'tls://' : 'tcp://') + addr.address + ':' + addr.port;
return addr;
};

Expand Down Expand Up @@ -261,13 +261,37 @@ Socket.prototype.connect = function(port, host, fn){
}

var max = self.get('retry max timeout');
var sock = new net.Socket;
sock.setNoDelay();
this.type = 'client';
var sock;
var tlsOpts = this.get('tls');

var onConnect = function() {
debug('%s connect', self.type);
self.connected = true;
self.addSocket(sock);
self.retry = self.get('retry timeout');
self.emit('connect', sock);
fn && fn();
};

if (tlsOpts) {
tlsOpts.host = host;
tlsOpts.port = port;
debug('%s connect attempt %s:%s', self.type, host, port);
sock = tls.connect(tlsOpts);
sock.on('secureConnect', onConnect);
} else {
sock = new net.Socket();
debug('%s connect attempt %s:%s', self.type, host, port);
sock.connect(port, host);
sock.on('connect', onConnect);
}

sock.setNoDelay();

this.handleErrors(sock);

sock.on('close', function(){
sock.on('close', function() {
self.emit('socket close', sock);
self.connected = false;
self.removeSocket(sock);
Expand All @@ -282,17 +306,6 @@ Socket.prototype.connect = function(port, host, fn){
}, retry);
});

sock.on('connect', function(){
debug('%s connect', self.type);
self.connected = true;
self.addSocket(sock);
self.retry = self.get('retry timeout');
self.emit('connect', sock);
fn && fn();
});

debug('%s connect attempt %s:%s', self.type, host, port);
sock.connect(port, host);
return this;
};

Expand All @@ -306,11 +319,15 @@ Socket.prototype.connect = function(port, host, fn){
Socket.prototype.onconnect = function(sock){
var self = this;
var addr = sock.remoteAddress + ':' + sock.remotePort;
var tlsOptions = self.get('tls');
if (tlsOptions && !sock.authorized) {
debug('%s denied %s for authorizationError %s', self.type, addr, sock.authorizationError);
}
debug('%s accept %s', self.type, addr);
this.addSocket(sock);
this.handleErrors(sock);
this.emit('connect', sock);
sock.on('close', function(){
sock.on('close', function() {
debug('%s disconnect %s', self.type, addr);
self.emit('disconnect', sock);
self.removeSocket(sock);
Expand Down Expand Up @@ -362,7 +379,14 @@ Socket.prototype.bind = function(port, host, fn){

this.type = 'server';

this.server = net.createServer(this.onconnect.bind(this));
var tlsOptions = this.get('tls');
if (tlsOptions) {
tlsOptions.requestCert = tlsOptions.requestCert !== false;
tlsOptions.rejectUnauthorized = tlsOptions.rejectUnauthorized !== false;
this.server = tls.createServer(tlsOptions, this.onconnect.bind(this));
} else {
this.server = net.createServer(this.onconnect.bind(this));
}

debug('%s bind %s:%s', this.type, host, port);
this.server.on('listening', this.emit.bind(this, 'bind'));
Expand Down