diff --git a/server/database.js b/server/database.js index 7af75423a..4c7e53648 100644 --- a/server/database.js +++ b/server/database.js @@ -3,11 +3,25 @@ const { R } = require("redbean-node"); const { setSetting, setting } = require("./util-server"); const { debug, sleep } = require("../src/util"); const dayjs = require("dayjs"); +const knex = require("knex"); +/** + * Database & App Data Folder + */ class Database { static templatePath = "./db/kuma.db"; + + /** + * Data Dir (Default: ./data) + */ static dataDir; + + /** + * User Upload Dir (Default: ./data/upload) + */ + static uploadDir; + static path; /** @@ -31,6 +45,11 @@ class Database { "patch-setting-value-type.sql": true, "patch-improve-performance.sql": true, "patch-2fa.sql": true, + "patch-add-retry-interval-monitor.sql": true, + "patch-incident-table.sql": true, + "patch-group-table.sql": true, + "patch-monitor-push_token.sql": true, + "patch-http-monitor-method-body-and-headers.sql": true, } /** @@ -41,29 +60,56 @@ class Database { static noReject = true; + static init(args) { + // Data Directory (must be end with "/") + Database.dataDir = process.env.DATA_DIR || args["data-dir"] || "./data/"; + Database.path = Database.dataDir + "kuma.db"; + if (! fs.existsSync(Database.dataDir)) { + fs.mkdirSync(Database.dataDir, { recursive: true }); + } + + Database.uploadDir = Database.dataDir + "upload/"; + + if (! fs.existsSync(Database.uploadDir)) { + fs.mkdirSync(Database.uploadDir, { recursive: true }); + } + + console.log(`Data Dir: ${Database.dataDir}`); + } + static async connect() { const acquireConnectionTimeout = 120 * 1000; - R.setup("sqlite", { - filename: Database.path, + const Dialect = require("knex/lib/dialects/sqlite3/index.js"); + Dialect.prototype._driver = () => require("@louislam/sqlite3"); + + const knexInstance = knex({ + client: Dialect, + connection: { + filename: Database.path, + acquireConnectionTimeout: acquireConnectionTimeout, + }, useNullAsDefault: true, - acquireConnectionTimeout: acquireConnectionTimeout, - }, { - min: 1, - max: 1, - idleTimeoutMillis: 120 * 1000, - propagateCreateError: false, - acquireTimeoutMillis: acquireConnectionTimeout, + pool: { + min: 1, + max: 1, + idleTimeoutMillis: 120 * 1000, + propagateCreateError: false, + acquireTimeoutMillis: acquireConnectionTimeout, + } }); + R.setup(knexInstance); + if (process.env.SQL_LOG === "1") { R.debug(true); } // Auto map the model to a bean object - R.freeze(true) + R.freeze(true); await R.autoloadModels("./server/model"); + await R.exec("PRAGMA foreign_keys = ON"); // Change to WAL await R.exec("PRAGMA journal_mode = WAL"); await R.exec("PRAGMA cache_size = -12000"); @@ -71,6 +117,7 @@ class Database { console.log("SQLite config:"); console.log(await R.getAll("PRAGMA journal_mode")); console.log(await R.getAll("PRAGMA cache_size")); + console.log("SQLite Version: " + await R.getCell("SELECT sqlite_version()")); } static async patch() { @@ -84,11 +131,11 @@ class Database { console.info("Latest database version: " + this.latestVersion); if (version === this.latestVersion) { - console.info("Database no need to patch"); + console.info("Database patch not needed"); } else if (version > this.latestVersion) { console.info("Warning: Database version is newer than expected"); } else { - console.info("Database patch is needed") + console.info("Database patch is needed"); this.backup(version); @@ -103,11 +150,12 @@ class Database { } } catch (ex) { await Database.close(); - this.restore(); - console.error(ex) - console.error("Start Uptime-Kuma failed due to patch db failed") - console.error("Please submit the bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues") + console.error(ex); + console.error("Start Uptime-Kuma failed due to issue patching the database"); + console.error("Please submit a bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues"); + + this.restore(); process.exit(1); } } @@ -132,7 +180,7 @@ class Database { try { for (let sqlFilename in this.patchList) { - await this.patch2Recursion(sqlFilename, databasePatchedFiles) + await this.patch2Recursion(sqlFilename, databasePatchedFiles); } if (this.patched) { @@ -141,11 +189,13 @@ class Database { } catch (ex) { await Database.close(); + + console.error(ex); + console.error("Start Uptime-Kuma failed due to issue patching the database"); + console.error("Please submit the bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues"); + this.restore(); - console.error(ex) - console.error("Start Uptime-Kuma failed due to patch db failed"); - console.error("Please submit the bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues"); process.exit(1); } @@ -182,10 +232,10 @@ class Database { this.patched = true; await this.importSQLFile("./db/" + sqlFilename); databasePatchedFiles[sqlFilename] = true; - console.log(sqlFilename + " is patched successfully"); + console.log(sqlFilename + " was patched successfully"); } else { - console.log(sqlFilename + " is already patched, skip"); + debug(sqlFilename + " is already patched, skip"); } } @@ -203,12 +253,12 @@ class Database { // Remove all comments (--) let lines = text.split("\n"); lines = lines.filter((line) => { - return ! line.startsWith("--") + return ! line.startsWith("--"); }); // Split statements by semicolon // Filter out empty line - text = lines.join("\n") + text = lines.join("\n"); let statements = text.split(";") .map((statement) => { @@ -216,7 +266,7 @@ class Database { }) .filter((statement) => { return statement !== ""; - }) + }); for (let statement of statements) { await R.exec(statement); @@ -237,7 +287,7 @@ class Database { }; process.addListener("unhandledRejection", listener); - console.log("Closing DB"); + console.log("Closing the database"); while (true) { Database.noReject = true; @@ -247,7 +297,7 @@ class Database { if (Database.noReject) { break; } else { - console.log("Waiting to close the db"); + console.log("Waiting to close the database"); } } console.log("SQLite closed"); @@ -262,7 +312,7 @@ class Database { */ static backup(version) { if (! this.backupPath) { - console.info("Backup the db") + console.info("Backup the database"); this.backupPath = this.dataDir + "kuma.db.bak" + version; fs.copyFileSync(Database.path, this.backupPath); @@ -285,7 +335,7 @@ class Database { */ static restore() { if (this.backupPath) { - console.error("Patch db failed!!! Restoring the backup"); + console.error("Patching the database failed!!! Restoring the backup"); const shmPath = Database.path + "-shm"; const walPath = Database.path + "-wal"; @@ -304,7 +354,7 @@ class Database { fs.unlinkSync(walPath); } } catch (e) { - console.log("Restore failed, you may need to restore the backup manually"); + console.log("Restore failed; you may need to restore the backup manually"); process.exit(1); } diff --git a/server/server.js b/server/server.js index 8448b3c8a..329484f56 100644 --- a/server/server.js +++ b/server/server.js @@ -1,14 +1,25 @@ console.log("Welcome to Uptime Kuma"); +const args = require("args-parser")(process.argv); +const { sleep, debug, getRandomInt, genSecret } = require("../src/util"); + +debug(args); + +if (! process.env.NODE_ENV) { + process.env.NODE_ENV = "production"; +} + +// Demo Mode? +const demoMode = args["demo"] || false; +exports.demoMode = demoMode; + console.log("Node Env: " + process.env.NODE_ENV); -const { sleep, debug, TimeLogger, getRandomInt } = require("../src/util"); - -console.log("Importing Node libraries") +console.log("Importing Node libraries"); const fs = require("fs"); const http = require("http"); const https = require("https"); -console.log("Importing 3rd-party libraries") +console.log("Importing 3rd-party libraries"); debug("Importing express"); const express = require("express"); debug("Importing socket.io"); @@ -21,6 +32,8 @@ debug("Importing http-graceful-shutdown"); const gracefulShutdown = require("http-graceful-shutdown"); debug("Importing prometheus-api-metrics"); const prometheusAPIMetrics = require("prometheus-api-metrics"); +debug("Importing compare-versions"); +const compareVersions = require("compare-versions"); debug("Importing 2FA Modules"); const notp = require("notp"); @@ -30,7 +43,7 @@ console.log("Importing this project modules"); debug("Importing Monitor"); const Monitor = require("./model/monitor"); debug("Importing Settings"); -const { getSettings, setSettings, setting, initJWTSecret, genSecret } = require("./util-server"); +const { getSettings, setSettings, setting, initJWTSecret, checkLogin, startUnitTest, FBSD } = require("./util-server"); debug("Importing Notification"); const { Notification } = require("./notification"); @@ -43,36 +56,39 @@ const { basicAuth } = require("./auth"); const { login } = require("./auth"); const passwordHash = require("./password-hash"); -const args = require("args-parser")(process.argv); - const checkVersion = require("./check-version"); console.info("Version: " + checkVersion.version); // If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available and the unspecified IPv4 address (0.0.0.0) otherwise. // Dual-stack support for (::) -const hostname = process.env.HOST || args.host; -const port = parseInt(process.env.PORT || args.port || 3001); +let hostname = process.env.UPTIME_KUMA_HOST || args.host; + +// Also read HOST if not FreeBSD, as HOST is a system environment variable in FreeBSD +if (!hostname && !FBSD) { + hostname = process.env.HOST; +} + +if (hostname) { + console.log("Custom hostname: " + hostname); +} + +const port = parseInt(process.env.UPTIME_KUMA_PORT || process.env.PORT || args.port || 3001); // SSL -const sslKey = process.env.SSL_KEY || args["ssl-key"] || undefined; -const sslCert = process.env.SSL_CERT || args["ssl-cert"] || undefined; +const sslKey = process.env.UPTIME_KUMA_SSL_KEY || process.env.SSL_KEY || args["ssl-key"] || undefined; +const sslCert = process.env.UPTIME_KUMA_SSL_CERT || process.env.SSL_CERT || args["ssl-cert"] || undefined; -// Demo Mode? -const demoMode = args["demo"] || false; +/** + * Run unit test after the server is ready + * @type {boolean} + */ +const testMode = !!args["test"] || false; if (demoMode) { console.log("==== Demo Mode ===="); } -// Data Directory (must be end with "/") -Database.dataDir = process.env.DATA_DIR || args["data-dir"] || "./data/"; -Database.path = Database.dataDir + "kuma.db"; -if (! fs.existsSync(Database.dataDir)) { - fs.mkdirSync(Database.dataDir, { recursive: true }); -} -console.log(`Data Dir: ${Database.dataDir}`); - -console.log("Creating express and socket.io instance") +console.log("Creating express and socket.io instance"); const app = express(); let server; @@ -92,7 +108,8 @@ const io = new Server(server); module.exports.io = io; // Must be after io instantiation -const { sendNotificationList, sendHeartbeatList, sendImportantHeartbeatList } = require("./client"); +const { sendNotificationList, sendHeartbeatList, sendImportantHeartbeatList, sendInfo } = require("./client"); +const { statusPageSocketHandler } = require("./socket-handlers/status-page-socket-handler"); app.use(express.json()); @@ -126,12 +143,19 @@ let needSetup = false; */ let indexHTML = fs.readFileSync("./dist/index.html").toString(); +exports.entryPage = "dashboard"; + (async () => { + Database.init(args); await initDatabase(); - console.log("Adding route") + exports.entryPage = await setting("entryPage"); + console.log("Adding route"); + + // *************************** // Normal Router here + // *************************** // Robots.txt app.get("/robots.txt", async (_request, response) => { @@ -151,28 +175,36 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); app.use("/", express.static("dist")); + // ./data/upload + app.use("/upload", express.static(Database.uploadDir)); + app.get("/.well-known/change-password", async (_, response) => { response.redirect("https://github.com/louislam/uptime-kuma/wiki/Reset-Password-via-CLI"); }); - // Universal Route Handler, must be at the end + // API Router + const apiRouter = require("./routers/api-router"); + app.use(apiRouter); + + // Universal Route Handler, must be at the end of all express route. app.get("*", async (_request, response) => { - response.send(indexHTML); + if (_request.originalUrl.startsWith("/upload/")) { + response.status(404).send("File not found."); + } else { + response.send(indexHTML); + } }); - console.log("Adding socket handler") + console.log("Adding socket handler"); io.on("connection", async (socket) => { - socket.emit("info", { - version: checkVersion.version, - latestVersion: checkVersion.latestVersion, - }) + sendInfo(socket); totalClient++; if (needSetup) { - console.log("Redirect to setup page") - socket.emit("setup") + console.log("Redirect to setup page"); + socket.emit("setup"); } socket.on("disconnect", () => { @@ -180,7 +212,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); }); // *************************** - // Public API + // Public Socket API // *************************** socket.on("loginByToken", async (token, callback) => { @@ -188,44 +220,44 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); try { let decoded = jwt.verify(token, jwtSecret); - console.log("Username from JWT: " + decoded.username) + console.log("Username from JWT: " + decoded.username); let user = await R.findOne("user", " username = ? AND active = 1 ", [ decoded.username, - ]) + ]); if (user) { - debug("afterLogin") + debug("afterLogin"); - afterLogin(socket, user) + afterLogin(socket, user); - debug("afterLogin ok") + debug("afterLogin ok"); callback({ ok: true, - }) + }); } else { callback({ ok: false, msg: "The user is inactive or deleted.", - }) + }); } } catch (error) { callback({ ok: false, msg: "Invalid token.", - }) + }); } }); socket.on("login", async (data, callback) => { - console.log("Login") + console.log("Login"); - let user = await login(data.username, data.password) + let user = await login(data.username, data.password); if (user) { - afterLogin(socket, user) + afterLogin(socket, user); if (user.twofaStatus == 0) { callback({ @@ -233,13 +265,13 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); token: jwt.sign({ username: data.username, }, jwtSecret), - }) + }); } if (user.twofaStatus == 1 && !data.token) { callback({ tokenRequired: true, - }) + }); } if (data.token) { @@ -251,40 +283,46 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); token: jwt.sign({ username: data.username, }, jwtSecret), - }) + }); } else { callback({ ok: false, msg: "Invalid Token!", - }) + }); } } } else { callback({ ok: false, msg: "Incorrect username or password.", - }) + }); } }); socket.on("logout", async (callback) => { - socket.leave(socket.userID) + socket.leave(socket.userID); socket.userID = null; callback(); }); socket.on("prepare2FA", async (callback) => { try { - checkLogin(socket) + checkLogin(socket); let user = await R.findOne("user", " id = ? AND active = 1 ", [ socket.userID, - ]) + ]); if (user.twofa_status == 0) { - let newSecret = await genSecret() + let newSecret = await genSecret(); let encodedSecret = base32.encode(newSecret); + + // Google authenticator doesn't like equal signs + // The fix is found at https://github.com/guyht/notp + // Related issue: https://github.com/louislam/uptime-kuma/issues/486 + encodedSecret = encodedSecret.toString().replace(/=/g, ""); + let uri = `otpauth://totp/Uptime%20Kuma:${user.username}?secret=${encodedSecret}`; await R.exec("UPDATE `user` SET twofa_secret = ? WHERE id = ? ", [ @@ -295,24 +333,24 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); callback({ ok: true, uri: uri, - }) + }); } else { callback({ ok: false, msg: "2FA is already enabled.", - }) + }); } } catch (error) { callback({ ok: false, msg: "Error while trying to prepare 2FA.", - }) + }); } }); socket.on("save2FA", async (callback) => { try { - checkLogin(socket) + checkLogin(socket); await R.exec("UPDATE `user` SET twofa_status = 1 WHERE id = ? ", [ socket.userID, @@ -321,18 +359,18 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); callback({ ok: true, msg: "2FA Enabled.", - }) + }); } catch (error) { callback({ ok: false, msg: "Error while trying to change 2FA.", - }) + }); } }); socket.on("disable2FA", async (callback) => { try { - checkLogin(socket) + checkLogin(socket); await R.exec("UPDATE `user` SET twofa_status = 0 WHERE id = ? ", [ socket.userID, @@ -341,19 +379,19 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); callback({ ok: true, msg: "2FA Disabled.", - }) + }); } catch (error) { callback({ ok: false, msg: "Error while trying to change 2FA.", - }) + }); } }); socket.on("verifyToken", async (token, callback) => { let user = await R.findOne("user", " id = ? AND active = 1 ", [ socket.userID, - ]) + ]); let verify = notp.totp.verify(token, user.twofa_secret); @@ -361,40 +399,40 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); callback({ ok: true, valid: true, - }) + }); } else { callback({ ok: false, msg: "Invalid Token.", valid: false, - }) + }); } }); socket.on("twoFAStatus", async (callback) => { - checkLogin(socket) + checkLogin(socket); try { let user = await R.findOne("user", " id = ? AND active = 1 ", [ socket.userID, - ]) + ]); if (user.twofa_status == 1) { callback({ ok: true, status: true, - }) + }); } else { callback({ ok: true, status: false, - }) + }); } } catch (error) { callback({ ok: false, msg: "Error while trying to get 2FA status.", - }) + }); } }); @@ -405,13 +443,13 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("setup", async (username, password, callback) => { try { if ((await R.count("user")) !== 0) { - throw new Error("Uptime Kuma has been setup. If you want to setup again, please delete the database.") + throw new Error("Uptime Kuma has been set up. If you want to setup again, please delete the database."); } - let user = R.dispense("user") + let user = R.dispense("user"); user.username = username; - user.password = passwordHash.generate(password) - await R.store(user) + user.password = passwordHash.generate(password); + await R.store(user); needSetup = false; @@ -435,8 +473,8 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); // Add a new monitor socket.on("add", async (monitor, callback) => { try { - checkLogin(socket) - let bean = R.dispense("monitor") + checkLogin(socket); + let bean = R.dispense("monitor"); let notificationIDList = monitor.notificationIDList; delete monitor.notificationIDList; @@ -444,11 +482,11 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); monitor.accepted_statuscodes_json = JSON.stringify(monitor.accepted_statuscodes); delete monitor.accepted_statuscodes; - bean.import(monitor) - bean.user_id = socket.userID - await R.store(bean) + bean.import(monitor); + bean.user_id = socket.userID; + await R.store(bean); - await updateMonitorNotification(bean.id, notificationIDList) + await updateMonitorNotification(bean.id, notificationIDList); await startMonitor(socket.userID, bean.id); await sendMonitorList(socket); @@ -470,18 +508,22 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); // Edit a monitor socket.on("editMonitor", async (monitor, callback) => { try { - checkLogin(socket) + checkLogin(socket); - let bean = await R.findOne("monitor", " id = ? ", [ monitor.id ]) + let bean = await R.findOne("monitor", " id = ? ", [ monitor.id ]); if (bean.user_id !== socket.userID) { - throw new Error("Permission denied.") + throw new Error("Permission denied."); } - bean.name = monitor.name - bean.type = monitor.type - bean.url = monitor.url - bean.interval = monitor.interval + bean.name = monitor.name; + bean.type = monitor.type; + bean.url = monitor.url; + bean.method = monitor.method; + bean.body = monitor.body; + bean.headers = monitor.headers; + bean.interval = monitor.interval; + bean.retryInterval = monitor.retryInterval; bean.hostname = monitor.hostname; bean.maxretries = monitor.maxretries; bean.port = monitor.port; @@ -492,13 +534,14 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); bean.accepted_statuscodes_json = JSON.stringify(monitor.accepted_statuscodes); bean.dns_resolve_type = monitor.dns_resolve_type; bean.dns_resolve_server = monitor.dns_resolve_server; + bean.pushToken = monitor.pushToken; - await R.store(bean) + await R.store(bean); - await updateMonitorNotification(bean.id, monitor.notificationIDList) + await updateMonitorNotification(bean.id, monitor.notificationIDList); if (bean.active) { - await restartMonitor(socket.userID, bean.id) + await restartMonitor(socket.userID, bean.id); } await sendMonitorList(socket); @@ -510,7 +553,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); }); } catch (e) { - console.error(e) + console.error(e); callback({ ok: false, msg: e.message, @@ -520,13 +563,13 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("getMonitorList", async (callback) => { try { - checkLogin(socket) + checkLogin(socket); await sendMonitorList(socket); callback({ ok: true, }); } catch (e) { - console.error(e) + console.error(e); callback({ ok: false, msg: e.message, @@ -536,14 +579,14 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("getMonitor", async (monitorID, callback) => { try { - checkLogin(socket) + checkLogin(socket); - console.log(`Get Monitor: ${monitorID} User ID: ${socket.userID}`) + console.log(`Get Monitor: ${monitorID} User ID: ${socket.userID}`); let bean = await R.findOne("monitor", " id = ? AND user_id = ? ", [ monitorID, socket.userID, - ]) + ]); callback({ ok: true, @@ -561,7 +604,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); // Start or Resume the monitor socket.on("resumeMonitor", async (monitorID, callback) => { try { - checkLogin(socket) + checkLogin(socket); await startMonitor(socket.userID, monitorID); await sendMonitorList(socket); @@ -580,8 +623,8 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("pauseMonitor", async (monitorID, callback) => { try { - checkLogin(socket) - await pauseMonitor(socket.userID, monitorID) + checkLogin(socket); + await pauseMonitor(socket.userID, monitorID); await sendMonitorList(socket); callback({ @@ -599,13 +642,13 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("deleteMonitor", async (monitorID, callback) => { try { - checkLogin(socket) + checkLogin(socket); - console.log(`Delete Monitor: ${monitorID} User ID: ${socket.userID}`) + console.log(`Delete Monitor: ${monitorID} User ID: ${socket.userID}`); if (monitorID in monitorList) { monitorList[monitorID].stop(); - delete monitorList[monitorID] + delete monitorList[monitorID]; } await R.exec("DELETE FROM monitor WHERE id = ? AND user_id = ? ", [ @@ -619,6 +662,8 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); }); await sendMonitorList(socket); + // Clear heartbeat list on client + await sendImportantHeartbeatList(socket, monitorID, true, true); } catch (e) { callback({ @@ -630,9 +675,9 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("getTags", async (callback) => { try { - checkLogin(socket) + checkLogin(socket); - const list = await R.findAll("tag") + const list = await R.findAll("tag"); callback({ ok: true, @@ -649,12 +694,12 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("addTag", async (tag, callback) => { try { - checkLogin(socket) + checkLogin(socket); - let bean = R.dispense("tag") - bean.name = tag.name - bean.color = tag.color - await R.store(bean) + let bean = R.dispense("tag"); + bean.name = tag.name; + bean.color = tag.color; + await R.store(bean); callback({ ok: true, @@ -671,12 +716,12 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("editTag", async (tag, callback) => { try { - checkLogin(socket) + checkLogin(socket); - let bean = await R.findOne("monitor", " id = ? ", [ tag.id ]) - bean.name = tag.name - bean.color = tag.color - await R.store(bean) + let bean = await R.findOne("monitor", " id = ? ", [ tag.id ]); + bean.name = tag.name; + bean.color = tag.color; + await R.store(bean); callback({ ok: true, @@ -693,9 +738,9 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("deleteTag", async (tagID, callback) => { try { - checkLogin(socket) + checkLogin(socket); - await R.exec("DELETE FROM tag WHERE id = ? ", [ tagID ]) + await R.exec("DELETE FROM tag WHERE id = ? ", [ tagID ]); callback({ ok: true, @@ -712,13 +757,13 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("addMonitorTag", async (tagID, monitorID, value, callback) => { try { - checkLogin(socket) + checkLogin(socket); await R.exec("INSERT INTO monitor_tag (tag_id, monitor_id, value) VALUES (?, ?, ?)", [ tagID, monitorID, value, - ]) + ]); callback({ ok: true, @@ -735,13 +780,13 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("editMonitorTag", async (tagID, monitorID, value, callback) => { try { - checkLogin(socket) + checkLogin(socket); await R.exec("UPDATE monitor_tag SET value = ? WHERE tag_id = ? AND monitor_id = ?", [ value, tagID, monitorID, - ]) + ]); callback({ ok: true, @@ -758,13 +803,13 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("deleteMonitorTag", async (tagID, monitorID, value, callback) => { try { - checkLogin(socket) + checkLogin(socket); await R.exec("DELETE FROM monitor_tag WHERE tag_id = ? AND monitor_id = ? AND value = ?", [ tagID, monitorID, value, - ]) + ]); // Cleanup unused Tags await R.exec("delete from tag where ( select count(*) from monitor_tag mt where tag.id = mt.tag_id ) = 0"); @@ -784,15 +829,15 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("changePassword", async (password, callback) => { try { - checkLogin(socket) + checkLogin(socket); if (! password.currentPassword) { - throw new Error("Invalid new password") + throw new Error("Invalid new password"); } let user = await R.findOne("user", " id = ? AND active = 1 ", [ socket.userID, - ]) + ]); if (user && passwordHash.verify(password.currentPassword, user.password)) { @@ -801,9 +846,9 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); callback({ ok: true, msg: "Password has been updated successfully.", - }) + }); } else { - throw new Error("Incorrect current password") + throw new Error("Incorrect current password"); } } catch (e) { @@ -816,7 +861,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("getSettings", async (callback) => { try { - checkLogin(socket) + checkLogin(socket); callback({ ok: true, @@ -833,15 +878,18 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("setSettings", async (data, callback) => { try { - checkLogin(socket) + checkLogin(socket); - await setSettings("general", data) + await setSettings("general", data); + exports.entryPage = data.entryPage; callback({ ok: true, msg: "Saved" }); + sendInfo(socket); + } catch (e) { callback({ ok: false, @@ -853,10 +901,10 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); // Add or Edit socket.on("addNotification", async (notification, notificationID, callback) => { try { - checkLogin(socket) + checkLogin(socket); - let notificationBean = await Notification.save(notification, notificationID, socket.userID) - await sendNotificationList(socket) + let notificationBean = await Notification.save(notification, notificationID, socket.userID); + await sendNotificationList(socket); callback({ ok: true, @@ -874,10 +922,10 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("deleteNotification", async (notificationID, callback) => { try { - checkLogin(socket) + checkLogin(socket); - await Notification.delete(notificationID, socket.userID) - await sendNotificationList(socket) + await Notification.delete(notificationID, socket.userID); + await sendNotificationList(socket); callback({ ok: true, @@ -894,9 +942,9 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("testNotification", async (notification, callback) => { try { - checkLogin(socket) + checkLogin(socket); - let msg = await Notification.send(notification, notification.name + " Testing") + let msg = await Notification.send(notification, notification.name + " Testing"); callback({ ok: true, @@ -904,7 +952,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); }); } catch (e) { - console.error(e) + console.error(e); callback({ ok: false, @@ -915,73 +963,171 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("checkApprise", async (callback) => { try { - checkLogin(socket) + checkLogin(socket); callback(Notification.checkApprise()); } catch (e) { callback(false); } }); - socket.on("uploadBackup", async (uploadedJSON, callback) => { + socket.on("uploadBackup", async (uploadedJSON, importHandle, callback) => { try { - checkLogin(socket) + checkLogin(socket); let backupData = JSON.parse(uploadedJSON); - console.log(`Importing Backup, User ID: ${socket.userID}, Version: ${backupData.version}`) + console.log(`Importing Backup, User ID: ${socket.userID}, Version: ${backupData.version}`); - let notificationList = backupData.notificationList; - let monitorList = backupData.monitorList; + let notificationListData = backupData.notificationList; + let monitorListData = backupData.monitorList; - if (notificationList.length >= 1) { - for (let i = 0; i < notificationList.length; i++) { - let notification = JSON.parse(notificationList[i].config); - await Notification.save(notification, null, socket.userID) + let version17x = compareVersions.compare(backupData.version, "1.7.0", ">="); + + // If the import option is "overwrite" it'll clear most of the tables, except "settings" and "user" + if (importHandle == "overwrite") { + // Stops every monitor first, so it doesn't execute any heartbeat while importing + for (let id in monitorList) { + let monitor = monitorList[id]; + await monitor.stop(); + } + await R.exec("DELETE FROM heartbeat"); + await R.exec("DELETE FROM monitor_notification"); + await R.exec("DELETE FROM monitor_tls_info"); + await R.exec("DELETE FROM notification"); + await R.exec("DELETE FROM monitor_tag"); + await R.exec("DELETE FROM tag"); + await R.exec("DELETE FROM monitor"); + } + + // Only starts importing if the backup file contains at least one notification + if (notificationListData.length >= 1) { + // Get every existing notification name and puts them in one simple string + let notificationNameList = await R.getAll("SELECT name FROM notification"); + let notificationNameListString = JSON.stringify(notificationNameList); + + for (let i = 0; i < notificationListData.length; i++) { + // Only starts importing the notification if the import option is "overwrite", "keep" or "skip" but the notification doesn't exists + if ((importHandle == "skip" && notificationNameListString.includes(notificationListData[i].name) == false) || importHandle == "keep" || importHandle == "overwrite") { + + let notification = JSON.parse(notificationListData[i].config); + await Notification.save(notification, null, socket.userID); + + } } } - if (monitorList.length >= 1) { - for (let i = 0; i < monitorList.length; i++) { - let monitor = { - name: monitorList[i].name, - type: monitorList[i].type, - url: monitorList[i].url, - interval: monitorList[i].interval, - hostname: monitorList[i].hostname, - maxretries: monitorList[i].maxretries, - port: monitorList[i].port, - keyword: monitorList[i].keyword, - ignoreTls: monitorList[i].ignoreTls, - upsideDown: monitorList[i].upsideDown, - maxredirects: monitorList[i].maxredirects, - accepted_statuscodes: monitorList[i].accepted_statuscodes, - dns_resolve_type: monitorList[i].dns_resolve_type, - dns_resolve_server: monitorList[i].dns_resolve_server, - notificationIDList: {}, - } + // Only starts importing if the backup file contains at least one monitor + if (monitorListData.length >= 1) { + // Get every existing monitor name and puts them in one simple string + let monitorNameList = await R.getAll("SELECT name FROM monitor"); + let monitorNameListString = JSON.stringify(monitorNameList); - let bean = R.dispense("monitor") + for (let i = 0; i < monitorListData.length; i++) { + // Only starts importing the monitor if the import option is "overwrite", "keep" or "skip" but the notification doesn't exists + if ((importHandle == "skip" && monitorNameListString.includes(monitorListData[i].name) == false) || importHandle == "keep" || importHandle == "overwrite") { - let notificationIDList = monitor.notificationIDList; - delete monitor.notificationIDList; + // Define in here every new variable for monitors which where implemented after the first version of the Import/Export function (1.6.0) + // --- Start --- - monitor.accepted_statuscodes_json = JSON.stringify(monitor.accepted_statuscodes); - delete monitor.accepted_statuscodes; + // Define default values + let retryInterval = 0; - bean.import(monitor) - bean.user_id = socket.userID - await R.store(bean) + /* + Only replace the default value with the backup file data for the specific version, where it appears the first time + More information about that where "let version" will be defined + */ + if (version17x) { + retryInterval = monitorListData[i].retryInterval; + } - await updateMonitorNotification(bean.id, notificationIDList) + // --- End --- + + let monitor = { + // Define the new variable from earlier here + name: monitorListData[i].name, + type: monitorListData[i].type, + url: monitorListData[i].url, + method: monitorListData[i].method || "GET", + body: monitorListData[i].body, + headers: monitorListData[i].headers, + interval: monitorListData[i].interval, + retryInterval: retryInterval, + hostname: monitorListData[i].hostname, + maxretries: monitorListData[i].maxretries, + port: monitorListData[i].port, + keyword: monitorListData[i].keyword, + ignoreTls: monitorListData[i].ignoreTls, + upsideDown: monitorListData[i].upsideDown, + maxredirects: monitorListData[i].maxredirects, + accepted_statuscodes: monitorListData[i].accepted_statuscodes, + dns_resolve_type: monitorListData[i].dns_resolve_type, + dns_resolve_server: monitorListData[i].dns_resolve_server, + notificationIDList: {}, + }; + + if (monitorListData[i].pushToken) { + monitor.pushToken = monitorListData[i].pushToken; + } + + let bean = R.dispense("monitor"); + + let notificationIDList = monitor.notificationIDList; + delete monitor.notificationIDList; + + monitor.accepted_statuscodes_json = JSON.stringify(monitor.accepted_statuscodes); + delete monitor.accepted_statuscodes; + + bean.import(monitor); + bean.user_id = socket.userID; + await R.store(bean); + + // Only for backup files with the version 1.7.0 or higher, since there was the tag feature implemented + if (version17x) { + // Only import if the specific monitor has tags assigned + for (const oldTag of monitorListData[i].tags) { + + // Check if tag already exists and get data -> + let tag = await R.findOne("tag", " name = ?", [ + oldTag.name, + ]); + + let tagId; + if (! tag) { + // -> If it doesn't exist, create new tag from backup file + let beanTag = R.dispense("tag"); + beanTag.name = oldTag.name; + beanTag.color = oldTag.color; + await R.store(beanTag); + + tagId = beanTag.id; + } else { + // -> If it already exist, set tagId to value from database + tagId = tag.id; + } + + // Assign the new created tag to the monitor + await R.exec("INSERT INTO monitor_tag (tag_id, monitor_id, value) VALUES (?, ?, ?)", [ + tagId, + bean.id, + oldTag.value, + ]); + + } + } + + await updateMonitorNotification(bean.id, notificationIDList); + + // If monitor was active start it immediately, otherwise pause it + if (monitorListData[i].active == 1) { + await startMonitor(socket.userID, bean.id); + } else { + await pauseMonitor(socket.userID, bean.id); + } - if (monitorList[i].active == 1) { - await startMonitor(socket.userID, bean.id); - } else { - await pauseMonitor(socket.userID, bean.id); } } - await sendNotificationList(socket) + await sendNotificationList(socket); await sendMonitorList(socket); } @@ -1000,9 +1146,9 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("clearEvents", async (monitorID, callback) => { try { - checkLogin(socket) + checkLogin(socket); - console.log(`Clear Events Monitor: ${monitorID} User ID: ${socket.userID}`) + console.log(`Clear Events Monitor: ${monitorID} User ID: ${socket.userID}`); await R.exec("UPDATE heartbeat SET msg = ?, important = ? WHERE monitor_id = ? ", [ "", @@ -1026,9 +1172,9 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("clearHeartbeats", async (monitorID, callback) => { try { - checkLogin(socket) + checkLogin(socket); - console.log(`Clear Heartbeats Monitor: ${monitorID} User ID: ${socket.userID}`) + console.log(`Clear Heartbeats Monitor: ${monitorID} User ID: ${socket.userID}`); await R.exec("DELETE FROM heartbeat WHERE monitor_id = ?", [ monitorID @@ -1050,9 +1196,9 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); socket.on("clearStatistics", async (callback) => { try { - checkLogin(socket) + checkLogin(socket); - console.log(`Clear Statistics User ID: ${socket.userID}`) + console.log(`Clear Statistics User ID: ${socket.userID}`); await R.exec("DELETE FROM heartbeat"); @@ -1068,24 +1214,27 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); } }); - debug("added all socket handlers") + // Status Page Socket Handler for admin only + statusPageSocketHandler(socket); + + debug("added all socket handlers"); // *************************** // Better do anything after added all socket handlers here // *************************** - debug("check auto login") + debug("check auto login"); if (await setting("disableAuth")) { - console.log("Disabled Auth: auto login to admin") - afterLogin(socket, await R.findOne("user")) - socket.emit("autoLogin") + console.log("Disabled Auth: auto login to admin"); + afterLogin(socket, await R.findOne("user")); + socket.emit("autoLogin"); } else { - debug("need auth") + debug("need auth"); } }); - console.log("Init the server") + console.log("Init the server"); server.once("error", async (err) => { console.error("Cannot listen: " + err.message); @@ -1100,6 +1249,10 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); } startMonitors(); checkVersion.startInterval(); + + if (testMode) { + startUnitTest(); + } }); })(); @@ -1107,14 +1260,14 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); async function updateMonitorNotification(monitorID, notificationIDList) { await R.exec("DELETE FROM monitor_notification WHERE monitor_id = ? ", [ monitorID, - ]) + ]); for (let notificationID in notificationIDList) { if (notificationIDList[notificationID]) { let relation = R.dispense("monitor_notification"); relation.monitor_id = monitorID; relation.notification_id = notificationID; - await R.store(relation) + await R.store(relation); } } } @@ -1123,7 +1276,7 @@ async function checkOwner(userID, monitorID) { let row = await R.getRow("SELECT id FROM monitor WHERE id = ? AND user_id = ? ", [ monitorID, userID, - ]) + ]); if (! row) { throw new Error("You do not own this monitor."); @@ -1132,16 +1285,16 @@ async function checkOwner(userID, monitorID) { async function sendMonitorList(socket) { let list = await getMonitorJSONList(socket.userID); - io.to(socket.userID).emit("monitorList", list) + io.to(socket.userID).emit("monitorList", list); return list; } async function afterLogin(socket, user) { socket.userID = user.id; - socket.join(user.id) + socket.join(user.id); - let monitorList = await sendMonitorList(socket) - sendNotificationList(socket) + let monitorList = await sendMonitorList(socket); + sendNotificationList(socket); await sleep(500); @@ -1154,7 +1307,7 @@ async function afterLogin(socket, user) { } for (let monitorID in monitorList) { - await Monitor.sendStats(io, monitorID, user.id) + await Monitor.sendStats(io, monitorID, user.id); } } @@ -1163,7 +1316,7 @@ async function getMonitorJSONList(userID) { let monitorList = await R.find("monitor", " user_id = ? ORDER BY weight DESC, name", [ userID, - ]) + ]); for (let monitor of monitorList) { result[monitor.id] = await monitor.toJSON(); @@ -1172,24 +1325,18 @@ async function getMonitorJSONList(userID) { return result; } -function checkLogin(socket) { - if (! socket.userID) { - throw new Error("You are not logged in."); - } -} - async function initDatabase() { if (! fs.existsSync(Database.path)) { - console.log("Copying Database") + console.log("Copying Database"); fs.copyFileSync(Database.templatePath, Database.path); } - console.log("Connecting to Database") + console.log("Connecting to the Database"); await Database.connect(); - console.log("Connected") + console.log("Connected"); // Patch the database - await Database.patch() + await Database.patch(); let jwtSecretBean = await R.findOne("setting", " `key` = ? ", [ "jwtSecret", @@ -1205,7 +1352,7 @@ async function initDatabase() { // If there is no record in user table, it is a new Uptime Kuma instance, need to setup if ((await R.count("user")) === 0) { - console.log("No user, need setup") + console.log("No user, need setup"); needSetup = true; } @@ -1213,9 +1360,9 @@ async function initDatabase() { } async function startMonitor(userID, monitorID) { - await checkOwner(userID, monitorID) + await checkOwner(userID, monitorID); - console.log(`Resume Monitor: ${monitorID} User ID: ${userID}`) + console.log(`Resume Monitor: ${monitorID} User ID: ${userID}`); await R.exec("UPDATE monitor SET active = 1 WHERE id = ? AND user_id = ? ", [ monitorID, @@ -1224,24 +1371,24 @@ async function startMonitor(userID, monitorID) { let monitor = await R.findOne("monitor", " id = ? ", [ monitorID, - ]) + ]); if (monitor.id in monitorList) { monitorList[monitor.id].stop(); } monitorList[monitor.id] = monitor; - monitor.start(io) + monitor.start(io); } async function restartMonitor(userID, monitorID) { - return await startMonitor(userID, monitorID) + return await startMonitor(userID, monitorID); } async function pauseMonitor(userID, monitorID) { - await checkOwner(userID, monitorID) + await checkOwner(userID, monitorID); - console.log(`Pause Monitor: ${monitorID} User ID: ${userID}`) + console.log(`Pause Monitor: ${monitorID} User ID: ${userID}`); await R.exec("UPDATE monitor SET active = 0 WHERE id = ? AND user_id = ? ", [ monitorID, @@ -1257,7 +1404,7 @@ async function pauseMonitor(userID, monitorID) { * Resume active monitors */ async function startMonitors() { - let list = await R.find("monitor", " active = 1 ") + let list = await R.find("monitor", " active = 1 "); for (let monitor of list) { monitorList[monitor.id] = monitor; @@ -1274,17 +1421,17 @@ async function shutdownFunction(signal) { console.log("Shutdown requested"); console.log("Called signal: " + signal); - console.log("Stopping all monitors") + console.log("Stopping all monitors"); for (let id in monitorList) { - let monitor = monitorList[id] - monitor.stop() + let monitor = monitorList[id]; + monitor.stop(); } await sleep(2000); await Database.close(); } function finalFunction() { - console.log("Graceful shutdown successfully!"); + console.log("Graceful shutdown successful!"); } gracefulShutdown(server, { diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index cc836efee..062582566 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -26,19 +26,35 @@ + +
{{ $t("Not available, please setup.") }}
@@ -189,6 +216,51 @@
+
+
+
+ {{ $t("HTTP Options") }}
+
+
+