From 082c2dd32d286946609f26aaf44ab7419c329346 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Thu, 7 Apr 2022 14:45:37 +0800 Subject: [PATCH] Remove restartMonitors() and move proxy socket events to a socket handler file --- server/server.js | 61 +------------------ .../socket-handlers/proxy-socket-handler.js | 51 ++++++++++++++++ 2 files changed, 53 insertions(+), 59 deletions(-) create mode 100644 server/socket-handlers/proxy-socket-handler.js diff --git a/server/server.js b/server/server.js index dc97d742d..5133b534d 100644 --- a/server/server.js +++ b/server/server.js @@ -138,6 +138,7 @@ const databaseSocketHandler = require("./socket-handlers/database-socket-handler const TwoFA = require("./2fa"); const StatusPage = require("./model/status_page"); const { cloudflaredSocketHandler, autoStart: cloudflaredAutoStart, stop: cloudflaredStop } = require("./socket-handlers/cloudflared-socket-handler"); +const { proxySocketHandler } = require("./socket-handlers/proxy-socket-handler"); app.use(express.json()); @@ -1112,52 +1113,6 @@ exports.entryPage = "dashboard"; } }); - socket.on("addProxy", async (proxy, proxyID, callback) => { - try { - checkLogin(socket); - - const proxyBean = await Proxy.save(proxy, proxyID, socket.userID); - await sendProxyList(socket); - - if (proxy.applyExisting) { - await restartMonitors(socket.userID); - } - - callback({ - ok: true, - msg: "Saved", - id: proxyBean.id, - }); - - } catch (e) { - callback({ - ok: false, - msg: e.message, - }); - } - }); - - socket.on("deleteProxy", async (proxyID, callback) => { - try { - checkLogin(socket); - - await Proxy.delete(proxyID, socket.userID); - await sendProxyList(socket); - await restartMonitors(socket.userID); - - callback({ - ok: true, - msg: "Deleted", - }); - - } catch (e) { - callback({ - ok: false, - msg: e.message, - }); - } - }); - socket.on("checkApprise", async (callback) => { try { checkLogin(socket); @@ -1438,6 +1393,7 @@ exports.entryPage = "dashboard"; statusPageSocketHandler(socket); cloudflaredSocketHandler(socket); databaseSocketHandler(socket); + proxySocketHandler(socket); debug("added all socket handlers"); @@ -1615,19 +1571,6 @@ async function restartMonitor(userID, monitorID) { return await startMonitor(userID, monitorID); } -async function restartMonitors(userID) { - // Fetch all active monitors for user - const monitors = await R.getAll("SELECT id FROM monitor WHERE active = 1 AND user_id = ?", [userID]); - - for (const monitor of monitors) { - // Start updated monitor - await startMonitor(userID, monitor.id); - - // Give some delays, so all monitors won't make request at the same moment when just start the server. - await sleep(getRandomInt(300, 1000)); - } -} - async function pauseMonitor(userID, monitorID) { await checkOwner(userID, monitorID); diff --git a/server/socket-handlers/proxy-socket-handler.js b/server/socket-handlers/proxy-socket-handler.js new file mode 100644 index 000000000..a05a00b93 --- /dev/null +++ b/server/socket-handlers/proxy-socket-handler.js @@ -0,0 +1,51 @@ +const { checkLogin } = require("../util-server"); +const { Proxy } = require("../proxy"); +const { sendProxyList } = require("../client"); + +module.exports.proxySocketHandler = (socket) => { + socket.on("addProxy", async (proxy, proxyID, callback) => { + try { + checkLogin(socket); + + const proxyBean = await Proxy.save(proxy, proxyID, socket.userID); + await sendProxyList(socket); + + if (proxy.applyExisting) { + // TODO: await restartMonitors(socket.userID); + } + + callback({ + ok: true, + msg: "Saved", + id: proxyBean.id, + }); + + } catch (e) { + callback({ + ok: false, + msg: e.message, + }); + } + }); + + socket.on("deleteProxy", async (proxyID, callback) => { + try { + checkLogin(socket); + + await Proxy.delete(proxyID, socket.userID); + await sendProxyList(socket); + // TODO: await restartMonitors(socket.userID); + + callback({ + ok: true, + msg: "Deleted", + }); + + } catch (e) { + callback({ + ok: false, + msg: e.message, + }); + } + }); +};