From 6687a1d5dcc6f61f069c955daac3a9f11f96f7bb Mon Sep 17 00:00:00 2001 From: Caleb James DeLisle Date: Thu, 4 Dec 2014 10:53:47 +0100 Subject: [PATCH] added ability to use https --- config.js.dist | 7 ++++++- package.json | 1 + server.js | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/config.js.dist b/config.js.dist index 1447f4898..5cab8572a 100644 --- a/config.js.dist +++ b/config.js.dist @@ -3,5 +3,10 @@ module.exports = { websocketPort: 3001, mongoUri: "mongodb://demo_user:demo_password@ds027769.mongolab.com:27769/demo_database", // mongoUri: "mongodb://localhost:27017/cryptpad", - mongoCollectionName: 'cryptpad' + mongoCollectionName: 'cryptpad', + //privKeyAndCertFiles: [ + // '/etc/apache2/ssl/my_secret.key', + // '/etc/apache2/ssl/my_public_cert.crt', + // '/etc/apache2/ssl/my_certificate_authorities_cert_chain.ca' + //], }; diff --git a/package.json b/package.json index bb2f0893d..45dcc5b6e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "name": "cryptpad", "description": "realtime collaborative visual editor with zero knowlege server", + "version": "0.1.0", "dependencies": { "express": "~4.10.1", "ws": "~0.4.32", diff --git a/server.js b/server.js index 22a0cb872..bc5b41c0e 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,7 @@ var Express = require('express'); var Http = require('http'); +var Https = require('https'); +var Fs = require('fs'); var WebSocketServer = require('ws').Server; var ChainPadSrv = require('./ChainPadSrv'); var Storage = require('./Storage'); @@ -10,15 +12,41 @@ config.websocketPort = config.websocketPort || config.httpPort; var app = Express(); app.use(Express.static(__dirname + '/www')); +var httpsOpts; +if (config.privKeyAndCertFiles) { + var privKeyAndCerts = ''; + config.privKeyAndCertFiles.forEach(function (file) { + privKeyAndCerts = privKeyAndCerts + Fs.readFileSync(file); + }); + var array = privKeyAndCerts.split('\n-----BEGIN '); + for (var i = 1; i < array.length; i++) { array[i] = '-----BEGIN ' + array[i] } + var privKey; + for (var i = 0; i < array.length; i++) { + if (array[i].indexOf('PRIVATE KEY-----\n') !== -1) { + privKey = array[i]; + array.splice(i, 1); + break; + } + } + if (!privKey) { throw new Error("cannot find private key"); } + httpsOpts = { + cert: array.shift(), + key: privKey, + ca: array + } +} + app.get('/api/config', function(req, res){ var host = req.headers.host.replace(/\:[0-9]+/, ''); res.setHeader('Content-Type', 'text/javascript'); res.send('define(' + JSON.stringify({ - websocketURL:'ws://' + host + ':' + config.websocketPort + '/cryptpad_websocket' + websocketURL:'ws' + ((httpsOpts) ? 's' : '') + '://' + host + ':' + + config.websocketPort + '/cryptpad_websocket' }) + ');'); }); -var httpServer = Http.createServer(app); +var httpServer = httpsOpts ? Https.createServer(httpsOpts, app) : Http.createServer(app); + httpServer.listen(config.httpPort); console.log('listening on port ' + config.httpPort);