From 37162b277c49ab693a26da3e4d8aa683222bd5a9 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sat, 5 Sep 2020 02:01:32 -0400 Subject: close socket with bogus user --- lib/sockets.js | 30 ++++++++++++++++-------------- lib/utils.js | 2 +- 2 files changed, 17 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/sockets.js b/lib/sockets.js index e1896b8f..fcca9386 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -41,13 +41,13 @@ broadcast = (type, data) => { } } - Object.values(all).map((ws) => { + Object.values(all).map(ws => { ws.send(JSON.stringify({ type, data })); }); }; -server.on("upgrade", function (req, socket, head) { - wss.handleUpgrade(req, socket, head, function (ws) { +server.on("upgrade", function(req, socket, head) { + wss.handleUpgrade(req, socket, head, function(ws) { wss.emit("connection", ws, req); }); }); @@ -64,13 +64,13 @@ setInterval(() => { } }, 2000); -wss.on("connection", function (ws, req) { +wss.on("connection", function(ws, req) { const id = uuidv4(); ws.beats = 0; ws.send(JSON.stringify({ type: "connected", data: id })); all[id] = ws; - ws.on("message", async (message) => { + ws.on("message", async message => { let type, data; try { ({ type, data } = JSON.parse(message)); @@ -93,7 +93,7 @@ wss.on("connection", function (ws, req) { if (!sockets[username]) sockets[username] = {}; const existing = Object.keys(sockets[username]); if (existing.length > 4) { - const p = existing.find((sid) => sid !== id); + const p = existing.find(sid => sid !== id); sockets[username][p].send(JSON.stringify({ type: "logout" })); setTimeout(() => { if (sockets[username][p]) sockets[username][p].close(); @@ -105,14 +105,14 @@ wss.on("connection", function (ws, req) { let user = await getUser(username); let payments = await user.getPayments({ where: { - account_id: user.account_id, + account_id: user.account_id }, order: [["id", "DESC"]], limit: 12, include: { model: db.Account, - as: "account", - }, + as: "account" + } }); let accounts = await user.getAccounts(); let keys = await user.getKeys(); @@ -128,7 +128,9 @@ wss.on("connection", function (ws, req) { ); } catch (e) { l.error("caught socket error", e.message); - ws.close(); + if (e.message.includes("not found")) + ws.send(JSON.stringify({ type: "logout" })); + setTimeout(ws.close, 1000); } break; case "token": @@ -142,12 +144,12 @@ wss.on("connection", function (ws, req) { const payment = await db.Payment.findOne({ where: { user_id: ws.user.id, - id: data.id, + id: data.id }, include: { model: db.Account, - as: "account", - }, + as: "account" + } }); if (payment) { payment.memo = data.memo; @@ -163,7 +165,7 @@ wss.on("connection", function (ws, req) { } }); - ws.on("close", function () { + ws.on("close", function() { try { const username = names[id]; if (sockets[username] && sockets[username][id]) { diff --git a/lib/utils.js b/lib/utils.js index 17b5d134..23c811c1 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -18,7 +18,7 @@ getUser = async (username, transaction) => { } let user = await db.User.findOne(params); - if (!user) throw new Error("User not found"); + if (!user) throw new Error(`User ${username} not found`); return user; }; -- cgit v1.2.3