Skip to content
This repository was archived by the owner on Nov 28, 2018. It is now read-only.

Commit ba227bc

Browse files
author
Joshua Rubin
committed
add ssl support
to use ssl, there are a few new arguments in the object passed to the Stomp constructor: - ssl: `{boolean}`, use ssl for the connection - ssl_validate: `{boolean}`, disconnect if the certificate does not validate - ssl_options: `{object}`, passed into the tls.connect method as the `options` parameter, see <http://nodejs.org/docs/v0.4.8/api/tls.html> Signed-off-by: Joshua Rubin <jrubin@zvelo.com>
1 parent 5fd25f2 commit ba227bc

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

lib/stomp.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222

2323
var net = require('net'),
24+
tls = require('tls'),
2425
sys = require('sys'),
2526
frame = require('./frame'),
2627
stomp_utils = require('./stomp-utils'),
@@ -91,21 +92,41 @@ function parse_frame(chunk) {
9192
};
9293

9394
function _connect(stomp) {
94-
var socket = stomp.socket = net.Stream();
9595
log = stomp.log;
96-
log.debug('Connecting to ' + stomp.host + ':' + stomp.port);
97-
socket.connect(stomp.port, stomp.host);
9896

99-
socket.on('connect', function() {
97+
if (stomp.ssl) {
98+
log.debug('Connecting to ' + stomp.host + ':' + stomp.port + ' using SSL');
99+
stomp.socket = tls.connect(stomp.port, stomp.host, stomp.ssl_options, function() {
100+
log.debug('SSL connection complete');
101+
if (!stomp.socket.authorized) {
102+
log.error('SSL is not authorized: '+stomp.socket.authorizationError);
103+
if (stomp.ssl_validate) {
104+
_disconnect(stomp);
105+
return;
106+
}
107+
}
108+
_setupListeners(stomp);
109+
});
110+
} else {
111+
log.debug('Connecting to ' + stomp.host + ':' + stomp.port);
112+
stomp.socket = new net.Socket();
113+
stomp.socket.connect(stomp.port, stomp.host);
114+
_setupListeners(stomp);
115+
}
116+
}
117+
118+
function _setupListeners(stomp) {
119+
function _connected() {
100120
log.debug('Connected to socket');
101121
if (utils.really_defined(stomp.login) &&
102122
utils.really_defined(stomp.passcode)) {
103123
stomp_connect(stomp, {login: stomp.login, passcode: stomp.passcode});
104-
}
105-
else {
124+
} else {
106125
stomp_connect(stomp);
107126
}
108-
});
127+
}
128+
129+
var socket = stomp.socket;
109130

110131
socket.on('drain', function(data) {
111132
log.debug('draining');
@@ -122,10 +143,9 @@ function _connect(stomp) {
122143
var parsed_frame = null;
123144
var _frame = null;
124145
while (_frame = frames.shift()) {
125-
parsed_frame = parse_frame(_frame)
146+
parsed_frame = parse_frame(_frame);
126147
stomp.handle_new_frame(parsed_frame);
127148
}
128-
129149
});
130150

131151
socket.on('end', function() {
@@ -142,6 +162,12 @@ function _connect(stomp) {
142162
log.error('Disconnected with error: ' + error);
143163
stomp.emit("disconnected");
144164
});
165+
166+
if (stomp.ssl) {
167+
_connected();
168+
} else {
169+
socket.on('connect', _connected);
170+
}
145171
};
146172

147173
function stomp_connect(stomp, headers) {
@@ -155,7 +181,6 @@ function stomp_connect(stomp, headers) {
155181
var frame_to_send = _frame.build_frame(args);
156182

157183
send_frame(stomp, frame_to_send);
158-
log.debug('Connected to STOMP');
159184
};
160185

161186
function _disconnect(stomp) {
@@ -208,6 +233,9 @@ function Stomp(args) {
208233
this.log = new StompLogging(this.debug);
209234
this._subscribed_to = {};
210235
this.session = null;
236+
this.ssl = args['ssl'] ? true : false;
237+
this.ssl_validate = args['ssl_validate'] ? true : false;
238+
this.ssl_options = args['ssl_options'] || [];
211239
};
212240

213241
Stomp.prototype = new process.EventEmitter();
@@ -234,6 +262,7 @@ Stomp.prototype.handle_new_frame = function(this_frame) {
234262
self.emit('message', this_frame);
235263
break;
236264
case "CONNECTED":
265+
log.debug('Connected to STOMP');
237266
self.session = this_frame.headers['session'];
238267
self.emit('connected');
239268
break;

0 commit comments

Comments
 (0)