2023-10-20 22:35:26 +08:00
|
|
|
// SPDX-FileCopyrightText: 2023 XWiki CryptPad Team <contact@cryptpad.org> and contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2016-02-15 23:47:53 +08:00
|
|
|
/*
|
2022-10-06 18:04:58 +08:00
|
|
|
globals process
|
2016-02-15 23:47:53 +08:00
|
|
|
*/
|
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 Fs = require('fs');
|
2018-01-26 22:24:07 +08:00
|
|
|
var nThen = require("nthen");
|
2020-02-28 02:48:19 +08:00
|
|
|
var Util = require("./lib/common-util");
|
2022-12-20 17:19:47 +08:00
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
var OS = require("node:os");
|
|
|
|
var Cluster = require("node:cluster");
|
2014-10-31 23:42:58 +08:00
|
|
|
|
2019-04-17 19:48:39 +08:00
|
|
|
var config = require("./lib/load-config");
|
2022-12-20 16:50:59 +08:00
|
|
|
var Environment = require("./lib/env");
|
|
|
|
var Env = Environment.create(config);
|
|
|
|
var Default = require("./lib/defaults");
|
2019-04-01 20:39:32 +08:00
|
|
|
|
2019-12-24 06:15:07 +08:00
|
|
|
var app = Express();
|
2016-10-07 04:44:58 +08:00
|
|
|
|
2020-02-29 01:01:52 +08:00
|
|
|
(function () {
|
2021-06-15 06:22:12 +08:00
|
|
|
// you absolutely must provide an 'httpUnsafeOrigin' (a truthy string)
|
2022-03-14 19:39:22 +08:00
|
|
|
if (typeof(Env.httpUnsafeOrigin) !== 'string' || !Env.httpUnsafeOrigin.trim()) {
|
2020-02-29 01:01:52 +08:00
|
|
|
throw new Error("No 'httpUnsafeOrigin' provided");
|
|
|
|
}
|
|
|
|
}());
|
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
var COMMANDS = {};
|
2022-02-18 16:24:33 +08:00
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
COMMANDS.LOG = function (msg, cb) {
|
|
|
|
var level = msg.level;
|
|
|
|
Env.Log[level](msg.tag, msg.info);
|
|
|
|
cb();
|
2022-03-14 19:39:22 +08:00
|
|
|
};
|
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
COMMANDS.UPDATE_QUOTA = function (msg, cb) {
|
|
|
|
var Quota = require("./lib/commands/quota");
|
2022-12-20 17:19:47 +08:00
|
|
|
Quota.updateCachedLimits(Env, (err) => {
|
|
|
|
if (err) {
|
|
|
|
Env.Log.warn('UPDATE_QUOTA_ERR', err);
|
2022-12-20 16:50:59 +08:00
|
|
|
return void cb(err);
|
|
|
|
}
|
|
|
|
Env.Log.info('QUOTA_UPDATED', {});
|
|
|
|
cb();
|
|
|
|
});
|
2022-02-18 16:24:33 +08:00
|
|
|
};
|
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
COMMANDS.GET_PROFILING_DATA = function (msg, cb) {
|
|
|
|
cb(void 0, Env.bytesWritten);
|
2022-02-18 16:24:33 +08:00
|
|
|
};
|
2016-10-07 05:02:30 +08:00
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
nThen(function (w) {
|
|
|
|
require("./lib/log").create(config, w(function (_log) {
|
|
|
|
Env.Log = _log;
|
|
|
|
config.log = _log;
|
|
|
|
}));
|
2022-12-20 18:59:38 +08:00
|
|
|
}).nThen(function (w) {
|
|
|
|
Fs.exists("customize", w(function (e) {
|
|
|
|
if (e) { return; }
|
|
|
|
Env.Log.info('NO_CUSTOMIZE_FOLDER', {
|
|
|
|
message: "CryptPad is customizable, see customize.dist/readme.md for details",
|
|
|
|
});
|
|
|
|
}));
|
2023-05-06 17:56:21 +08:00
|
|
|
}).nThen(function (w) {
|
2022-12-20 18:59:38 +08:00
|
|
|
// check that a valid origin was provided in the config
|
|
|
|
try {
|
|
|
|
var url = new URL('', Env.httpUnsafeOrigin).href;
|
|
|
|
Env.Log.info("WEBSERVER_LISTENING", {
|
|
|
|
origin: url,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!Env.admins.length) {
|
|
|
|
Env.Log.info('NO_ADMIN_CONFIGURED', {
|
|
|
|
message: `Your instance is not correctly configured for production usage. Review its checkup page for more information.`,
|
|
|
|
details: new URL('/checkup/', Env.httpUnsafeOrigin).href,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Env.Log.error("INVALID_ORIGIN", {
|
|
|
|
httpUnsafeOrigin: Env.httpUnsafeOrigin,
|
|
|
|
});
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2022-12-20 16:50:59 +08:00
|
|
|
Env.httpServer = Http.createServer(app);
|
2023-08-18 16:40:35 +08:00
|
|
|
Env.httpServer.listen(Env.websocketPort, Env.httpAddress, w(function () {
|
2022-12-20 18:59:38 +08:00
|
|
|
Env.Log.info('WEBSOCKET_LISTENING', {
|
|
|
|
port: Env.websocketPort,
|
|
|
|
});
|
2022-12-20 16:50:59 +08:00
|
|
|
}));
|
|
|
|
}).nThen(function (w) {
|
|
|
|
var limit = Env.maxWorkers;
|
|
|
|
var workerState = {
|
|
|
|
Env: Environment.serialize(Env),
|
|
|
|
};
|
2017-04-18 21:51:42 +08:00
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
Cluster.setupPrimary({
|
|
|
|
exec: './lib/http-worker.js',
|
|
|
|
args: [],
|
2017-04-18 21:51:42 +08:00
|
|
|
});
|
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
var launchWorker = (online) => {
|
|
|
|
var worker = Cluster.fork(workerState);
|
|
|
|
worker.on('online', () => {
|
|
|
|
online();
|
|
|
|
});
|
|
|
|
|
|
|
|
worker.on('message', msg => {
|
|
|
|
if (!msg) { return; }
|
|
|
|
var txid = msg.txid;
|
2023-03-07 13:47:46 +08:00
|
|
|
var content = msg.content;
|
|
|
|
if (!content) { return; }
|
2022-12-20 16:50:59 +08:00
|
|
|
|
|
|
|
var command = COMMANDS[content.command];
|
|
|
|
if (typeof(command) !== 'function') {
|
|
|
|
return void Env.Log.error('UNHANDLED_HTTP_WORKER_COMMAND', msg);
|
2020-11-24 23:38:31 +08:00
|
|
|
}
|
2016-10-18 17:48:29 +08:00
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
const cb = Util.once(Util.mkAsync(function (err, value) {
|
|
|
|
worker.send({
|
|
|
|
type: 'REPLY',
|
|
|
|
error: Util.serializeError(err),
|
|
|
|
txid: txid,
|
|
|
|
pid: msg.pid,
|
2022-12-20 19:40:10 +08:00
|
|
|
value: value,
|
2022-12-20 16:50:59 +08:00
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
command(content, cb);
|
|
|
|
});
|
2022-12-20 18:59:38 +08:00
|
|
|
|
|
|
|
worker.on('exit', (code, signal) => {
|
|
|
|
if (!signal && code === 0) { return; }
|
|
|
|
// relaunch http workers if they crash
|
|
|
|
Env.Log.error('HTTP_WORKER_EXIT', {
|
|
|
|
signal,
|
|
|
|
code,
|
|
|
|
});
|
|
|
|
// update the environment with the latest state before relaunching
|
|
|
|
workerState.Env = Environment.serialize(Env);
|
|
|
|
launchWorker(function () {
|
|
|
|
Env.Log.info('HTTP_WORKER_RELAUNCH', {});
|
|
|
|
});
|
|
|
|
});
|
2022-12-20 16:50:59 +08:00
|
|
|
};
|
2020-02-28 02:48:19 +08:00
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
var txids = {};
|
2020-02-28 02:48:19 +08:00
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
var sendCommand = (worker, command, data /*, cb */) => {
|
|
|
|
worker.send({
|
|
|
|
type: 'EVENT',
|
|
|
|
txid: Util.guid(txids),
|
|
|
|
command: command,
|
|
|
|
data: data,
|
|
|
|
});
|
|
|
|
};
|
2020-10-09 15:58:13 +08:00
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
var broadcast = (command, data, cb) => {
|
2023-07-13 20:12:47 +08:00
|
|
|
cb = cb; // TODO nThen/concurrency
|
2022-12-20 16:50:59 +08:00
|
|
|
for (const worker of Object.values(Cluster.workers)) {
|
|
|
|
sendCommand(worker, command, data /*, cb */);
|
2020-02-28 02:48:19 +08:00
|
|
|
}
|
2021-03-12 19:46:11 +08:00
|
|
|
};
|
2021-04-12 15:49:11 +08:00
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
var throttledEnvChange = Util.throttle(function () {
|
2022-12-20 18:59:38 +08:00
|
|
|
Env.Log.info('WORKER_ENV_UPDATE', 'Updating HTTP workers with latest state');
|
|
|
|
broadcast('ENV_UPDATE', Environment.serialize(Env));
|
2022-12-20 16:50:59 +08:00
|
|
|
}, 250);
|
2021-03-12 19:46:11 +08:00
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
var throttledCacheFlush = Util.throttle(function () {
|
2022-12-20 18:59:38 +08:00
|
|
|
Env.Log.info('WORKER_CACHE_FLUSH', 'Instructing HTTP workers to flush cache');
|
2022-12-20 16:50:59 +08:00
|
|
|
broadcast('FLUSH_CACHE', Env.FRESH_KEY);
|
|
|
|
}, 250);
|
2014-11-03 18:13:41 +08:00
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
Env.envUpdated.reg(throttledEnvChange);
|
|
|
|
Env.cacheFlushed.reg(throttledCacheFlush);
|
2022-05-03 20:50:18 +08:00
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
OS.cpus().forEach((cpu, index) => {
|
|
|
|
if (limit && index >= limit) {
|
|
|
|
return;
|
2022-07-11 16:17:53 +08:00
|
|
|
}
|
2022-12-20 16:50:59 +08:00
|
|
|
launchWorker(w());
|
2022-03-22 19:08:42 +08:00
|
|
|
});
|
2018-01-26 22:24:07 +08:00
|
|
|
}).nThen(function () {
|
2022-12-20 16:50:59 +08:00
|
|
|
if (Env.shouldUpdateNode) {
|
|
|
|
Env.Log.warn("NODEJS_OLD_VERSION", {
|
|
|
|
message: `The CryptPad development team recommends using at least NodeJS v${Default.recommendedVersion.join('.')}`,
|
|
|
|
currentVersion: process.version,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (Env.OFFLINE_MODE) { return; }
|
2023-07-13 20:12:47 +08:00
|
|
|
if (Env.websocketPath) { return; }
|
2020-10-12 20:09:53 +08:00
|
|
|
|
2022-12-20 16:50:59 +08:00
|
|
|
require("./lib/api").create(Env);
|
2018-01-26 22:24:07 +08:00
|
|
|
});
|
2020-01-28 06:57:39 +08:00
|
|
|
|