2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2023-08-10 18:45:27 +05:30
|
|
|
getStatus,
|
2015-10-27 16:00:31 -07: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'),
|
2019-10-22 14:06:19 -07:00
|
|
|
BoxError = require('./boxerror.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'),
|
2023-08-11 19:41:05 +05:30
|
|
|
dashboard = require('./dashboard.js'),
|
2021-08-13 17:22:28 -07:00
|
|
|
dns = require('./dns.js'),
|
2019-02-04 20:24:28 -08:00
|
|
|
eventlog = require('./eventlog.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'),
|
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-08-12 21:47:24 +05:30
|
|
|
translation = require('./translation.js');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2023-08-10 18:45:27 +05:30
|
|
|
async function getStatus() {
|
|
|
|
|
return {
|
|
|
|
|
version: constants.VERSION,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2023-08-11 19:41:05 +05:30
|
|
|
await dashboard.setLocation(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);
|
|
|
|
|
}
|