cryptpad/server.js

108 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-02-15 15:47:53 +00:00
/*
globals require console
*/
2014-10-31 15:42:58 +00:00
var Express = require('express');
var Http = require('http');
2014-12-04 09:53:47 +00:00
var Https = require('https');
var Fs = require('fs');
2014-10-31 15:42:58 +00:00
var WebSocketServer = require('ws').Server;
var NetfluxSrv = require('./NetfluxWebsocketSrv');
2016-03-08 10:45:03 +00:00
var WebRTCSrv = require('./WebRTCSrv');
2014-10-31 15:42:58 +00:00
var config = require('./config');
var websocketPort = config.websocketPort || config.httpPort;
var useSecureWebsockets = config.useSecureWebsockets || false;
2014-10-31 15:42:58 +00:00
// support multiple storage back ends
var Storage = require(config.storage||'./storage/file');
2014-10-31 15:42:58 +00:00
var app = Express();
var httpsOpts;
var setHeaders = (function () {
if (typeof(config.httpHeaders) !== 'object') { return function () {}; }
var headers = JSON.parse(JSON.stringify(config.httpHeaders));
if (Object.keys(headers).length) {
return function (res) {
2016-10-18 09:51:35 +00:00
for (var header in headers) { res.setHeader(header, headers[header]); }
};
}
return function () {};
}());
app.use(function (req, res, next) {
setHeaders(res);
next();
});
2014-10-31 15:42:58 +00:00
app.use(Express.static(__dirname + '/www'));
2016-09-27 10:17:38 +00:00
Fs.exists(__dirname + "/customize", function (e) {
if (e) { return; }
2015-01-30 17:12:20 +00:00
console.log("Cryptpad is customizable, see customize.dist/readme.md for details");
});
// FIXME I think this is a regression caused by a recent PR
// correct this hack without breaking the contributor's intended behaviour.
var mainPages = config.mainPages || ['index', 'privacy', 'terms', 'about'];
var mainPagePattern = new RegExp('^\/(' + mainPages.join('|') + ').html$');
app.get(mainPagePattern, Express.static(__dirname + '/customize.dist'));
app.use("/customize", Express.static(__dirname + '/customize'));
app.use("/customize", Express.static(__dirname + '/customize.dist'));
app.use(/^\/[^\/]*$/, Express.static('customize'));
app.use(/^\/[^\/]*$/, Express.static('customize.dist'));
2015-01-30 17:12:20 +00:00
2014-12-04 09:53:47 +00:00
if (config.privKeyAndCertFiles) {
var privKeyAndCerts = '';
2016-09-27 10:17:38 +00:00
config.privKeyAndCertFiles.forEach(function (file) {
2014-12-04 09:53:47 +00:00
privKeyAndCerts = privKeyAndCerts + Fs.readFileSync(file);
});
var array = privKeyAndCerts.split('\n-----BEGIN ');
2016-02-12 10:39:37 +00:00
for (var i = 1; i < array.length; i++) { array[i] = '-----BEGIN ' + array[i]; }
2014-12-04 09:53:47 +00:00
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
2016-02-12 10:39:37 +00:00
};
2014-12-04 09:53:47 +00:00
}
2016-09-27 10:17:38 +00:00
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({
websocketPath: config.websocketPath,
websocketURL:'ws' + ((useSecureWebsockets) ? 's' : '') + '://' + host + ':' +
websocketPort + '/cryptpad_websocket',
}) + ');');
});
2014-12-04 09:53:47 +00:00
var httpServer = httpsOpts ? Https.createServer(httpsOpts, app) : Http.createServer(app);
2016-09-27 10:17:38 +00:00
httpServer.listen(config.httpPort,config.httpAddress,function(){
2016-09-30 13:51:23 +00:00
console.log('[%s] listening on port %s', new Date().toISOString(), config.httpPort);
});
2014-10-31 15:42:58 +00:00
var wsConfig = { server: httpServer };
if (websocketPort !== config.httpPort) {
console.log("setting up a new websocket server");
wsConfig = { port: websocketPort};
}
var wsSrv = new WebSocketServer(wsConfig);
2016-09-27 10:17:38 +00:00
Storage.create(config, function (store) {
2016-05-14 10:52:34 +00:00
NetfluxSrv.run(store, wsSrv, config);
WebRTCSrv.run(wsSrv);
2016-09-30 13:51:23 +00:00
});