From afaa7bb2f0bc955816e99bfcafd3e1699793d607 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Fri, 24 Nov 2023 16:03:35 +0800 Subject: [PATCH 1/4] Do not process debug log for production --- src/util.js | 3 +++ src/util.ts | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/util.js b/src/util.js index 6b8f8f37..8936ffbf 100644 --- a/src/util.js +++ b/src/util.js @@ -101,6 +101,9 @@ class Logger { * @param level Log level. One of INFO, WARN, ERROR, DEBUG or can be customized. */ log(module, msg, level) { + if (level === "DEBUG" && !exports.isDev) { + return; + } if (this.hideLog[level] && this.hideLog[level].includes(module.toLowerCase())) { return; } diff --git a/src/util.ts b/src/util.ts index e8a2706e..4f62fe76 100644 --- a/src/util.ts +++ b/src/util.ts @@ -115,6 +115,10 @@ class Logger { * @param level Log level. One of INFO, WARN, ERROR, DEBUG or can be customized. */ log(module: string, msg: any, level: string) { + if (level === "DEBUG" && !isDev) { + return; + } + if (this.hideLog[level] && this.hideLog[level].includes(module.toLowerCase())) { return; } From b689733d59139f158de95b0017865c867d0c7159 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Fri, 24 Nov 2023 16:37:52 +0800 Subject: [PATCH 2/4] Fix getGameList, testChrome without checkLogin --- .../socket-handlers/general-socket-handler.js | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/server/socket-handlers/general-socket-handler.js b/server/socket-handlers/general-socket-handler.js index 2f0c63b4..c7f77b65 100644 --- a/server/socket-handlers/general-socket-handler.js +++ b/server/socket-handlers/general-socket-handler.js @@ -42,24 +42,40 @@ module.exports.generalSocketHandler = (socket, server) => { }); socket.on("getGameList", async (callback) => { - callback({ - ok: true, - gameList: getGameList(), - }); - }); - - socket.on("testChrome", (executable, callback) => { - // Just noticed that await call could block the whole socket.io server!!! Use pure promise instead. - testChrome(executable).then((version) => { + try { + checkLogin(socket); callback({ ok: true, - msg: "Found Chromium/Chrome. Version: " + version, + gameList: getGameList(), }); - }).catch((e) => { + } catch (e) { callback({ ok: false, msg: e.message, + }) + } + }); + + socket.on("testChrome", (executable, callback) => { + try { + checkLogin(socket); + // Just noticed that await call could block the whole socket.io server!!! Use pure promise instead. + testChrome(executable).then((version) => { + callback({ + ok: true, + msg: "Found Chromium/Chrome. Version: " + version, + }); + }).catch((e) => { + callback({ + ok: false, + msg: e.message, + }); }); - }); + } catch (e) { + callback({ + ok: false, + msg: e.message, + }) + } }); }; From 4255496b113abf4ee989333510c62f5e3f2ddc37 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Fri, 24 Nov 2023 17:29:42 +0800 Subject: [PATCH 3/4] Rewrite Tailscale ping using spawnSync --- server/monitor-types/tailscale-ping.js | 41 +++++++++++--------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/server/monitor-types/tailscale-ping.js b/server/monitor-types/tailscale-ping.js index eeec9e3f..3b2ac487 100644 --- a/server/monitor-types/tailscale-ping.js +++ b/server/monitor-types/tailscale-ping.js @@ -1,6 +1,6 @@ const { MonitorType } = require("./monitor-type"); -const { UP, log } = require("../../src/util"); -const exec = require("child_process").exec; +const { UP } = require("../../src/util"); +const childProcess = require("child_process"); /** * A TailscalePing class extends the MonitorType. @@ -23,7 +23,6 @@ class TailscalePing extends MonitorType { let tailscaleOutput = await this.runTailscalePing(monitor.hostname, monitor.interval); this.parseTailscaleOutput(tailscaleOutput, heartbeat); } catch (err) { - log.debug("Tailscale", err); // trigger log function somewhere to display a notification or alert to the user (but how?) throw new Error(`Error checking Tailscale ping: ${err}`); } @@ -33,30 +32,26 @@ class TailscalePing extends MonitorType { * Runs the Tailscale ping command to the given URL. * * @param {string} hostname - The hostname to ping. + * @param {number} interval * @returns {Promise} - A Promise that resolves to the output of the Tailscale ping command * @throws Will throw an error if the command execution encounters any error. */ async runTailscalePing(hostname, interval) { - let cmd = `tailscale ping ${hostname}`; - - log.debug("Tailscale", cmd); - - return new Promise((resolve, reject) => { - let timeout = interval * 1000 * 0.8; - exec(cmd, { timeout: timeout }, (error, stdout, stderr) => { - // we may need to handle more cases if tailscale reports an error that isn't necessarily an error (such as not-logged in or DERP health-related issues) - if (error) { - reject(`Execution error: ${error.message}`); - return; - } - if (stderr) { - reject(`Error in output: ${stderr}`); - return; - } - - resolve(stdout); - }); + let timeout = interval * 1000 * 0.8; + let res = childProcess.spawnSync("tailscale", [ "ping", hostname ], { + timeout: timeout }); + if (res.error) { + throw new Error(`Execution error: ${res.error.message}`); + } + if (res.stderr && res.stderr.toString()) { + throw new Error(`Error in output: ${res.stderr.toString()}`); + } + if (res.stdout && res.stdout.toString()) { + return res.stdout.toString(); + } else { + throw new Error("No output from Tailscale ping"); + } } /** @@ -74,7 +69,7 @@ class TailscalePing extends MonitorType { heartbeat.status = UP; let time = line.split(" in ")[1].split(" ")[0]; heartbeat.ping = parseInt(time); - heartbeat.msg = line; + heartbeat.msg = "OK"; break; } else if (line.includes("timed out")) { throw new Error(`Ping timed out: "${line}"`); From 9536c6aa6a5fc12749fa54192d0aeb86465a6877 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Fri, 24 Nov 2023 17:33:13 +0800 Subject: [PATCH 4/4] Minor --- server/socket-handlers/general-socket-handler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/socket-handlers/general-socket-handler.js b/server/socket-handlers/general-socket-handler.js index c7f77b65..3fc6f1d5 100644 --- a/server/socket-handlers/general-socket-handler.js +++ b/server/socket-handlers/general-socket-handler.js @@ -52,7 +52,7 @@ module.exports.generalSocketHandler = (socket, server) => { callback({ ok: false, msg: e.message, - }) + }); } }); @@ -75,7 +75,7 @@ module.exports.generalSocketHandler = (socket, server) => { callback({ ok: false, msg: e.message, - }) + }); } }); };