Files
cloudron-box/box.js
T

98 lines
3.3 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
import constants from './src/constants.js';
import fs from 'node:fs';
2026-02-14 15:43:24 +01:00
import ldapServer from './src/ldapserver.js';
import net from 'node:net';
2026-02-14 15:43:24 +01:00
import oidcServer from './src/oidcserver.js';
import paths from './src/paths.js';
2026-02-14 15:43:24 +01:00
import proxyAuth from './src/proxyauth.js';
import safe from 'safetydance';
2026-02-14 15:43:24 +01:00
import server from './src/server.js';
import directoryServer from './src/directoryserver.js';
2026-03-12 22:55:28 +05:30
import logger from './src/logger.js';
2026-03-12 23:23:23 +05:30
const { log } = logger('box');
2021-09-07 11:23:54 -07:00
let logFd;
async function setupLogging() {
2023-10-01 13:52:19 +05:30
if (constants.TEST) return;
2020-08-04 22:16:38 -07:00
2021-09-07 11:23:54 -07:00
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() {
2024-10-31 10:07:11 +01:00
net.setDefaultAutoSelectFamilyAttemptTimeout(2500);
}
2021-09-07 11:23:54 -07:00
// 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 () {});
2021-09-07 11:23:54 -07:00
fs.fsyncSync(logFd);
fs.closeSync(logFd);
process.exit(status.code);
2021-09-07 09:57:49 -07:00
}
2020-08-04 09:34:03 -07:00
2021-09-07 09:57:49 -07:00
async function startServers() {
2021-09-07 11:23:54 -07:00
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();
2024-01-06 13:29:23 +01:00
await ldapServer.start();
2023-08-03 06:34:55 +05:30
const conf = await directoryServer.getConfig();
2022-08-15 19:14:02 +02:00
if (conf.enabled) await directoryServer.start();
2020-08-04 09:34:03 -07:00
}
2026-02-14 16:52:16 +01:00
const [error] = await safe(startServers());
if (error) exitSync({ error, code: 1, message: 'Error starting servers' });
2021-09-07 09:57:49 -07:00
2026-02-14 16:52:16 +01:00
process.on('SIGHUP', async function () {
2026-03-12 22:55:28 +05:30
log('Received SIGHUP. Re-reading configs.');
2026-02-14 16:52:16 +01:00
const conf = await directoryServer.getConfig();
if (conf.enabled) await directoryServer.checkCertificate();
});
process.on('SIGINT', async function () {
2026-03-12 22:55:28 +05:30
log('Received SIGINT. Shutting down.');
2026-02-14 16:52:16 +01:00
await proxyAuth.stop();
await server.stop();
await directoryServer.stop();
await ldapServer.stop();
await oidcServer.stop();
setTimeout(() => {
2026-03-12 22:55:28 +05:30
log('Shutdown complete');
2026-02-14 16:52:16 +01:00
process.exit();
}, 2000); // need to wait for the task processes to die
});
process.on('SIGTERM', async function () {
2026-03-12 22:55:28 +05:30
log('Received SIGTERM. Shutting down.');
2026-02-14 16:52:16 +01:00
await proxyAuth.stop();
await server.stop();
await directoryServer.stop();
await ldapServer.stop();
await oidcServer.stop();
setTimeout(() => {
2026-03-12 22:55:28 +05:30
log('Shutdown complete');
2026-02-14 16:52:16 +01:00
process.exit();
}, 2000); // need to wait for the task processes to die
});
2026-03-12 23:23:23 +05:30
process.on('uncaughtException', (uncaughtError) => exitSync({ error: uncaughtError, code: 1, message: 'From uncaughtException handler.' }));