2022-03-10 13:34:30 +00:00
|
|
|
const { BeanModel } = require("redbean-node/dist/bean-model");
|
|
|
|
const { R } = require("redbean-node");
|
2022-05-10 17:51:11 +01:00
|
|
|
const cheerio = require("cheerio");
|
2022-05-30 08:45:44 +01:00
|
|
|
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
2022-03-10 13:34:30 +00:00
|
|
|
|
|
|
|
class StatusPage extends BeanModel {
|
|
|
|
|
2022-05-30 08:45:44 +01:00
|
|
|
/**
|
|
|
|
* Like this: { "test-uptime.kuma.pet": "default" }
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
2022-04-06 15:43:22 +01:00
|
|
|
static domainMappingList = { };
|
|
|
|
|
2022-05-30 08:45:44 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Response} response
|
|
|
|
* @param {string} indexHTML
|
|
|
|
* @param {string} slug
|
|
|
|
*/
|
|
|
|
static async handleStatusPageResponse(response, indexHTML, slug) {
|
|
|
|
let statusPage = await R.findOne("status_page", " slug = ? ", [
|
|
|
|
slug
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (statusPage) {
|
2022-06-01 06:05:12 +01:00
|
|
|
response.send(await StatusPage.renderHTML(indexHTML, statusPage));
|
2022-05-30 08:45:44 +01:00
|
|
|
} else {
|
|
|
|
response.status(404).send(UptimeKumaServer.getInstance().indexHTML);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 17:51:11 +01:00
|
|
|
/**
|
|
|
|
* SSR for status pages
|
|
|
|
* @param {string} indexHTML
|
|
|
|
* @param {StatusPage} statusPage
|
|
|
|
*/
|
2022-06-01 06:05:12 +01:00
|
|
|
static async renderHTML(indexHTML, statusPage) {
|
2022-05-10 17:51:11 +01:00
|
|
|
const $ = cheerio.load(indexHTML);
|
2022-06-07 07:57:23 +01:00
|
|
|
const description155 = statusPage.description?.substring(0, 155);
|
2022-05-10 17:51:11 +01:00
|
|
|
|
2022-05-30 08:45:44 +01:00
|
|
|
$("title").text(statusPage.title);
|
2022-05-31 15:53:48 +01:00
|
|
|
$("meta[name=description]").attr("content", description155);
|
2022-05-30 08:45:44 +01:00
|
|
|
|
|
|
|
if (statusPage.icon) {
|
|
|
|
$("link[rel=icon]")
|
|
|
|
.attr("href", statusPage.icon)
|
|
|
|
.removeAttr("type");
|
2022-07-04 14:58:27 +01:00
|
|
|
|
|
|
|
$("link[rel=apple-touch-icon]").remove();
|
2022-05-30 08:45:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const head = $("head");
|
|
|
|
|
2022-05-31 15:53:48 +01:00
|
|
|
// OG Meta Tags
|
|
|
|
head.append(`<meta property="og:title" content="${statusPage.title}" />`);
|
|
|
|
head.append(`<meta property="og:description" content="${description155}" />`);
|
|
|
|
|
2022-06-01 06:05:12 +01:00
|
|
|
// Preload data
|
|
|
|
const json = JSON.stringify(await StatusPage.getStatusPageData(statusPage));
|
|
|
|
head.append(`
|
|
|
|
<script>
|
|
|
|
window.preloadData = ${json}
|
|
|
|
</script>
|
|
|
|
`);
|
|
|
|
|
2022-07-04 14:58:27 +01:00
|
|
|
// manifest.json
|
|
|
|
$("link[rel=manifest]").attr("href", `/api/status-page/${statusPage.slug}/manifest.json`);
|
|
|
|
|
2022-05-10 17:51:11 +01:00
|
|
|
return $.root().html();
|
|
|
|
}
|
|
|
|
|
2022-06-01 06:05:12 +01:00
|
|
|
/**
|
|
|
|
* Get all status page data in one call
|
|
|
|
* @param {StatusPage} statusPage
|
|
|
|
*/
|
|
|
|
static async getStatusPageData(statusPage) {
|
|
|
|
// Incident
|
|
|
|
let incident = await R.findOne("incident", " pin = 1 AND active = 1 AND status_page_id = ? ", [
|
|
|
|
statusPage.id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (incident) {
|
|
|
|
incident = incident.toPublicJSON();
|
|
|
|
}
|
|
|
|
|
2022-09-17 09:20:10 +01:00
|
|
|
let maintenance = await StatusPage.getMaintenanceList(statusPage.id);
|
|
|
|
|
2022-06-01 06:05:12 +01:00
|
|
|
// Public Group List
|
|
|
|
const publicGroupList = [];
|
|
|
|
const showTags = !!statusPage.show_tags;
|
|
|
|
|
|
|
|
const list = await R.find("group", " public = 1 AND status_page_id = ? ORDER BY weight ", [
|
|
|
|
statusPage.id
|
|
|
|
]);
|
|
|
|
|
|
|
|
for (let groupBean of list) {
|
|
|
|
let monitorGroup = await groupBean.toPublicJSON(showTags);
|
|
|
|
publicGroupList.push(monitorGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response
|
|
|
|
return {
|
|
|
|
config: await statusPage.toPublicJSON(),
|
|
|
|
incident,
|
2022-09-17 09:20:10 +01:00
|
|
|
publicGroupList,
|
|
|
|
maintenance,
|
2022-06-01 06:05:12 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-06 15:43:22 +01:00
|
|
|
/**
|
2022-04-21 18:30:04 +01:00
|
|
|
* Loads domain mapping from DB
|
2022-04-06 15:43:22 +01:00
|
|
|
* Return object like this: { "test-uptime.kuma.pet": "default" }
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async loadDomainMappingList() {
|
2022-04-09 17:25:27 +01:00
|
|
|
StatusPage.domainMappingList = await R.getAssoc(`
|
2022-04-06 15:43:22 +01:00
|
|
|
SELECT domain, slug
|
|
|
|
FROM status_page, status_page_cname
|
|
|
|
WHERE status_page.id = status_page_cname.status_page_id
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
2022-04-21 18:30:04 +01:00
|
|
|
/**
|
|
|
|
* Send status page list to client
|
|
|
|
* @param {Server} io io Socket server instance
|
|
|
|
* @param {Socket} socket Socket.io instance
|
|
|
|
* @returns {Promise<Bean[]>}
|
|
|
|
*/
|
2022-03-10 13:34:30 +00:00
|
|
|
static async sendStatusPageList(io, socket) {
|
|
|
|
let result = {};
|
|
|
|
|
|
|
|
let list = await R.findAll("status_page", " ORDER BY title ");
|
|
|
|
|
|
|
|
for (let item of list) {
|
|
|
|
result[item.id] = await item.toJSON();
|
|
|
|
}
|
|
|
|
|
|
|
|
io.to(socket.userID).emit("statusPageList", result);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2022-04-21 18:30:04 +01:00
|
|
|
/**
|
|
|
|
* Update list of domain names
|
2022-04-21 20:02:18 +01:00
|
|
|
* @param {string[]} domainNameList
|
2022-04-21 18:30:04 +01:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2022-04-09 17:25:27 +01:00
|
|
|
async updateDomainNameList(domainNameList) {
|
|
|
|
|
|
|
|
if (!Array.isArray(domainNameList)) {
|
|
|
|
throw new Error("Invalid array");
|
|
|
|
}
|
|
|
|
|
|
|
|
let trx = await R.begin();
|
|
|
|
|
|
|
|
await trx.exec("DELETE FROM status_page_cname WHERE status_page_id = ?", [
|
|
|
|
this.id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (let domain of domainNameList) {
|
|
|
|
if (typeof domain !== "string") {
|
|
|
|
throw new Error("Invalid domain");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (domain.trim() === "") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the domain name is used in another status page, delete it
|
|
|
|
await trx.exec("DELETE FROM status_page_cname WHERE domain = ?", [
|
|
|
|
domain,
|
|
|
|
]);
|
|
|
|
|
|
|
|
let mapping = trx.dispense("status_page_cname");
|
|
|
|
mapping.status_page_id = this.id;
|
|
|
|
mapping.domain = domain;
|
|
|
|
await trx.store(mapping);
|
|
|
|
}
|
|
|
|
await trx.commit();
|
|
|
|
} catch (error) {
|
|
|
|
await trx.rollback();
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 18:30:04 +01:00
|
|
|
/**
|
|
|
|
* Get list of domain names
|
2022-04-21 20:02:18 +01:00
|
|
|
* @returns {Object[]}
|
2022-04-21 18:30:04 +01:00
|
|
|
*/
|
2022-04-09 17:25:27 +01:00
|
|
|
getDomainNameList() {
|
2022-04-06 15:43:22 +01:00
|
|
|
let domainList = [];
|
|
|
|
for (let domain in StatusPage.domainMappingList) {
|
|
|
|
let s = StatusPage.domainMappingList[domain];
|
|
|
|
|
|
|
|
if (this.slug === s) {
|
|
|
|
domainList.push(domain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return domainList;
|
|
|
|
}
|
|
|
|
|
2022-04-21 18:30:04 +01:00
|
|
|
/**
|
2022-04-22 19:10:13 +01:00
|
|
|
* Return an object that ready to parse to JSON
|
2022-04-21 18:30:04 +01:00
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2022-03-10 13:34:30 +00:00
|
|
|
async toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
slug: this.slug,
|
|
|
|
title: this.title,
|
2022-03-16 06:14:47 +00:00
|
|
|
description: this.description,
|
2022-03-17 08:42:26 +00:00
|
|
|
icon: this.getIcon(),
|
2022-03-10 13:34:30 +00:00
|
|
|
theme: this.theme,
|
|
|
|
published: !!this.published,
|
|
|
|
showTags: !!this.show_tags,
|
2022-04-09 17:25:27 +01:00
|
|
|
domainNameList: this.getDomainNameList(),
|
2022-04-17 07:53:13 +01:00
|
|
|
customCSS: this.custom_css,
|
|
|
|
footerText: this.footer_text,
|
|
|
|
showPoweredBy: !!this.show_powered_by,
|
2022-03-10 13:34:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-21 18:30:04 +01:00
|
|
|
/**
|
2022-04-22 19:10:13 +01:00
|
|
|
* Return an object that ready to parse to JSON for public
|
2022-04-21 18:30:04 +01:00
|
|
|
* Only show necessary data to public
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2022-03-10 13:34:30 +00:00
|
|
|
async toPublicJSON() {
|
|
|
|
return {
|
|
|
|
slug: this.slug,
|
|
|
|
title: this.title,
|
2022-03-16 06:14:47 +00:00
|
|
|
description: this.description,
|
2022-03-17 08:42:26 +00:00
|
|
|
icon: this.getIcon(),
|
2022-03-10 13:34:30 +00:00
|
|
|
theme: this.theme,
|
|
|
|
published: !!this.published,
|
|
|
|
showTags: !!this.show_tags,
|
2022-04-17 07:53:13 +01:00
|
|
|
customCSS: this.custom_css,
|
|
|
|
footerText: this.footer_text,
|
|
|
|
showPoweredBy: !!this.show_powered_by,
|
2022-03-10 13:34:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-21 18:30:04 +01:00
|
|
|
/**
|
|
|
|
* Convert slug to status page ID
|
|
|
|
* @param {string} slug
|
|
|
|
*/
|
2022-03-16 07:38:10 +00:00
|
|
|
static async slugToID(slug) {
|
|
|
|
return await R.getCell("SELECT id FROM status_page WHERE slug = ? ", [
|
|
|
|
slug
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-04-21 18:30:04 +01:00
|
|
|
/**
|
|
|
|
* Get path to the icon for the page
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-03-17 08:42:26 +00:00
|
|
|
getIcon() {
|
|
|
|
if (!this.icon) {
|
|
|
|
return "/icon.svg";
|
|
|
|
} else {
|
|
|
|
return this.icon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-17 09:20:10 +01:00
|
|
|
/**
|
|
|
|
* Get list of maintenances
|
|
|
|
* @param {number} statusPageId ID of status page to get maintenance for
|
|
|
|
* @returns {Object} Object representing maintenances sanitized for public
|
|
|
|
*/
|
|
|
|
static async getMaintenanceList(statusPageId) {
|
|
|
|
try {
|
|
|
|
const publicMaintenanceList = [];
|
|
|
|
|
|
|
|
let maintenanceBeanList = R.convertToBeans("maintenance", await R.getAll(`
|
2022-09-27 13:44:44 +01:00
|
|
|
SELECT m.*
|
|
|
|
FROM maintenance m, maintenance_status_page msp, maintenance_timeslot
|
|
|
|
WHERE msp.maintenance_id = m.id
|
|
|
|
AND maintenance_timeslot.maintenance.id = m.id
|
|
|
|
AND maintenance_timeslot.start_date <= DATETIME('now')
|
|
|
|
AND maintenance_timeslot.end_date >= DATETIME('now')
|
|
|
|
AND msp.status_page_id = ?
|
|
|
|
ORDER BY m.end_date
|
|
|
|
`, [ statusPageId ]));
|
2022-09-17 09:20:10 +01:00
|
|
|
|
|
|
|
for (const bean of maintenanceBeanList) {
|
|
|
|
publicMaintenanceList.push(await bean.toPublicJSON());
|
|
|
|
}
|
|
|
|
|
|
|
|
return publicMaintenanceList;
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2022-03-10 13:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = StatusPage;
|