Files
cloudron-box/src/dashboard.js
T

128 lines
4.6 KiB
JavaScript
Raw Normal View History

2023-08-11 19:41:05 +05:30
'use strict';
exports = module.exports = {
getLocation,
2023-08-12 21:47:24 +05:30
setLocation,
2023-08-13 10:29:24 +05:30
clearLocation,
2023-08-12 21:47:24 +05:30
2023-08-14 09:40:31 +05:30
startPrepareLocation,
2023-08-13 10:29:24 +05:30
prepareLocation,
2023-08-13 21:36:56 +05:30
setupLocation,
2023-08-12 21:47:24 +05:30
getConfig,
2023-08-11 19:41:05 +05:30
};
const apps = require('./apps.js'),
appstore = require('./appstore.js'),
2023-08-12 21:47:24 +05:30
assert = require('assert'),
BoxError = require('./boxerror.js'),
2023-08-12 21:47:24 +05:30
branding = require('./branding.js'),
2023-08-11 19:41:05 +05:30
constants = require('./constants.js'),
debug = require('debug')('box:dashboard'),
eventlog = require('./eventlog.js'),
dns = require('./dns.js'),
2023-08-12 21:47:24 +05:30
mailServer = require('./mailserver.js'),
2023-08-13 10:29:24 +05:30
platform = require('./platform.js'),
reverseProxy = require('./reverseproxy.js'),
safe = require('safetydance'),
2023-08-12 21:47:24 +05:30
settings = require('./settings.js'),
system = require('./system.js'),
tasks = require('./tasks.js'),
2023-08-12 21:47:24 +05:30
users = require('./users.js');
2023-08-11 19:41:05 +05:30
2023-08-13 10:29:24 +05:30
async function getLocation() {
const domain = await settings.get(settings.DASHBOARD_DOMAIN_KEY) || '';
const fqdn = await settings.get(settings.DASHBOARD_FQDN_KEY) || '';
return { domain, fqdn, subdomain: constants.DASHBOARD_SUBDOMAIN };
}
async function setLocation(domain) {
2023-08-11 19:41:05 +05:30
assert.strictEqual(typeof domain, 'string');
2023-08-13 10:29:24 +05:30
const fqdn = dns.fqdn(constants.DASHBOARD_SUBDOMAIN, domain);
2023-08-11 19:41:05 +05:30
await settings.set(settings.DASHBOARD_DOMAIN_KEY, domain);
await settings.set(settings.DASHBOARD_FQDN_KEY, fqdn);
2023-08-13 21:36:56 +05:30
debug(`setLocation: ${domain || '<cleared>'}`);
2023-08-13 10:29:24 +05:30
}
2023-08-11 19:41:05 +05:30
2023-08-13 10:29:24 +05:30
async function clearLocation() {
await setLocation('');
2023-08-11 19:41:05 +05:30
}
2023-08-12 21:47:24 +05:30
async function getConfig() {
const ubuntuVersion = await system.getUbuntuVersion();
const profileConfig = await users.getProfileConfig();
const { fqdn:adminFqdn, domain:adminDomain } = await getLocation();
// be picky about what we send out here since this is sent for 'normal' users as well
return {
apiServerOrigin: await appstore.getApiServerOrigin(),
webServerOrigin: await appstore.getWebServerOrigin(),
consoleServerOrigin: await appstore.getConsoleServerOrigin(),
adminDomain,
adminFqdn,
mailFqdn: await mailServer.getLocation().fqdn,
version: constants.VERSION,
ubuntuVersion,
isDemo: constants.DEMO,
cloudronName: await branding.getCloudronName(),
footer: await branding.renderFooter(),
features: appstore.getFeatures(),
profileLocked: profileConfig.lockUserProfiles,
mandatory2FA: profileConfig.mandatory2FA,
};
}
2023-08-14 09:40:31 +05:30
async function startPrepareLocation(domain, auditSource) {
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof auditSource, 'object');
2023-08-13 10:29:24 +05:30
debug(`prepareLocation: ${domain}`);
if (constants.DEMO) throw new BoxError(BoxError.CONFLICT, 'Not allowed in demo mode');
const fqdn = dns.fqdn(constants.DASHBOARD_SUBDOMAIN, domain);
const result = await apps.list();
if (result.some(app => app.fqdn === fqdn)) throw new BoxError(BoxError.BAD_STATE, 'Dashboard location conflicts with an existing app');
2023-08-14 09:40:31 +05:30
const taskId = await tasks.add(tasks.TASK_PREPARE_DASHBOARD_LOCATION, [ constants.DASHBOARD_SUBDOMAIN, domain, auditSource ]);
tasks.startTask(taskId, {});
return taskId;
}
2023-08-14 09:40:31 +05:30
async function prepareLocation(subdomain, domain, auditSource, progressCallback) {
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof auditSource, 'object');
assert.strictEqual(typeof progressCallback, 'function');
const location = { subdomain, domain };
const fqdn = dns.fqdn(subdomain, domain);
progressCallback({ percent: 20, message: `Updating DNS of ${fqdn}` });
await dns.registerLocations([location], { overwriteDns: true }, progressCallback);
progressCallback({ percent: 40, message: `Waiting for DNS of ${fqdn}` });
await dns.waitForLocations([location], progressCallback);
progressCallback({ percent: 60, message: `Getting certificate of ${fqdn}` });
await reverseProxy.ensureCertificate(location, {}, auditSource);
}
2023-08-13 21:36:56 +05:30
async function setupLocation(domain, auditSource) {
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof auditSource, 'object');
2023-08-13 21:36:56 +05:30
debug(`setupLocation: ${domain}`);
2023-08-13 10:29:24 +05:30
if (constants.DEMO) throw new BoxError(BoxError.CONFLICT, 'Not allowed in demo mode');
await reverseProxy.writeDashboardConfig(domain);
const fqdn = dns.fqdn(constants.DASHBOARD_SUBDOMAIN, domain);
2023-08-13 10:29:24 +05:30
await setLocation(domain);
await safe(appstore.updateCloudron({ domain }), { debug });
await eventlog.add(eventlog.ACTION_DASHBOARD_DOMAIN_UPDATE, auditSource, { domain, fqdn });
2023-08-13 10:29:24 +05:30
await platform.onDashboardLocationChanged(auditSource);
}