diff --git a/server/auth.js b/server/auth.js index 36316241c..597cf3d75 100644 --- a/server/auth.js +++ b/server/auth.js @@ -1,6 +1,7 @@ const basicAuth = require("express-basic-auth"); const passwordHash = require("./password-hash"); const { R } = require("redbean-node"); +const { setting } = require("./util-server"); const { log } = require("../src/util"); const { loginRateLimiter, apiRateLimiter } = require("./rate-limiter"); const { Settings } = require("./settings"); @@ -138,7 +139,7 @@ exports.basicAuth = async function (req, res, next) { challenge: true, }); - const disabledAuth = await Settings.get("disableAuth"); + const disabledAuth = await setting("disableAuth"); if (!disabledAuth) { middleware(req, res, next); diff --git a/server/check-version.js b/server/check-version.js index 154ebe373..c6d5cfb92 100644 --- a/server/check-version.js +++ b/server/check-version.js @@ -1,7 +1,7 @@ +const { setSetting, setting } = require("./util-server"); const axios = require("axios"); const compareVersions = require("compare-versions"); const { log } = require("../src/util"); -const { Settings } = require("./settings"); exports.version = require("../package.json").version; exports.latestVersion = null; @@ -14,7 +14,7 @@ let interval; exports.startInterval = () => { let check = async () => { - if (await Settings.get("checkUpdate") === false) { + if (await setting("checkUpdate") === false) { return; } @@ -28,7 +28,7 @@ exports.startInterval = () => { res.data.slow = "1000.0.0"; } - let checkBeta = await Settings.get("checkBeta"); + let checkBeta = await setting("checkBeta"); if (checkBeta && res.data.beta) { if (compareVersions.compare(res.data.beta, res.data.slow, ">")) { @@ -57,7 +57,7 @@ exports.startInterval = () => { * @returns {Promise} */ exports.enableCheckUpdate = async (value) => { - await Settings.set("checkUpdate", value); + await setSetting("checkUpdate", value); clearInterval(interval); diff --git a/server/client.js b/server/client.js index 6929f81aa..72f0a4e8e 100644 --- a/server/client.js +++ b/server/client.js @@ -6,8 +6,8 @@ const { R } = require("redbean-node"); const { UptimeKumaServer } = require("./uptime-kuma-server"); const server = UptimeKumaServer.getInstance(); const io = server.io; +const { setting } = require("./util-server"); const checkVersion = require("./check-version"); -const { Settings } = require("./settings"); const Database = require("./database"); /** @@ -158,8 +158,8 @@ async function sendInfo(socket, hideVersion = false) { version, latestVersion, isContainer, - primaryBaseURL: await Settings.get("primaryBaseURL"), dbType, + primaryBaseURL: await setting("primaryBaseURL"), serverTimezone: await server.getTimezone(), serverTimezoneOffset: server.getTimezoneOffset(), }); diff --git a/server/database.js b/server/database.js index 7928bb855..5bcd79887 100644 --- a/server/database.js +++ b/server/database.js @@ -1,11 +1,11 @@ const fs = require("fs"); const { R } = require("redbean-node"); +const { setSetting, setting } = require("./util-server"); const { log, sleep } = require("../src/util"); const knex = require("knex"); const path = require("path"); const { EmbeddedMariaDB } = require("./embedded-mariadb"); const mysql = require("mysql2/promise"); -const { Settings } = require("./settings"); const { UptimeCalculator } = require("./uptime-calculator"); const dayjs = require("dayjs"); @@ -436,7 +436,7 @@ class Database { * @deprecated */ static async patchSqlite() { - let version = parseInt(await Settings.get("database_version")); + let version = parseInt(await setting("database_version")); if (! version) { version = 0; @@ -461,7 +461,7 @@ class Database { log.info("db", `Patching ${sqlFile}`); await Database.importSQLFile(sqlFile); log.info("db", `Patched ${sqlFile}`); - await Settings.set("database_version", i); + await setSetting("database_version", i); } } catch (ex) { await Database.close(); @@ -487,7 +487,7 @@ class Database { */ static async patchSqlite2() { log.debug("db", "Database Patch 2.0 Process"); - let databasePatchedFiles = await Settings.get("databasePatchedFiles"); + let databasePatchedFiles = await setting("databasePatchedFiles"); if (! databasePatchedFiles) { databasePatchedFiles = {}; @@ -515,7 +515,7 @@ class Database { process.exit(1); } - await Settings.set("databasePatchedFiles", databasePatchedFiles); + await setSetting("databasePatchedFiles", databasePatchedFiles); } /** @@ -528,27 +528,27 @@ class Database { // Fix 1.13.0 empty slug bug await R.exec("UPDATE status_page SET slug = 'empty-slug-recover' WHERE TRIM(slug) = ''"); - let title = await Settings.get("title"); + let title = await setting("title"); if (title) { - log.info("database", "Migrating Status Page"); + console.log("Migrating Status Page"); let statusPageCheck = await R.findOne("status_page", " slug = 'default' "); if (statusPageCheck !== null) { - log.info("database", "Migrating Status Page - Skip, default slug record is already existing"); + console.log("Migrating Status Page - Skip, default slug record is already existing"); return; } let statusPage = R.dispense("status_page"); statusPage.slug = "default"; statusPage.title = title; - statusPage.description = await Settings.get("description"); - statusPage.icon = await Settings.get("icon"); - statusPage.theme = await Settings.get("statusPageTheme"); - statusPage.published = !!await Settings.get("statusPagePublished"); - statusPage.search_engine_index = !!await Settings.get("searchEngineIndex"); - statusPage.show_tags = !!await Settings.get("statusPageTags"); + statusPage.description = await setting("description"); + statusPage.icon = await setting("icon"); + statusPage.theme = await setting("statusPageTheme"); + statusPage.published = !!await setting("statusPagePublished"); + statusPage.search_engine_index = !!await setting("searchEngineIndex"); + statusPage.show_tags = !!await setting("statusPageTags"); statusPage.password = null; if (!statusPage.title) { @@ -576,13 +576,13 @@ class Database { await R.exec("DELETE FROM setting WHERE type = 'statusPage'"); // Migrate Entry Page if it is status page - let entryPage = await Settings.get("entryPage"); + let entryPage = await setting("entryPage"); if (entryPage === "statusPage") { - await Settings.set("entryPage", "statusPage-default", "general"); + await setSetting("entryPage", "statusPage-default", "general"); } - log.info("database", "Migrating Status Page - Done"); + console.log("Migrating Status Page - Done"); } } diff --git a/server/jobs/clear-old-data.js b/server/jobs/clear-old-data.js index f7acb83f5..ada7f8882 100644 --- a/server/jobs/clear-old-data.js +++ b/server/jobs/clear-old-data.js @@ -1,7 +1,7 @@ const { R } = require("redbean-node"); const { log } = require("../../src/util"); +const { setSetting, setting } = require("../util-server"); const Database = require("../database"); -const { Settings } = require("../settings"); const DEFAULT_KEEP_PERIOD = 180; @@ -25,7 +25,7 @@ const clearOldData = async () => { * - keep according to the setting (keepDataPeriodDays) (default 180 days) */ - let period = await Settings.get("keepDataPeriodDays"); + let period = await setting("keepDataPeriodDays"); // Set Default Period if (period == null) { @@ -39,7 +39,7 @@ const clearOldData = async () => { parsedPeriod = parseInt(period); } catch (_) { log.warn("clearOldData", "Failed to parse setting, resetting to default.."); - await Settings.set("keepDataPeriodDays", DEFAULT_KEEP_PERIOD, "general"); + await setSetting("keepDataPeriodDays", DEFAULT_KEEP_PERIOD, "general"); parsedPeriod = DEFAULT_KEEP_PERIOD; } diff --git a/server/model/monitor.js b/server/model/monitor.js index ff73036c9..5b7e5871a 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -4,7 +4,7 @@ const { Prometheus } = require("../prometheus"); const { log, UP, DOWN, PENDING, MAINTENANCE, flipStatus, MAX_INTERVAL_SECOND, MIN_INTERVAL_SECOND, SQL_DATETIME_FORMAT, evaluateJsonQuery } = require("../../src/util"); -const { tcping, ping, checkCertificate, checkStatusCode, getTotalClientInRoom, mssqlQuery, postgresQuery, mysqlQuery, httpNtlm, radius, grpcQuery, +const { tcping, ping, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, mssqlQuery, postgresQuery, mysqlQuery, setSetting, httpNtlm, radius, grpcQuery, redisPingAsync, kafkaProducerAsync, getOidcTokenClientCredentials, rootCertificatesFingerprints, axiosAbortSignal } = require("../util-server"); const { R } = require("redbean-node"); @@ -24,7 +24,6 @@ const { CookieJar } = require("tough-cookie"); const { HttpsCookieAgent } = require("http-cookie-agent/http"); const https = require("https"); const http = require("http"); -const { Settings } = require("../settings"); const rootCertificates = rootCertificatesFingerprints(); @@ -652,7 +651,7 @@ class Monitor extends BeanModel { } else if (this.type === "steam") { const steamApiUrl = "https://api.steampowered.com/IGameServersService/GetServerList/v1/"; - const steamAPIKey = await Settings.get("steamAPIKey"); + const steamAPIKey = await setting("steamAPIKey"); const filter = `addr\\${this.hostname}:${this.port}`; if (!steamAPIKey) { @@ -1374,12 +1373,11 @@ class Monitor extends BeanModel { return; } - let notifyDays = await Settings.get("tlsExpiryNotifyDays"); + let notifyDays = await setting("tlsExpiryNotifyDays"); if (notifyDays == null || !Array.isArray(notifyDays)) { // Reset Default - await Settings.set("tlsExpiryNotifyDays", [ 7, 14, 21 ], "general"); + await setSetting("tlsExpiryNotifyDays", [ 7, 14, 21 ], "general"); notifyDays = [ 7, 14, 21 ]; - await Settings.set("tlsExpiryNotifyDays", notifyDays, "general"); } if (Array.isArray(notifyDays)) { diff --git a/server/modules/apicache/apicache.js b/server/modules/apicache/apicache.js index 80136d0a4..41930b24d 100644 --- a/server/modules/apicache/apicache.js +++ b/server/modules/apicache/apicache.js @@ -528,7 +528,7 @@ function ApiCache() { /** * Get index of a group - * @param {string} group + * @param {string} group * @returns {number} */ this.getIndex = function (group) { @@ -543,9 +543,9 @@ function ApiCache() { * Express middleware * @param {(string|number)} strDuration Duration to cache responses * for. - * @param {function(Object, Object):boolean} middlewareToggle + * @param {function(Object, Object):boolean} middlewareToggle * @param {Object} localOptions Options for APICache - * @returns + * @returns */ this.middleware = function cache(strDuration, middlewareToggle, localOptions) { let duration = instance.getDuration(strDuration); @@ -859,7 +859,7 @@ function ApiCache() { /** * Process options - * @param {Object} options + * @param {Object} options * @returns {Object} */ this.options = function (options) { diff --git a/server/monitor-types/real-browser-monitor-type.js b/server/monitor-types/real-browser-monitor-type.js index ce3abcb2d..f1219af18 100644 --- a/server/monitor-types/real-browser-monitor-type.js +++ b/server/monitor-types/real-browser-monitor-type.js @@ -63,7 +63,7 @@ if (process.platform === "win32") { * @returns {Promise} The executable is allowed? */ async function isAllowedChromeExecutable(executablePath) { - log.info("Chromium", config.args); + console.log(config.args); if (config.args["allow-all-chrome-exec"] || process.env.UPTIME_KUMA_ALLOW_ALL_CHROME_EXEC === "1") { return true; } @@ -102,8 +102,7 @@ async function getBrowser() { */ async function getRemoteBrowser(remoteBrowserID, userId) { let remoteBrowser = await RemoteBrowser.get(remoteBrowserID, userId); - log.debug("Chromium", `Using remote browser: ${remoteBrowser.name} (${remoteBrowser.id})`); - browser = chromium.connect(remoteBrowser.url); + log.debug("MONITOR", `Using remote browser: ${remoteBrowser.name} (${remoteBrowser.id})`); browser = await chromium.connect(remoteBrowser.url); return browser; } diff --git a/server/notification-providers/alertnow.js b/server/notification-providers/alertnow.js index ecc03e84b..4257ca9cd 100644 --- a/server/notification-providers/alertnow.js +++ b/server/notification-providers/alertnow.js @@ -1,7 +1,7 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); +const { setting } = require("../util-server"); const { getMonitorRelativeURL, UP, DOWN } = require("../../src/util"); -const { Settings } = require("../settings"); class AlertNow extends NotificationProvider { name = "AlertNow"; @@ -29,7 +29,7 @@ class AlertNow extends NotificationProvider { textMsg += ` - ${msg}`; - const baseURL = await Settings.get("primaryBaseURL"); + const baseURL = await setting("primaryBaseURL"); if (baseURL && monitorJSON) { textMsg += ` >> ${baseURL + getMonitorRelativeURL(monitorJSON.id)}`; } diff --git a/server/notification-providers/flashduty.js b/server/notification-providers/flashduty.js index 09ee8913a..c340ed06f 100644 --- a/server/notification-providers/flashduty.js +++ b/server/notification-providers/flashduty.js @@ -1,7 +1,7 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); const { UP, DOWN, getMonitorRelativeURL } = require("../../src/util"); -const { Settings } = require("../settings"); +const { setting } = require("../util-server"); const successMessage = "Sent Successfully."; class FlashDuty extends NotificationProvider { @@ -84,7 +84,7 @@ class FlashDuty extends NotificationProvider { } }; - const baseURL = await Settings.get("primaryBaseURL"); + const baseURL = await setting("primaryBaseURL"); if (baseURL && monitorInfo) { options.client = "Uptime Kuma"; options.client_url = baseURL + getMonitorRelativeURL(monitorInfo.id); diff --git a/server/notification-providers/google-chat.js b/server/notification-providers/google-chat.js index e683a2070..0b72fea95 100644 --- a/server/notification-providers/google-chat.js +++ b/server/notification-providers/google-chat.js @@ -1,7 +1,7 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); +const { setting } = require("../util-server"); const { getMonitorRelativeURL, UP } = require("../../src/util"); -const { Settings } = require("../settings"); class GoogleChat extends NotificationProvider { name = "GoogleChat"; @@ -45,7 +45,7 @@ class GoogleChat extends NotificationProvider { } // add button for monitor link if available - const baseURL = await Settings.get("primaryBaseURL"); + const baseURL = await setting("primaryBaseURL"); if (baseURL) { const urlPath = monitorJSON ? getMonitorRelativeURL(monitorJSON.id) : "/"; sectionWidgets.push({ diff --git a/server/notification-providers/pagerduty.js b/server/notification-providers/pagerduty.js index 7aa19bb4b..c60d782e7 100644 --- a/server/notification-providers/pagerduty.js +++ b/server/notification-providers/pagerduty.js @@ -1,7 +1,7 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); const { UP, DOWN, getMonitorRelativeURL } = require("../../src/util"); -const { Settings } = require("../settings"); +const { setting } = require("../util-server"); let successMessage = "Sent Successfully."; class PagerDuty extends NotificationProvider { @@ -95,7 +95,7 @@ class PagerDuty extends NotificationProvider { } }; - const baseURL = await Settings.get("primaryBaseURL"); + const baseURL = await setting("primaryBaseURL"); if (baseURL && monitorInfo) { options.client = "Uptime Kuma"; options.client_url = baseURL + getMonitorRelativeURL(monitorInfo.id); diff --git a/server/notification-providers/pagertree.js b/server/notification-providers/pagertree.js index 62f229ace..c7a5338d1 100644 --- a/server/notification-providers/pagertree.js +++ b/server/notification-providers/pagertree.js @@ -1,7 +1,7 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); const { UP, DOWN, getMonitorRelativeURL } = require("../../src/util"); -const { Settings } = require("../settings"); +const { setting } = require("../util-server"); let successMessage = "Sent Successfully."; class PagerTree extends NotificationProvider { @@ -74,7 +74,7 @@ class PagerTree extends NotificationProvider { } }; - const baseURL = await Settings.get("primaryBaseURL"); + const baseURL = await setting("primaryBaseURL"); if (baseURL && monitorJSON) { options.client = "Uptime Kuma"; options.client_url = baseURL + getMonitorRelativeURL(monitorJSON.id); diff --git a/server/notification-providers/rocket-chat.js b/server/notification-providers/rocket-chat.js index 174a94950..690e33a86 100644 --- a/server/notification-providers/rocket-chat.js +++ b/server/notification-providers/rocket-chat.js @@ -1,8 +1,8 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); const Slack = require("./slack"); +const { setting } = require("../util-server"); const { getMonitorRelativeURL, DOWN } = require("../../src/util"); -const { Settings } = require("../settings"); class RocketChat extends NotificationProvider { name = "rocket.chat"; @@ -49,7 +49,7 @@ class RocketChat extends NotificationProvider { await Slack.deprecateURL(notification.rocketbutton); } - const baseURL = await Settings.get("primaryBaseURL"); + const baseURL = await setting("primaryBaseURL"); if (baseURL) { data.attachments[0].title_link = baseURL + getMonitorRelativeURL(monitorJSON.id); diff --git a/server/notification-providers/slack.js b/server/notification-providers/slack.js index 772642050..3676d6df3 100644 --- a/server/notification-providers/slack.js +++ b/server/notification-providers/slack.js @@ -1,8 +1,7 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); +const { setSettings, setting } = require("../util-server"); const { getMonitorRelativeURL, UP } = require("../../src/util"); -const { Settings } = require("../settings"); -const { log } = require("../../src/util"); class Slack extends NotificationProvider { name = "slack"; @@ -15,13 +14,15 @@ class Slack extends NotificationProvider { * @returns {Promise} */ static async deprecateURL(url) { - let currentPrimaryBaseURL = await Settings.get("primaryBaseURL"); + let currentPrimaryBaseURL = await setting("primaryBaseURL"); if (!currentPrimaryBaseURL) { - log.error("notification", "Move the url to be the primary base URL"); - await Settings.set("primaryBaseURL", url, "general"); + console.log("Move the url to be the primary base URL"); + await setSettings("general", { + primaryBaseURL: url, + }); } else { - log.debug("notification", "Already there, no need to move the primary base URL"); + console.log("Already there, no need to move the primary base URL"); } } @@ -135,7 +136,7 @@ class Slack extends NotificationProvider { return okMsg; } - const baseURL = await Settings.get("primaryBaseURL"); + const baseURL = await setting("primaryBaseURL"); const title = "Uptime Kuma Alert"; let data = { diff --git a/server/notification-providers/splunk.js b/server/notification-providers/splunk.js index ad4dc6b39..e07c51039 100644 --- a/server/notification-providers/splunk.js +++ b/server/notification-providers/splunk.js @@ -1,7 +1,7 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); const { UP, DOWN, getMonitorRelativeURL } = require("../../src/util"); -const { Settings } = require("../settings"); +const { setting } = require("../util-server"); let successMessage = "Sent Successfully."; class Splunk extends NotificationProvider { @@ -95,7 +95,7 @@ class Splunk extends NotificationProvider { } }; - const baseURL = await Settings.get("primaryBaseURL"); + const baseURL = await setting("primaryBaseURL"); if (baseURL && monitorInfo) { options.client = "Uptime Kuma"; options.client_url = baseURL + getMonitorRelativeURL(monitorInfo.id); diff --git a/server/notification-providers/stackfield.js b/server/notification-providers/stackfield.js index e4e31a9ab..65a92459a 100644 --- a/server/notification-providers/stackfield.js +++ b/server/notification-providers/stackfield.js @@ -1,7 +1,7 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); +const { setting } = require("../util-server"); const { getMonitorRelativeURL } = require("../../src/util"); -const { Settings } = require("../settings"); class Stackfield extends NotificationProvider { name = "stackfield"; @@ -23,7 +23,7 @@ class Stackfield extends NotificationProvider { textMsg += `\n${msg}`; - const baseURL = await Settings.get("primaryBaseURL"); + const baseURL = await setting("primaryBaseURL"); if (baseURL) { textMsg += `\n${baseURL + getMonitorRelativeURL(monitorJSON.id)}`; } diff --git a/server/proxy.js b/server/proxy.js index 3f1215b25..3f3771ab9 100644 --- a/server/proxy.js +++ b/server/proxy.js @@ -2,7 +2,7 @@ const { R } = require("redbean-node"); const HttpProxyAgent = require("http-proxy-agent"); const HttpsProxyAgent = require("https-proxy-agent"); const SocksProxyAgent = require("socks-proxy-agent"); -const { log } = require("../src/util"); +const { debug } = require("../src/util"); const { UptimeKumaServer } = require("./uptime-kuma-server"); const { CookieJar } = require("tough-cookie"); const { createCookieAgent } = require("http-cookie-agent/http"); @@ -110,9 +110,9 @@ class Proxy { proxyOptions.auth = `${proxy.username}:${proxy.password}`; } - log.debug("update-proxy", `Proxy Options: ${JSON.stringify(proxyOptions)}`); - log.debug("update-proxy", `HTTP Agent Options: ${JSON.stringify(httpAgentOptions)}`); - log.debug("update-proxy", `HTTPS Agent Options: ${JSON.stringify(httpsAgentOptions)}`); + debug(`Proxy Options: ${JSON.stringify(proxyOptions)}`); + debug(`HTTP Agent Options: ${JSON.stringify(httpAgentOptions)}`); + debug(`HTTPS Agent Options: ${JSON.stringify(httpsAgentOptions)}`); switch (proxy.protocol) { case "http": diff --git a/server/routers/api-router.js b/server/routers/api-router.js index 9e7c9348c..ed6db2cd1 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -1,5 +1,6 @@ let express = require("express"); const { + setting, allowDevAllOrigin, allowAllOrigin, percentageToColor, @@ -17,7 +18,6 @@ const { makeBadge } = require("badge-maker"); const { Prometheus } = require("../prometheus"); const Database = require("../database"); const { UptimeCalculator } = require("../uptime-calculator"); -const { Settings } = require("../settings"); let router = express.Router(); @@ -30,7 +30,7 @@ router.get("/api/entry-page", async (request, response) => { let result = { }; let hostname = request.hostname; - if ((await Settings.get("trustProxy")) && request.headers["x-forwarded-host"]) { + if ((await setting("trustProxy")) && request.headers["x-forwarded-host"]) { hostname = request.headers["x-forwarded-host"]; } diff --git a/server/server.js b/server/server.js index 59d636259..669c8dc54 100644 --- a/server/server.js +++ b/server/server.js @@ -90,7 +90,8 @@ const Monitor = require("./model/monitor"); const User = require("./model/user"); log.debug("server", "Importing Settings"); -const { initJWTSecret, checkLogin, doubleCheckPassword, shake256, SHAKE256_LENGTH, allowDevAllOrigin } = require("./util-server"); +const { getSettings, setSettings, setting, initJWTSecret, checkLogin, doubleCheckPassword, shake256, SHAKE256_LENGTH, allowDevAllOrigin, +} = require("./util-server"); log.debug("server", "Importing Notification"); const { Notification } = require("./notification"); @@ -200,7 +201,7 @@ let needSetup = false; // Entry Page app.get("/", async (request, response) => { let hostname = request.hostname; - if (await Settings.get("trustProxy")) { + if (await setting("trustProxy")) { const proxy = request.headers["x-forwarded-host"]; if (proxy) { hostname = proxy; @@ -280,7 +281,7 @@ let needSetup = false; // Robots.txt app.get("/robots.txt", async (_request, response) => { let txt = "User-agent: *\nDisallow:"; - if (!await Settings.get("searchEngineIndex")) { + if (!await setting("searchEngineIndex")) { txt += " /"; } response.setHeader("Content-Type", "text/plain"); @@ -1326,7 +1327,7 @@ let needSetup = false; socket.on("getSettings", async (callback) => { try { checkLogin(socket); - const data = await Settings.getSettings("general"); + const data = await getSettings("general"); if (!data.serverTimezone) { data.serverTimezone = await server.getTimezone(); @@ -1354,7 +1355,7 @@ let needSetup = false; // Disabled Auth + Want to Enable Auth => No Check // Enabled Auth + Want to Disable Auth => Check!! // Enabled Auth + Want to Enable Auth => No Check - const currentDisabledAuth = await Settings.get("disableAuth"); + const currentDisabledAuth = await setting("disableAuth"); if (!currentDisabledAuth && data.disableAuth) { await doubleCheckPassword(socket, currentPassword); } @@ -1368,7 +1369,7 @@ let needSetup = false; const previousChromeExecutable = await Settings.get("chromeExecutable"); const previousNSCDStatus = await Settings.get("nscd"); - await Settings.setSettings("general", data); + await setSettings("general", data); server.entryPage = data.entryPage; // Also need to apply timezone globally @@ -1464,7 +1465,7 @@ let needSetup = false; }); } catch (e) { - log.error("server", e); + console.error(e); callback({ ok: false, @@ -1577,7 +1578,7 @@ let needSetup = false; // *************************** log.debug("auth", "check auto login"); - if (await Settings.get("disableAuth")) { + if (await setting("disableAuth")) { log.info("auth", "Disabled Auth: auto login to admin"); await afterLogin(socket, await R.findOne("user")); socket.emit("autoLogin"); diff --git a/server/socket-handlers/api-key-socket-handler.js b/server/socket-handlers/api-key-socket-handler.js index 1394f8044..f76b90991 100644 --- a/server/socket-handlers/api-key-socket-handler.js +++ b/server/socket-handlers/api-key-socket-handler.js @@ -60,7 +60,7 @@ module.exports.apiKeySocketHandler = (socket) => { ok: true, }); } catch (e) { - log.error("apikeys", e); + console.error(e); callback({ ok: false, msg: e.message, diff --git a/server/socket-handlers/cloudflared-socket-handler.js b/server/socket-handlers/cloudflared-socket-handler.js index 1cd0c53d0..809191fe8 100644 --- a/server/socket-handlers/cloudflared-socket-handler.js +++ b/server/socket-handlers/cloudflared-socket-handler.js @@ -1,8 +1,7 @@ -const { checkLogin, doubleCheckPassword } = require("../util-server"); +const { checkLogin, setSetting, setting, doubleCheckPassword } = require("../util-server"); const { CloudflaredTunnel } = require("node-cloudflared-tunnel"); const { UptimeKumaServer } = require("../uptime-kuma-server"); const { log } = require("../../src/util"); -const { Settings } = require("../settings"); const io = UptimeKumaServer.getInstance().io; const prefix = "cloudflared_"; @@ -41,7 +40,7 @@ module.exports.cloudflaredSocketHandler = (socket) => { socket.join("cloudflared"); io.to(socket.userID).emit(prefix + "installed", cloudflared.checkInstalled()); io.to(socket.userID).emit(prefix + "running", cloudflared.running); - io.to(socket.userID).emit(prefix + "token", await Settings.get("cloudflaredTunnelToken")); + io.to(socket.userID).emit(prefix + "token", await setting("cloudflaredTunnelToken")); } catch (error) { } }); @@ -56,7 +55,7 @@ module.exports.cloudflaredSocketHandler = (socket) => { try { checkLogin(socket); if (token && typeof token === "string") { - await Settings.set("cloudflaredTunnelToken", token); + await setSetting("cloudflaredTunnelToken", token); cloudflared.token = token; } else { cloudflared.token = null; @@ -68,7 +67,7 @@ module.exports.cloudflaredSocketHandler = (socket) => { socket.on(prefix + "stop", async (currentPassword, callback) => { try { checkLogin(socket); - const disabledAuth = await Settings.get("disableAuth"); + const disabledAuth = await setting("disableAuth"); if (!disabledAuth) { await doubleCheckPassword(socket, currentPassword); } @@ -84,7 +83,7 @@ module.exports.cloudflaredSocketHandler = (socket) => { socket.on(prefix + "removeToken", async () => { try { checkLogin(socket); - await Settings.set("cloudflaredTunnelToken", ""); + await setSetting("cloudflaredTunnelToken", ""); } catch (error) { } }); @@ -97,15 +96,15 @@ module.exports.cloudflaredSocketHandler = (socket) => { */ module.exports.autoStart = async (token) => { if (!token) { - token = await Settings.get("cloudflaredTunnelToken"); + token = await setting("cloudflaredTunnelToken"); } else { // Override the current token via args or env var - await Settings.set("cloudflaredTunnelToken", token); - log.info("cloudflare", "Use cloudflared token from args or env var"); + await setSetting("cloudflaredTunnelToken", token); + console.log("Use cloudflared token from args or env var"); } if (token) { - log.info("cloudflare", "Start cloudflared"); + console.log("Start cloudflared"); cloudflared.token = token; cloudflared.start(); } diff --git a/server/socket-handlers/maintenance-socket-handler.js b/server/socket-handlers/maintenance-socket-handler.js index 201014c22..7de13fe57 100644 --- a/server/socket-handlers/maintenance-socket-handler.js +++ b/server/socket-handlers/maintenance-socket-handler.js @@ -67,7 +67,7 @@ module.exports.maintenanceSocketHandler = (socket) => { }); } catch (e) { - log.error("maintenance", e); + console.error(e); callback({ ok: false, msg: e.message, @@ -177,7 +177,7 @@ module.exports.maintenanceSocketHandler = (socket) => { ok: true, }); } catch (e) { - log.error("maintenance", e); + console.error(e); callback({ ok: false, msg: e.message, @@ -201,7 +201,7 @@ module.exports.maintenanceSocketHandler = (socket) => { }); } catch (e) { - log.error("maintenance", e); + console.error(e); callback({ ok: false, msg: e.message, @@ -225,7 +225,7 @@ module.exports.maintenanceSocketHandler = (socket) => { }); } catch (e) { - log.error("maintenance", e); + console.error(e); callback({ ok: false, msg: e.message, diff --git a/server/socket-handlers/status-page-socket-handler.js b/server/socket-handlers/status-page-socket-handler.js index cf0accc23..0804da15d 100644 --- a/server/socket-handlers/status-page-socket-handler.js +++ b/server/socket-handlers/status-page-socket-handler.js @@ -1,5 +1,5 @@ const { R } = require("redbean-node"); -const { checkLogin } = require("../util-server"); +const { checkLogin, setSetting } = require("../util-server"); const dayjs = require("dayjs"); const { log } = require("../../src/util"); const ImageDataURI = require("../image-data-uri"); @@ -7,7 +7,6 @@ const Database = require("../database"); const apicache = require("../modules/apicache"); const StatusPage = require("../model/status_page"); const { UptimeKumaServer } = require("../uptime-kuma-server"); -const { Settings } = require("../settings"); /** * Socket handlers for status page @@ -234,7 +233,7 @@ module.exports.statusPageSocketHandler = (socket) => { // Also change entry page to new slug if it is the default one, and slug is changed. if (server.entryPage === "statusPage-" + slug && statusPage.slug !== slug) { server.entryPage = "statusPage-" + statusPage.slug; - await Settings.set("entryPage", server.entryPage, "general"); + await setSetting("entryPage", server.entryPage, "general"); } apicache.clear(); @@ -292,7 +291,7 @@ module.exports.statusPageSocketHandler = (socket) => { }); } catch (error) { - log.error("socket", error); + console.error(error); callback({ ok: false, msg: error.message, @@ -314,7 +313,7 @@ module.exports.statusPageSocketHandler = (socket) => { // Reset entry page if it is the default one. if (server.entryPage === "statusPage-" + slug) { server.entryPage = "dashboard"; - await Settings.set("entryPage", server.entryPage, "general"); + await setSetting("entryPage", server.entryPage, "general"); } // No need to delete records from `status_page_cname`, because it has cascade foreign key. diff --git a/server/util-server.js b/server/util-server.js index f4a4a67fe..5ebc62ac5 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -12,6 +12,7 @@ const { Client } = require("pg"); const postgresConParse = require("pg-connection-string").parse; const mysql = require("mysql2"); const { NtlmClient } = require("./modules/axios-ntlm/lib/ntlmClient.js"); +const { Settings } = require("./settings"); const grpc = require("@grpc/grpc-js"); const protojs = require("protobufjs"); const radiusClient = require("node-radius-client"); @@ -520,6 +521,46 @@ exports.redisPingAsync = function (dsn, rejectUnauthorized) { }); }; +/** + * Retrieve value of setting based on key + * @param {string} key Key of setting to retrieve + * @returns {Promise} Value + * @deprecated Use await Settings.get(key) + */ +exports.setting = async function (key) { + return await Settings.get(key); +}; + +/** + * Sets the specified setting to specified value + * @param {string} key Key of setting to set + * @param {any} value Value to set to + * @param {?string} type Type of setting + * @returns {Promise} + */ +exports.setSetting = async function (key, value, type = null) { + await Settings.set(key, value, type); +}; + +/** + * Get settings based on type + * @param {string} type The type of setting + * @returns {Promise} Settings of requested type + */ +exports.getSettings = async function (type) { + return await Settings.getSettings(type); +}; + +/** + * Set settings based on type + * @param {string} type Type of settings to set + * @param {object} data Values of settings + * @returns {Promise} + */ +exports.setSettings = async function (type, data) { + await Settings.setSettings(type, data); +}; + // ssl-checker by @dyaa //https://github.com/dyaa/ssl-checker/blob/master/src/index.ts diff --git a/src/util.js b/src/util.js index 370004e0b..b1da34922 100644 --- a/src/util.js +++ b/src/util.js @@ -8,34 +8,17 @@ // Backend uses the compiled file util.js // Frontend uses util.ts */ -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.sleep = exports.flipStatus = exports.badgeConstants = exports.CONSOLE_STYLE_BgGray = exports.CONSOLE_STYLE_BgWhite = exports.CONSOLE_STYLE_BgCyan = exports.CONSOLE_STYLE_BgMagenta = exports.CONSOLE_STYLE_BgBlue = exports.CONSOLE_STYLE_BgYellow = exports.CONSOLE_STYLE_BgGreen = exports.CONSOLE_STYLE_BgRed = exports.CONSOLE_STYLE_BgBlack = exports.CONSOLE_STYLE_FgPink = exports.CONSOLE_STYLE_FgBrown = exports.CONSOLE_STYLE_FgViolet = exports.CONSOLE_STYLE_FgLightBlue = exports.CONSOLE_STYLE_FgLightGreen = exports.CONSOLE_STYLE_FgOrange = exports.CONSOLE_STYLE_FgGray = exports.CONSOLE_STYLE_FgWhite = exports.CONSOLE_STYLE_FgCyan = exports.CONSOLE_STYLE_FgMagenta = exports.CONSOLE_STYLE_FgBlue = exports.CONSOLE_STYLE_FgYellow = exports.CONSOLE_STYLE_FgGreen = exports.CONSOLE_STYLE_FgRed = exports.CONSOLE_STYLE_FgBlack = exports.CONSOLE_STYLE_Hidden = exports.CONSOLE_STYLE_Reverse = exports.CONSOLE_STYLE_Blink = exports.CONSOLE_STYLE_Underscore = exports.CONSOLE_STYLE_Dim = exports.CONSOLE_STYLE_Bright = exports.CONSOLE_STYLE_Reset = exports.MIN_INTERVAL_SECOND = exports.MAX_INTERVAL_SECOND = exports.SQL_DATETIME_FORMAT_WITHOUT_SECOND = exports.SQL_DATETIME_FORMAT = exports.SQL_DATE_FORMAT = exports.STATUS_PAGE_MAINTENANCE = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.MAINTENANCE = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isNode = exports.isDev = void 0; -exports.evaluateJsonQuery = exports.intHash = exports.localToUTC = exports.utcToLocal = exports.utcToISODateTime = exports.isoToUTCDateTime = exports.parseTimeFromTimeObject = exports.parseTimeObject = exports.getMaintenanceRelativeURL = exports.getMonitorRelativeURL = exports.genSecret = exports.getCryptoRandomInt = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.log = exports.ucfirst = void 0; +exports.evaluateJsonQuery = exports.intHash = exports.localToUTC = exports.utcToLocal = exports.utcToISODateTime = exports.isoToUTCDateTime = exports.parseTimeFromTimeObject = exports.parseTimeObject = exports.getMaintenanceRelativeURL = exports.getMonitorRelativeURL = exports.genSecret = exports.getCryptoRandomInt = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.log = exports.debug = exports.ucfirst = void 0; +exports.intHash = exports.localToUTC = exports.utcToLocal = exports.utcToISODateTime = exports.isoToUTCDateTime = exports.parseTimeFromTimeObject = exports.parseTimeObject = exports.getMaintenanceRelativeURL = exports.getMonitorRelativeURL = exports.genSecret = exports.getCryptoRandomInt = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.log = exports.debug = exports.ucfirst = void 0; const dayjs_1 = __importDefault(require("dayjs")); -const jsonata = __importStar(require("jsonata")); +const dayjs = require("dayjs"); +const jsonata = require("jsonata"); exports.isDev = process.env.NODE_ENV === "development"; exports.isNode = typeof process !== "undefined" && ((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node); exports.appName = "Uptime Kuma"; @@ -142,6 +125,10 @@ function ucfirst(str) { return firstLetter.toUpperCase() + str.substr(1); } exports.ucfirst = ucfirst; +function debug(msg) { + exports.log.log("", msg, "debug"); +} +exports.debug = debug; class Logger { constructor() { this.hideLog = { @@ -169,6 +156,8 @@ class Logger { if (this.hideLog[level] && this.hideLog[level].includes(module.toLowerCase())) { return; } + module = module.toUpperCase(); + level = level.toUpperCase(); let now; if (dayjs_1.default.tz) { now = dayjs_1.default.tz(new Date()).format(); @@ -178,20 +167,10 @@ class Logger { } const levelColor = consoleLevelColors[level]; const moduleColor = consoleModuleColors[intHash(module, consoleModuleColors.length)]; - let timePart = now; - let modulePart = module; - let levelPart = level; - let msgPart = msg; - if (process.env.UPTIME_KUMA_LOG_FORMAT === "json") { - console.log(JSON.stringify({ - time: timePart, - module: modulePart, - level: levelPart, - msg: typeof msg === "string" ? msg : JSON.stringify(msg), - })); - return; - } - module = module.toUpperCase(); + let timePart; + let modulePart; + let levelPart; + let msgPart; if (exports.isNode) { switch (level) { case "DEBUG": @@ -208,17 +187,28 @@ class Logger { if (typeof msg === "string") { msgPart = exports.CONSOLE_STYLE_FgRed + msg + exports.CONSOLE_STYLE_Reset; } + else { + msgPart = msg; + } break; case "DEBUG": if (typeof msg === "string") { msgPart = exports.CONSOLE_STYLE_FgGray + msg + exports.CONSOLE_STYLE_Reset; } + else { + msgPart = msg; + } + break; + default: + msgPart = msg; break; } } else { + timePart = now; modulePart = `[${module}]`; levelPart = `${level}:`; + msgPart = msg; } switch (level) { case "ERROR": @@ -241,23 +231,23 @@ class Logger { } } info(module, msg) { - this.log(module, msg, "INFO"); + this.log(module, msg, "info"); } warn(module, msg) { - this.log(module, msg, "WARN"); + this.log(module, msg, "warn"); } error(module, msg) { - this.log(module, msg, "ERROR"); + this.log(module, msg, "error"); } debug(module, msg) { - this.log(module, msg, "DEBUG"); + this.log(module, msg, "debug"); } exception(module, exception, msg) { let finalMessage = exception; if (msg) { finalMessage = `${msg}: ${exception}`; } - this.log(module, finalMessage, "ERROR"); + this.log(module, finalMessage, "error"); } } exports.log = new Logger(); diff --git a/src/util.ts b/src/util.ts index fa71a48fa..efc8f3a89 100644 --- a/src/util.ts +++ b/src/util.ts @@ -156,6 +156,15 @@ export function ucfirst(str: string) { return firstLetter.toUpperCase() + str.substr(1); } +/** + * @deprecated Use log.debug (https://github.com/louislam/uptime-kuma/pull/910) + * @param msg Message to write + * @returns {void} + */ +export function debug(msg: unknown) { + log.log("", msg, "debug"); +} + class Logger { /** @@ -197,13 +206,12 @@ class Logger { /** * Write a message to the log - * @private * @param module The module the log comes from * @param msg Message to write - * @param level {"INFO"|"WARN"|"ERROR"|"DEBUG"} Log level + * @param level Log level. One of INFO, WARN, ERROR, DEBUG or can be customized. * @returns {void} */ - log(module: string, msg: unknown, level: "INFO"|"WARN"|"ERROR"|"DEBUG"): void { + log(module: string, msg: any, level: string) { if (level === "DEBUG" && !isDev) { return; } @@ -212,6 +220,9 @@ class Logger { return; } + module = module.toUpperCase(); + level = level.toUpperCase(); + let now; if (dayjs.tz) { now = dayjs.tz(new Date()).format(); @@ -222,23 +233,10 @@ class Logger { const levelColor = consoleLevelColors[level]; const moduleColor = consoleModuleColors[intHash(module, consoleModuleColors.length)]; - let timePart: string = now; - let modulePart: string = module; - let levelPart: string = level; - let msgPart: unknown = msg; - - if (process.env.UPTIME_KUMA_LOG_FORMAT === "json") { - console.log(JSON.stringify({ - time: timePart, - module: modulePart, - level: levelPart, - msg: typeof msg === "string" ? msg : JSON.stringify(msg), - })); - return; - } - - // Console rendering: - module = module.toUpperCase(); + let timePart: string; + let modulePart: string; + let levelPart: string; + let msgPart: string; if (isNode) { // Add console colors @@ -259,18 +257,27 @@ class Logger { case "ERROR": if (typeof msg === "string") { msgPart = CONSOLE_STYLE_FgRed + msg + CONSOLE_STYLE_Reset; + } else { + msgPart = msg; } break; case "DEBUG": if (typeof msg === "string") { msgPart = CONSOLE_STYLE_FgGray + msg + CONSOLE_STYLE_Reset; + } else { + msgPart = msg; } break; + default: + msgPart = msg; + break; } } else { // No console colors + timePart = now; modulePart = `[${module}]`; levelPart = `${level}:`; + msgPart = msg; } // Write to console @@ -301,8 +308,8 @@ class Logger { * @param msg Message to write * @returns {void} */ - info(module: string, msg: string): void { - this.log(module, msg, "INFO"); + info(module: string, msg: unknown) { + this.log(module, msg, "info"); } /** @@ -311,8 +318,8 @@ class Logger { * @param msg Message to write * @returns {void} */ - warn(module: string, msg: string): void { - this.log(module, msg, "WARN"); + warn(module: string, msg: unknown) { + this.log(module, msg, "warn"); } /** @@ -321,8 +328,8 @@ class Logger { * @param msg Message to write * @returns {void} */ - error(module: string, msg: string): void { - this.log(module, msg, "ERROR"); + error(module: string, msg: unknown) { + this.log(module, msg, "error"); } /** @@ -331,8 +338,8 @@ class Logger { * @param msg Message to write * @returns {void} */ - debug(module: string, msg: string): void { - this.log(module, msg, "DEBUG"); + debug(module: string, msg: unknown) { + this.log(module, msg, "debug"); } /** @@ -349,7 +356,7 @@ class Logger { finalMessage = `${msg}: ${exception}`; } - this.log(module, finalMessage, "ERROR"); + this.log(module, finalMessage, "error"); } } @@ -393,7 +400,7 @@ export class TimeLogger { * @param name Name of monitor * @returns {void} */ - print(name: string): void { + print(name: string) { if (isDev && process.env.TIMELOGGER === "1") { console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms"); }