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
|
|
|
|
|
2020-01-28 06:57:39 +08:00
|
|
|
const WebSocketServer = require('ws').Server;
|
2020-02-03 23:03:43 +08:00
|
|
|
const NetfluxSrv = require('chainpad-server');
|
2020-10-12 20:09:53 +08:00
|
|
|
const Decrees = require("./decrees");
|
2020-01-28 06:57:39 +08:00
|
|
|
|
2020-10-12 20:09:53 +08:00
|
|
|
const nThen = require("nthen");
|
2022-03-08 20:50:11 +08:00
|
|
|
const Fs = require("fs");
|
2022-05-17 16:08:56 +08:00
|
|
|
const Fse = require("fs-extra");
|
2022-03-08 20:50:11 +08:00
|
|
|
const Path = require("path");
|
2023-05-03 18:49:01 +08:00
|
|
|
const Nacl = require("tweetnacl/nacl-fast");
|
2024-06-21 21:49:24 +08:00
|
|
|
const Hash = require('./common-hash');
|
2020-10-12 20:09:53 +08:00
|
|
|
|
|
|
|
module.exports.create = function (Env) {
|
|
|
|
var log = Env.Log;
|
|
|
|
|
|
|
|
nThen(function (w) {
|
|
|
|
Decrees.load(Env, w(function (err) {
|
2021-06-08 22:54:30 +08:00
|
|
|
Env.flushCache();
|
2020-10-12 21:45:48 +08:00
|
|
|
if (err) {
|
2020-10-12 20:09:53 +08:00
|
|
|
log.error('DECREES_LOADING', {
|
|
|
|
error: err.code || err,
|
|
|
|
message: err.message,
|
|
|
|
});
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}));
|
2024-06-21 21:49:24 +08:00
|
|
|
}).nThen(function (w) {
|
|
|
|
let admins = Env.admins || [];
|
|
|
|
|
|
|
|
// If we don't have any admin on this instance, print an onboarding link
|
|
|
|
if (Array.isArray(admins) && admins.length) { return; }
|
|
|
|
let token = Env.installToken;
|
|
|
|
let printLink = () => {
|
|
|
|
let url = `${Env.httpUnsafeOrigin}/install/#${token}`;
|
|
|
|
console.log('=============================');
|
|
|
|
console.log('Create your first admin account and customize your instance by visiting');
|
|
|
|
console.log(url);
|
|
|
|
console.log('=============================');
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// If we already have a token, print it
|
|
|
|
if (token) { return void printLink(); }
|
|
|
|
|
|
|
|
// Otherwise create a new token
|
|
|
|
let decreeName = Path.join(Env.paths.decree, 'decree.ndjson');
|
|
|
|
token = Hash.createChannelId() + Hash.createChannelId();
|
|
|
|
let decree = ["ADD_INSTALL_TOKEN",[token],"",+new Date()];
|
|
|
|
Fs.appendFile(decreeName, JSON.stringify(decree) + '\n', w(function (err) {
|
|
|
|
if (err) { console.log(err); return; }
|
2024-06-25 17:29:13 +08:00
|
|
|
Env.installToken = token;
|
|
|
|
Env.envUpdated.fire();
|
2024-06-21 21:49:24 +08:00
|
|
|
printLink();
|
|
|
|
}));
|
2024-06-24 20:32:54 +08:00
|
|
|
}).nThen(function () {
|
2024-06-21 21:49:24 +08:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
2023-05-03 18:49:01 +08:00
|
|
|
}).nThen(function (w) {
|
|
|
|
// we assume the server has generated a secret used to validate JWT tokens
|
|
|
|
if (typeof(Env.bearerSecret) === 'string') { return; }
|
|
|
|
// if one does not exist, then create one and remember it
|
|
|
|
// 256 bits
|
|
|
|
var bearerSecret = Nacl.util.encodeBase64(Nacl.randomBytes(32));
|
|
|
|
Env.Log.info("GENERATING_BEARER_SECRET", {});
|
|
|
|
Decrees.write(Env, [
|
|
|
|
'SET_BEARER_SECRET',
|
|
|
|
[bearerSecret],
|
|
|
|
'INTERNAL',
|
|
|
|
+new Date()
|
|
|
|
], w(function (err) {
|
|
|
|
if (err) { throw err; }
|
|
|
|
}));
|
2022-05-17 16:08:56 +08:00
|
|
|
}).nThen(function (w) {
|
|
|
|
Fse.mkdirp(Env.paths.block, w(function (err) {
|
2024-06-24 23:02:07 +08:00
|
|
|
if (err) {
|
2022-05-17 16:08:56 +08:00
|
|
|
log.error("BLOCK_FOLDER_CREATE_FAILED", err);
|
|
|
|
}
|
|
|
|
}));
|
2022-03-08 20:50:11 +08:00
|
|
|
}).nThen(function (w) {
|
|
|
|
var fullPath = Path.join(Env.paths.block, 'placeholder.txt');
|
2022-05-17 16:08:56 +08:00
|
|
|
Fs.writeFile(fullPath, 'PLACEHOLDER\n', w(function (err) {
|
|
|
|
if (err) {
|
|
|
|
log.error('BLOCK_PLACEHOLDER_CREATE_FAILED', err);
|
|
|
|
}
|
|
|
|
}));
|
2020-10-12 20:09:53 +08:00
|
|
|
}).nThen(function () {
|
2020-02-08 07:58:57 +08:00
|
|
|
// asynchronously create a historyKeeper and RPC together
|
2020-10-12 20:09:53 +08:00
|
|
|
require('./historyKeeper.js').create(Env, function (err, historyKeeper) {
|
2020-02-08 07:58:57 +08:00
|
|
|
if (err) { throw err; }
|
2020-01-28 06:57:39 +08:00
|
|
|
|
|
|
|
|
2020-04-29 04:57:52 +08:00
|
|
|
var noop = function () {};
|
|
|
|
|
|
|
|
var special_errors = {};
|
|
|
|
['EPIPE', 'ECONNRESET'].forEach(function (k) { special_errors[k] = noop; });
|
|
|
|
special_errors.NF_ENOENT = function (error, label, info) {
|
2020-05-07 01:50:48 +08:00
|
|
|
delete info.stack;
|
2020-04-29 04:57:52 +08:00
|
|
|
log.error(label, {
|
|
|
|
info: info,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-02-08 07:58:57 +08:00
|
|
|
// spawn ws server and attach netflux event handlers
|
2024-06-12 00:14:45 +08:00
|
|
|
let Server = NetfluxSrv.create(new WebSocketServer({ server: Env.httpServer}))
|
2020-02-08 07:58:57 +08:00
|
|
|
.on('channelClose', historyKeeper.channelClose)
|
|
|
|
.on('channelMessage', historyKeeper.channelMessage)
|
|
|
|
.on('channelOpen', historyKeeper.channelOpen)
|
2020-02-20 03:22:12 +08:00
|
|
|
.on('sessionClose', historyKeeper.sessionClose)
|
2024-02-20 01:15:05 +08:00
|
|
|
.on('sessionOpen', historyKeeper.sessionOpen)
|
2020-02-08 07:58:57 +08:00
|
|
|
.on('error', function (error, label, info) {
|
|
|
|
if (!error) { return; }
|
2020-05-07 01:50:48 +08:00
|
|
|
var code = error && (error.code || error.message);
|
|
|
|
if (code) {
|
2020-04-29 04:57:52 +08:00
|
|
|
/* EPIPE,ECONNERESET, NF_ENOENT */
|
2020-05-07 01:50:48 +08:00
|
|
|
if (typeof(special_errors[code]) === 'function') {
|
|
|
|
return void special_errors[code](error, label, info);
|
2020-04-29 04:57:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 07:58:57 +08:00
|
|
|
/* labels:
|
|
|
|
SEND_MESSAGE_FAIL, SEND_MESSAGE_FAIL_2, FAIL_TO_DISCONNECT,
|
|
|
|
FAIL_TO_TERMINATE, HANDLE_CHANNEL_LEAVE, NETFLUX_BAD_MESSAGE,
|
2020-04-29 04:57:52 +08:00
|
|
|
NETFLUX_WEBSOCKET_ERROR
|
2020-02-08 07:58:57 +08:00
|
|
|
*/
|
|
|
|
log.error(label, {
|
|
|
|
code: error.code,
|
|
|
|
message: error.message,
|
|
|
|
stack: error.stack,
|
|
|
|
info: info,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.register(historyKeeper.id, historyKeeper.directMessage);
|
2024-06-12 00:14:45 +08:00
|
|
|
// Store max active WS during the last day (reset when sending ping if enabled)
|
|
|
|
setInterval(() => {
|
|
|
|
try {
|
|
|
|
// Concurrent usage data
|
|
|
|
let oldWs = Env.maxConcurrentWs || 0;
|
|
|
|
let oldUniqueWs = Env.maxConcurrentUniqueWs || 0;
|
|
|
|
let oldChans = Env.maxActiveChannels || 0;
|
|
|
|
let oldUsers = Env.maxConcurrentRegUsers || 0;
|
|
|
|
let stats = Server.getSessionStats();
|
|
|
|
let chans = Server.getActiveChannelCount();
|
|
|
|
let reg = 0;
|
|
|
|
let regKeys = [];
|
|
|
|
Object.keys(Env.netfluxUsers).forEach(id => {
|
|
|
|
let keys = Env.netfluxUsers[id];
|
|
|
|
let key = Object.keys(keys || {})[0];
|
|
|
|
if (!key) { return; }
|
|
|
|
if (regKeys.includes(key)) { return; }
|
|
|
|
reg++;
|
|
|
|
regKeys.push(key);
|
|
|
|
});
|
|
|
|
Env.maxConcurrentWs = Math.max(oldWs, stats.total);
|
|
|
|
Env.maxConcurrentUniqueWs = Math.max(oldUniqueWs, stats.unique);
|
|
|
|
Env.maxConcurrentRegUsers = Math.max(oldUsers, reg);
|
|
|
|
Env.maxActiveChannels = Math.max(oldChans, chans);
|
|
|
|
} catch (e) {}
|
|
|
|
}, 10000);
|
|
|
|
// Clean up active registered users and channels (possible memory leak)
|
|
|
|
setInterval(() => {
|
|
|
|
try {
|
|
|
|
let users = Env.netfluxUsers || {};
|
|
|
|
let online = Server.getOnlineUsers() || [];
|
|
|
|
let removed = 0;
|
|
|
|
Object.keys(users).forEach(id => {
|
|
|
|
if (!online.includes(id)) {
|
|
|
|
delete users[id];
|
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (removed) {
|
|
|
|
Env.Log.info("CLEANED_ACTIVE_USERS_MAP", {removed});
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
|
|
|
try {
|
|
|
|
let HK = require('./hk-utils');
|
|
|
|
let chans = Env.channel_cache || {};
|
|
|
|
let active = Server.getActiveChannels() || [];
|
|
|
|
let removed = 0;
|
|
|
|
Object.keys(chans).forEach(id => {
|
|
|
|
if (!active.includes(id)) {
|
|
|
|
HK.dropChannel(Env, id);
|
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
});
|
2024-07-04 17:33:43 +08:00
|
|
|
if (Env.store) {
|
|
|
|
Env.store.closeInactiveChannels(active);
|
|
|
|
}
|
2024-06-12 00:14:45 +08:00
|
|
|
if (removed) {
|
|
|
|
Env.Log.info("CLEANED_ACTIVE_CHANNELS_MAP", {removed});
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
|
|
|
}, 30000);
|
|
|
|
|
2020-01-28 06:57:39 +08:00
|
|
|
});
|
2020-10-12 20:09:53 +08:00
|
|
|
});
|
|
|
|
|
2020-01-28 06:57:39 +08:00
|
|
|
};
|