Files
cloudron-box/box.js

104 lines
3.5 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
'use strict';
2023-10-01 13:52:19 +05:30
const constants = require('./src/constants.js'),
fs = require('fs'),
ldapServer = require('./src/ldapserver.js'),
net = require('net'),
2025-06-11 22:00:09 +02:00
oidcServer = require('./src/oidcserver.js'),
paths = require('./src/paths.js'),
2020-11-10 09:59:28 -08:00
proxyAuth = require('./src/proxyauth.js'),
2021-09-07 09:57:49 -07:00
safe = require('safetydance'),
server = require('./src/server.js'),
directoryServer = require('./src/directoryserver.js');
let logFd;
async function setupLogging() {
2023-10-01 13:52:19 +05:30
if (constants.TEST) return;
logFd = fs.openSync(paths.BOX_LOG_FILE, 'a');
// we used to write using a stream before but it caches internally and there is no way to flush it when things crash
process.stdout.write = process.stderr.write = function (...args) {
const callback = typeof args[args.length-1] === 'function' ? args.pop() : function () {}; // callback is required for fs.write
fs.write.apply(fs, [logFd, ...args, callback]);
};
}
// happy eyeballs workaround. when there is no ipv6, nodejs timesout prematurely since the default for ipv4 is just 250ms
// https://github.com/nodejs/node/issues/54359
async function setupNetworking() {
net.setDefaultAutoSelectFamilyAttemptTimeout(2500);
}
// this is also used as the 'uncaughtException' handler which can only have synchronous functions
function exitSync(status) {
2024-10-14 16:21:25 +02:00
const ts = new Date().toISOString();
if (status.message) fs.write(logFd, `${ts} ${status.message}\n`, function () {});
2024-10-14 16:21:25 +02:00
const msg = status.error.stack.replace(/\n/g, `\n${ts} `); // prefix each line with ts
if (status.error) fs.write(logFd, `${ts} ${msg}\n`, function () {});
fs.fsyncSync(logFd);
fs.closeSync(logFd);
process.exit(status.code);
2021-09-07 09:57:49 -07:00
}
2021-09-07 09:57:49 -07:00
async function startServers() {
await setupLogging();
await setupNetworking();
2021-09-07 09:57:49 -07:00
await server.start(); // do this first since it also inits the database
await proxyAuth.start();
await ldapServer.start();
2023-08-03 06:34:55 +05:30
const conf = await directoryServer.getConfig();
if (conf.enabled) await directoryServer.start();
}
2021-09-07 09:57:49 -07:00
async function main() {
const [error] = await safe(startServers());
if (error) return exitSync({ error, code: 1, message: 'Error starting servers' });
2022-02-21 17:34:51 -08:00
// require this here so that logging handler is already setup
const debug = require('debug')('box:box');
process.on('SIGHUP', async function () {
debug('Received SIGHUP. Re-reading configs.');
2023-08-03 06:34:55 +05:30
const conf = await directoryServer.getConfig();
if (conf.enabled) await directoryServer.checkCertificate();
});
2021-09-07 09:57:49 -07:00
process.on('SIGINT', async function () {
debug('Received SIGINT. Shutting down.');
2018-11-08 14:35:22 +01:00
2021-09-07 09:57:49 -07:00
await proxyAuth.stop();
await server.stop();
await directoryServer.stop();
await ldapServer.stop();
2025-06-11 22:00:09 +02:00
await oidcServer.stop();
2025-06-17 22:28:11 +02:00
setTimeout(() => {
debug('Shutdown complete');
process.exit();
}, 2000); // need to wait for the task processes to die
});
2021-09-07 09:57:49 -07:00
process.on('SIGTERM', async function () {
debug('Received SIGTERM. Shutting down.');
2021-09-07 09:57:49 -07:00
await proxyAuth.stop();
await server.stop();
await directoryServer.stop();
await ldapServer.stop();
2025-06-11 22:00:09 +02:00
await oidcServer.stop();
2025-06-17 22:28:11 +02:00
setTimeout(() => {
debug('Shutdown complete');
process.exit();
}, 2000); // need to wait for the task processes to die
});
2018-11-08 14:35:22 +01:00
process.on('uncaughtException', (error) => exitSync({ error, code: 1, message: 'From uncaughtException handler.' }));
2021-09-07 09:57:49 -07:00
}
main();