cryptpad/server.js

184 lines
6.0 KiB
JavaScript
Raw Normal View History

2016-02-15 23:47:53 +08:00
/*
globals require console
*/
2014-10-31 23:42:58 +08:00
var Express = require('express');
var Http = require('http');
2014-12-04 17:53:47 +08:00
var Https = require('https');
var Fs = require('fs');
2014-10-31 23:42:58 +08:00
var WebSocketServer = require('ws').Server;
var NetfluxSrv = require('./node_modules/chainpad-server/NetfluxWebsocketSrv');
var Package = require('./package.json');
2017-05-19 22:56:45 +08:00
var Path = require("path");
2014-10-31 23:42:58 +08:00
var config = require('./config');
var websocketPort = config.websocketPort || config.httpPort;
var useSecureWebsockets = config.useSecureWebsockets || false;
2014-10-31 23:42:58 +08:00
// support multiple storage back ends
var Storage = require(config.storage||'./storage/file');
2014-10-31 23:42:58 +08:00
var app = Express();
var httpsOpts;
var DEV_MODE = !!process.env.DEV
if (DEV_MODE) {
console.log("DEV MODE ENABLED");
}
const clone = (x) => (JSON.parse(JSON.stringify(x)));
var setHeaders = (function () {
if (typeof(config.httpHeaders) !== 'object') { return function () {}; }
const headers = clone(config.httpHeaders);
2017-07-22 22:43:53 +08:00
if (config.contentSecurity && false) {
headers['Content-Security-Policy'] = clone(config.contentSecurity);
if (!/;$/.test(headers['Content-Security-Policy'])) { headers['Content-Security-Policy'] += ';' }
if (headers['Content-Security-Policy'].indexOf('frame-ancestors') === -1) {
// backward compat for those who do not merge the new version of the config
// when updating. This prevents endless spinner if someone clicks donate.
headers['Content-Security-Policy'] += "frame-ancestors 'self' accounts.cryptpad.fr;";
}
}
const padHeaders = clone(headers);
if (config.padContentSecurity) {
padHeaders['Content-Security-Policy'] = clone(config.padContentSecurity);
}
if (Object.keys(headers).length) {
return function (req, res) {
const h = /^\/pad\/inner\.html.*/.test(req.url) ? padHeaders : headers;
for (let header in h) { res.setHeader(header, h[header]); }
};
}
return function () {};
}());
(function () {
if (!config.logFeedback) { return; }
const logFeedback = function (url) {
url.replace(/\?(.*?)=/, function (all, fb) {
console.log('[FEEDBACK] %s', fb);
});
};
app.head(/^\/common\/feedback\.html/, function (req, res, next) {
logFeedback(req.url);
next();
});
}());
app.use(function (req, res, next) {
setHeaders(req, res);
if (/[\?\&]ver=[^\/]+$/.test(req.url)) { res.setHeader("Cache-Control", "max-age=31536000"); }
next();
});
2014-10-31 23:42:58 +08:00
app.use(Express.static(__dirname + '/www'));
2016-09-27 18:17:38 +08:00
Fs.exists(__dirname + "/customize", function (e) {
if (e) { return; }
2015-01-31 01:12:20 +08: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.
2017-01-17 01:28:37 +08:00
var mainPages = config.mainPages || ['index', 'privacy', 'terms', 'about', 'contact'];
var mainPagePattern = new RegExp('^\/(' + mainPages.join('|') + ').html$');
app.get(mainPagePattern, Express.static(__dirname + '/customize.dist'));
app.use("/blob", Express.static(Path.join(__dirname, (config.blobPath || './blob')), {
maxAge: DEV_MODE? "0d": "365d"
}));
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-31 01:12:20 +08:00
2014-12-04 17:53:47 +08:00
if (config.privKeyAndCertFiles) {
var privKeyAndCerts = '';
2016-09-27 18:17:38 +08:00
config.privKeyAndCertFiles.forEach(function (file) {
2014-12-04 17:53:47 +08:00
privKeyAndCerts = privKeyAndCerts + Fs.readFileSync(file);
});
var array = privKeyAndCerts.split('\n-----BEGIN ');
2016-02-12 18:39:37 +08:00
for (var i = 1; i < array.length; i++) { array[i] = '-----BEGIN ' + array[i]; }
2014-12-04 17:53:47 +08: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 18:39:37 +08:00
};
2014-12-04 17:53:47 +08:00
}
2016-09-27 18:17:38 +08: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({
requireConf: {
waitSeconds: 60,
urlArgs: 'ver=' + Package.version + (DEV_MODE? '-' + (+new Date()): ''),
},
removeDonateButton: (config.removeDonateButton === true),
allowSubscriptions: (config.allowSubscriptions === true),
websocketPath: config.useExternalWebsocket ? undefined : config.websocketPath,
websocketURL:'ws' + ((useSecureWebsockets) ? 's' : '') + '://' + host + ':' +
websocketPort + '/cryptpad_websocket',
}) + ');');
});
2014-12-04 17:53:47 +08:00
var httpServer = httpsOpts ? Https.createServer(httpsOpts, app) : Http.createServer(app);
2016-09-27 18:17:38 +08:00
httpServer.listen(config.httpPort,config.httpAddress,function(){
var host = config.httpAddress;
var hostName = !host.indexOf(':') ? '[' + host + ']' : host;
var port = config.httpPort;
var ps = port === 80? '': ':' + port;
console.log('\n[%s] server available http://%s%s', new Date().toISOString(), hostName, ps);
});
2014-10-31 23:42:58 +08:00
var wsConfig = { server: httpServer };
var createSocketServer = function (err, rpc) {
if(!config.useExternalWebsocket) {
if (websocketPort !== config.httpPort) {
console.log("setting up a new websocket server");
wsConfig = { port: websocketPort};
}
var wsSrv = new WebSocketServer(wsConfig);
Storage.create(config, function (store) {
NetfluxSrv.run(store, wsSrv, config, rpc);
});
}
};
var loadRPC = function (cb) {
config.rpc = typeof(config.rpc) === 'undefined'? './rpc.js' : config.rpc;
if (typeof(config.rpc) === 'string') {
// load pin store...
var Rpc = require(config.rpc);
Rpc.create(config, function (e, rpc) {
if (e) { throw e; }
cb(void 0, rpc);
});
} else {
cb();
}
};
loadRPC(createSocketServer);