mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-11-23 23:04:04 +00:00
Merge branch '1.14.X'
# Conflicts: # package.json # server/server.js
This commit is contained in:
commit
29d2d95c71
9 changed files with 112 additions and 83 deletions
|
@ -37,7 +37,7 @@
|
||||||
"build-docker-nightly-alpine": "docker buildx build -f docker/dockerfile-alpine --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:nightly-alpine --target nightly . --push",
|
"build-docker-nightly-alpine": "docker buildx build -f docker/dockerfile-alpine --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:nightly-alpine --target nightly . --push",
|
||||||
"build-docker-nightly-amd64": "docker buildx build -f docker/dockerfile --platform linux/amd64 -t louislam/uptime-kuma:nightly-amd64 --target nightly . --push --progress plain",
|
"build-docker-nightly-amd64": "docker buildx build -f docker/dockerfile --platform linux/amd64 -t louislam/uptime-kuma:nightly-amd64 --target nightly . --push --progress plain",
|
||||||
"upload-artifacts": "docker buildx build -f docker/dockerfile --platform linux/amd64 -t louislam/uptime-kuma:upload-artifact --build-arg VERSION --build-arg GITHUB_TOKEN --target upload-artifact . --progress plain",
|
"upload-artifacts": "docker buildx build -f docker/dockerfile --platform linux/amd64 -t louislam/uptime-kuma:upload-artifact --build-arg VERSION --build-arg GITHUB_TOKEN --target upload-artifact . --progress plain",
|
||||||
"setup": "git checkout 1.14.0 && npm ci --production && npm run download-dist",
|
"setup": "git checkout 1.14.1 && npm ci --production && npm run download-dist",
|
||||||
"download-dist": "node extra/download-dist.js",
|
"download-dist": "node extra/download-dist.js",
|
||||||
"mark-as-nightly": "node extra/mark-as-nightly.js",
|
"mark-as-nightly": "node extra/mark-as-nightly.js",
|
||||||
"reset-password": "node extra/reset-password.js",
|
"reset-password": "node extra/reset-password.js",
|
||||||
|
|
|
@ -3,7 +3,8 @@
|
||||||
*/
|
*/
|
||||||
const { TimeLogger } = require("../src/util");
|
const { TimeLogger } = require("../src/util");
|
||||||
const { R } = require("redbean-node");
|
const { R } = require("redbean-node");
|
||||||
const { io } = require("./server");
|
const { UptimeKumaServer } = require("./uptime-kuma-server");
|
||||||
|
const io = UptimeKumaServer.getInstance().io;
|
||||||
const { setting } = require("./util-server");
|
const { setting } = require("./util-server");
|
||||||
const checkVersion = require("./check-version");
|
const checkVersion = require("./check-version");
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ const HttpProxyAgent = require("http-proxy-agent");
|
||||||
const HttpsProxyAgent = require("https-proxy-agent");
|
const HttpsProxyAgent = require("https-proxy-agent");
|
||||||
const SocksProxyAgent = require("socks-proxy-agent");
|
const SocksProxyAgent = require("socks-proxy-agent");
|
||||||
const { debug } = require("../src/util");
|
const { debug } = require("../src/util");
|
||||||
const server = require("./server");
|
const { UptimeKumaServer } = require("./uptime-kuma-server");
|
||||||
|
|
||||||
class Proxy {
|
class Proxy {
|
||||||
|
|
||||||
|
@ -151,6 +151,8 @@ class Proxy {
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
static async reloadProxy() {
|
static async reloadProxy() {
|
||||||
|
const server = UptimeKumaServer.getInstance();
|
||||||
|
|
||||||
let updatedList = await R.getAssoc("SELECT id, proxy_id FROM monitor");
|
let updatedList = await R.getAssoc("SELECT id, proxy_id FROM monitor");
|
||||||
|
|
||||||
for (let monitorID in server.monitorList) {
|
for (let monitorID in server.monitorList) {
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
let express = require("express");
|
let express = require("express");
|
||||||
const { allowDevAllOrigin } = require("../util-server");
|
const { allowDevAllOrigin } = require("../util-server");
|
||||||
const { R } = require("redbean-node");
|
const { R } = require("redbean-node");
|
||||||
const server = require("../server");
|
|
||||||
const apicache = require("../modules/apicache");
|
const apicache = require("../modules/apicache");
|
||||||
const Monitor = require("../model/monitor");
|
const Monitor = require("../model/monitor");
|
||||||
const dayjs = require("dayjs");
|
const dayjs = require("dayjs");
|
||||||
const { UP, flipStatus, log } = require("../../src/util");
|
const { UP, flipStatus, log } = require("../../src/util");
|
||||||
const StatusPage = require("../model/status_page");
|
const StatusPage = require("../model/status_page");
|
||||||
|
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
||||||
let router = express.Router();
|
let router = express.Router();
|
||||||
|
|
||||||
let cache = apicache.middleware;
|
let cache = apicache.middleware;
|
||||||
|
const server = UptimeKumaServer.getInstance();
|
||||||
let io = server.io;
|
let io = server.io;
|
||||||
|
|
||||||
router.get("/api/entry-page", async (request, response) => {
|
router.get("/api/entry-page", async (request, response) => {
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
/*
|
||||||
|
* Uptime Kuma Server
|
||||||
|
* node "server/server.js"
|
||||||
|
* DO NOT require("./server") in other modules, it likely creates circular dependency!
|
||||||
|
*/
|
||||||
console.log("Welcome to Uptime Kuma");
|
console.log("Welcome to Uptime Kuma");
|
||||||
|
|
||||||
// Check Node.js Version
|
// Check Node.js Version
|
||||||
|
@ -26,14 +31,10 @@ log.info("server", "Node Env: " + process.env.NODE_ENV);
|
||||||
|
|
||||||
log.info("server", "Importing Node libraries");
|
log.info("server", "Importing Node libraries");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const http = require("http");
|
|
||||||
const https = require("https");
|
|
||||||
|
|
||||||
log.info("server", "Importing 3rd-party libraries");
|
log.info("server", "Importing 3rd-party libraries");
|
||||||
log.debug("server", "Importing express");
|
log.debug("server", "Importing express");
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
log.debug("server", "Importing socket.io");
|
|
||||||
const { Server } = require("socket.io");
|
|
||||||
log.debug("server", "Importing redbean-node");
|
log.debug("server", "Importing redbean-node");
|
||||||
const { R } = require("redbean-node");
|
const { R } = require("redbean-node");
|
||||||
log.debug("server", "Importing jsonwebtoken");
|
log.debug("server", "Importing jsonwebtoken");
|
||||||
|
@ -50,26 +51,10 @@ log.debug("server", "Importing 2FA Modules");
|
||||||
const notp = require("notp");
|
const notp = require("notp");
|
||||||
const base32 = require("thirty-two");
|
const base32 = require("thirty-two");
|
||||||
|
|
||||||
/**
|
const { UptimeKumaServer } = require("./uptime-kuma-server");
|
||||||
* `module.exports` (alias: `server`) should be inside this class, in order to avoid circular dependency issue.
|
const server = UptimeKumaServer.getInstance(args);
|
||||||
* @type {UptimeKumaServer}
|
const io = module.exports.io = server.io;
|
||||||
*/
|
const app = server.app;
|
||||||
class UptimeKumaServer {
|
|
||||||
/**
|
|
||||||
* Main monitor list
|
|
||||||
* @type {{}}
|
|
||||||
*/
|
|
||||||
monitorList = {};
|
|
||||||
entryPage = "dashboard";
|
|
||||||
|
|
||||||
async sendMonitorList(socket) {
|
|
||||||
let list = await getMonitorJSONList(socket.userID);
|
|
||||||
io.to(socket.userID).emit("monitorList", list);
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const server = module.exports = new UptimeKumaServer();
|
|
||||||
|
|
||||||
log.info("server", "Importing this project modules");
|
log.info("server", "Importing this project modules");
|
||||||
log.debug("server", "Importing Monitor");
|
log.debug("server", "Importing Monitor");
|
||||||
|
@ -112,10 +97,7 @@ const port = [ args.port, process.env.UPTIME_KUMA_PORT, process.env.PORT, 3001 ]
|
||||||
.map(portValue => parseInt(portValue))
|
.map(portValue => parseInt(portValue))
|
||||||
.find(portValue => !isNaN(portValue));
|
.find(portValue => !isNaN(portValue));
|
||||||
|
|
||||||
// SSL
|
const disableFrameSameOrigin = !!process.env.UPTIME_KUMA_DISABLE_FRAME_SAMEORIGIN || args["disable-frame-sameorigin"] || false;
|
||||||
const sslKey = args["ssl-key"] || process.env.UPTIME_KUMA_SSL_KEY || process.env.SSL_KEY || undefined;
|
|
||||||
const sslCert = args["ssl-cert"] || process.env.UPTIME_KUMA_SSL_CERT || process.env.SSL_CERT || undefined;
|
|
||||||
const disableFrameSameOrigin = args["disable-frame-sameorigin"] || !!process.env.UPTIME_KUMA_DISABLE_FRAME_SAMEORIGIN || false;
|
|
||||||
const cloudflaredToken = args["cloudflared-token"] || process.env.UPTIME_KUMA_CLOUDFLARED_TOKEN || undefined;
|
const cloudflaredToken = args["cloudflared-token"] || process.env.UPTIME_KUMA_CLOUDFLARED_TOKEN || undefined;
|
||||||
|
|
||||||
// 2FA / notp verification defaults
|
// 2FA / notp verification defaults
|
||||||
|
@ -134,25 +116,6 @@ if (config.demoMode) {
|
||||||
log.info("server", "==== Demo Mode ====");
|
log.info("server", "==== Demo Mode ====");
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("server", "Creating express and socket.io instance");
|
|
||||||
const app = express();
|
|
||||||
|
|
||||||
let httpServer;
|
|
||||||
|
|
||||||
if (sslKey && sslCert) {
|
|
||||||
log.info("server", "Server Type: HTTPS");
|
|
||||||
httpServer = https.createServer({
|
|
||||||
key: fs.readFileSync(sslKey),
|
|
||||||
cert: fs.readFileSync(sslCert)
|
|
||||||
}, app);
|
|
||||||
} else {
|
|
||||||
log.info("server", "Server Type: HTTP");
|
|
||||||
httpServer = http.createServer(app);
|
|
||||||
}
|
|
||||||
|
|
||||||
const io = new Server(httpServer);
|
|
||||||
module.exports.io = io;
|
|
||||||
|
|
||||||
// Must be after io instantiation
|
// Must be after io instantiation
|
||||||
const { sendNotificationList, sendHeartbeatList, sendImportantHeartbeatList, sendInfo, sendProxyList } = require("./client");
|
const { sendNotificationList, sendHeartbeatList, sendImportantHeartbeatList, sendInfo, sendProxyList } = require("./client");
|
||||||
const { statusPageSocketHandler } = require("./socket-handlers/status-page-socket-handler");
|
const { statusPageSocketHandler } = require("./socket-handlers/status-page-socket-handler");
|
||||||
|
@ -1483,12 +1446,12 @@ try {
|
||||||
|
|
||||||
log.info("server", "Init the server");
|
log.info("server", "Init the server");
|
||||||
|
|
||||||
httpServer.once("error", async (err) => {
|
server.httpServer.once("error", async (err) => {
|
||||||
console.error("Cannot listen: " + err.message);
|
console.error("Cannot listen: " + err.message);
|
||||||
await shutdownFunction();
|
await shutdownFunction();
|
||||||
});
|
});
|
||||||
|
|
||||||
httpServer.listen(port, hostname, () => {
|
server.httpServer.listen(port, hostname, () => {
|
||||||
if (hostname) {
|
if (hostname) {
|
||||||
log.info("server", `Listening on ${hostname}:${port}`);
|
log.info("server", `Listening on ${hostname}:${port}`);
|
||||||
} else {
|
} else {
|
||||||
|
@ -1578,32 +1541,6 @@ async function afterLogin(socket, user) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a list of monitors for the given user.
|
|
||||||
* @param {string} userID - The ID of the user to get monitors for.
|
|
||||||
* @returns {Promise<Object>} A promise that resolves to an object with monitor IDs as keys and monitor objects as values.
|
|
||||||
*
|
|
||||||
* Generated by Trelent
|
|
||||||
*/
|
|
||||||
async function getMonitorJSONList(userID) {
|
|
||||||
let result = {};
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connect to the database and patch it if necessary.
|
|
||||||
*
|
|
||||||
* Generated by Trelent
|
|
||||||
*/
|
|
||||||
async function initDatabase(testMode = false) {
|
async function initDatabase(testMode = false) {
|
||||||
if (! fs.existsSync(Database.path)) {
|
if (! fs.existsSync(Database.path)) {
|
||||||
log.info("server", "Copying Database");
|
log.info("server", "Copying Database");
|
||||||
|
@ -1740,7 +1677,7 @@ function finalFunction() {
|
||||||
log.info("server", "Graceful shutdown successful!");
|
log.info("server", "Graceful shutdown successful!");
|
||||||
}
|
}
|
||||||
|
|
||||||
gracefulShutdown(httpServer, {
|
gracefulShutdown(server.httpServer, {
|
||||||
signals: "SIGINT SIGTERM",
|
signals: "SIGINT SIGTERM",
|
||||||
timeout: 30000, // timeout: 30 secs
|
timeout: 30000, // timeout: 30 secs
|
||||||
development: false, // not in dev mode
|
development: false, // not in dev mode
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
const { checkLogin, setSetting, setting, doubleCheckPassword } = require("../util-server");
|
const { checkLogin, setSetting, setting, doubleCheckPassword } = require("../util-server");
|
||||||
const { CloudflaredTunnel } = require("node-cloudflared-tunnel");
|
const { CloudflaredTunnel } = require("node-cloudflared-tunnel");
|
||||||
const { io } = require("../server");
|
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
||||||
|
const io = UptimeKumaServer.getInstance().io;
|
||||||
|
|
||||||
const prefix = "cloudflared_";
|
const prefix = "cloudflared_";
|
||||||
const cloudflared = new CloudflaredTunnel();
|
const cloudflared = new CloudflaredTunnel();
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
const { checkLogin } = require("../util-server");
|
const { checkLogin } = require("../util-server");
|
||||||
const { Proxy } = require("../proxy");
|
const { Proxy } = require("../proxy");
|
||||||
const { sendProxyList } = require("../client");
|
const { sendProxyList } = require("../client");
|
||||||
const server = require("../server");
|
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
||||||
|
const server = UptimeKumaServer.getInstance();
|
||||||
|
|
||||||
module.exports.proxySocketHandler = (socket) => {
|
module.exports.proxySocketHandler = (socket) => {
|
||||||
socket.on("addProxy", async (proxy, proxyID, callback) => {
|
socket.on("addProxy", async (proxy, proxyID, callback) => {
|
||||||
|
|
|
@ -6,7 +6,7 @@ const ImageDataURI = require("../image-data-uri");
|
||||||
const Database = require("../database");
|
const Database = require("../database");
|
||||||
const apicache = require("../modules/apicache");
|
const apicache = require("../modules/apicache");
|
||||||
const StatusPage = require("../model/status_page");
|
const StatusPage = require("../model/status_page");
|
||||||
const server = require("../server");
|
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
||||||
|
|
||||||
module.exports.statusPageSocketHandler = (socket) => {
|
module.exports.statusPageSocketHandler = (socket) => {
|
||||||
|
|
||||||
|
@ -215,6 +215,8 @@ module.exports.statusPageSocketHandler = (socket) => {
|
||||||
];
|
];
|
||||||
await R.exec(`DELETE FROM \`group\` WHERE id NOT IN (${slots}) AND status_page_id = ?`, data);
|
await R.exec(`DELETE FROM \`group\` WHERE id NOT IN (${slots}) AND status_page_id = ?`, data);
|
||||||
|
|
||||||
|
const server = UptimeKumaServer.getInstance();
|
||||||
|
|
||||||
// Also change entry page to new slug if it is the default one, and slug is changed.
|
// 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) {
|
if (server.entryPage === "statusPage-" + slug && statusPage.slug !== slug) {
|
||||||
server.entryPage = "statusPage-" + statusPage.slug;
|
server.entryPage = "statusPage-" + statusPage.slug;
|
||||||
|
@ -284,6 +286,8 @@ module.exports.statusPageSocketHandler = (socket) => {
|
||||||
|
|
||||||
// Delete a status page
|
// Delete a status page
|
||||||
socket.on("deleteStatusPage", async (slug, callback) => {
|
socket.on("deleteStatusPage", async (slug, callback) => {
|
||||||
|
const server = UptimeKumaServer.getInstance();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
checkLogin(socket);
|
checkLogin(socket);
|
||||||
|
|
||||||
|
|
82
server/uptime-kuma-server.js
Normal file
82
server/uptime-kuma-server.js
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
const express = require("express");
|
||||||
|
const https = require("https");
|
||||||
|
const fs = require("fs");
|
||||||
|
const http = require("http");
|
||||||
|
const { Server } = require("socket.io");
|
||||||
|
const { R } = require("redbean-node");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `module.exports` (alias: `server`) should be inside this class, in order to avoid circular dependency issue.
|
||||||
|
* @type {UptimeKumaServer}
|
||||||
|
*/
|
||||||
|
class UptimeKumaServer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @type {UptimeKumaServer}
|
||||||
|
*/
|
||||||
|
static instance = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main monitor list
|
||||||
|
* @type {{}}
|
||||||
|
*/
|
||||||
|
monitorList = {};
|
||||||
|
entryPage = "dashboard";
|
||||||
|
app = undefined;
|
||||||
|
httpServer = undefined;
|
||||||
|
io = undefined;
|
||||||
|
|
||||||
|
static getInstance(args) {
|
||||||
|
if (UptimeKumaServer.instance == null) {
|
||||||
|
UptimeKumaServer.instance = new UptimeKumaServer(args);
|
||||||
|
}
|
||||||
|
return UptimeKumaServer.instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(args) {
|
||||||
|
// SSL
|
||||||
|
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;
|
||||||
|
|
||||||
|
console.log("Creating express and socket.io instance");
|
||||||
|
this.app = express();
|
||||||
|
|
||||||
|
if (sslKey && sslCert) {
|
||||||
|
console.log("Server Type: HTTPS");
|
||||||
|
this.httpServer = https.createServer({
|
||||||
|
key: fs.readFileSync(sslKey),
|
||||||
|
cert: fs.readFileSync(sslCert)
|
||||||
|
}, this.app);
|
||||||
|
} else {
|
||||||
|
console.log("Server Type: HTTP");
|
||||||
|
this.httpServer = http.createServer(this.app);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.io = new Server(this.httpServer);
|
||||||
|
}
|
||||||
|
|
||||||
|
async sendMonitorList(socket) {
|
||||||
|
let list = await this.getMonitorJSONList(socket.userID);
|
||||||
|
this.io.to(socket.userID).emit("monitorList", list);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getMonitorJSONList(userID) {
|
||||||
|
let result = {};
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
UptimeKumaServer
|
||||||
|
};
|
Loading…
Reference in a new issue