Files
cloudron-box/src/dashboard.js
T

136 lines
5.0 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
setupDnsAndCert,
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'),
network = require('./network.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-13 10:29:24 +05:30
async function prepareLocation(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');
const taskId = await tasks.add(tasks.TASK_SETUP_DNS_AND_CERT, [ constants.DASHBOARD_SUBDOMAIN, domain, auditSource ]);
tasks.startTask(taskId, {});
return taskId;
}
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);
}
async function setupDnsAndCert(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 dashboardFqdn = dns.fqdn(subdomain, domain);
const ipv4 = await network.getIPv4();
const ipv6 = await network.getIPv6();
progressCallback({ percent: 20, message: `Updating DNS of ${dashboardFqdn}` });
await dns.upsertDnsRecords(subdomain, domain, 'A', [ ipv4 ]);
if (ipv6) await dns.upsertDnsRecords(subdomain, domain, 'AAAA', [ ipv6 ]);
progressCallback({ percent: 40, message: `Waiting for DNS of ${dashboardFqdn}` });
await dns.waitForDnsRecord(subdomain, domain, 'A', ipv4, { interval: 30000, times: 50000 });
if (ipv6) await dns.waitForDnsRecord(subdomain, domain, 'AAAA', ipv6, { interval: 30000, times: 50000 });
progressCallback({ percent: 60, message: `Getting certificate of ${dashboardFqdn}` });
const location = { subdomain, domain, fqdn: dashboardFqdn, type: apps.LOCATION_TYPE_DASHBOARD, certificate: null };
await reverseProxy.ensureCertificate(location, {}, auditSource);
}