2022-03-10 05:34:30 -08:00
|
|
|
const { BeanModel } = require("redbean-node/dist/bean-model");
|
|
|
|
const { R } = require("redbean-node");
|
2022-05-10 09:51:11 -07:00
|
|
|
const cheerio = require("cheerio");
|
2022-05-30 00:45:44 -07:00
|
|
|
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
2022-10-07 00:02:19 -07:00
|
|
|
const jsesc = require("jsesc");
|
2023-02-04 00:58:39 -08:00
|
|
|
const googleAnalytics = require("../google-analytics");
|
2024-07-31 08:58:13 -07:00
|
|
|
const { marked } = require("marked");
|
2022-03-10 05:34:30 -08:00
|
|
|
|
|
|
|
class StatusPage extends BeanModel {
|
|
|
|
|
2022-05-30 00:45:44 -07:00
|
|
|
/**
|
|
|
|
* Like this: { "test-uptime.kuma.pet": "default" }
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
2022-04-06 07:43:22 -07:00
|
|
|
static domainMappingList = { };
|
|
|
|
|
2022-05-30 00:45:44 -07:00
|
|
|
/**
|
2023-08-11 00:46:41 -07:00
|
|
|
* Handle responses to status page
|
|
|
|
* @param {Response} response Response object
|
|
|
|
* @param {string} indexHTML HTML to render
|
|
|
|
* @param {string} slug Status page slug
|
2024-03-15 07:02:55 -07:00
|
|
|
* @returns {Promise<void>}
|
2022-05-30 00:45:44 -07:00
|
|
|
*/
|
|
|
|
static async handleStatusPageResponse(response, indexHTML, slug) {
|
2023-12-10 11:05:13 -08:00
|
|
|
// 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 00:45:44 -07:00
|
|
|
let statusPage = await R.findOne("status_page", " slug = ? ", [
|
|
|
|
slug
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (statusPage) {
|
2022-05-31 22:05:12 -07:00
|
|
|
response.send(await StatusPage.renderHTML(indexHTML, statusPage));
|
2022-05-30 00:45:44 -07:00
|
|
|
} else {
|
|
|
|
response.status(404).send(UptimeKumaServer.getInstance().indexHTML);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 09:51:11 -07:00
|
|
|
/**
|
|
|
|
* SSR for status pages
|
2023-08-11 00:46:41 -07:00
|
|
|
* @param {string} indexHTML HTML page to render
|
|
|
|
* @param {StatusPage} statusPage Status page populate HTML with
|
2024-03-15 07:02:55 -07:00
|
|
|
* @returns {Promise<string>} the rendered html
|
2022-05-10 09:51:11 -07:00
|
|
|
*/
|
2022-05-31 22:05:12 -07:00
|
|
|
static async renderHTML(indexHTML, statusPage) {
|
2022-05-10 09:51:11 -07:00
|
|
|
const $ = cheerio.load(indexHTML);
|
2024-07-31 08:58:13 -07:00
|
|
|
|
|
|
|
const description155 = marked(statusPage.description ?? "")
|
|
|
|
.replace(/<[^>]+>/gm, "")
|
|
|
|
.trim()
|
|
|
|
.substring(0, 155);
|
2022-05-10 09:51:11 -07:00
|
|
|
|
2022-05-30 00:45:44 -07:00
|
|
|
$("title").text(statusPage.title);
|
2022-05-31 07:53:48 -07:00
|
|
|
$("meta[name=description]").attr("content", description155);
|
2022-05-30 00:45:44 -07:00
|
|
|
|
|
|
|
if (statusPage.icon) {
|
|
|
|
$("link[rel=icon]")
|
|
|
|
.attr("href", statusPage.icon)
|
|
|
|
.removeAttr("type");
|
2022-07-04 06:58:27 -07:00
|
|
|
|
|
|
|
$("link[rel=apple-touch-icon]").remove();
|
2022-05-30 00:45:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const head = $("head");
|
|
|
|
|
2023-01-22 08:59:09 -08:00
|
|
|
if (statusPage.googleAnalyticsTagId) {
|
2023-02-04 00:58:39 -08:00
|
|
|
let escapedGoogleAnalyticsScript = googleAnalytics.getGoogleAnalyticsScript(statusPage.googleAnalyticsTagId);
|
2023-01-22 08:59:09 -08:00
|
|
|
head.append($(escapedGoogleAnalyticsScript));
|
|
|
|
}
|
2023-01-10 12:25:45 -08:00
|
|
|
|
2022-05-31 07:53:48 -07:00
|
|
|
// OG Meta Tags
|
2023-02-10 01:29:32 -08:00
|
|
|
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 07:53:48 -07:00
|
|
|
|
2022-05-31 22:05:12 -07:00
|
|
|
// Preload data
|
2022-10-07 00:02:19 -07:00
|
|
|
// Add jsesc, fix https://github.com/louislam/uptime-kuma/issues/2186
|
2022-10-09 11:47:24 -07:00
|
|
|
const escapedJSONObject = jsesc(await StatusPage.getStatusPageData(statusPage), {
|
2022-10-07 00:02:19 -07:00
|
|
|
"isScriptContext": true
|
|
|
|
});
|
|
|
|
|
|
|
|
const script = $(`
|
|
|
|
<script id="preload-data" data-json="{}">
|
|
|
|
window.preloadData = ${escapedJSONObject};
|
2022-05-31 22:05:12 -07:00
|
|
|
</script>
|
|
|
|
`);
|
|
|
|
|
2022-10-07 00:02:19 -07:00
|
|
|
head.append(script);
|
|
|
|
|
2022-07-04 06:58:27 -07:00
|
|
|
// manifest.json
|
|
|
|
$("link[rel=manifest]").attr("href", `/api/status-page/${statusPage.slug}/manifest.json`);
|
|
|
|
|
2022-05-10 09:51:11 -07:00
|
|
|
return $.root().html();
|
|
|
|
}
|
|
|
|
|
2022-05-31 22:05:12 -07:00
|
|
|
/**
|
|
|
|
* Get all status page data in one call
|
2023-08-11 00:46:41 -07:00
|
|
|
* @param {StatusPage} statusPage Status page to get data for
|
|
|
|
* @returns {object} Status page data
|
2022-05-31 22:05:12 -07:00
|
|
|
*/
|
|
|
|
static async getStatusPageData(statusPage) {
|
2023-07-04 16:37:45 -07:00
|
|
|
const config = await statusPage.toPublicJSON();
|
|
|
|
|
2022-05-31 22:05:12 -07:00
|
|
|
// Incident
|
|
|
|
let incident = await R.findOne("incident", " pin = 1 AND active = 1 AND status_page_id = ? ", [
|
|
|
|
statusPage.id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (incident) {
|
|
|
|
incident = incident.toPublicJSON();
|
|
|
|
}
|
|
|
|
|
2022-10-11 05:56:48 -07:00
|
|
|
let maintenanceList = await StatusPage.getMaintenanceList(statusPage.id);
|
2022-09-17 01:20:10 -07:00
|
|
|
|
2022-05-31 22:05:12 -07: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) {
|
2023-07-04 16:37:45 -07:00
|
|
|
let monitorGroup = await groupBean.toPublicJSON(showTags, config?.showCertificateExpiry);
|
2022-05-31 22:05:12 -07:00
|
|
|
publicGroupList.push(monitorGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response
|
|
|
|
return {
|
2023-07-04 16:37:45 -07:00
|
|
|
config,
|
2022-05-31 22:05:12 -07:00
|
|
|
incident,
|
2022-09-17 01:20:10 -07:00
|
|
|
publicGroupList,
|
2022-10-11 05:56:48 -07:00
|
|
|
maintenanceList,
|
2022-05-31 22:05:12 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-06 07:43:22 -07:00
|
|
|
/**
|
2022-04-21 10:30:04 -07:00
|
|
|
* Loads domain mapping from DB
|
2022-04-06 07:43:22 -07:00
|
|
|
* Return object like this: { "test-uptime.kuma.pet": "default" }
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async loadDomainMappingList() {
|
2022-04-09 09:25:27 -07:00
|
|
|
StatusPage.domainMappingList = await R.getAssoc(`
|
2022-04-06 07:43:22 -07:00
|
|
|
SELECT domain, slug
|
|
|
|
FROM status_page, status_page_cname
|
|
|
|
WHERE status_page.id = status_page_cname.status_page_id
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:30:04 -07:00
|
|
|
/**
|
|
|
|
* Send status page list to client
|
|
|
|
* @param {Server} io io Socket server instance
|
|
|
|
* @param {Socket} socket Socket.io instance
|
2023-08-11 00:46:41 -07:00
|
|
|
* @returns {Promise<Bean[]>} Status page list
|
2022-04-21 10:30:04 -07:00
|
|
|
*/
|
2022-03-10 05:34:30 -08: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 10:30:04 -07:00
|
|
|
/**
|
|
|
|
* Update list of domain names
|
2023-08-11 00:46:41 -07:00
|
|
|
* @param {string[]} domainNameList List of status page domains
|
2022-04-21 10:30:04 -07:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2022-04-09 09:25:27 -07: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 10:30:04 -07:00
|
|
|
/**
|
|
|
|
* Get list of domain names
|
2023-08-11 00:46:41 -07:00
|
|
|
* @returns {object[]} List of status page domains
|
2022-04-21 10:30:04 -07:00
|
|
|
*/
|
2022-04-09 09:25:27 -07:00
|
|
|
getDomainNameList() {
|
2022-04-06 07:43:22 -07: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 10:30:04 -07:00
|
|
|
/**
|
2022-04-22 11:10:13 -07:00
|
|
|
* Return an object that ready to parse to JSON
|
2023-08-11 00:46:41 -07:00
|
|
|
* @returns {object} Object ready to parse
|
2022-04-21 10:30:04 -07:00
|
|
|
*/
|
2022-03-10 05:34:30 -08:00
|
|
|
async toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
slug: this.slug,
|
|
|
|
title: this.title,
|
2022-03-15 23:14:47 -07:00
|
|
|
description: this.description,
|
2022-03-17 01:42:26 -07:00
|
|
|
icon: this.getIcon(),
|
2022-03-10 05:34:30 -08:00
|
|
|
theme: this.theme,
|
2024-05-19 12:56:55 -07:00
|
|
|
autoRefreshInterval: this.autoRefreshInterval,
|
2022-03-10 05:34:30 -08:00
|
|
|
published: !!this.published,
|
|
|
|
showTags: !!this.show_tags,
|
2022-04-09 09:25:27 -07:00
|
|
|
domainNameList: this.getDomainNameList(),
|
2022-04-16 23:53:13 -07:00
|
|
|
customCSS: this.custom_css,
|
|
|
|
footerText: this.footer_text,
|
|
|
|
showPoweredBy: !!this.show_powered_by,
|
2023-01-10 12:25:45 -08:00
|
|
|
googleAnalyticsId: this.google_analytics_tag_id,
|
2023-07-04 16:37:45 -07:00
|
|
|
showCertificateExpiry: !!this.show_certificate_expiry,
|
2022-03-10 05:34:30 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:30:04 -07:00
|
|
|
/**
|
2022-04-22 11:10:13 -07:00
|
|
|
* Return an object that ready to parse to JSON for public
|
2022-04-21 10:30:04 -07:00
|
|
|
* Only show necessary data to public
|
2023-08-11 00:46:41 -07:00
|
|
|
* @returns {object} Object ready to parse
|
2022-04-21 10:30:04 -07:00
|
|
|
*/
|
2022-03-10 05:34:30 -08:00
|
|
|
async toPublicJSON() {
|
|
|
|
return {
|
|
|
|
slug: this.slug,
|
|
|
|
title: this.title,
|
2022-03-15 23:14:47 -07:00
|
|
|
description: this.description,
|
2022-03-17 01:42:26 -07:00
|
|
|
icon: this.getIcon(),
|
2024-05-19 12:56:55 -07:00
|
|
|
autoRefreshInterval: this.autoRefreshInterval,
|
2022-03-10 05:34:30 -08:00
|
|
|
theme: this.theme,
|
|
|
|
published: !!this.published,
|
|
|
|
showTags: !!this.show_tags,
|
2022-04-16 23:53:13 -07:00
|
|
|
customCSS: this.custom_css,
|
|
|
|
footerText: this.footer_text,
|
|
|
|
showPoweredBy: !!this.show_powered_by,
|
2023-01-10 12:25:45 -08:00
|
|
|
googleAnalyticsId: this.google_analytics_tag_id,
|
2023-07-04 16:37:45 -07:00
|
|
|
showCertificateExpiry: !!this.show_certificate_expiry,
|
2022-03-10 05:34:30 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:30:04 -07:00
|
|
|
/**
|
|
|
|
* Convert slug to status page ID
|
2023-08-11 00:46:41 -07:00
|
|
|
* @param {string} slug Status page slug
|
|
|
|
* @returns {Promise<number>} ID of status page
|
2022-04-21 10:30:04 -07:00
|
|
|
*/
|
2022-03-16 00:38:10 -07:00
|
|
|
static async slugToID(slug) {
|
|
|
|
return await R.getCell("SELECT id FROM status_page WHERE slug = ? ", [
|
|
|
|
slug
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-04-21 10:30:04 -07:00
|
|
|
/**
|
|
|
|
* Get path to the icon for the page
|
2023-08-11 00:46:41 -07:00
|
|
|
* @returns {string} Path
|
2022-04-21 10:30:04 -07:00
|
|
|
*/
|
2022-03-17 01:42:26 -07:00
|
|
|
getIcon() {
|
|
|
|
if (!this.icon) {
|
|
|
|
return "/icon.svg";
|
|
|
|
} else {
|
|
|
|
return this.icon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-17 01:20:10 -07:00
|
|
|
/**
|
|
|
|
* Get list of maintenances
|
|
|
|
* @param {number} statusPageId ID of status page to get maintenance for
|
2023-08-11 00:46:41 -07:00
|
|
|
* @returns {object} Object representing maintenances sanitized for public
|
2022-09-17 01:20:10 -07:00
|
|
|
*/
|
|
|
|
static async getMaintenanceList(statusPageId) {
|
|
|
|
try {
|
|
|
|
const publicMaintenanceList = [];
|
|
|
|
|
2023-03-31 06:34:05 -07: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());
|
|
|
|
}
|
2022-09-17 01:20:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return publicMaintenanceList;
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2022-03-10 05:34:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = StatusPage;
|