From 21a6b4a13161622de05272ec1b5919c9c5a9f6ae Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Mon, 4 Dec 2023 20:04:31 +0800 Subject: [PATCH 1/3] WIP --- server/socket-handlers/general-socket-handler.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/socket-handlers/general-socket-handler.js b/server/socket-handlers/general-socket-handler.js index 1269bc25e..a791addaf 100644 --- a/server/socket-handlers/general-socket-handler.js +++ b/server/socket-handlers/general-socket-handler.js @@ -4,7 +4,7 @@ const { sendInfo } = require("../client"); const { checkLogin } = require("../util-server"); const GameResolver = require("gamedig/lib/GameResolver"); const { testChrome } = require("../monitor-types/real-browser-monitor-type"); -const fs = require("fs"); +const fs = require("fs/promises"); const path = require("path"); let gameResolver = new GameResolver(); @@ -85,17 +85,17 @@ module.exports.generalSocketHandler = (socket, server) => { } }); - socket.on("getPushExample", (language, callback) => { + socket.on("getPushExample", async (language, callback) => { try { let dir = path.join("./extra/push-examples", language); - let files = fs.readdirSync(dir); + let files = await fs.readdir(dir); for (let file of files) { if (file.startsWith("index.")) { callback({ ok: true, - code: fs.readFileSync(path.join(dir, file), "utf8"), + code: await fs.readFile(path.join(dir, file), "utf8"), }); return; } From 2f5a6d9648ff37155bd197f8a75ede430fdaece3 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Mon, 4 Dec 2023 20:46:57 +0800 Subject: [PATCH 2/3] Update docker.js --- server/docker.js | 16 ++++++++-------- server/model/monitor.js | 2 +- server/util-server.js | 8 ++++++++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/server/docker.js b/server/docker.js index bec0e0b12..12b49a8b9 100644 --- a/server/docker.js +++ b/server/docker.js @@ -1,10 +1,10 @@ const axios = require("axios"); const { R } = require("redbean-node"); const https = require("https"); -const fs = require("fs"); +const fs = require("fs/promises"); const path = require("path"); const Database = require("./database"); -const { axiosAbortSignal } = require("./util-server"); +const { axiosAbortSignal, fileExists } = require("./util-server"); class DockerHost { @@ -81,7 +81,7 @@ class DockerHost { options.socketPath = dockerHost.dockerDaemon; } else if (dockerHost.dockerType === "tcp") { options.baseURL = DockerHost.patchDockerURL(dockerHost.dockerDaemon); - options.httpsAgent = new https.Agent(DockerHost.getHttpsAgentOptions(dockerHost.dockerType, options.baseURL)); + options.httpsAgent = new https.Agent(await DockerHost.getHttpsAgentOptions(dockerHost.dockerType, options.baseURL)); } try { @@ -143,7 +143,7 @@ class DockerHost { * @param {string} url The docker host URL rewritten to https:// * @returns {object} HTTP agent options */ - static getHttpsAgentOptions(dockerType, url) { + static async getHttpsAgentOptions(dockerType, url) { let baseOptions = { maxCachedSessions: 0, rejectUnauthorized: true @@ -156,10 +156,10 @@ class DockerHost { let certPath = path.join(Database.dockerTLSDir, dirName, DockerHost.CertificateFileNameCert); let keyPath = path.join(Database.dockerTLSDir, dirName, DockerHost.CertificateFileNameKey); - if (dockerType === "tcp" && fs.existsSync(caPath) && fs.existsSync(certPath) && fs.existsSync(keyPath)) { - let ca = fs.readFileSync(caPath); - let key = fs.readFileSync(keyPath); - let cert = fs.readFileSync(certPath); + if (dockerType === "tcp" && await fileExists(caPath) && await fileExists(certPath) && await fileExists(keyPath)) { + let ca = await fs.readFile(caPath); + let key = await fs.readFile(keyPath); + let cert = await fs.readFile(certPath); certOptions = { ca, key, diff --git a/server/model/monitor.js b/server/model/monitor.js index e0b52062c..70ae0fb05 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -740,7 +740,7 @@ class Monitor extends BeanModel { } else if (dockerHost._dockerType === "tcp") { options.baseURL = DockerHost.patchDockerURL(dockerHost._dockerDaemon); options.httpsAgent = CacheableDnsHttpAgent.getHttpsAgent( - DockerHost.getHttpsAgentOptions(dockerHost._dockerType, options.baseURL) + await DockerHost.getHttpsAgentOptions(dockerHost._dockerType, options.baseURL) ); } diff --git a/server/util-server.js b/server/util-server.js index 3246925e9..6bdf99a6b 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -1,3 +1,5 @@ +import fs from "fs"; + const tcpp = require("tcp-ping"); const ping = require("@louislam/ping"); const { R } = require("redbean-node"); @@ -1096,3 +1098,9 @@ module.exports.axiosAbortSignal = (timeoutMs) => { } } }; + +module.exports.fileExists = (file) => { + return fs.promises.access(file, fs.constants.F_OK) + .then(() => true) + .catch(() => false); +}; From a70a2c6b3547fc386526823d3b185a5075dd1289 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Mon, 4 Dec 2023 22:48:57 +0800 Subject: [PATCH 3/3] Minor --- server/util-server.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/server/util-server.js b/server/util-server.js index 6bdf99a6b..d0e67db68 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -1,5 +1,3 @@ -import fs from "fs"; - const tcpp = require("tcp-ping"); const ping = require("@louislam/ping"); const { R } = require("redbean-node"); @@ -24,6 +22,7 @@ const radiusClient = require("node-radius-client"); const redis = require("redis"); const oidc = require("openid-client"); const tls = require("tls"); +const fs = require("fs"); const { dictionaries: {