From 4490a05123dfb0e4e0a7449191fdd250f8400188 Mon Sep 17 00:00:00 2001 From: Hemanth Date: Fri, 27 Jun 2025 22:55:34 +0530 Subject: [PATCH] eslint fix --- server/model/monitor.js | 4 +- server/monitor-types/rtsp.js | 101 +++++++++++++++-------------------- server/server.js | 6 +-- server/uptime-kuma-server.js | 2 +- 4 files changed, 48 insertions(+), 65 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index dbc365487..c8d7ddc6f 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -199,9 +199,7 @@ class Monitor extends BeanModel { rabbitmqPassword: this.rabbitmqPassword, rtspUsername: this.rtspUsername, rtspPassword: this.rtspPassword, - rtspPath:this.rtspPath - - + rtspPath: this.rtspPath }; } diff --git a/server/monitor-types/rtsp.js b/server/monitor-types/rtsp.js index a52e40aa4..e6a8571c3 100644 --- a/server/monitor-types/rtsp.js +++ b/server/monitor-types/rtsp.js @@ -1,68 +1,53 @@ const RTSPClient = require("rtsp-client"); -const { log, UP, DOWN } = require("../../src/util"); +const { log, UP, DOWN } = require("../../src/util"); + class RtspMonitorType { - name = "rtsp"; + name = "rtsp"; - /** - * @param {Object} monitor - monitor config containing hostname, port, username, password, path, and timeout - * @param {Object} heartbeat - object to update with status and message - */ - async check(monitor, heartbeat) { - const { rtsp_username, rtsp_password, hostname, port, rtsp_path, timeout } = monitor; - const timeoutMs = (timeout || 10) * 1000; - - // Construct the RTSP URL from individual components - let url = `rtsp://${hostname}:${port}${rtsp_path}`; + /** + * Check the availability of an RTSP stream. + * @param {object} monitor - Monitor config: hostname, port, username, password, path. + * @param {object} heartbeat - Heartbeat object to update with status and message. + */ + async check(monitor, heartbeat) { + const { rtspUsername, rtspPassword, hostname, port, rtspPath } = monitor; - // If username and password are provided, inject them into the URL - if (rtsp_username && rtsp_password !== undefined) { - const auth = `${rtsp_username}:${rtsp_password}@`; - const urlPattern = /^rtsp:\/\//; + // Construct RTSP URL + let url = `rtsp://${hostname}:${port}${rtspPath}`; + if (rtspUsername && rtspPassword !== undefined) { + url = url.replace(/^rtsp:\/\//, `rtsp://${rtspUsername}:${rtspPassword}@`); + } - // Inject authentication details into URL (before host) - url = url.replace(urlPattern, `rtsp://${auth}`); + // Default heartbeat status + heartbeat.status = DOWN; + heartbeat.msg = "Starting RTSP stream check..."; + + // Validate URL + if (!url || !url.startsWith("rtsp://")) { + heartbeat.msg = "Invalid RTSP URL"; + return; + } + + const client = new RTSPClient(); + + try { + await client.connect(url); + await client.describe(); + heartbeat.status = UP; + heartbeat.msg = "RTSP stream is accessible"; + } catch (error) { + heartbeat.msg = `Error: ${error.message}`; + log.debug("monitor", `[${monitor.name}] RTSP check failed: ${error.message}`); + } finally { + try { + await client.close(); + } catch (closeError) { + log.debug("monitor", `Error closing RTSP client: ${closeError.message}`); + } + } } - - heartbeat.status = DOWN; - heartbeat.msg = "Starting RTSP stream check..."; - - if (!url || !url.startsWith("rtsp://")) { - heartbeat.status = DOWN; - heartbeat.msg = "Invalid RTSP URL"; - return; - } - - const client = new RTSPClient(); - - // Timeout promise to kill hanging connections - const timeoutPromise = new Promise((_, reject) => - setTimeout(() => reject(new Error("RTSP connection timed out")), timeoutMs) - ); - - try { - // Use Promise.race to enforce timeout - await Promise.race([ - (async () => { - await client.connect(url); - const describe = await client.describe(); - await client.close(); - - heartbeat.status = UP; - heartbeat.msg = "RTSP stream is accessible"; - })(), - timeoutPromise, - ]); - } catch (error) { - heartbeat.status = DOWN; - heartbeat.msg = `Error: ${error.message}`; - log.debug("monitor", `[${monitor.name}] RTSP check failed: ${error.message}`); - try { - await client.close(); - } catch {} - } - } } module.exports = { - RtspMonitorType, + RtspMonitorType, }; diff --git a/server/server.js b/server/server.js index 9a5042310..e7d480f64 100644 --- a/server/server.js +++ b/server/server.js @@ -877,9 +877,9 @@ let needSetup = false; bean.rabbitmqPassword = monitor.rabbitmqPassword; bean.conditions = JSON.stringify(monitor.conditions); bean.manual_status = monitor.manual_status; - bean.rtspUsername=monitor.rtspUsername - bean.rtspPassword=monitor.rtspPassword - bean.rtspPath=monitor.path + bean.rtspUsername = monitor.rtspUsername; + bean.rtspPassword = monitor.rtspPassword; + bean.rtspPath = monitor.path; // ping advanced options bean.ping_numeric = monitor.ping_numeric; diff --git a/server/uptime-kuma-server.js b/server/uptime-kuma-server.js index 7bd7f6196..bad37f7f4 100644 --- a/server/uptime-kuma-server.js +++ b/server/uptime-kuma-server.js @@ -561,5 +561,5 @@ const { SNMPMonitorType } = require("./monitor-types/snmp"); const { MongodbMonitorType } = require("./monitor-types/mongodb"); const { RabbitMqMonitorType } = require("./monitor-types/rabbitmq"); const { ManualMonitorType } = require("./monitor-types/manual"); -const {RtspMonitorType}= require("./monitor-types/rtsp") +const { RtspMonitorType } = require("./monitor-types/rtsp"); const Monitor = require("./model/monitor");