added ability to use https

This commit is contained in:
Caleb James DeLisle 2014-12-04 10:53:47 +01:00
parent f696cfc954
commit 6687a1d5dc
3 changed files with 37 additions and 3 deletions

View File

@ -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'
//],
};

View File

@ -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",

View File

@ -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);