2021-09-04 18:03:40 +00:00
|
|
|
/*
|
|
|
|
* For Client Socket
|
|
|
|
*/
|
|
|
|
const { TimeLogger } = require("../src/util");
|
|
|
|
const { R } = require("redbean-node");
|
2022-04-19 07:38:59 +00:00
|
|
|
const { UptimeKumaServer } = require("./uptime-kuma-server");
|
2022-10-09 18:28:03 +00:00
|
|
|
const server = UptimeKumaServer.getInstance();
|
|
|
|
const io = server.io;
|
2021-10-08 12:03:52 +00:00
|
|
|
const checkVersion = require("./check-version");
|
2024-09-24 09:51:21 +00:00
|
|
|
const { Settings } = require("./settings");
|
2024-03-26 07:17:52 +00:00
|
|
|
const Database = require("./database");
|
2021-09-04 18:03:40 +00:00
|
|
|
|
2022-04-20 18:56:40 +00:00
|
|
|
/**
|
|
|
|
* Send list of notification providers to client
|
|
|
|
* @param {Socket} socket Socket.io socket instance
|
2023-08-11 07:46:41 +00:00
|
|
|
* @returns {Promise<Bean[]>} List of notifications
|
2022-04-20 18:56:40 +00:00
|
|
|
*/
|
2021-09-04 18:03:40 +00:00
|
|
|
async function sendNotificationList(socket) {
|
|
|
|
const timeLogger = new TimeLogger();
|
|
|
|
|
|
|
|
let result = [];
|
|
|
|
let list = await R.find("notification", " user_id = ? ", [
|
|
|
|
socket.userID,
|
|
|
|
]);
|
|
|
|
|
|
|
|
for (let bean of list) {
|
2022-06-15 11:11:52 +00:00
|
|
|
let notificationObject = bean.export();
|
|
|
|
notificationObject.isDefault = (notificationObject.isDefault === 1);
|
|
|
|
notificationObject.active = (notificationObject.active === 1);
|
|
|
|
result.push(notificationObject);
|
2021-09-04 18:03:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 12:03:52 +00:00
|
|
|
io.to(socket.userID).emit("notificationList", result);
|
2021-09-04 18:03:40 +00:00
|
|
|
|
|
|
|
timeLogger.print("Send Notification List");
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send Heartbeat History list to socket
|
2022-04-20 18:56:40 +00:00
|
|
|
* @param {Socket} socket Socket.io instance
|
|
|
|
* @param {number} monitorID ID of monitor to send heartbeat history
|
2023-08-11 07:46:41 +00:00
|
|
|
* @param {boolean} toUser True = send to all browsers with the same user id, False = send to the current browser only
|
|
|
|
* @param {boolean} overwrite Overwrite client-side's heartbeat list
|
2022-04-20 18:56:40 +00:00
|
|
|
* @returns {Promise<void>}
|
2021-09-04 18:03:40 +00:00
|
|
|
*/
|
|
|
|
async function sendHeartbeatList(socket, monitorID, toUser = false, overwrite = false) {
|
2021-09-09 03:57:23 +00:00
|
|
|
let list = await R.getAll(`
|
|
|
|
SELECT * FROM heartbeat
|
|
|
|
WHERE monitor_id = ?
|
2021-09-08 11:54:37 +00:00
|
|
|
ORDER BY time DESC
|
|
|
|
LIMIT 100
|
|
|
|
`, [
|
|
|
|
monitorID,
|
2021-10-08 12:03:52 +00:00
|
|
|
]);
|
2021-09-04 18:03:40 +00:00
|
|
|
|
2021-09-09 03:57:23 +00:00
|
|
|
let result = list.reverse();
|
2021-09-04 18:03:40 +00:00
|
|
|
|
|
|
|
if (toUser) {
|
|
|
|
io.to(socket.userID).emit("heartbeatList", monitorID, result, overwrite);
|
|
|
|
} else {
|
|
|
|
socket.emit("heartbeatList", monitorID, result, overwrite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-21 12:01:22 +00:00
|
|
|
* Important Heart beat list (aka event list)
|
2022-04-20 18:56:40 +00:00
|
|
|
* @param {Socket} socket Socket.io instance
|
|
|
|
* @param {number} monitorID ID of monitor to send heartbeat history
|
2023-08-11 07:46:41 +00:00
|
|
|
* @param {boolean} toUser True = send to all browsers with the same user id, False = send to the current browser only
|
|
|
|
* @param {boolean} overwrite Overwrite client-side's heartbeat list
|
2022-04-20 18:56:40 +00:00
|
|
|
* @returns {Promise<void>}
|
2021-09-04 18:03:40 +00:00
|
|
|
*/
|
|
|
|
async function sendImportantHeartbeatList(socket, monitorID, toUser = false, overwrite = false) {
|
|
|
|
const timeLogger = new TimeLogger();
|
|
|
|
|
|
|
|
let list = await R.find("heartbeat", `
|
|
|
|
monitor_id = ?
|
|
|
|
AND important = 1
|
|
|
|
ORDER BY time DESC
|
|
|
|
LIMIT 500
|
|
|
|
`, [
|
|
|
|
monitorID,
|
2021-10-08 12:03:52 +00:00
|
|
|
]);
|
2021-09-04 18:03:40 +00:00
|
|
|
|
|
|
|
timeLogger.print(`[Monitor: ${monitorID}] sendImportantHeartbeatList`);
|
|
|
|
|
|
|
|
if (toUser) {
|
|
|
|
io.to(socket.userID).emit("importantHeartbeatList", monitorID, list, overwrite);
|
|
|
|
} else {
|
|
|
|
socket.emit("importantHeartbeatList", monitorID, list, overwrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:56:40 +00:00
|
|
|
/**
|
2022-04-21 12:01:22 +00:00
|
|
|
* Emit proxy list to client
|
|
|
|
* @param {Socket} socket Socket.io socket instance
|
2023-08-11 07:46:41 +00:00
|
|
|
* @returns {Promise<Bean[]>} List of proxies
|
2021-10-30 17:37:15 +00:00
|
|
|
*/
|
|
|
|
async function sendProxyList(socket) {
|
|
|
|
const timeLogger = new TimeLogger();
|
|
|
|
|
2022-04-17 07:27:35 +00:00
|
|
|
const list = await R.find("proxy", " user_id = ? ", [ socket.userID ]);
|
2021-10-30 17:37:15 +00:00
|
|
|
io.to(socket.userID).emit("proxyList", list.map(bean => bean.export()));
|
|
|
|
|
|
|
|
timeLogger.print("Send Proxy List");
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2023-02-14 19:49:04 +00:00
|
|
|
/**
|
|
|
|
* Emit API key list to client
|
|
|
|
* @param {Socket} socket Socket.io socket instance
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async function sendAPIKeyList(socket) {
|
|
|
|
const timeLogger = new TimeLogger();
|
|
|
|
|
|
|
|
let result = [];
|
|
|
|
const list = await R.find(
|
|
|
|
"api_key",
|
|
|
|
"user_id=?",
|
|
|
|
[ socket.userID ],
|
|
|
|
);
|
|
|
|
|
|
|
|
for (let bean of list) {
|
|
|
|
result.push(bean.toPublicJSON());
|
|
|
|
}
|
|
|
|
|
|
|
|
io.to(socket.userID).emit("apiKeyList", result);
|
|
|
|
timeLogger.print("Sent API Key List");
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2021-11-10 05:24:31 +00:00
|
|
|
/**
|
|
|
|
* Emits the version information to the client.
|
2022-04-20 18:56:40 +00:00
|
|
|
* @param {Socket} socket Socket.io socket instance
|
2023-08-11 07:46:41 +00:00
|
|
|
* @param {boolean} hideVersion Should we hide the version information in the response?
|
2022-04-20 18:56:40 +00:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2023-07-14 10:02:49 +00:00
|
|
|
async function sendInfo(socket, hideVersion = false) {
|
|
|
|
let version;
|
|
|
|
let latestVersion;
|
2023-08-28 08:15:48 +00:00
|
|
|
let isContainer;
|
2024-03-26 07:17:52 +00:00
|
|
|
let dbType;
|
2023-07-14 10:02:49 +00:00
|
|
|
|
|
|
|
if (!hideVersion) {
|
|
|
|
version = checkVersion.version;
|
|
|
|
latestVersion = checkVersion.latestVersion;
|
2023-08-28 08:15:48 +00:00
|
|
|
isContainer = (process.env.UPTIME_KUMA_IS_CONTAINER === "1");
|
2024-03-26 07:17:52 +00:00
|
|
|
dbType = Database.dbConfig.type;
|
2023-07-14 10:02:49 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 12:03:52 +00:00
|
|
|
socket.emit("info", {
|
2023-07-14 10:02:49 +00:00
|
|
|
version,
|
|
|
|
latestVersion,
|
2023-08-28 08:15:48 +00:00
|
|
|
isContainer,
|
2024-09-24 09:51:21 +00:00
|
|
|
primaryBaseURL: await Settings.get("primaryBaseURL"),
|
2024-03-26 07:17:52 +00:00
|
|
|
dbType,
|
2022-10-09 18:28:03 +00:00
|
|
|
serverTimezone: await server.getTimezone(),
|
2022-10-11 10:23:17 +00:00
|
|
|
serverTimezoneOffset: server.getTimezoneOffset(),
|
2021-10-08 12:03:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-22 15:57:40 +00:00
|
|
|
/**
|
|
|
|
* Send list of docker hosts to client
|
|
|
|
* @param {Socket} socket Socket.io socket instance
|
2023-08-11 07:46:41 +00:00
|
|
|
* @returns {Promise<Bean[]>} List of docker hosts
|
2022-07-22 15:57:40 +00:00
|
|
|
*/
|
2022-07-22 15:47:04 +00:00
|
|
|
async function sendDockerHostList(socket) {
|
|
|
|
const timeLogger = new TimeLogger();
|
|
|
|
|
|
|
|
let result = [];
|
|
|
|
let list = await R.find("docker_host", " user_id = ? ", [
|
|
|
|
socket.userID,
|
|
|
|
]);
|
|
|
|
|
|
|
|
for (let bean of list) {
|
2022-07-30 11:48:12 +00:00
|
|
|
result.push(bean.toJSON());
|
2022-07-22 15:47:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
io.to(socket.userID).emit("dockerHostList", result);
|
|
|
|
|
|
|
|
timeLogger.print("Send Docker Host List");
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2023-12-01 07:29:10 +00:00
|
|
|
/**
|
|
|
|
* Send list of docker hosts to client
|
|
|
|
* @param {Socket} socket Socket.io socket instance
|
|
|
|
* @returns {Promise<Bean[]>} List of docker hosts
|
|
|
|
*/
|
|
|
|
async function sendRemoteBrowserList(socket) {
|
|
|
|
const timeLogger = new TimeLogger();
|
|
|
|
|
|
|
|
let result = [];
|
|
|
|
let list = await R.find("remote_browser", " user_id = ? ", [
|
|
|
|
socket.userID,
|
|
|
|
]);
|
|
|
|
|
|
|
|
for (let bean of list) {
|
|
|
|
result.push(bean.toJSON());
|
|
|
|
}
|
|
|
|
|
|
|
|
io.to(socket.userID).emit("remoteBrowserList", result);
|
|
|
|
|
|
|
|
timeLogger.print("Send Remote Browser List");
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2024-08-30 19:48:13 +00:00
|
|
|
/**
|
|
|
|
* Send list of monitor types to client
|
|
|
|
* @param {Socket} socket Socket.io socket instance
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async function sendMonitorTypeList(socket) {
|
|
|
|
const result = Object.entries(UptimeKumaServer.monitorTypeList).map(([ key, type ]) => {
|
|
|
|
return [ key, {
|
|
|
|
supportsConditions: type.supportsConditions,
|
|
|
|
conditionVariables: type.conditionVariables.map(v => {
|
|
|
|
return {
|
|
|
|
id: v.id,
|
|
|
|
operators: v.operators.map(o => {
|
|
|
|
return {
|
|
|
|
id: o.id,
|
|
|
|
caption: o.caption,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
}];
|
|
|
|
});
|
|
|
|
|
|
|
|
io.to(socket.userID).emit("monitorTypeList", Object.fromEntries(result));
|
|
|
|
}
|
|
|
|
|
2021-09-04 18:03:40 +00:00
|
|
|
module.exports = {
|
|
|
|
sendNotificationList,
|
|
|
|
sendImportantHeartbeatList,
|
|
|
|
sendHeartbeatList,
|
2021-10-30 17:37:15 +00:00
|
|
|
sendProxyList,
|
2023-02-14 19:49:04 +00:00
|
|
|
sendAPIKeyList,
|
2021-10-30 17:37:15 +00:00
|
|
|
sendInfo,
|
2023-12-01 07:29:10 +00:00
|
|
|
sendDockerHostList,
|
|
|
|
sendRemoteBrowserList,
|
2024-08-30 19:48:13 +00:00
|
|
|
sendMonitorTypeList,
|
2021-10-08 12:03:52 +00:00
|
|
|
};
|