Merge branch 'master' into 1.23.X-merge-to-2.X.X

# Conflicts:
#	docker/debian-base.dockerfile
#	package-lock.json
#	server/database.js
#	server/model/monitor.js
#	server/uptime-kuma-server.js
#	server/util-server.js
This commit is contained in:
Louis Lam
2023-11-13 21:15:51 +08:00
316 changed files with 9487 additions and 4997 deletions

View File

@@ -21,7 +21,7 @@ const axios = require("axios");
*/
class UptimeKumaServer {
/**
*
* Current server instance
* @type {UptimeKumaServer}
*/
static instance = null;
@@ -50,7 +50,6 @@ class UptimeKumaServer {
indexHTML = "";
/**
*
* @type {{}}
*/
static monitorTypeList = {
@@ -65,6 +64,12 @@ class UptimeKumaServer {
checkMonitorsInterval = null;
/**
* Get the current instance of the server if it exists, otherwise
* create a new instance.
* @param {object} args Arguments to pass to instance constructor
* @returns {UptimeKumaServer} Server instance
*/
static getInstance(args) {
if (UptimeKumaServer.instance == null) {
UptimeKumaServer.instance = new UptimeKumaServer(args);
@@ -72,12 +77,18 @@ class UptimeKumaServer {
return UptimeKumaServer.instance;
}
/**
* @param {object} args Arguments to initialise server with
*/
constructor(args) {
// SSL
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 sslKeyPassphrase = args["ssl-key-passphrase"] || process.env.UPTIME_KUMA_SSL_KEY_PASSPHRASE || process.env.SSL_KEY_PASSPHRASE || undefined;
// Set axios default user-agent to Uptime-Kuma/version
axios.defaults.headers.common["User-Agent"] = this.getUserAgent();
// Set default axios timeout to 5 minutes instead of infinity
axios.defaults.timeout = 300 * 1000;
@@ -108,11 +119,15 @@ class UptimeKumaServer {
// Set Monitor Types
UptimeKumaServer.monitorTypeList["real-browser"] = new RealBrowserMonitorType();
UptimeKumaServer.monitorTypeList["tailscale-ping"] = new TailscalePing();
UptimeKumaServer.monitorTypeList["dns"] = new DnsMonitorType();
this.io = new Server(this.httpServer);
}
/** Initialise app after the database has been set up */
/**
* Initialise app after the database has been set up
* @returns {Promise<void>}
*/
async initAfterDatabaseReady() {
// Static
this.app.use("/screenshots", express.static(Database.screenshotDir));
@@ -129,8 +144,8 @@ class UptimeKumaServer {
/**
* Send list of monitors to client
* @param {Socket} socket
* @returns {Object} List of monitors
* @param {Socket} socket Socket to send list on
* @returns {object} List of monitors
*/
async sendMonitorList(socket) {
let list = await this.getMonitorJSONList(socket.userID);
@@ -141,7 +156,7 @@ class UptimeKumaServer {
/**
* 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.
* @returns {Promise<object>} A promise that resolves to an object with monitor IDs as keys and monitor objects as values.
*
* Generated by Trelent
*/
@@ -162,7 +177,7 @@ class UptimeKumaServer {
/**
* Send maintenance list to client
* @param {Socket} socket Socket.io instance to send to
* @returns {Object}
* @returns {object} Maintenance list
*/
async sendMaintenanceList(socket) {
return await this.sendMaintenanceListByUserID(socket.userID);
@@ -170,8 +185,8 @@ class UptimeKumaServer {
/**
* Send list of maintenances to user
* @param {number} userID
* @returns {Object}
* @param {number} userID User to send list to
* @returns {object} Maintenance list
*/
async sendMaintenanceListByUserID(userID) {
let list = await this.getMaintenanceJSONList(userID);
@@ -182,7 +197,7 @@ class UptimeKumaServer {
/**
* Get a list of maintenances for the given user.
* @param {string} userID - The ID of the user to get maintenances for.
* @returns {Promise<Object>} A promise that resolves to an object with maintenance IDs as keys and maintenances objects as values.
* @returns {Promise<object>} A promise that resolves to an object with maintenance IDs as keys and maintenances objects as values.
*/
async getMaintenanceJSONList(userID) {
let result = {};
@@ -194,7 +209,7 @@ class UptimeKumaServer {
/**
* Load maintenance list and run
* @param userID
* @param {any} userID Unused
* @returns {Promise<void>}
*/
async loadMaintenanceList(userID) {
@@ -208,6 +223,11 @@ class UptimeKumaServer {
}
}
/**
* Retrieve a specific maintenance
* @param {number} maintenanceID ID of maintenance to retrieve
* @returns {(object|null)} Maintenance if it exists
*/
getMaintenance(maintenanceID) {
if (this.maintenanceList[maintenanceID]) {
return this.maintenanceList[maintenanceID];
@@ -219,6 +239,7 @@ class UptimeKumaServer {
* Write error to log file
* @param {any} error The error to write
* @param {boolean} outputToConsole Should the error also be output to console?
* @returns {void}
*/
static errorLog(error, outputToConsole = true) {
const errorLogStream = fs.createWriteStream(path.join(Database.dataDir, "/error.log"), {
@@ -243,8 +264,8 @@ class UptimeKumaServer {
/**
* Get the IP of the client connected to the socket
* @param {Socket} socket
* @returns {string}
* @param {Socket} socket Socket to query
* @returns {string} IP of client
*/
async getClientIP(socket) {
let clientIP = socket.client.conn.remoteAddress;
@@ -268,7 +289,7 @@ class UptimeKumaServer {
* Attempt to get the current server timezone
* If this fails, fall back to environment variables and then make a
* guess.
* @returns {Promise<string>}
* @returns {Promise<string>} Current timezone
*/
async getTimezone() {
// From process.env.TZ
@@ -313,7 +334,7 @@ class UptimeKumaServer {
/**
* Get the current offset
* @returns {string}
* @returns {string} Time offset
*/
getTimezoneOffset() {
return dayjs().format("Z");
@@ -321,7 +342,9 @@ class UptimeKumaServer {
/**
* Throw an error if the timezone is invalid
* @param timezone
* @param {string} timezone Timezone to test
* @returns {void}
* @throws The timezone is invalid
*/
checkTimezone(timezone) {
try {
@@ -333,7 +356,8 @@ class UptimeKumaServer {
/**
* Set the current server timezone and environment variables
* @param {string} timezone
* @param {string} timezone Timezone to set
* @returns {Promise<void>}
*/
async setTimezone(timezone) {
this.checkTimezone(timezone);
@@ -375,6 +399,7 @@ class UptimeKumaServer {
/**
* Start all system services (e.g. nscd)
* For now, only used in Docker
* @returns {void}
*/
startNSCDServices() {
if (process.env.UPTIME_KUMA_IS_CONTAINER) {
@@ -389,6 +414,7 @@ class UptimeKumaServer {
/**
* Stop all system services
* @returns {void}
*/
stopNSCDServices() {
if (process.env.UPTIME_KUMA_IS_CONTAINER) {
@@ -477,6 +503,14 @@ class UptimeKumaServer {
log.debug("monitor_checker", "Checking monitors end");
}
/**
* Default User-Agent when making HTTP requests
* @returns {string} User-Agent
*/
getUserAgent() {
return "Uptime-Kuma/" + require("../package.json").version;
}
}
module.exports = {
@@ -486,3 +520,4 @@ module.exports = {
// Must be at the end to avoid circular dependencies
const { RealBrowserMonitorType } = require("./monitor-types/real-browser-monitor-type");
const { TailscalePing } = require("./monitor-types/tailscale-ping");
const { DnsMonitorType } = require("./monitor-types/dns");