aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sockets.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sockets.js')
-rw-r--r--lib/sockets.js103
1 files changed, 27 insertions, 76 deletions
diff --git a/lib/sockets.js b/lib/sockets.js
index 5980304b..ecf15dd7 100644
--- a/lib/sockets.js
+++ b/lib/sockets.js
@@ -1,4 +1,4 @@
-import store from "./store";
+import store from "$lib/store";
import config from "$config";
import app from "$app";
import jwt from "jsonwebtoken";
@@ -9,7 +9,6 @@ import fastifyWs from "@fastify/websocket";
import whitelist from "./whitelist";
import { l, err, warn } from "$lib/logging";
import { getUser } from "$lib/utils";
-import db from "$db";
const code = 1000;
@@ -20,8 +19,8 @@ const all = {};
const subscriptions = [];
const lastSeen = {};
-export const emit = (username, type, data) => {
- if (data.username !== undefined) data = pick(data, ...whitelist);
+export const emit = (uuid, type, data) => {
+ if (data.uuid !== undefined) data = pick(data, ...whitelist);
if (type === "payment" && data.amount > 0) {
for (let i = subscriptions.length - 1; i >= 0; i--) {
@@ -33,18 +32,18 @@ export const emit = (username, type, data) => {
}
}
- if (!store.sockets[username]) return;
- for (let id in store.sockets[username]) {
- const ws = store.sockets[username][id];
+ if (!store.sockets[uuid]) return;
+ for (let id in store.sockets[uuid]) {
+ const ws = store.sockets[uuid][id];
ws.send(JSON.stringify({ type, data }));
}
};
export const broadcast = (type, data) => {
const sent = [];
- for (const username in store.sockets) {
- for (const id in store.sockets[username]) {
- const ws = store.sockets[username][id];
+ for (const uuid in store.sockets) {
+ for (const id in store.sockets[uuid]) {
+ const ws = store.sockets[uuid][id];
ws.send(JSON.stringify({ type, data }));
sent.push(ws.id);
}
@@ -85,7 +84,6 @@ app.register(async function(app) {
break;
case "login":
- let user;
let token = data;
if (lastSeen[token] > subtractSeconds(5)) return;
lastSeen[token] = new Date();
@@ -93,51 +91,26 @@ app.register(async function(app) {
try {
if (!token || token === "null" || !jwt.decode(token))
return ws.close(code, `bad token ${token}`);
- let { username, uuid } = jwt.decode(token);
- if (uuid) user = await getUser(uuid);
- if (user) ({ username } = user);
- if (!(username || uuid)) throw new Error("Invalid JWT token");
+ let { uuid } = jwt.decode(token);
+ if (!uuid) throw new Error("Invalid JWT token");
+ let user = await getUser(uuid);
+ if (!user) throw new Error("User not found");
- if (!store.sockets[username]) store.sockets[username] = {};
- const existing = Object.keys(store.sockets[username]);
+ if (!store.sockets[uuid]) store.sockets[uuid] = {};
+ const existing = Object.keys(store.sockets[uuid]);
if (existing.length > 4) {
const p = existing.find(sid => sid !== id);
- store.sockets[username][p].send(
- JSON.stringify({ type: "logout" })
- );
+ store.sockets[uuid][p].send(JSON.stringify({ type: "logout" }));
setTimeout(() => {
- if (store.sockets[username][p])
- store.sockets[username][p].close(code, "too many sockets");
+ if (store.sockets[uuid][p])
+ store.sockets[uuid][p].close(code, "too many sockets");
}, 5000);
}
- store.sockets[username][id] = ws;
- names[id] = username;
-
- if (!user) user = await getUser(username);
+ store.sockets[uuid][id] = ws;
+ names[id] = uuid;
let { haspin } = user;
- if (user.locked) throw new Error("Account is locked");
- let payments = await user.getPayments({
- where: {
- account_id: user.account_id
- },
- order: [["id", "DESC"]],
- limit: 12,
- include: [
- {
- model: db.Account,
- as: "account"
- },
- {
- model: db.Payment,
- as: "fee_payment"
- }
- ]
- });
-
- let accounts = await user.getAccounts();
-
let keys = await user.getKeys();
user = user.get({ plain: true });
user.accounts = accounts;
@@ -154,10 +127,6 @@ app.register(async function(app) {
);
} catch (e) {
err("caught socket error", e.message);
- if (e.message.includes("locked")) {
- ws.send(JSON.stringify({ type: "locked" }));
- ws.send(JSON.stringify({ type: "logout" }));
- }
if (e.message.includes("not found"))
ws.send(JSON.stringify({ type: "logout" }));
setTimeout(
@@ -173,23 +142,6 @@ app.register(async function(app) {
case "lnurl":
store.sessions[data] = ws;
break;
- case "updateMemo":
- const payment = await db.Payment.findOne({
- where: {
- user_id: ws.user.id,
- id: data.id
- },
- include: {
- model: db.Account,
- as: "account"
- }
- });
- if (payment) {
- payment.memo = data.memo;
- await payment.save();
- emit(ws.user.username, "payment", payment);
- }
- break;
case "subscribe":
subscriptions.push({ invoice: data, ws });
break;
@@ -199,11 +151,10 @@ app.register(async function(app) {
});
ws.on("close", function(code, reason) {
- // console.log("closing socket", reason && reason.toString(), id);
try {
- const username = names[id];
- if (store.sockets[username] && store.sockets[username][id]) {
- delete store.sockets[username][id];
+ const uuid = names[id];
+ if (store.sockets[uuid] && store.sockets[uuid][id]) {
+ delete store.sockets[uuid][id];
}
if (all[id]) delete all[id];
@@ -217,11 +168,11 @@ app.register(async function(app) {
const names = {};
setInterval(() => {
- for (const username in store.sockets) {
- for (const id in store.sockets[username]) {
- let ws = store.sockets[username][id];
+ for (const uuid in store.sockets) {
+ for (const id in store.sockets[uuid]) {
+ let ws = store.sockets[uuid][id];
ws.beats++;
- if (ws.beats > 10) ws.close(code, `${username} ${id} lost heartbeat`);
+ if (ws.beats > 10) ws.close(code, `${uuid} ${id} lost heartbeat`);
}
}
}, 2000);