This commit is contained in:
parent
74a7b9a63b
commit
abc551eee2
24
package-lock.json
generated
24
package-lock.json
generated
@ -24,8 +24,7 @@
|
||||
"warframe-riven-info": "^0.1.2",
|
||||
"websocket": "^1.0.35",
|
||||
"winston": "^3.17.0",
|
||||
"winston-daily-rotate-file": "^5.0.0",
|
||||
"ws": "^8.18.2"
|
||||
"winston-daily-rotate-file": "^5.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@typescript-eslint/eslint-plugin": "^8.28.0",
|
||||
@ -3644,27 +3643,6 @@
|
||||
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
|
||||
"license": "ISC"
|
||||
},
|
||||
"node_modules/ws": {
|
||||
"version": "8.18.2",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.18.2.tgz",
|
||||
"integrity": "sha512-DMricUmwGZUVr++AEAe2uiVM7UoO9MAVZMDu05UQOaUII0lp+zOzLLU4Xqh/JvTqklB1T4uELaaPBKyjE1r4fQ==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"bufferutil": "^4.0.1",
|
||||
"utf-8-validate": ">=5.0.2"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"bufferutil": {
|
||||
"optional": true
|
||||
},
|
||||
"utf-8-validate": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/yaeti": {
|
||||
"version": "0.0.6",
|
||||
"resolved": "https://registry.npmjs.org/yaeti/-/yaeti-0.0.6.tgz",
|
||||
|
@ -33,8 +33,7 @@
|
||||
"warframe-riven-info": "^0.1.2",
|
||||
"websocket": "^1.0.35",
|
||||
"winston": "^3.17.0",
|
||||
"winston-daily-rotate-file": "^5.0.0",
|
||||
"ws": "^8.18.2"
|
||||
"winston-daily-rotate-file": "^5.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@typescript-eslint/eslint-plugin": "^8.28.0",
|
||||
|
@ -5,7 +5,6 @@ import { config } from "./configService";
|
||||
import { logger } from "../utils/logger";
|
||||
import { app } from "../app";
|
||||
import { AddressInfo } from "node:net";
|
||||
import ws from "ws";
|
||||
import { Account } from "../models/loginModel";
|
||||
import { createAccount, createNonce, getUsernameFromEmail, isCorrectPassword } from "./loginService";
|
||||
import { IDatabaseAccountJson } from "../types/loginTypes";
|
||||
@ -14,8 +13,8 @@ import websocket from "websocket";
|
||||
|
||||
let httpServer: http.Server | undefined;
|
||||
let httpsServer: https.Server | undefined;
|
||||
let wsServer: ws.Server | undefined;
|
||||
let wssServer: ws.Server | undefined;
|
||||
let wsServer: websocket.server | undefined;
|
||||
let wssServer: websocket.server | undefined;
|
||||
|
||||
const tlsOptions = {
|
||||
key: fs.readFileSync("static/certs/key.pem"),
|
||||
@ -29,16 +28,16 @@ export const startWebServer = (): void => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||
httpServer = http.createServer(app);
|
||||
httpServer.listen(httpPort, () => {
|
||||
wsServer = new ws.Server({ server: httpServer });
|
||||
wsServer.on("connection", wsOnConnect);
|
||||
wsServer = new websocket.server({ httpServer: httpServer! });
|
||||
wsServer.on("request", wsOnRequest);
|
||||
|
||||
logger.info("HTTP server started on port " + httpPort);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||
httpsServer = https.createServer(tlsOptions, app);
|
||||
httpsServer.listen(httpsPort, () => {
|
||||
wssServer = new ws.Server({ server: httpsServer });
|
||||
wssServer.on("connection", wsOnConnect);
|
||||
wssServer = new websocket.server({ httpServer: httpsServer! });
|
||||
wssServer.on("request", wsOnRequest);
|
||||
|
||||
logger.info("HTTPS server started on port " + httpsPort);
|
||||
|
||||
@ -88,6 +87,13 @@ export const getWebPorts = (): Record<"http" | "https", number | undefined> => {
|
||||
};
|
||||
|
||||
export const stopWebServer = async (): Promise<void> => {
|
||||
if (wsServer) {
|
||||
wsServer.shutDown();
|
||||
}
|
||||
if (wssServer) {
|
||||
wssServer.shutDown();
|
||||
}
|
||||
|
||||
const promises: Promise<void>[] = [];
|
||||
if (httpServer) {
|
||||
promises.push(
|
||||
@ -107,28 +113,10 @@ export const stopWebServer = async (): Promise<void> => {
|
||||
})
|
||||
);
|
||||
}
|
||||
if (wsServer) {
|
||||
promises.push(
|
||||
new Promise(resolve => {
|
||||
wsServer!.close(() => {
|
||||
resolve();
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
if (wssServer) {
|
||||
promises.push(
|
||||
new Promise(resolve => {
|
||||
wssServer!.close(() => {
|
||||
resolve();
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
await Promise.all(promises);
|
||||
};
|
||||
|
||||
interface IWsCustomData extends ws {
|
||||
interface IWsCustomData extends websocket.connection {
|
||||
accountId?: string;
|
||||
}
|
||||
|
||||
@ -159,15 +147,19 @@ interface IWsMsgToClient {
|
||||
logged_out?: boolean;
|
||||
}
|
||||
|
||||
const wsOnConnect = (ws: ws, req: http.IncomingMessage): void => {
|
||||
if (req.url == "/custom/selftest") {
|
||||
const wsOnRequest = (req: websocket.request): void => {
|
||||
const ws = req.accept();
|
||||
if (req.httpRequest.url == "/custom/selftest") {
|
||||
ws.send("SpaceNinjaServer");
|
||||
ws.close();
|
||||
return;
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||
ws.on("message", async msg => {
|
||||
const data = JSON.parse(String(msg)) as IWsMsgFromClient;
|
||||
if (msg.type != "utf8") {
|
||||
return;
|
||||
}
|
||||
const data = JSON.parse(String(msg.utf8Data)) as IWsMsgFromClient;
|
||||
if (data.auth) {
|
||||
let account: IDatabaseAccountJson | null = await Account.findOne({ email: data.auth.email });
|
||||
if (account) {
|
||||
@ -221,12 +213,12 @@ const wsOnConnect = (ws: ws, req: http.IncomingMessage): void => {
|
||||
export const sendWsBroadcast = (data: IWsMsgToClient): void => {
|
||||
const msg = JSON.stringify(data);
|
||||
if (wsServer) {
|
||||
for (const client of wsServer.clients) {
|
||||
for (const client of wsServer.connections) {
|
||||
client.send(msg);
|
||||
}
|
||||
}
|
||||
if (wssServer) {
|
||||
for (const client of wssServer.clients) {
|
||||
for (const client of wssServer.connections) {
|
||||
client.send(msg);
|
||||
}
|
||||
}
|
||||
@ -235,14 +227,14 @@ export const sendWsBroadcast = (data: IWsMsgToClient): void => {
|
||||
export const sendWsBroadcastTo = (accountId: string, data: IWsMsgToClient): void => {
|
||||
const msg = JSON.stringify(data);
|
||||
if (wsServer) {
|
||||
for (const client of wsServer.clients) {
|
||||
for (const client of wsServer.connections) {
|
||||
if ((client as IWsCustomData).accountId == accountId) {
|
||||
client.send(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (wssServer) {
|
||||
for (const client of wssServer.clients) {
|
||||
for (const client of wssServer.connections) {
|
||||
if ((client as IWsCustomData).accountId == accountId) {
|
||||
client.send(msg);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user