2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2020-08-15 22:54:32 -07:00
|
|
|
initialize,
|
|
|
|
|
uninitialize,
|
2023-08-10 18:45:27 +05:30
|
|
|
|
|
|
|
|
getStatus,
|
2020-08-15 22:54:32 -07:00
|
|
|
getConfig,
|
2015-10-27 16:00:31 -07:00
|
|
|
|
2020-08-15 22:54:32 -07:00
|
|
|
onActivated,
|
2018-01-29 15:47:26 -08:00
|
|
|
|
2020-08-15 23:17:29 -07:00
|
|
|
setupDnsAndCert,
|
|
|
|
|
|
2020-08-15 22:54:32 -07:00
|
|
|
prepareDashboardDomain,
|
|
|
|
|
setDashboardDomain,
|
|
|
|
|
updateDashboardDomain,
|
2018-11-11 09:29:11 -08:00
|
|
|
|
2023-08-04 10:10:08 +05:30
|
|
|
getTimeZone,
|
|
|
|
|
setTimeZone,
|
|
|
|
|
|
|
|
|
|
getLanguage,
|
|
|
|
|
setLanguage,
|
2015-09-28 23:10:09 -07:00
|
|
|
};
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-01-21 11:31:35 -08:00
|
|
|
const apps = require('./apps.js'),
|
2020-02-14 12:20:15 +01:00
|
|
|
appstore = require('./appstore.js'),
|
2019-03-18 19:13:44 -07:00
|
|
|
assert = require('assert'),
|
2021-09-30 09:50:30 -07:00
|
|
|
AuditSource = require('./auditsource.js'),
|
2019-10-22 14:06:19 -07:00
|
|
|
BoxError = require('./boxerror.js'),
|
2020-10-18 10:15:36 -07:00
|
|
|
branding = require('./branding.js'),
|
2018-12-14 09:57:28 -08:00
|
|
|
constants = require('./constants.js'),
|
2017-01-09 11:00:09 -08:00
|
|
|
cron = require('./cron.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
debug = require('debug')('box:cloudron'),
|
2021-08-13 17:22:28 -07:00
|
|
|
dns = require('./dns.js'),
|
2022-02-09 17:28:46 -08:00
|
|
|
dockerProxy = require('./dockerproxy.js'),
|
2019-02-04 20:24:28 -08:00
|
|
|
eventlog = require('./eventlog.js'),
|
2023-08-04 21:37:38 +05:30
|
|
|
mailServer = require('./mailserver.js'),
|
2023-08-04 10:10:08 +05:30
|
|
|
moment = require('moment-timezone'),
|
2023-08-03 13:38:42 +05:30
|
|
|
network = require('./network.js'),
|
2023-03-21 14:59:28 +01:00
|
|
|
oidc = require('./oidc.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
paths = require('./paths.js'),
|
2017-01-07 23:33:20 -08:00
|
|
|
platform = require('./platform.js'),
|
2018-01-30 12:23:27 -08:00
|
|
|
reverseProxy = require('./reverseproxy.js'),
|
2019-08-03 13:59:11 -07:00
|
|
|
safe = require('safetydance'),
|
2021-01-21 11:31:35 -08:00
|
|
|
services = require('./services.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
settings = require('./settings.js'),
|
2018-12-10 20:20:53 -08:00
|
|
|
tasks = require('./tasks.js'),
|
2023-05-14 10:53:50 +02:00
|
|
|
timers = require('timers/promises'),
|
2023-08-04 10:10:08 +05:30
|
|
|
translation = require('./translation.js'),
|
2021-09-17 09:22:46 -07:00
|
|
|
users = require('./users.js');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-06-03 11:42:32 -07:00
|
|
|
async function initialize() {
|
2021-11-02 13:58:32 -07:00
|
|
|
safe(runStartupTasks(), { debug }); // background
|
2019-05-08 15:24:37 -07:00
|
|
|
|
2021-06-03 11:42:32 -07:00
|
|
|
await notifyUpdate();
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
async function uninitialize() {
|
|
|
|
|
await cron.stopJobs();
|
2022-02-09 17:28:46 -08:00
|
|
|
await dockerProxy.stop();
|
2021-09-07 09:57:49 -07:00
|
|
|
await platform.stopAllTasks();
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-17 09:22:46 -07:00
|
|
|
async function onActivated(options) {
|
2021-02-24 15:03:49 -08:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
2017-11-22 21:31:30 -08:00
|
|
|
|
2021-05-02 23:28:41 -07:00
|
|
|
debug('onActivated: running post activation tasks');
|
|
|
|
|
|
2017-11-22 21:31:30 -08:00
|
|
|
// Starting the platform after a user is available means:
|
|
|
|
|
// 1. mail bounces can now be sent to the cloudron owner
|
|
|
|
|
// 2. the restore code path can run without sudo (since mail/ is non-root)
|
2021-09-17 09:22:46 -07:00
|
|
|
await platform.start(options);
|
|
|
|
|
await cron.startJobs();
|
2022-02-09 17:28:46 -08:00
|
|
|
await dockerProxy.start(); // this relies on the 'cloudron' docker network interface to be available
|
2023-03-26 16:50:03 +02:00
|
|
|
await oidc.start(); // this requires dashboardFqdn to be set
|
2021-09-17 09:22:46 -07:00
|
|
|
|
|
|
|
|
// disable responding to api calls via IP to not leak domain info. this is carefully placed as the last item, so it buys
|
|
|
|
|
// the UI some time to query the dashboard domain in the restore code path
|
2023-05-14 10:53:50 +02:00
|
|
|
await timers.setTimeout(30000);
|
2021-09-17 09:22:46 -07:00
|
|
|
await reverseProxy.writeDefaultConfig({ activated :true });
|
2018-11-10 18:21:15 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-03 11:42:32 -07:00
|
|
|
async function notifyUpdate() {
|
2019-08-03 13:59:11 -07:00
|
|
|
const version = safe.fs.readFileSync(paths.VERSION_FILE, 'utf8');
|
2021-06-03 11:42:32 -07:00
|
|
|
if (version === constants.VERSION) return;
|
2019-08-03 13:59:11 -07:00
|
|
|
|
2022-04-01 13:44:46 -07:00
|
|
|
if (!version) {
|
|
|
|
|
await eventlog.add(eventlog.ACTION_INSTALL_FINISH, AuditSource.CRON, { version: constants.VERSION });
|
|
|
|
|
} else {
|
|
|
|
|
await eventlog.add(eventlog.ACTION_UPDATE_FINISH, AuditSource.CRON, { errorMessage: '', oldVersion: version || 'dev', newVersion: constants.VERSION });
|
|
|
|
|
const [error] = await safe(tasks.setCompletedByType(tasks.TASK_UPDATE, { error: null }));
|
|
|
|
|
if (error && error.reason !== BoxError.NOT_FOUND) throw error; // when hotfixing, task may not exist
|
|
|
|
|
}
|
2019-08-03 13:59:11 -07:00
|
|
|
|
2021-07-12 23:35:30 -07:00
|
|
|
safe.fs.writeFileSync(paths.VERSION_FILE, constants.VERSION, 'utf8');
|
2019-08-03 13:59:11 -07:00
|
|
|
}
|
|
|
|
|
|
2018-11-10 18:21:15 -08:00
|
|
|
// each of these tasks can fail. we will add some routes to fix/re-run them
|
2021-09-17 09:22:46 -07:00
|
|
|
async function runStartupTasks() {
|
|
|
|
|
const tasks = [];
|
2020-08-06 22:04:46 -07:00
|
|
|
|
2021-09-17 09:22:46 -07:00
|
|
|
// stop all the systemd tasks
|
|
|
|
|
tasks.push(platform.stopAllTasks);
|
2018-11-11 08:19:24 -08:00
|
|
|
|
2021-09-17 09:22:46 -07:00
|
|
|
// always generate webadmin config since we have no versioning mechanism for the ejs
|
|
|
|
|
tasks.push(async function () {
|
|
|
|
|
if (!settings.dashboardDomain()) return;
|
2020-09-02 17:32:31 -07:00
|
|
|
|
2022-11-28 22:32:34 +01:00
|
|
|
await reverseProxy.writeDashboardConfig(settings.dashboardDomain());
|
2021-09-17 09:22:46 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tasks.push(async function () {
|
2020-09-02 17:32:31 -07:00
|
|
|
// check activation state and start the platform
|
2021-09-17 09:22:46 -07:00
|
|
|
const activated = await users.isActivated();
|
|
|
|
|
|
|
|
|
|
// configure nginx to be reachable by IP when not activated. for the moment, the IP based redirect exists even after domain is setup
|
|
|
|
|
// just in case user forgot or some network error happenned in the middle (then browser refresh takes you to activation page)
|
|
|
|
|
// we remove the config as a simple security measure to not expose IP <-> domain
|
|
|
|
|
if (!activated) {
|
|
|
|
|
debug('runStartupTasks: not activated. generating IP based redirection config');
|
|
|
|
|
return await reverseProxy.writeDefaultConfig({ activated: false });
|
2020-08-13 14:00:55 -07:00
|
|
|
}
|
2017-11-22 21:31:30 -08:00
|
|
|
|
2021-09-17 09:22:46 -07:00
|
|
|
await onActivated({});
|
2017-11-22 21:31:30 -08:00
|
|
|
});
|
2021-09-17 09:22:46 -07:00
|
|
|
|
|
|
|
|
// we used to run tasks in parallel but simultaneous nginx reloads was causing issues
|
|
|
|
|
for (let i = 0; i < tasks.length; i++) {
|
2021-11-02 22:30:38 -07:00
|
|
|
const [error] = await safe(tasks[i]());
|
2022-08-10 22:07:05 +02:00
|
|
|
if (error) debug(`Startup task at index ${i} failed: ${error.message} ${error.stack}`);
|
2021-09-17 09:22:46 -07:00
|
|
|
}
|
2017-11-22 21:31:30 -08:00
|
|
|
}
|
|
|
|
|
|
2023-08-10 18:45:27 +05:30
|
|
|
async function getStatus() {
|
|
|
|
|
return {
|
|
|
|
|
version: constants.VERSION,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 13:25:42 -07:00
|
|
|
async function getConfig() {
|
2021-05-18 14:37:11 -07:00
|
|
|
const release = safe.fs.readFileSync('/etc/lsb-release', 'utf-8');
|
2021-08-18 13:25:42 -07:00
|
|
|
if (release === null) throw new BoxError(BoxError.FS_ERROR, safe.error.message);
|
2021-05-18 14:37:11 -07:00
|
|
|
const ubuntuVersion = release.match(/DISTRIB_DESCRIPTION="(.*)"/)[1];
|
2023-08-03 09:03:47 +05:30
|
|
|
const profileConfig = await users.getProfileConfig();
|
2021-08-18 13:25:42 -07:00
|
|
|
|
|
|
|
|
// be picky about what we send out here since this is sent for 'normal' users as well
|
|
|
|
|
return {
|
2023-08-04 15:34:38 +05:30
|
|
|
apiServerOrigin: await appstore.getApiServerOrigin(),
|
|
|
|
|
webServerOrigin: await appstore.getWebServerOrigin(),
|
|
|
|
|
consoleServerOrigin: await appstore.getConsoleServerOrigin(),
|
2021-08-18 13:25:42 -07:00
|
|
|
adminDomain: settings.dashboardDomain(),
|
|
|
|
|
adminFqdn: settings.dashboardFqdn(),
|
2023-08-04 21:37:38 +05:30
|
|
|
mailFqdn: await mailServer.getLocation().fqdn,
|
2021-08-18 13:25:42 -07:00
|
|
|
version: constants.VERSION,
|
|
|
|
|
ubuntuVersion,
|
2023-08-04 14:13:30 +05:30
|
|
|
isDemo: constants.DEMO,
|
2023-08-02 21:01:11 +05:30
|
|
|
cloudronName: await branding.getCloudronName(),
|
|
|
|
|
footer: await branding.renderFooter(),
|
2021-08-18 13:25:42 -07:00
|
|
|
features: appstore.getFeatures(),
|
2023-08-03 09:03:47 +05:30
|
|
|
profileLocked: profileConfig.lockUserProfiles,
|
|
|
|
|
mandatory2FA: profileConfig.mandatory2FA,
|
2021-08-18 13:25:42 -07:00
|
|
|
};
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
async function prepareDashboardDomain(domain, auditSource) {
|
2018-12-14 09:57:28 -08:00
|
|
|
assert.strictEqual(typeof domain, 'string');
|
|
|
|
|
assert.strictEqual(typeof auditSource, 'object');
|
|
|
|
|
|
2018-12-15 15:27:16 -08:00
|
|
|
debug(`prepareDashboardDomain: ${domain}`);
|
2018-12-14 09:57:28 -08:00
|
|
|
|
2023-08-04 14:13:30 +05:30
|
|
|
if (constants.DEMO) throw new BoxError(BoxError.CONFLICT, 'Not allowed in demo mode');
|
2019-03-18 19:13:44 -07:00
|
|
|
|
2022-11-28 21:23:06 +01:00
|
|
|
const fqdn = dns.fqdn(constants.DASHBOARD_SUBDOMAIN, domain);
|
2019-03-18 19:13:44 -07:00
|
|
|
|
2021-08-30 14:00:50 -07:00
|
|
|
const result = await apps.list();
|
2021-08-30 14:42:07 -07:00
|
|
|
if (result.some(app => app.fqdn === fqdn)) throw new BoxError(BoxError.BAD_STATE, 'Dashboard location conflicts with an existing app');
|
2019-03-18 19:13:44 -07:00
|
|
|
|
2022-07-14 15:18:17 +05:30
|
|
|
const taskId = await tasks.add(tasks.TASK_SETUP_DNS_AND_CERT, [ constants.DASHBOARD_SUBDOMAIN, domain, auditSource ]);
|
2019-08-27 22:39:59 -07:00
|
|
|
|
2021-09-17 09:22:46 -07:00
|
|
|
tasks.startTask(taskId, {});
|
2019-08-27 22:39:59 -07:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
return taskId;
|
2018-12-14 09:57:28 -08:00
|
|
|
}
|
|
|
|
|
|
2019-02-26 19:43:18 -08:00
|
|
|
// call this only pre activation since it won't start mail server
|
2021-08-19 13:24:38 -07:00
|
|
|
async function setDashboardDomain(domain, auditSource) {
|
2018-12-07 16:15:21 -08:00
|
|
|
assert.strictEqual(typeof domain, 'string');
|
2019-02-04 20:24:28 -08:00
|
|
|
assert.strictEqual(typeof auditSource, 'object');
|
2018-12-07 16:15:21 -08:00
|
|
|
|
2018-12-08 18:18:45 -08:00
|
|
|
debug(`setDashboardDomain: ${domain}`);
|
2018-12-07 16:15:21 -08:00
|
|
|
|
2022-11-28 22:32:34 +01:00
|
|
|
await reverseProxy.writeDashboardConfig(domain);
|
2022-11-28 21:23:06 +01:00
|
|
|
const fqdn = dns.fqdn(constants.DASHBOARD_SUBDOMAIN, domain);
|
2018-12-07 16:15:21 -08:00
|
|
|
|
2021-08-19 13:24:38 -07:00
|
|
|
await settings.setDashboardLocation(domain, fqdn);
|
2019-01-16 21:36:48 -08:00
|
|
|
|
2022-03-31 22:46:14 -07:00
|
|
|
await safe(appstore.updateCloudron({ domain }), { debug });
|
2021-04-13 14:19:45 -07:00
|
|
|
|
2021-08-19 13:24:38 -07:00
|
|
|
await eventlog.add(eventlog.ACTION_DASHBOARD_DOMAIN_UPDATE, auditSource, { domain, fqdn });
|
2018-12-07 16:15:21 -08:00
|
|
|
}
|
2018-12-10 20:20:53 -08:00
|
|
|
|
2019-02-26 19:43:18 -08:00
|
|
|
// call this only post activation because it will restart mail server
|
2021-08-19 13:24:38 -07:00
|
|
|
async function updateDashboardDomain(domain, auditSource) {
|
2019-02-26 19:43:18 -08:00
|
|
|
assert.strictEqual(typeof domain, 'string');
|
|
|
|
|
assert.strictEqual(typeof auditSource, 'object');
|
|
|
|
|
|
2020-08-15 22:53:05 -07:00
|
|
|
debug(`updateDashboardDomain: ${domain}`);
|
2019-02-26 19:43:18 -08:00
|
|
|
|
2023-08-04 14:13:30 +05:30
|
|
|
if (constants.DEMO) throw new BoxError(BoxError.CONFLICT, 'Not allowed in demo mode');
|
2019-02-26 19:43:18 -08:00
|
|
|
|
2021-08-19 13:24:38 -07:00
|
|
|
await setDashboardDomain(domain, auditSource);
|
2019-02-26 19:43:18 -08:00
|
|
|
|
2023-07-21 17:10:25 +02:00
|
|
|
// mark apps using oidc addon to be reconfigured
|
|
|
|
|
const [, installedApps] = await safe(apps.list());
|
|
|
|
|
await safe(apps.configureInstalledApps(installedApps.filter((a) => !!a.manifest.addons.oidc), auditSource));
|
|
|
|
|
|
|
|
|
|
await safe(services.rebuildService('turn', auditSource), { debug }); // to update the realm variable
|
2023-03-21 14:59:28 +01:00
|
|
|
|
|
|
|
|
await oidc.stop();
|
|
|
|
|
await oidc.start();
|
2019-02-26 19:43:18 -08:00
|
|
|
}
|
|
|
|
|
|
2021-08-27 09:52:24 -07:00
|
|
|
async function setupDnsAndCert(subdomain, domain, auditSource, progressCallback) {
|
2020-08-15 23:17:29 -07:00
|
|
|
assert.strictEqual(typeof subdomain, 'string');
|
|
|
|
|
assert.strictEqual(typeof domain, 'string');
|
|
|
|
|
assert.strictEqual(typeof auditSource, 'object');
|
|
|
|
|
assert.strictEqual(typeof progressCallback, 'function');
|
|
|
|
|
|
2022-11-28 21:23:06 +01:00
|
|
|
const dashboardFqdn = dns.fqdn(subdomain, domain);
|
2020-08-15 23:17:29 -07:00
|
|
|
|
2023-08-03 13:38:42 +05:30
|
|
|
const ipv4 = await network.getIPv4();
|
|
|
|
|
const ipv6 = await network.getIPv6();
|
2020-08-15 23:17:29 -07:00
|
|
|
|
2022-02-06 11:21:32 -08:00
|
|
|
progressCallback({ percent: 20, message: `Updating DNS of ${dashboardFqdn}` });
|
2022-01-06 17:02:16 -08:00
|
|
|
await dns.upsertDnsRecords(subdomain, domain, 'A', [ ipv4 ]);
|
2022-02-15 12:31:55 -08:00
|
|
|
if (ipv6) await dns.upsertDnsRecords(subdomain, domain, 'AAAA', [ ipv6 ]);
|
2022-02-06 11:21:32 -08:00
|
|
|
progressCallback({ percent: 40, message: `Waiting for DNS of ${dashboardFqdn}` });
|
2022-01-06 17:02:16 -08:00
|
|
|
await dns.waitForDnsRecord(subdomain, domain, 'A', ipv4, { interval: 30000, times: 50000 });
|
2022-02-15 12:31:55 -08:00
|
|
|
if (ipv6) await dns.waitForDnsRecord(subdomain, domain, 'AAAA', ipv6, { interval: 30000, times: 50000 });
|
2022-02-06 11:21:32 -08:00
|
|
|
progressCallback({ percent: 60, message: `Getting certificate of ${dashboardFqdn}` });
|
2022-11-28 22:32:34 +01:00
|
|
|
const location = { subdomain, domain, fqdn: dashboardFqdn, type: apps.LOCATION_TYPE_DASHBOARD, certificate: null };
|
2023-01-31 23:45:17 +01:00
|
|
|
await reverseProxy.ensureCertificate(location, {}, auditSource);
|
2020-08-15 23:17:29 -07:00
|
|
|
}
|
2021-02-24 18:42:39 -08:00
|
|
|
|
2023-08-04 10:10:08 +05:30
|
|
|
async function getTimeZone() {
|
|
|
|
|
const tz = await settings.get(settings.TIME_ZONE_KEY);
|
|
|
|
|
return tz || 'UTC';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function setTimeZone(tz) {
|
|
|
|
|
assert.strictEqual(typeof tz, 'string');
|
|
|
|
|
|
|
|
|
|
if (moment.tz.names().indexOf(tz) === -1) throw new BoxError(BoxError.BAD_FIELD, 'Bad timeZone');
|
|
|
|
|
|
|
|
|
|
await settings.set(settings.TIME_ZONE_KEY, tz);
|
|
|
|
|
await cron.handleTimeZoneChanged(tz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getLanguage() {
|
|
|
|
|
const value = await settings.get(settings.LANGUAGE_KEY);
|
|
|
|
|
return value || 'en';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function setLanguage(language) {
|
|
|
|
|
assert.strictEqual(typeof language, 'string');
|
|
|
|
|
|
2023-08-10 16:20:33 +05:30
|
|
|
const languages = await translation.listLanguages();
|
2023-08-04 10:10:08 +05:30
|
|
|
if (languages.indexOf(language) === -1) throw new BoxError(BoxError.BAD_FIELD, 'Language not found');
|
|
|
|
|
|
|
|
|
|
await settings.set(settings.LANGUAGE_KEY, language);
|
|
|
|
|
}
|