fix: add try/catch around websocket message event handler
Some checks failed
Build / build (pull_request) Failing after 1m15s
Some checks failed
Build / build (pull_request) Failing after 1m15s
This commit is contained in:
parent
0f6b55beed
commit
65a41ccc68
@ -1,16 +1,11 @@
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { logger } from "@/src/utils/logger";
|
||||
import { logError } from "@/src/utils/logger";
|
||||
|
||||
export const errorHandler = (err: Error, req: Request, res: Response, _next: NextFunction): void => {
|
||||
if (err.message == "Invalid accountId-nonce pair") {
|
||||
res.status(400).send("Log-in expired");
|
||||
} else if (err.stack) {
|
||||
const stackArr = err.stack.split("\n");
|
||||
stackArr[0] += ` while processing ${req.path} request`;
|
||||
logger.error(stackArr.join("\n"));
|
||||
res.status(500).end();
|
||||
} else {
|
||||
logger.error(`uncaught error while processing ${req.path} request: ${err.message}`);
|
||||
logError(err, `processing ${req.path} request`);
|
||||
res.status(500).end();
|
||||
}
|
||||
};
|
||||
|
@ -5,6 +5,7 @@ import { Account } from "@/src/models/loginModel";
|
||||
import { createAccount, createNonce, getUsernameFromEmail, isCorrectPassword } from "@/src/services/loginService";
|
||||
import { IDatabaseAccountJson } from "@/src/types/loginTypes";
|
||||
import { HydratedDocument } from "mongoose";
|
||||
import { logError } from "../utils/logger";
|
||||
|
||||
let wsServer: ws.Server | undefined;
|
||||
let wssServer: ws.Server | undefined;
|
||||
@ -88,63 +89,67 @@ const wsOnConnect = (ws: ws, req: http.IncomingMessage): void => {
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||
ws.on("message", async msg => {
|
||||
const data = JSON.parse(String(msg)) as IWsMsgFromClient;
|
||||
if (data.auth) {
|
||||
let account: IDatabaseAccountJson | null = await Account.findOne({ email: data.auth.email });
|
||||
if (account) {
|
||||
if (isCorrectPassword(data.auth.password, account.password)) {
|
||||
if (!account.Nonce) {
|
||||
account.ClientType = "webui";
|
||||
account.Nonce = createNonce();
|
||||
await (account as HydratedDocument<IDatabaseAccountJson>).save();
|
||||
try {
|
||||
const data = JSON.parse(String(msg)) as IWsMsgFromClient;
|
||||
if (data.auth) {
|
||||
let account: IDatabaseAccountJson | null = await Account.findOne({ email: data.auth.email });
|
||||
if (account) {
|
||||
if (isCorrectPassword(data.auth.password, account.password)) {
|
||||
if (!account.Nonce) {
|
||||
account.ClientType = "webui";
|
||||
account.Nonce = createNonce();
|
||||
await (account as HydratedDocument<IDatabaseAccountJson>).save();
|
||||
}
|
||||
} else {
|
||||
account = null;
|
||||
}
|
||||
} else if (data.auth.isRegister) {
|
||||
const name = await getUsernameFromEmail(data.auth.email);
|
||||
account = await createAccount({
|
||||
email: data.auth.email,
|
||||
password: data.auth.password,
|
||||
ClientType: "webui",
|
||||
LastLogin: new Date(),
|
||||
DisplayName: name,
|
||||
Nonce: createNonce()
|
||||
});
|
||||
}
|
||||
if (account) {
|
||||
(ws as IWsCustomData).accountId = account.id;
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
auth_succ: {
|
||||
id: account.id,
|
||||
DisplayName: account.DisplayName,
|
||||
Nonce: account.Nonce
|
||||
}
|
||||
} satisfies IWsMsgToClient)
|
||||
);
|
||||
} else {
|
||||
account = null;
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
auth_fail: {
|
||||
isRegister: data.auth.isRegister
|
||||
}
|
||||
} satisfies IWsMsgToClient)
|
||||
);
|
||||
}
|
||||
} else if (data.auth.isRegister) {
|
||||
const name = await getUsernameFromEmail(data.auth.email);
|
||||
account = await createAccount({
|
||||
email: data.auth.email,
|
||||
password: data.auth.password,
|
||||
ClientType: "webui",
|
||||
LastLogin: new Date(),
|
||||
DisplayName: name,
|
||||
Nonce: createNonce()
|
||||
});
|
||||
}
|
||||
if (account) {
|
||||
(ws as IWsCustomData).accountId = account.id;
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
auth_succ: {
|
||||
id: account.id,
|
||||
DisplayName: account.DisplayName,
|
||||
Nonce: account.Nonce
|
||||
}
|
||||
} satisfies IWsMsgToClient)
|
||||
);
|
||||
} else {
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
auth_fail: {
|
||||
isRegister: data.auth.isRegister
|
||||
}
|
||||
} satisfies IWsMsgToClient)
|
||||
if (data.logout) {
|
||||
const accountId = (ws as IWsCustomData).accountId;
|
||||
(ws as IWsCustomData).accountId = undefined;
|
||||
await Account.updateOne(
|
||||
{
|
||||
_id: accountId,
|
||||
ClientType: "webui"
|
||||
},
|
||||
{
|
||||
Nonce: 0
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
if (data.logout) {
|
||||
const accountId = (ws as IWsCustomData).accountId;
|
||||
(ws as IWsCustomData).accountId = undefined;
|
||||
await Account.updateOne(
|
||||
{
|
||||
_id: accountId,
|
||||
ClientType: "webui"
|
||||
},
|
||||
{
|
||||
Nonce: 0
|
||||
}
|
||||
);
|
||||
} catch (e) {
|
||||
logError(e as Error, `processing websocket message`);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
@ -108,3 +108,13 @@ errorLog.on("new", filename => logger.info(`Using error log file: ${filename}`))
|
||||
combinedLog.on("new", filename => logger.info(`Using combined log file: ${filename}`));
|
||||
errorLog.on("rotate", filename => logger.info(`Rotated error log file: ${filename}`));
|
||||
combinedLog.on("rotate", filename => logger.info(`Rotated combined log file: ${filename}`));
|
||||
|
||||
export const logError = (err: Error, context: string): void => {
|
||||
if (err.stack) {
|
||||
const stackArr = err.stack.split("\n");
|
||||
stackArr[0] += ` while ${context}`;
|
||||
logger.error(stackArr.join("\n"));
|
||||
} else {
|
||||
logger.error(`uncaught error while ${context}: ${err.message}`);
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user