2022-04-19 07:38:59 +00:00
|
|
|
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");
|
2022-04-19 08:46:45 +00:00
|
|
|
const { log } = require("../src/util");
|
2022-05-06 06:41:34 +00:00
|
|
|
const Database = require("./database");
|
|
|
|
const util = require("util");
|
2022-07-18 14:33:35 +00:00
|
|
|
const { CacheableDnsHttpAgent } = require("./cacheable-dns-http-agent");
|
2022-07-31 15:41:29 +00:00
|
|
|
const { Settings } = require("./settings");
|
2022-04-19 07:38:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* `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;
|
|
|
|
|
2022-05-30 07:45:44 +00:00
|
|
|
/**
|
|
|
|
* Cache Index HTML
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
indexHTML = "";
|
|
|
|
|
2022-04-19 07:38:59 +00:00
|
|
|
static getInstance(args) {
|
|
|
|
if (UptimeKumaServer.instance == null) {
|
|
|
|
UptimeKumaServer.instance = new UptimeKumaServer(args);
|
|
|
|
}
|
|
|
|
return UptimeKumaServer.instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(args) {
|
|
|
|
// SSL
|
2022-04-19 08:46:45 +00:00
|
|
|
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;
|
2022-04-19 07:38:59 +00:00
|
|
|
|
2022-04-19 08:46:45 +00:00
|
|
|
log.info("server", "Creating express and socket.io instance");
|
2022-04-19 07:38:59 +00:00
|
|
|
this.app = express();
|
|
|
|
if (sslKey && sslCert) {
|
2022-04-19 08:46:45 +00:00
|
|
|
log.info("server", "Server Type: HTTPS");
|
2022-04-19 07:38:59 +00:00
|
|
|
this.httpServer = https.createServer({
|
|
|
|
key: fs.readFileSync(sslKey),
|
|
|
|
cert: fs.readFileSync(sslCert)
|
|
|
|
}, this.app);
|
|
|
|
} else {
|
2022-04-19 08:46:45 +00:00
|
|
|
log.info("server", "Server Type: HTTP");
|
2022-04-19 07:38:59 +00:00
|
|
|
this.httpServer = http.createServer(this.app);
|
|
|
|
}
|
|
|
|
|
2022-05-30 07:45:44 +00:00
|
|
|
try {
|
|
|
|
this.indexHTML = fs.readFileSync("./dist/index.html").toString();
|
|
|
|
} catch (e) {
|
|
|
|
// "dist/index.html" is not necessary for development
|
|
|
|
if (process.env.NODE_ENV !== "development") {
|
|
|
|
log.error("server", "Error: Cannot find 'dist/index.html', did you install correctly?");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 14:33:35 +00:00
|
|
|
CacheableDnsHttpAgent.registerGlobalAgent();
|
2022-06-29 14:17:47 +00:00
|
|
|
|
2022-04-19 07:38:59 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-04-19 08:46:45 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2022-04-19 07:38:59 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-05-06 06:41:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write error to log file
|
|
|
|
* @param {any} error The error to write
|
|
|
|
* @param {boolean} outputToConsole Should the error also be output to console?
|
|
|
|
*/
|
|
|
|
static errorLog(error, outputToConsole = true) {
|
|
|
|
const errorLogStream = fs.createWriteStream(Database.dataDir + "/error.log", {
|
|
|
|
flags: "a"
|
|
|
|
});
|
|
|
|
|
|
|
|
errorLogStream.on("error", () => {
|
|
|
|
log.info("", "Cannot write to error.log");
|
|
|
|
});
|
|
|
|
|
|
|
|
if (errorLogStream) {
|
|
|
|
const dateTime = R.isoDateTime();
|
|
|
|
errorLogStream.write(`[${dateTime}] ` + util.format(error) + "\n");
|
|
|
|
|
|
|
|
if (outputToConsole) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errorLogStream.end();
|
|
|
|
}
|
2022-07-31 15:36:33 +00:00
|
|
|
|
|
|
|
async getClientIP(socket) {
|
2022-08-01 07:42:58 +00:00
|
|
|
let clientIP = socket.client.conn.remoteAddress;
|
|
|
|
|
|
|
|
if (clientIP === undefined) {
|
|
|
|
clientIP = "";
|
|
|
|
}
|
2022-07-31 15:36:33 +00:00
|
|
|
|
2022-07-31 15:41:29 +00:00
|
|
|
if (await Settings.get("trustProxy")) {
|
2022-10-05 15:45:21 +00:00
|
|
|
const forwardedFor = socket.client.conn.request.headers["x-forwarded-for"];
|
|
|
|
|
|
|
|
return (typeof forwardedFor === "string" ? forwardedFor.split(",")[0].trim() : null)
|
2022-07-31 15:36:33 +00:00
|
|
|
|| socket.client.conn.request.headers["x-real-ip"]
|
2022-08-01 07:42:58 +00:00
|
|
|
|| clientIP.replace(/^.*:/, "");
|
2022-07-31 15:36:33 +00:00
|
|
|
} else {
|
2022-08-01 07:42:58 +00:00
|
|
|
return clientIP.replace(/^.*:/, "");
|
2022-07-31 15:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-19 07:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
UptimeKumaServer
|
|
|
|
};
|