From 942b55ca031b4a487d299c7e189052634fdb9c17 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Tue, 3 Jan 2023 21:45:55 +0800 Subject: [PATCH 1/6] Fix: Add support for maintenance in badges --- server/config.js | 1 + server/routers/api-router.js | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/server/config.js b/server/config.js index d46f24b75..398ddbb10 100644 --- a/server/config.js +++ b/server/config.js @@ -5,6 +5,7 @@ const badgeConstants = { naColor: "#999", defaultUpColor: "#66c20a", defaultDownColor: "#c2290a", + defaultMaintenanceColor: "#1747f5", defaultPingColor: "blue", // as defined by badge-maker / shields.io defaultStyle: "flat", defaultPingValueSuffix: "ms", diff --git a/server/routers/api-router.js b/server/routers/api-router.js index bbecbced3..45236d206 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -111,8 +111,10 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response label, upLabel = "Up", downLabel = "Down", + maintenanceLabel = "Maintenance", upColor = badgeConstants.defaultUpColor, downColor = badgeConstants.defaultDownColor, + maintenanceColor = badgeConstants.defaultMaintenanceColor, style = badgeConstants.defaultStyle, value, // for demo purpose only } = request.query; @@ -139,11 +141,39 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response badgeValues.color = badgeConstants.naColor; } else { const heartbeat = await Monitor.getPreviousHeartbeat(requestedMonitorId); - const state = overrideValue !== undefined ? overrideValue : heartbeat.status === 1; + const state = overrideValue !== undefined ? overrideValue : heartbeat.status; badgeValues.label = label ? label : ""; - badgeValues.color = state ? upColor : downColor; - badgeValues.message = label ?? state ? upLabel : downLabel; + switch (state) { + case 1: + badgeValues.color = upColor; + break; + case 3: + badgeValues.color = maintenanceColor; + break; + case 0: + badgeValues.color = downColor; + break; + default: + badgeValues.color = badgeConstants.naColor; + } + if (label !== undefined) { + badgeValues.message = label; + } else { + switch (state) { + case 1: + badgeValues.message = upLabel; + break; + case 3: + badgeValues.message = maintenanceLabel; + break; + case 0: + badgeValues.message = downLabel; + break; + default: + badgeValues.message = "N/A"; + } + } } // build the svg based on given values From dd82f36da3fa3b2852bd470e3f62374f27e8d8e2 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Mon, 9 Jan 2023 00:16:18 +0800 Subject: [PATCH 2/6] Fix: Improve syntax & fix weird label logic --- server/routers/api-router.js | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/server/routers/api-router.js b/server/routers/api-router.js index 45236d206..41608a049 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -143,36 +143,23 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response const heartbeat = await Monitor.getPreviousHeartbeat(requestedMonitorId); const state = overrideValue !== undefined ? overrideValue : heartbeat.status; - badgeValues.label = label ? label : ""; + badgeValues.label = label ?? ""; switch (state) { case 1: badgeValues.color = upColor; + badgeValues.message = upLabel; break; case 3: badgeValues.color = maintenanceColor; + badgeValues.message = maintenanceLabel; break; case 0: badgeValues.color = downColor; + badgeValues.message = downLabel; break; default: badgeValues.color = badgeConstants.naColor; - } - if (label !== undefined) { - badgeValues.message = label; - } else { - switch (state) { - case 1: - badgeValues.message = upLabel; - break; - case 3: - badgeValues.message = maintenanceLabel; - break; - case 0: - badgeValues.message = downLabel; - break; - default: - badgeValues.message = "N/A"; - } + badgeValues.message = "N/A"; } } From 21b418230cb76d970f40a81fd6cf53145f3d7ae9 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Tue, 10 Jan 2023 19:25:31 +0800 Subject: [PATCH 3/6] Chore: reorder cases Co-authored-by: Frank Elsinga --- server/routers/api-router.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/routers/api-router.js b/server/routers/api-router.js index 41608a049..f2e0874e2 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -145,6 +145,10 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response badgeValues.label = label ?? ""; switch (state) { + case 0: + badgeValues.color = downColor; + badgeValues.message = downLabel; + break; case 1: badgeValues.color = upColor; badgeValues.message = upLabel; @@ -153,10 +157,6 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response badgeValues.color = maintenanceColor; badgeValues.message = maintenanceLabel; break; - case 0: - badgeValues.color = downColor; - badgeValues.message = downLabel; - break; default: badgeValues.color = badgeConstants.naColor; badgeValues.message = "N/A"; From 0ed3dd5e4fd8e33ef2d1d579e1b9d882ce4f09c3 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Thu, 12 Jan 2023 04:14:46 +0800 Subject: [PATCH 4/6] Fix: Add support for pending in badges --- server/config.js | 1 + server/routers/api-router.js | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/server/config.js b/server/config.js index 398ddbb10..0523e7078 100644 --- a/server/config.js +++ b/server/config.js @@ -5,6 +5,7 @@ const badgeConstants = { naColor: "#999", defaultUpColor: "#66c20a", defaultDownColor: "#c2290a", + defaultPendingColor: "#f8a306", defaultMaintenanceColor: "#1747f5", defaultPingColor: "blue", // as defined by badge-maker / shields.io defaultStyle: "flat", diff --git a/server/routers/api-router.js b/server/routers/api-router.js index f2e0874e2..6001d58e0 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -111,9 +111,11 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response label, upLabel = "Up", downLabel = "Down", + pendingLabel = "Pending", maintenanceLabel = "Maintenance", upColor = badgeConstants.defaultUpColor, downColor = badgeConstants.defaultDownColor, + pendingColor = badgeConstants.defaultPendingColor, maintenanceColor = badgeConstants.defaultMaintenanceColor, style = badgeConstants.defaultStyle, value, // for demo purpose only @@ -153,6 +155,10 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response badgeValues.color = upColor; badgeValues.message = upLabel; break; + case 2: + badgeValues.color = pendingColor; + badgeValues.message = pendingLabel; + break; case 3: badgeValues.color = maintenanceColor; badgeValues.message = maintenanceLabel; From ddce8f0cb06016d20c4f1910693d5258f95ff275 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Sat, 28 Jan 2023 19:00:13 +0800 Subject: [PATCH 5/6] Fix plugin installation --- docker/alpine-base.dockerfile | 2 +- docker/debian-base.dockerfile | 2 +- server/plugins-manager.js | 37 ++++++++++++++++++----- server/socket-handlers/plugins-handler.js | 8 +++-- src/components/settings/Plugins.vue | 2 +- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/docker/alpine-base.dockerfile b/docker/alpine-base.dockerfile index 82bc7bb05..276d6e450 100644 --- a/docker/alpine-base.dockerfile +++ b/docker/alpine-base.dockerfile @@ -3,6 +3,6 @@ FROM node:16-alpine3.12 WORKDIR /app # Install apprise, iputils for non-root ping, setpriv -RUN apk add --no-cache iputils setpriv dumb-init python3 py3-cryptography py3-pip py3-six py3-yaml py3-click py3-markdown py3-requests py3-requests-oauthlib && \ +RUN apk add --no-cache iputils setpriv dumb-init python3 py3-cryptography py3-pip py3-six py3-yaml py3-click py3-markdown py3-requests py3-requests-oauthlib git && \ pip3 --no-cache-dir install apprise==1.2.1 && \ rm -rf /root/.cache diff --git a/docker/debian-base.dockerfile b/docker/debian-base.dockerfile index d94b4c7fe..026189c47 100644 --- a/docker/debian-base.dockerfile +++ b/docker/debian-base.dockerfile @@ -10,7 +10,7 @@ WORKDIR /app # Stupid python3 and python3-pip actually install a lot of useless things into Debian, specify --no-install-recommends to skip them, make the base even smaller than alpine! RUN apt update && \ apt --yes --no-install-recommends install python3 python3-pip python3-cryptography python3-six python3-yaml python3-click python3-markdown python3-requests python3-requests-oauthlib \ - sqlite3 iputils-ping util-linux dumb-init && \ + sqlite3 iputils-ping util-linux dumb-init git && \ pip3 --no-cache-dir install apprise==1.2.1 && \ rm -rf /var/lib/apt/lists/* && \ apt --yes autoremove diff --git a/server/plugins-manager.js b/server/plugins-manager.js index e48c53c89..674ab9691 100644 --- a/server/plugins-manager.js +++ b/server/plugins-manager.js @@ -72,6 +72,12 @@ class PluginsManager { * @param {string} name Directory name, also known as plugin unique name */ downloadPlugin(repoURL, name) { + if (fs.existsSync(this.pluginsDir + name)) { + log.info("plugin", "Plugin folder already exists? Removing..."); + fs.rmSync(this.pluginsDir + name, { + recursive: true + }); + } log.info("plugin", "Installing plugin: " + name + " " + repoURL); let result = Git.clone(repoURL, this.pluginsDir, name); log.info("plugin", "Install result: " + result); @@ -115,13 +121,19 @@ class PluginsManager { * @returns {Promise<[]>} */ async fetchPluginList() { - const res = await axios.get("https://uptime.kuma.pet/c/plugins.json"); - const list = res.data.pluginList; + let remotePluginList; + try { + const res = await axios.get("https://uptime.kuma.pet/c/plugins.json"); + remotePluginList = res.data.pluginList; + } catch (e) { + log.error("plugin", "Failed to fetch plugin list: " + e.message); + remotePluginList = []; + } for (let plugin of this.pluginList) { let find = false; // Try to merge - for (let remotePlugin of list) { + for (let remotePlugin of remotePluginList) { if (remotePlugin.name === plugin.info.name) { find = true; remotePlugin.installed = true; @@ -136,17 +148,17 @@ class PluginsManager { // Local plugin if (!find) { plugin.info.local = true; - list.push(plugin.info); + remotePluginList.push(plugin.info); } } // Sort Installed first, then sort by name - return list.sort((a, b) => { + return remotePluginList.sort((a, b) => { if (a.installed === b.installed) { - if ( a.fullName < b.fullName ) { + if (a.fullName < b.fullName) { return -1; } - if ( a.fullName > b.fullName ) { + if (a.fullName > b.fullName) { return 1; } return 0; @@ -191,15 +203,24 @@ class PluginWrapper { let indexFile = this.pluginDir + "/index.js"; let packageJSON = this.pluginDir + "/package.json"; + log.info("plugin", "Installing dependencies"); + if (fs.existsSync(indexFile)) { // Install dependencies - childProcess.execSync("npm install", { + let result = childProcess.spawnSync("npm", [ "install" ], { cwd: this.pluginDir, env: { + ...process.env, PLAYWRIGHT_BROWSERS_PATH: "../../browsers", // Special handling for read-browser-monitor } }); + if (result.stdout) { + log.info("plugin", "Install dependencies result: " + result.stdout.toString("utf-8")); + } else { + log.warn("plugin", "Install dependencies result: no output"); + } + this.pluginClass = require(path.join(process.cwd(), indexFile)); let pluginClassType = typeof this.pluginClass; diff --git a/server/socket-handlers/plugins-handler.js b/server/socket-handlers/plugins-handler.js index 4ee712c79..533da309b 100644 --- a/server/socket-handlers/plugins-handler.js +++ b/server/socket-handlers/plugins-handler.js @@ -1,5 +1,6 @@ const { checkLogin } = require("../util-server"); -const { PluginManager } = require("../plugins-manager"); +const { PluginsManager } = require("../plugins-manager"); +const { log } = require("../../src/util.js"); /** * Handlers for plugins @@ -15,7 +16,9 @@ module.exports.pluginsHandler = (socket, server) => { try { checkLogin(socket); - if (PluginManager.disable) { + log.debug("plugin", "PluginManager.disable: " + PluginsManager.disable); + + if (PluginsManager.disable) { throw new Error("Plugin Disabled: In order to enable plugin feature, you need to use the default data directory: ./data/"); } @@ -25,6 +28,7 @@ module.exports.pluginsHandler = (socket, server) => { pluginList, }); } catch (error) { + log.warn("plugin", "Error: " + error.message); callback({ ok: false, msg: error.message, diff --git a/src/components/settings/Plugins.vue b/src/components/settings/Plugins.vue index ca39e7adc..614034fcb 100644 --- a/src/components/settings/Plugins.vue +++ b/src/components/settings/Plugins.vue @@ -48,7 +48,7 @@ export default { this.remotePluginList = res.pluginList; this.remotePluginListMsg = ""; } else { - this.remotePluginListMsg = this.$t("loadingError") + " " + res.message; + this.remotePluginListMsg = this.$t("loadingError") + " " + res.msg; } }); } From 6828d337aee65a65490d66d32c26e37a72e09f71 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Mon, 30 Jan 2023 00:00:41 +0800 Subject: [PATCH 6/6] Disable HTTP(s) - Browser Engine Reason: Unfortunately, after some test, I found that Playwright requires a lot of libraries to be installed on the Linux host in order to start Chrome or Firefox. It will be hard to install, so I hide this feature for now. --- src/pages/EditMonitor.vue | 4 ++++ src/pages/Settings.vue | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 0f7fc4a58..0f7e86a3c 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -71,12 +71,16 @@ + + diff --git a/src/pages/Settings.vue b/src/pages/Settings.vue index 2b08d04ea..b034a5411 100644 --- a/src/pages/Settings.vue +++ b/src/pages/Settings.vue @@ -113,9 +113,12 @@ export default { backup: { title: this.$t("Backup"), }, + /* + Hidden for now: Unfortunately, after some test, I found that Playwright requires a lot of libraries to be installed on the Linux host in order to start Chrome or Firefox. + It will be hard to install, so I hide this feature for now. But it still accessible via URL: /settings/plugins. plugins: { title: this.$tc("plugin", 2), - }, + },*/ about: { title: this.$t("About"), },