uptime-kuma/server/model/status_page.js

498 lines
15 KiB
JavaScript
Raw Normal View History

2022-03-10 13:34:30 +00:00
const { BeanModel } = require("redbean-node/dist/bean-model");
const { R } = require("redbean-node");
2022-05-10 16:51:11 +00:00
const cheerio = require("cheerio");
2022-05-30 07:45:44 +00:00
const { UptimeKumaServer } = require("../uptime-kuma-server");
2022-10-07 07:02:19 +00:00
const jsesc = require("jsesc");
2023-02-04 08:58:39 +00:00
const googleAnalytics = require("../google-analytics");
const { marked } = require("marked");
const { Feed } = require("feed");
const config = require("../config");
const { STATUS_PAGE_ALL_DOWN, STATUS_PAGE_ALL_UP, STATUS_PAGE_MAINTENANCE, STATUS_PAGE_PARTIAL_DOWN, UP, MAINTENANCE, DOWN } = require("../../src/util");
2022-03-10 13:34:30 +00:00
class StatusPage extends BeanModel {
2022-05-30 07:45:44 +00:00
/**
* Like this: { "test-uptime.kuma.pet": "default" }
* @type {{}}
*/
2022-04-06 14:43:22 +00:00
static domainMappingList = { };
2022-05-30 07:45:44 +00:00
/**
* Handle responses to RSS pages
* @param {Response} response Response object
* @param {string} slug Status page slug
* @returns {Promise<void>}
*/
static async handleStatusPageRSSResponse(response, slug) {
let statusPage = await R.findOne("status_page", " slug = ? ", [
slug
]);
if (statusPage) {
response.send(await StatusPage.renderRSS(statusPage, slug));
} else {
response.status(404).send(UptimeKumaServer.getInstance().indexHTML);
}
}
2022-05-30 07:45:44 +00:00
/**
* Handle responses to status page
* @param {Response} response Response object
* @param {string} indexHTML HTML to render
* @param {string} slug Status page slug
* @returns {Promise<void>}
2022-05-30 07:45:44 +00:00
*/
static async handleStatusPageResponse(response, indexHTML, slug) {
// Handle url with trailing slash (http://localhost:3001/status/)
// The slug comes from the route "/status/:slug". If the slug is empty, express converts it to "index.html"
if (slug === "index.html") {
slug = "default";
}
2022-05-30 07:45:44 +00:00
let statusPage = await R.findOne("status_page", " slug = ? ", [
slug
]);
if (statusPage) {
2022-06-01 05:05:12 +00:00
response.send(await StatusPage.renderHTML(indexHTML, statusPage));
2022-05-30 07:45:44 +00:00
} else {
response.status(404).send(UptimeKumaServer.getInstance().indexHTML);
}
}
/**
* SSR for RSS feed
* @param {statusPage} statusPage object
* @param {slug} slug from router
* @returns {Promise<string>} the rendered html
*/
static async renderRSS(statusPage, slug) {
const { heartbeats, statusDescription } = await StatusPage.getRSSPageData(statusPage);
let proto = config.isSSL ? "https" : "http";
let host = `${proto}://${config.hostname || "localhost"}:${config.port}/status/${slug}`;
const feed = new Feed({
title: "uptime kuma rss feed",
description: `current status: ${statusDescription}`,
link: host,
language: "en", // optional, used only in RSS 2.0, possible values: http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes
updated: new Date(), // optional, default = today
});
heartbeats.forEach(heartbeat => {
feed.addItem({
title: `${heartbeat.name} is down`,
description: `${heartbeat.name} has been down since ${heartbeat.time}`,
id: heartbeat.monitorID,
date: new Date(heartbeat.time),
});
});
return feed.rss2();
}
2022-05-10 16:51:11 +00:00
/**
* SSR for status pages
* @param {string} indexHTML HTML page to render
* @param {StatusPage} statusPage Status page populate HTML with
* @returns {Promise<string>} the rendered html
2022-05-10 16:51:11 +00:00
*/
2022-06-01 05:05:12 +00:00
static async renderHTML(indexHTML, statusPage) {
2022-05-10 16:51:11 +00:00
const $ = cheerio.load(indexHTML);
const description155 = marked(statusPage.description ?? "")
.replace(/<[^>]+>/gm, "")
.trim()
.substring(0, 155);
2022-05-10 16:51:11 +00:00
2022-05-30 07:45:44 +00:00
$("title").text(statusPage.title);
2022-05-31 14:53:48 +00:00
$("meta[name=description]").attr("content", description155);
2022-05-30 07:45:44 +00:00
if (statusPage.icon) {
$("link[rel=icon]")
.attr("href", statusPage.icon)
.removeAttr("type");
$("link[rel=apple-touch-icon]").remove();
2022-05-30 07:45:44 +00:00
}
const head = $("head");
if (statusPage.googleAnalyticsTagId) {
2023-02-04 08:58:39 +00:00
let escapedGoogleAnalyticsScript = googleAnalytics.getGoogleAnalyticsScript(statusPage.googleAnalyticsTagId);
head.append($(escapedGoogleAnalyticsScript));
}
2022-05-31 14:53:48 +00:00
// OG Meta Tags
let ogTitle = $("<meta property=\"og:title\" content=\"\" />").attr("content", statusPage.title);
head.append(ogTitle);
let ogDescription = $("<meta property=\"og:description\" content=\"\" />").attr("content", description155);
head.append(ogDescription);
2022-05-31 14:53:48 +00:00
2022-06-01 05:05:12 +00:00
// Preload data
2022-10-07 07:02:19 +00:00
// Add jsesc, fix https://github.com/louislam/uptime-kuma/issues/2186
2022-10-09 18:47:24 +00:00
const escapedJSONObject = jsesc(await StatusPage.getStatusPageData(statusPage), {
2022-10-07 07:02:19 +00:00
"isScriptContext": true
});
const script = $(`
<script id="preload-data" data-json="{}">
window.preloadData = ${escapedJSONObject};
2022-06-01 05:05:12 +00:00
</script>
`);
2022-10-07 07:02:19 +00:00
head.append(script);
// manifest.json
$("link[rel=manifest]").attr("href", `/api/status-page/${statusPage.slug}/manifest.json`);
2022-05-10 16:51:11 +00:00
return $.root().html();
}
/**
* @param {heartbeats} heartbeats from getRSSPageData
* @returns {number} status_page constant from util.ts
*/
static overallStatus(heartbeats) {
if (heartbeats.length === 0) {
return -1;
}
let status = STATUS_PAGE_ALL_UP;
let hasUp = false;
for (let beat of heartbeats) {
if (beat.status === MAINTENANCE) {
return STATUS_PAGE_MAINTENANCE;
} else if (beat.status === UP) {
hasUp = true;
} else {
status = STATUS_PAGE_PARTIAL_DOWN;
}
}
if (! hasUp) {
status = STATUS_PAGE_ALL_DOWN;
}
return status;
}
/**
* @param {number} status from overallStatus
* @returns {string} description
*/
static getStatusDescription(status) {
if (status === -1) {
return "No Services";
}
if (status === STATUS_PAGE_ALL_UP) {
return "All Systems Operational";
}
if (status === STATUS_PAGE_PARTIAL_DOWN) {
return "Partially Degraded Service";
}
if (status === STATUS_PAGE_ALL_DOWN) {
return "Degraded Service";
}
// TODO: show the real maintenance information: title, description, time
if (status === MAINTENANCE) {
return "Under maintenance";
}
return "?";
}
/**
* Get all data required for RSS
* @param {StatusPage} statusPage Status page to get data for
* @returns {object} Status page data
*/
static async getRSSPageData(statusPage) {
// get all heartbeats that correspond to this statusPage
const config = await statusPage.toPublicJSON();
// Public Group List
const showTags = !!statusPage.show_tags;
const list = await R.find("group", " public = 1 AND status_page_id = ? ORDER BY weight ", [
statusPage.id
]);
let heartbeats = [];
for (let groupBean of list) {
let monitorGroup = await groupBean.toPublicJSON(showTags, config?.showCertificateExpiry);
for (const monitor of monitorGroup.monitorList) {
const heartbeat = await R.findOne("heartbeat", "monitor_id = ? ORDER BY time DESC", [ monitor.id ]);
if (heartbeat) {
heartbeats.push({
...monitor,
status: heartbeat.status,
time: heartbeat.time
});
}
}
}
// calculate RSS feed description
let status = StatusPage.overallStatus(heartbeats);
let statusDescription = StatusPage.getStatusDescription(status);
// keep only DOWN heartbeats in the RSS feed
heartbeats = heartbeats.filter(heartbeat => heartbeat.status === DOWN);
return {
heartbeats,
statusDescription
};
}
2022-06-01 05:05:12 +00:00
/**
* Get all status page data in one call
* @param {StatusPage} statusPage
* @param {boolean} [includeStatus = false] whether each monitor should include the status of the monitor ("up" or "down")
* @param {boolean} [includeConfig = true] whether the config for the status paghe should be included in the returned JSON
2022-06-01 05:05:12 +00:00
*/
2022-07-26 11:47:23 +00:00
static async getStatusPageData(statusPage, includeStatus = false, includeConfig = true) {
2022-06-01 05:05:12 +00:00
// Incident
let incident = await R.findOne("incident", " pin = 1 AND active = 1 AND status_page_id = ? ", [
statusPage.id,
]);
if (incident) {
incident = incident.toPublicJSON();
}
let maintenanceList = await StatusPage.getMaintenanceList(statusPage.id);
2022-06-01 05:05:12 +00: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) {
2022-07-19 22:11:15 +00:00
let monitorGroup = await groupBean.toPublicJSON(showTags, includeStatus);
2022-06-01 05:05:12 +00:00
publicGroupList.push(monitorGroup);
}
2022-07-26 11:47:23 +00:00
let config = {};
if (includeConfig) {
config = {
config: await statusPage.toPublicJSON()
2022-07-28 10:44:06 +00:00
};
2022-07-26 11:47:23 +00:00
}
2022-06-01 05:05:12 +00:00
// Response
return {
2022-07-26 11:47:23 +00:00
...config,
2022-06-01 05:05:12 +00:00
incident,
publicGroupList,
maintenanceList,
2022-06-01 05:05:12 +00:00
};
}
2022-04-06 14:43:22 +00:00
/**
* Loads domain mapping from DB
2022-04-06 14:43:22 +00:00
* Return object like this: { "test-uptime.kuma.pet": "default" }
* @returns {Promise<void>}
*/
static async loadDomainMappingList() {
2022-04-09 16:25:27 +00:00
StatusPage.domainMappingList = await R.getAssoc(`
2022-04-06 14:43:22 +00:00
SELECT domain, slug
FROM status_page, status_page_cname
WHERE status_page.id = status_page_cname.status_page_id
`);
}
/**
* Send status page list to client
* @param {Server} io io Socket server instance
* @param {Socket} socket Socket.io instance
* @returns {Promise<Bean[]>} Status page list
*/
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;
}
/**
* Update list of domain names
* @param {string[]} domainNameList List of status page domains
* @returns {Promise<void>}
*/
2022-04-09 16:25:27 +00: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;
}
}
/**
* Get list of domain names
* @returns {object[]} List of status page domains
*/
2022-04-09 16:25:27 +00:00
getDomainNameList() {
2022-04-06 14:43:22 +00:00
let domainList = [];
for (let domain in StatusPage.domainMappingList) {
let s = StatusPage.domainMappingList[domain];
if (this.slug === s) {
domainList.push(domain);
}
}
return domainList;
}
/**
* Return an object that ready to parse to JSON
* @returns {object} Object ready to parse
*/
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,
icon: this.getIcon(),
2022-03-10 13:34:30 +00:00
theme: this.theme,
autoRefreshInterval: this.autoRefreshInterval,
2022-03-10 13:34:30 +00:00
published: !!this.published,
showTags: !!this.show_tags,
2022-04-09 16:25:27 +00:00
domainNameList: this.getDomainNameList(),
customCSS: this.custom_css,
footerText: this.footer_text,
showPoweredBy: !!this.show_powered_by,
googleAnalyticsId: this.google_analytics_tag_id,
2023-07-04 23:37:45 +00:00
showCertificateExpiry: !!this.show_certificate_expiry,
2022-03-10 13:34:30 +00:00
};
}
/**
* Return an object that ready to parse to JSON for public
* Only show necessary data to public
* @returns {object} Object ready to parse
*/
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,
icon: this.getIcon(),
autoRefreshInterval: this.autoRefreshInterval,
2022-03-10 13:34:30 +00:00
theme: this.theme,
published: !!this.published,
showTags: !!this.show_tags,
customCSS: this.custom_css,
footerText: this.footer_text,
showPoweredBy: !!this.show_powered_by,
googleAnalyticsId: this.google_analytics_tag_id,
2023-07-04 23:37:45 +00:00
showCertificateExpiry: !!this.show_certificate_expiry,
2022-03-10 13:34:30 +00:00
};
}
/**
* Convert slug to status page ID
* @param {string} slug Status page slug
* @returns {Promise<number>} ID of status page
*/
static async slugToID(slug) {
return await R.getCell("SELECT id FROM status_page WHERE slug = ? ", [
slug
]);
}
/**
* Get path to the icon for the page
* @returns {string} Path
*/
getIcon() {
if (!this.icon) {
return "/icon.svg";
} else {
return this.icon;
}
}
/**
* 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 = [];
2023-03-31 13:34:05 +00:00
let maintenanceIDList = await R.getCol(`
SELECT DISTINCT maintenance_id
FROM maintenance_status_page
WHERE status_page_id = ?
`, [ statusPageId ]);
for (const maintenanceID of maintenanceIDList) {
let maintenance = UptimeKumaServer.getInstance().getMaintenance(maintenanceID);
if (maintenance && await maintenance.isUnderMaintenance()) {
publicMaintenanceList.push(await maintenance.toPublicJSON());
}
}
return publicMaintenanceList;
} catch (error) {
return [];
}
}
2022-03-10 13:34:30 +00:00
}
module.exports = StatusPage;