added ability to use https
This commit is contained in:
32
server.js
32
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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user