Skip to content

Commit 0293932

Browse files
committed
Add ssl option
1 parent 715055c commit 0293932

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

index.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,38 @@
44
*/
55

66
var http = require('http'),
7+
https = require('https'),
78
faye = require('faye'),
89
server_auth = require('./server-auth'),
910
client_auth = require('./client-auth'),
11+
sslConfig = require('./ssl-config'),
1012
debug = require('debug')('openframe:pubsub'),
1113

1214
// Exported object
1315
pubsub = module.exports = {};
1416

1517
pubsub.connectedFrames = {};
1618

17-
pubsub.start = function(_port, _host) {
19+
pubsub.start = function (_port, _host) {
1820
var port = _port || process.env.PS_PORT || '8889',
1921
host = _host || process.env.PS_HOST || '0.0.0.0',
22+
protocol = process.env.PS_HOST || 'http',
2023
path = process.env.PS_PATH || '/faye',
2124
api_url = process.env.API_EXPOSED_URL || 'http://0.0.0.0:8888/api',
22-
server = http.createServer(),
23-
bayeux = new faye.NodeAdapter({mount: path, timeout: 5}),
25+
// server = http.createServer(),
26+
bayeux = new faye.NodeAdapter({ mount: path, timeout: 5 }),
2427
client = bayeux.getClient();
2528

29+
let server = null;
30+
if (protocol === 'https') {
31+
const options = {
32+
key: sslConfig.getPrivateKey(),
33+
cert: sslConfig.getCertificate(),
34+
};
35+
server = https.createServer(options);
36+
} else {
37+
server = http.createServer();
38+
}
2639

2740
// add server_auth extension
2841
bayeux.addExtension(server_auth(api_url));
@@ -33,13 +46,13 @@ pubsub.start = function(_port, _host) {
3346
/**
3447
* Handle new client connection
3548
*/
36-
bayeux.on('handshake', function(clientId) {
49+
bayeux.on('handshake', function (clientId) {
3750
debug('new bayeux connection: ', clientId);
3851
});
3952

4053
// When a client disconnects, see if it's in the hash of connected frames.
4154
// If so, publish a disconnect event to any subscribed pubsub clients (e.g. the API server)
42-
bayeux.on('disconnect', function(clientId) {
55+
bayeux.on('disconnect', function (clientId) {
4356
debug('bayeux connection closed: ', clientId);
4457
if (pubsub.connectedFrames[clientId]) {
4558
var frame_id = pubsub.connectedFrames[clientId];
@@ -54,7 +67,7 @@ pubsub.start = function(_port, _host) {
5467
// add it to the list of connected frames
5568
//
5669
// also re-publish frame_id-namespaced connect event
57-
bayeux.on('publish', function(clientId, channel, data) {
70+
bayeux.on('publish', function (clientId, channel, data) {
5871
debug('published', channel, data);
5972
if (channel === '/frame/connected') {
6073
pubsub.connectedFrames[clientId] = data;
@@ -70,11 +83,11 @@ pubsub.start = function(_port, _host) {
7083
// Just some logging...
7184
// TODO: log to file.
7285

73-
client.subscribe('/frame/connected', function(data) {
86+
client.subscribe('/frame/connected', function (data) {
7487
debug('/frame/connected', data);
7588
});
7689

77-
client.subscribe('/frame/disconnected', function(data) {
90+
client.subscribe('/frame/disconnected', function (data) {
7891
debug('/frame/disconnected', data);
7992
});
8093

ssl-config.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var fs = require('fs');
2+
3+
exports.getPrivateKey = () => {
4+
return fs.readFileSync(process.env.PATH_TO_PRIVATE_KEY).toString()
5+
}
6+
exports.getCertificate = () => {
7+
return fs.readFileSync(process.env.PATH_TO_CERT).toString()
8+
}

0 commit comments

Comments
 (0)