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;
|
2017-03-16 22:57:13 +08:00
|
|
|
var NetfluxSrv = require('./node_modules/chainpad-server/NetfluxWebsocketSrv');
|
2017-03-02 00:23:34 +08:00
|
|
|
var Package = require('./package.json');
|
2017-05-19 22:56:45 +08:00
|
|
|
var Path = require("path");
|
2018-01-26 22:24:07 +08:00
|
|
|
var nThen = require("nthen");
|
2014-10-31 23:42:58 +08:00
|
|
|
|
2019-04-17 19:48:39 +08:00
|
|
|
var config = require("./lib/load-config");
|
2019-04-01 20:39:32 +08:00
|
|
|
|
2016-10-07 04:37:25 +08:00
|
|
|
var websocketPort = config.websocketPort || config.httpPort;
|
2017-01-02 18:54:50 +08:00
|
|
|
var useSecureWebsockets = config.useSecureWebsockets || false;
|
2014-10-31 23:42:58 +08:00
|
|
|
|
2018-02-06 18:35:24 +08:00
|
|
|
// This is stuff which will become available to replify
|
|
|
|
const debuggableStore = new WeakMap();
|
|
|
|
const debuggable = function (name, x) {
|
|
|
|
if (name in debuggableStore) {
|
|
|
|
try { throw new Error(); } catch (e) {
|
|
|
|
console.error('cannot add ' + name + ' more than once [' + e.stack + ']');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debuggableStore[name] = x;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
};
|
|
|
|
debuggable('global', global);
|
|
|
|
debuggable('config', config);
|
|
|
|
|
2015-10-26 07:35:25 +08:00
|
|
|
// support multiple storage back ends
|
2016-10-07 04:37:25 +08:00
|
|
|
var Storage = require(config.storage||'./storage/file');
|
2015-10-26 07:35:25 +08:00
|
|
|
|
2018-02-06 18:35:24 +08:00
|
|
|
var app = debuggable('app', Express());
|
2016-10-07 04:37:25 +08:00
|
|
|
|
2016-10-07 04:44:58 +08:00
|
|
|
var httpsOpts;
|
|
|
|
|
2017-03-07 00:25:02 +08:00
|
|
|
var DEV_MODE = !!process.env.DEV
|
|
|
|
if (DEV_MODE) {
|
|
|
|
console.log("DEV MODE ENABLED");
|
|
|
|
}
|
|
|
|
|
2017-12-11 18:13:06 +08:00
|
|
|
var FRESH_MODE = !!process.env.FRESH;
|
|
|
|
var FRESH_KEY = '';
|
|
|
|
if (FRESH_MODE) {
|
|
|
|
console.log("FRESH MODE ENABLED");
|
|
|
|
FRESH_KEY = +new Date();
|
|
|
|
}
|
2019-03-28 00:00:28 +08:00
|
|
|
config.flushCache = function () {
|
|
|
|
FRESH_KEY = +new Date();
|
|
|
|
};
|
|
|
|
|
2017-12-11 18:13:06 +08:00
|
|
|
|
2017-03-02 00:23:34 +08:00
|
|
|
const clone = (x) => (JSON.parse(JSON.stringify(x)));
|
|
|
|
|
2016-10-18 17:48:29 +08:00
|
|
|
var setHeaders = (function () {
|
|
|
|
if (typeof(config.httpHeaders) !== 'object') { return function () {}; }
|
2016-10-07 04:37:25 +08:00
|
|
|
|
2017-03-02 00:23:34 +08:00
|
|
|
const headers = clone(config.httpHeaders);
|
2017-08-14 22:23:56 +08:00
|
|
|
if (config.contentSecurity) {
|
2017-03-02 00:23:34 +08:00
|
|
|
headers['Content-Security-Policy'] = clone(config.contentSecurity);
|
2017-06-15 20:45:01 +08:00
|
|
|
if (!/;$/.test(headers['Content-Security-Policy'])) { headers['Content-Security-Policy'] += ';' }
|
2017-06-07 15:51:10 +08:00
|
|
|
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.
|
2017-08-17 21:41:04 +08:00
|
|
|
// It also fixes the cross-domain iframe.
|
|
|
|
headers['Content-Security-Policy'] += "frame-ancestors *;";
|
2017-06-07 15:51:10 +08:00
|
|
|
}
|
2017-03-02 00:23:34 +08:00
|
|
|
}
|
|
|
|
const padHeaders = clone(headers);
|
|
|
|
if (config.padContentSecurity) {
|
|
|
|
padHeaders['Content-Security-Policy'] = clone(config.padContentSecurity);
|
|
|
|
}
|
2016-10-18 17:48:29 +08:00
|
|
|
if (Object.keys(headers).length) {
|
2017-03-02 00:23:34 +08:00
|
|
|
return function (req, res) {
|
2019-03-01 00:08:19 +08:00
|
|
|
const h = [
|
|
|
|
/^\/pad(2)?\/inner\.html.*/,
|
2019-01-28 19:18:18 +08:00
|
|
|
/^\/sheet\/inner\.html.*/,
|
|
|
|
/^\/common\/onlyoffice\/.*\/index\.html.*/
|
|
|
|
].some((regex) => {
|
2019-03-01 00:08:19 +08:00
|
|
|
return regex.test(req.url)
|
|
|
|
}) ? padHeaders : headers;
|
2017-03-02 00:23:34 +08:00
|
|
|
for (let header in h) { res.setHeader(header, h[header]); }
|
2016-10-18 17:48:29 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return function () {};
|
|
|
|
}());
|
2016-10-07 05:02:30 +08:00
|
|
|
|
2017-04-18 21:51:42 +08:00
|
|
|
(function () {
|
|
|
|
if (!config.logFeedback) { return; }
|
|
|
|
|
|
|
|
const logFeedback = function (url) {
|
|
|
|
url.replace(/\?(.*?)=/, function (all, fb) {
|
2019-04-09 00:11:36 +08:00
|
|
|
config.log.feedback(fb, '');
|
2017-04-18 21:51:42 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
app.head(/^\/common\/feedback\.html/, function (req, res, next) {
|
|
|
|
logFeedback(req.url);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}());
|
|
|
|
|
2016-10-18 17:48:29 +08:00
|
|
|
app.use(function (req, res, next) {
|
2017-03-02 00:23:34 +08:00
|
|
|
setHeaders(req, res);
|
|
|
|
if (/[\?\&]ver=[^\/]+$/.test(req.url)) { res.setHeader("Cache-Control", "max-age=31536000"); }
|
2016-10-07 04:37:25 +08:00
|
|
|
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) {
|
2016-07-12 18:17:03 +08:00
|
|
|
if (e) { return; }
|
2015-01-31 01:12:20 +08:00
|
|
|
console.log("Cryptpad is customizable, see customize.dist/readme.md for details");
|
2016-07-12 18:17:03 +08:00
|
|
|
});
|
|
|
|
|
2016-10-18 17:48:29 +08:00
|
|
|
// FIXME I think this is a regression caused by a recent PR
|
|
|
|
// correct this hack without breaking the contributor's intended behaviour.
|
2016-12-29 00:11:06 +08:00
|
|
|
|
2017-01-17 01:28:37 +08:00
|
|
|
var mainPages = config.mainPages || ['index', 'privacy', 'terms', 'about', 'contact'];
|
2016-12-29 00:11:06 +08:00
|
|
|
var mainPagePattern = new RegExp('^\/(' + mainPages.join('|') + ').html$');
|
2018-02-09 23:17:40 +08:00
|
|
|
app.get(mainPagePattern, Express.static(__dirname + '/customize'));
|
2016-12-29 00:11:06 +08:00
|
|
|
app.get(mainPagePattern, Express.static(__dirname + '/customize.dist'));
|
2016-10-18 17:48:29 +08:00
|
|
|
|
2017-06-19 17:29:13 +08:00
|
|
|
app.use("/blob", Express.static(Path.join(__dirname, (config.blobPath || './blob')), {
|
|
|
|
maxAge: DEV_MODE? "0d": "365d"
|
|
|
|
}));
|
2018-04-13 23:49:17 +08:00
|
|
|
app.use("/datastore", Express.static(Path.join(__dirname, (config.filePath || './datastore')), {
|
|
|
|
maxAge: "0d"
|
|
|
|
}));
|
2018-06-19 17:31:15 +08:00
|
|
|
app.use("/block", Express.static(Path.join(__dirname, (config.blockPath || '/block')), {
|
|
|
|
maxAge: "0d",
|
|
|
|
}));
|
2017-04-25 23:19:13 +08:00
|
|
|
|
2016-07-12 18:44:44 +08:00
|
|
|
app.use("/customize", Express.static(__dirname + '/customize'));
|
2016-07-12 18:17:03 +08:00
|
|
|
app.use("/customize", Express.static(__dirname + '/customize.dist'));
|
2017-12-06 00:48:30 +08:00
|
|
|
app.use("/customize.dist", Express.static(__dirname + '/customize.dist'));
|
2016-09-27 17:53:52 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-03-29 00:17:40 +08:00
|
|
|
var admins = [];
|
|
|
|
try {
|
|
|
|
admins = (config.adminKeys || []).map(function (k) {
|
|
|
|
k = k.replace(/\/+$/, '');
|
|
|
|
var s = k.split('/');
|
|
|
|
return s[s.length-1].replace(/-/g, '/');
|
|
|
|
});
|
|
|
|
} catch (e) { console.error("Can't parse admin keys"); }
|
|
|
|
|
2019-04-09 00:39:45 +08:00
|
|
|
// TODO, cache this /api/config responses instead of re-computing it each time
|
2016-09-27 18:17:38 +08:00
|
|
|
app.get('/api/config', function(req, res){
|
2019-03-29 00:17:40 +08:00
|
|
|
// TODO precompute any data that isn't dynamic to save some CPU time
|
2014-11-03 18:13:41 +08:00
|
|
|
var host = req.headers.host.replace(/\:[0-9]+/, '');
|
|
|
|
res.setHeader('Content-Type', 'text/javascript');
|
2017-08-17 21:41:04 +08:00
|
|
|
res.send('define(function(){\n' + [
|
|
|
|
'var obj = ' + JSON.stringify({
|
|
|
|
requireConf: {
|
2018-07-02 20:02:05 +08:00
|
|
|
waitSeconds: 600,
|
2017-12-11 18:13:06 +08:00
|
|
|
urlArgs: 'ver=' + Package.version + (FRESH_KEY? '-' + FRESH_KEY: '') + (DEV_MODE? '-' + (+new Date()): ''),
|
2017-08-17 21:41:04 +08:00
|
|
|
},
|
|
|
|
removeDonateButton: (config.removeDonateButton === true),
|
|
|
|
allowSubscriptions: (config.allowSubscriptions === true),
|
|
|
|
websocketPath: config.useExternalWebsocket ? undefined : config.websocketPath,
|
2019-04-09 00:39:45 +08:00
|
|
|
// FIXME don't send websocketURL if websocketPath is provided. deprecated.
|
2017-08-17 21:41:04 +08:00
|
|
|
websocketURL:'ws' + ((useSecureWebsockets) ? 's' : '') + '://' + host + ':' +
|
|
|
|
websocketPort + '/cryptpad_websocket',
|
2018-01-23 23:41:54 +08:00
|
|
|
httpUnsafeOrigin: config.httpUnsafeOrigin,
|
2019-03-29 00:18:04 +08:00
|
|
|
adminEmail: config.adminEmail,
|
2019-03-28 00:00:28 +08:00
|
|
|
adminKeys: admins,
|
2019-06-24 18:15:34 +08:00
|
|
|
supportMailbox: config.supportMailboxPublicKey
|
2017-08-17 21:41:04 +08:00
|
|
|
}, null, '\t'),
|
|
|
|
'obj.httpSafeOrigin = ' + (function () {
|
2018-10-17 19:48:31 +08:00
|
|
|
if (config.httpSafeOrigin) { return '"' + config.httpSafeOrigin + '"'; }
|
2017-08-17 21:41:04 +08:00
|
|
|
if (config.httpSafePort) {
|
|
|
|
return "(function () { return window.location.origin.replace(/\:[0-9]+$/, ':" +
|
|
|
|
config.httpSafePort + "'); }())";
|
|
|
|
}
|
|
|
|
return 'window.location.origin';
|
|
|
|
}()),
|
|
|
|
'return obj',
|
|
|
|
'});'
|
|
|
|
].join(';\n'));
|
2014-11-03 18:13:41 +08:00
|
|
|
});
|
|
|
|
|
2017-12-01 02:19:21 +08:00
|
|
|
var four04_path = Path.resolve(__dirname + '/customize.dist/404.html');
|
|
|
|
var custom_four04_path = Path.resolve(__dirname + '/customize/404.html');
|
|
|
|
|
|
|
|
var send404 = function (res, path) {
|
|
|
|
if (!path && path !== four04_path) { path = four04_path; }
|
|
|
|
Fs.exists(path, function (exists) {
|
2018-04-12 23:24:51 +08:00
|
|
|
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
2017-12-01 02:19:21 +08:00
|
|
|
if (exists) { return Fs.createReadStream(path).pipe(res); }
|
|
|
|
send404(res);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
app.use(function (req, res, next) {
|
|
|
|
res.status(404);
|
|
|
|
send404(res, custom_four04_path);
|
|
|
|
});
|
|
|
|
|
2014-12-04 17:53:47 +08:00
|
|
|
var httpServer = httpsOpts ? Https.createServer(httpsOpts, app) : Http.createServer(app);
|
|
|
|
|
2019-01-21 17:43:37 +08:00
|
|
|
httpServer.listen(config.httpPort,config.httpAddress,function(){
|
|
|
|
var host = config.httpAddress;
|
|
|
|
var hostName = !host.indexOf(':') ? '[' + host + ']' : host;
|
2017-06-14 16:27:29 +08:00
|
|
|
|
2019-01-21 17:43:37 +08:00
|
|
|
var port = config.httpPort;
|
|
|
|
var ps = port === 80? '': ':' + port;
|
2017-06-14 16:27:29 +08:00
|
|
|
|
2019-04-09 17:40:51 +08:00
|
|
|
console.log('[%s] server available http://%s%s', new Date().toISOString(), hostName, ps);
|
2019-01-21 17:43:37 +08:00
|
|
|
});
|
2017-08-17 21:41:04 +08:00
|
|
|
if (config.httpSafePort) {
|
2019-01-21 17:43:37 +08:00
|
|
|
Http.createServer(app).listen(config.httpSafePort, config.httpAddress);
|
2017-08-17 21:41:04 +08:00
|
|
|
}
|
2014-10-31 23:42:58 +08:00
|
|
|
|
2016-03-16 23:34:27 +08:00
|
|
|
var wsConfig = { server: httpServer };
|
2017-01-27 23:45:41 +08:00
|
|
|
|
2018-01-26 22:24:07 +08:00
|
|
|
var rpc;
|
2019-03-30 00:03:53 +08:00
|
|
|
var historyKeeper;
|
2017-03-11 01:03:15 +08:00
|
|
|
|
2019-04-09 17:40:51 +08:00
|
|
|
var log;
|
|
|
|
|
2019-08-21 19:22:43 +08:00
|
|
|
// Initialize logging, the the store, then tasks, then rpc, then history keeper and then start the server
|
2018-01-26 22:24:07 +08:00
|
|
|
var nt = nThen(function (w) {
|
2019-04-09 00:11:36 +08:00
|
|
|
// set up logger
|
|
|
|
var Logger = require("./lib/log");
|
2019-04-09 17:40:51 +08:00
|
|
|
//console.log("Loading logging module");
|
2019-04-09 00:11:36 +08:00
|
|
|
Logger.create(config, w(function (_log) {
|
2019-04-09 17:40:51 +08:00
|
|
|
log = config.log = _log;
|
2019-04-09 00:11:36 +08:00
|
|
|
}));
|
2019-03-30 00:03:53 +08:00
|
|
|
}).nThen(function (w) {
|
|
|
|
if (config.useExternalWebsocket) { return; }
|
|
|
|
Storage.create(config, w(function (_store) {
|
|
|
|
config.store = _store;
|
|
|
|
}));
|
2019-04-23 19:28:05 +08:00
|
|
|
}).nThen(function (w) {
|
|
|
|
var Tasks = require("./storage/tasks");
|
|
|
|
Tasks.create(config, w(function (e, tasks) {
|
|
|
|
if (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
config.tasks = tasks;
|
2019-08-21 19:36:27 +08:00
|
|
|
if (config.disableIntegratedTasks) { return; }
|
2019-04-23 19:28:05 +08:00
|
|
|
setInterval(function () {
|
|
|
|
tasks.runAll(function (err) {
|
|
|
|
if (err) {
|
|
|
|
// either TASK_CONCURRENCY or an error with tasks.list
|
|
|
|
// in either case it is already logged.
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 1000 * 60 * 5); // run every five minutes
|
|
|
|
}));
|
2018-01-26 22:24:07 +08:00
|
|
|
}).nThen(function (w) {
|
2017-03-11 01:03:15 +08:00
|
|
|
config.rpc = typeof(config.rpc) === 'undefined'? './rpc.js' : config.rpc;
|
2018-01-26 22:24:07 +08:00
|
|
|
if (typeof(config.rpc) !== 'string') { return; }
|
|
|
|
// load pin store...
|
|
|
|
var Rpc = require(config.rpc);
|
2018-02-06 18:41:57 +08:00
|
|
|
Rpc.create(config, debuggable, w(function (e, _rpc) {
|
2018-01-26 22:24:07 +08:00
|
|
|
if (e) {
|
|
|
|
w.abort();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
rpc = _rpc;
|
|
|
|
}));
|
|
|
|
}).nThen(function () {
|
2019-03-30 00:03:53 +08:00
|
|
|
if (config.useExternalWebsocket) { return; }
|
|
|
|
var HK = require('./historyKeeper.js');
|
|
|
|
var hkConfig = {
|
|
|
|
tasks: config.tasks,
|
|
|
|
rpc: rpc,
|
2019-04-17 21:28:29 +08:00
|
|
|
store: config.store,
|
|
|
|
log: log
|
2019-03-30 00:03:53 +08:00
|
|
|
};
|
|
|
|
historyKeeper = HK.create(hkConfig);
|
|
|
|
}).nThen(function () {
|
|
|
|
if (config.useExternalWebsocket) { return; }
|
2018-01-26 22:24:07 +08:00
|
|
|
if (websocketPort !== config.httpPort) {
|
2019-04-09 17:40:51 +08:00
|
|
|
log.debug("setting up a new websocket server");
|
2018-01-26 22:24:07 +08:00
|
|
|
wsConfig = { port: websocketPort};
|
2017-03-11 01:03:15 +08:00
|
|
|
}
|
2018-01-26 22:24:07 +08:00
|
|
|
var wsSrv = new WebSocketServer(wsConfig);
|
2019-03-30 00:03:53 +08:00
|
|
|
NetfluxSrv.run(wsSrv, config, historyKeeper);
|
2018-01-26 22:24:07 +08:00
|
|
|
});
|
2017-03-11 01:03:15 +08:00
|
|
|
|
2018-02-06 18:35:24 +08:00
|
|
|
if (config.debugReplName) {
|
|
|
|
require('replify')({ name: config.debugReplName, app: debuggableStore });
|
2018-02-19 19:11:06 +08:00
|
|
|
}
|