2022-04-12 14:21:19 +02:00
|
|
|
import { ExSocketInterface } from "../Model/Websocket/ExSocketInterface";
|
2022-03-11 17:02:58 +01:00
|
|
|
import { GameRoomPolicyTypes } from "../Model/PusherRoom";
|
2021-06-24 10:09:10 +02:00
|
|
|
import { PointInterface } from "../Model/Websocket/PointInterface";
|
2020-09-18 15:51:15 +02:00
|
|
|
import {
|
|
|
|
SetPlayerDetailsMessage,
|
|
|
|
SubMessage,
|
2020-09-24 17:24:37 +02:00
|
|
|
BatchMessage,
|
2020-09-28 18:52:54 +02:00
|
|
|
ItemEventMessage,
|
|
|
|
ViewportMessage,
|
|
|
|
ClientToServerMessage,
|
2020-09-29 16:01:22 +02:00
|
|
|
SilentMessage,
|
|
|
|
WebRtcSignalToServerMessage,
|
2020-10-06 15:37:00 +02:00
|
|
|
PlayGlobalMessage,
|
2020-10-15 17:25:16 +02:00
|
|
|
ReportPlayerMessage,
|
2021-06-01 15:35:25 +02:00
|
|
|
QueryJitsiJwtMessage,
|
|
|
|
SendUserMessage,
|
|
|
|
ServerToClientMessage,
|
|
|
|
CompanionMessage,
|
|
|
|
EmotePromptMessage,
|
2021-12-12 16:56:26 +01:00
|
|
|
FollowRequestMessage,
|
|
|
|
FollowConfirmationMessage,
|
|
|
|
FollowAbortMessage,
|
2021-07-16 11:22:36 +02:00
|
|
|
VariableMessage,
|
2022-03-03 12:22:16 +01:00
|
|
|
LockGroupPromptMessage,
|
2020-09-24 11:16:08 +02:00
|
|
|
} from "../Messages/generated/messages_pb";
|
2021-06-24 10:09:10 +02:00
|
|
|
import { UserMovesMessage } from "../Messages/generated/messages_pb";
|
|
|
|
import { parse } from "query-string";
|
2021-12-01 10:12:07 +01:00
|
|
|
import { AdminSocketTokenData, jwtTokenManager, tokenInvalidException } from "../Services/JWTTokenManager";
|
2021-07-16 09:52:51 +02:00
|
|
|
import { adminApi, FetchMemberDataByUuidResponse } from "../Services/AdminApi";
|
2022-04-12 14:21:19 +02:00
|
|
|
import { socketManager } from "../Services/SocketManager";
|
2021-06-24 10:09:10 +02:00
|
|
|
import { emitInBatch } from "../Services/IoSocketHelpers";
|
2022-01-27 18:38:33 +01:00
|
|
|
import { ADMIN_API_URL, ADMIN_SOCKETS_TOKEN, DISABLE_ANONYMOUS, SOCKET_IDLE_TIMER } from "../Enum/EnvironmentVariable";
|
2022-04-12 14:21:19 +02:00
|
|
|
import { Zone } from "../Model/Zone";
|
|
|
|
import { ExAdminSocketInterface } from "../Model/Websocket/ExAdminSocketInterface";
|
|
|
|
import { AdminMessageInterface, isAdminMessageInterface } from "../Model/Websocket/Admin/AdminMessages";
|
2021-12-01 10:12:07 +01:00
|
|
|
import Axios from "axios";
|
|
|
|
import { InvalidTokenError } from "../Controller/InvalidTokenError";
|
2022-03-11 17:02:58 +01:00
|
|
|
import HyperExpress from "hyper-express";
|
|
|
|
import { localWokaService } from "../Services/LocalWokaService";
|
|
|
|
import { WebSocket } from "uWebSockets.js";
|
2022-03-21 10:33:25 +01:00
|
|
|
import { WokaDetail } from "../Messages/JsonMessages/PlayerTextures";
|
2022-04-12 14:21:19 +02:00
|
|
|
import { z } from "zod";
|
2022-04-19 17:11:51 +02:00
|
|
|
import { ErrorApiData, isErrorApiData } from "../Messages/JsonMessages/ErrorApiData";
|
2022-03-11 17:02:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The object passed between the "open" and the "upgrade" methods when opening a websocket
|
|
|
|
*/
|
|
|
|
interface UpgradeData {
|
|
|
|
// Data passed here is accessible on the "websocket" socket object.
|
|
|
|
rejected: false;
|
|
|
|
token: string;
|
|
|
|
userUuid: string;
|
|
|
|
IPAddress: string;
|
|
|
|
roomId: string;
|
|
|
|
name: string;
|
|
|
|
companion: CompanionMessage | undefined;
|
|
|
|
characterLayers: WokaDetail[];
|
|
|
|
messages: unknown[];
|
|
|
|
tags: string[];
|
|
|
|
visitCardUrl: string | null;
|
|
|
|
userRoomToken: string | undefined;
|
|
|
|
position: PointInterface;
|
|
|
|
viewport: {
|
|
|
|
top: number;
|
|
|
|
right: number;
|
|
|
|
bottom: number;
|
|
|
|
left: number;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-19 16:50:21 +02:00
|
|
|
interface UpgradeFailedInvalidData {
|
2022-03-11 17:02:58 +01:00
|
|
|
rejected: true;
|
2022-04-19 16:50:21 +02:00
|
|
|
reason: "tokenInvalid" | "textureInvalid" | null;
|
2022-03-11 17:02:58 +01:00
|
|
|
message: string;
|
|
|
|
roomId: string;
|
|
|
|
}
|
2020-04-04 04:08:12 +02:00
|
|
|
|
2022-04-19 16:50:21 +02:00
|
|
|
interface UpgradeFailedErrorData {
|
|
|
|
rejected: true;
|
|
|
|
reason: "error";
|
|
|
|
error: ErrorApiData;
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpgradeFailedData = UpgradeFailedErrorData | UpgradeFailedInvalidData;
|
|
|
|
|
2020-05-03 16:28:18 +02:00
|
|
|
export class IoSocketController {
|
2020-09-18 13:57:38 +02:00
|
|
|
private nextUserId: number = 1;
|
2020-05-03 16:28:18 +02:00
|
|
|
|
2022-03-11 17:02:58 +01:00
|
|
|
constructor(private readonly app: HyperExpress.compressors.TemplatedApp) {
|
2020-04-04 04:08:12 +02:00
|
|
|
this.ioConnection();
|
2022-01-27 18:38:33 +01:00
|
|
|
if (ADMIN_SOCKETS_TOKEN) {
|
|
|
|
this.adminRoomSocket();
|
|
|
|
}
|
2020-05-08 00:35:36 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 17:25:16 +02:00
|
|
|
adminRoomSocket() {
|
2021-06-24 10:09:10 +02:00
|
|
|
this.app.ws("/admin/rooms", {
|
2020-10-16 14:36:43 +02:00
|
|
|
upgrade: (res, req, context) => {
|
2021-06-24 10:09:10 +02:00
|
|
|
const websocketKey = req.getHeader("sec-websocket-key");
|
|
|
|
const websocketProtocol = req.getHeader("sec-websocket-protocol");
|
|
|
|
const websocketExtensions = req.getHeader("sec-websocket-extensions");
|
2020-10-16 14:36:43 +02:00
|
|
|
|
2021-12-01 10:12:07 +01:00
|
|
|
res.upgrade({}, websocketKey, websocketProtocol, websocketExtensions, context);
|
2020-10-16 14:36:43 +02:00
|
|
|
},
|
2020-10-15 17:25:16 +02:00
|
|
|
open: (ws) => {
|
2021-12-01 10:12:07 +01:00
|
|
|
console.log("Admin socket connect to client on " + Buffer.from(ws.getRemoteAddressAsText()).toString());
|
2020-12-10 17:46:15 +01:00
|
|
|
ws.disconnecting = false;
|
2020-10-15 17:25:16 +02:00
|
|
|
},
|
2022-04-12 14:21:19 +02:00
|
|
|
message: (ws, arrayBuffer): void => {
|
2020-10-19 19:32:47 +02:00
|
|
|
try {
|
2022-04-12 14:21:19 +02:00
|
|
|
const message: AdminMessageInterface = JSON.parse(
|
|
|
|
new TextDecoder("utf-8").decode(new Uint8Array(arrayBuffer))
|
|
|
|
);
|
2021-12-01 10:12:07 +01:00
|
|
|
|
2022-04-12 14:21:19 +02:00
|
|
|
try {
|
|
|
|
isAdminMessageInterface.parse(message);
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof z.ZodError) {
|
|
|
|
console.error(err.issues);
|
|
|
|
}
|
2021-12-01 10:12:07 +01:00
|
|
|
console.error("Invalid message received.", message);
|
|
|
|
ws.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: "Error",
|
|
|
|
data: {
|
|
|
|
message: "Invalid message received! The connection has been closed.",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
ws.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const token = message.jwt;
|
|
|
|
|
|
|
|
let data: AdminSocketTokenData;
|
2020-10-19 19:32:47 +02:00
|
|
|
|
2021-12-01 10:12:07 +01:00
|
|
|
try {
|
|
|
|
data = jwtTokenManager.verifyAdminSocketToken(token);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Admin socket access refused for token: " + token, e);
|
|
|
|
ws.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: "Error",
|
|
|
|
data: {
|
|
|
|
message: "Admin socket access refused! The connection has been closed.",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
ws.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const authorizedRoomIds = data.authorizedRoomIds;
|
|
|
|
|
|
|
|
if (message.event === "listen") {
|
|
|
|
const notAuthorizedRoom = message.roomIds.filter(
|
|
|
|
(roomId) => !authorizedRoomIds.includes(roomId)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (notAuthorizedRoom.length > 0) {
|
|
|
|
const errorMessage = `Admin socket refused for client on ${Buffer.from(
|
|
|
|
ws.getRemoteAddressAsText()
|
|
|
|
).toString()} listening of : \n${JSON.stringify(notAuthorizedRoom)}`;
|
|
|
|
console.error();
|
|
|
|
ws.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: "Error",
|
|
|
|
data: {
|
|
|
|
message: errorMessage,
|
|
|
|
},
|
|
|
|
})
|
2021-06-24 10:09:10 +02:00
|
|
|
);
|
2021-12-01 10:12:07 +01:00
|
|
|
ws.close();
|
|
|
|
return;
|
2021-01-17 03:07:46 +01:00
|
|
|
}
|
2021-12-01 10:12:07 +01:00
|
|
|
|
|
|
|
for (const roomId of message.roomIds) {
|
|
|
|
socketManager
|
|
|
|
.handleAdminRoom(ws as ExAdminSocketInterface, roomId)
|
|
|
|
.catch((e) => console.error(e));
|
|
|
|
}
|
|
|
|
} else if (message.event === "user-message") {
|
|
|
|
const messageToEmit = message.message;
|
|
|
|
// Get roomIds of the world where we want broadcast the message
|
|
|
|
const roomIds = authorizedRoomIds.filter(
|
|
|
|
(authorizeRoomId) => authorizeRoomId.split("/")[5] === message.world
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const roomId of roomIds) {
|
|
|
|
if (messageToEmit.type === "banned") {
|
|
|
|
socketManager
|
|
|
|
.emitBan(messageToEmit.userUuid, messageToEmit.message, messageToEmit.type, roomId)
|
|
|
|
.catch((error) => console.error(error));
|
|
|
|
} else if (messageToEmit.type === "ban") {
|
|
|
|
socketManager
|
|
|
|
.emitSendUserMessage(
|
|
|
|
messageToEmit.userUuid,
|
|
|
|
messageToEmit.message,
|
|
|
|
messageToEmit.type,
|
|
|
|
roomId
|
|
|
|
)
|
|
|
|
.catch((error) => console.error(error));
|
|
|
|
}
|
2020-10-20 08:20:21 +02:00
|
|
|
}
|
2020-10-19 19:32:47 +02:00
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
} catch (err) {
|
2020-10-19 19:32:47 +02:00
|
|
|
console.error(err);
|
|
|
|
}
|
2020-10-15 17:25:16 +02:00
|
|
|
},
|
2022-04-12 14:21:19 +02:00
|
|
|
close: (ws) => {
|
2021-06-24 10:09:10 +02:00
|
|
|
const Client = ws as ExAdminSocketInterface;
|
2020-12-10 17:46:15 +01:00
|
|
|
try {
|
|
|
|
Client.disconnecting = true;
|
|
|
|
socketManager.leaveAdminRoom(Client);
|
|
|
|
} catch (e) {
|
|
|
|
console.error('An error occurred on admin "disconnect"');
|
|
|
|
console.error(e);
|
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
},
|
|
|
|
});
|
2020-10-15 17:25:16 +02:00
|
|
|
}
|
2020-04-05 15:51:47 +02:00
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
ioConnection() {
|
2021-06-24 10:09:10 +02:00
|
|
|
this.app.ws("/room", {
|
2020-09-28 18:52:54 +02:00
|
|
|
/* Options */
|
|
|
|
//compression: uWS.SHARED_COMPRESSOR,
|
2020-11-10 18:26:46 +01:00
|
|
|
idleTimeout: SOCKET_IDLE_TIMER,
|
2020-09-28 18:52:54 +02:00
|
|
|
maxPayloadLength: 16 * 1024 * 1024,
|
2020-09-30 12:16:39 +02:00
|
|
|
maxBackpressure: 65536, // Maximum 64kB of data in the buffer.
|
2020-09-29 16:01:22 +02:00
|
|
|
//idleTimeout: 10,
|
2020-09-30 10:12:40 +02:00
|
|
|
upgrade: (res, req, context) => {
|
|
|
|
(async () => {
|
|
|
|
/* Keep track of abortions */
|
2021-06-24 10:09:10 +02:00
|
|
|
const upgradeAborted = { aborted: false };
|
2020-09-30 10:12:40 +02:00
|
|
|
|
|
|
|
res.onAborted(() => {
|
|
|
|
/* We can simply signal that we were aborted */
|
|
|
|
upgradeAborted.aborted = true;
|
2020-07-27 22:36:07 +02:00
|
|
|
});
|
2020-04-04 12:42:02 +02:00
|
|
|
|
2021-04-29 23:47:30 +02:00
|
|
|
const query = parse(req.getQuery());
|
2021-06-24 10:09:10 +02:00
|
|
|
const websocketKey = req.getHeader("sec-websocket-key");
|
|
|
|
const websocketProtocol = req.getHeader("sec-websocket-protocol");
|
|
|
|
const websocketExtensions = req.getHeader("sec-websocket-extensions");
|
|
|
|
const IPAddress = req.getHeader("x-forwarded-for");
|
2020-10-06 15:37:00 +02:00
|
|
|
|
2022-04-13 17:42:44 +02:00
|
|
|
adminApi.setLocale(req.getHeader("accept-language"));
|
|
|
|
|
2021-04-29 23:47:30 +02:00
|
|
|
const roomId = query.roomId;
|
|
|
|
try {
|
2021-06-24 10:09:10 +02:00
|
|
|
if (typeof roomId !== "string") {
|
|
|
|
throw new Error("Undefined room ID: ");
|
2020-10-12 16:23:07 +02:00
|
|
|
}
|
2020-10-06 18:09:23 +02:00
|
|
|
|
2020-10-06 15:37:00 +02:00
|
|
|
const token = query.token;
|
2020-10-06 18:09:23 +02:00
|
|
|
const x = Number(query.x);
|
|
|
|
const y = Number(query.y);
|
|
|
|
const top = Number(query.top);
|
|
|
|
const bottom = Number(query.bottom);
|
|
|
|
const left = Number(query.left);
|
|
|
|
const right = Number(query.right);
|
|
|
|
const name = query.name;
|
2021-04-02 21:21:11 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
let companion: CompanionMessage | undefined = undefined;
|
2021-04-02 21:21:11 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
if (typeof query.companion === "string") {
|
2021-04-06 18:54:45 +02:00
|
|
|
companion = new CompanionMessage();
|
2021-04-02 21:21:11 +02:00
|
|
|
companion.setName(query.companion);
|
|
|
|
}
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
if (typeof name !== "string") {
|
|
|
|
throw new Error("Expecting name");
|
2020-10-06 18:09:23 +02:00
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
if (name === "") {
|
|
|
|
throw new Error("No empty name");
|
2020-10-06 18:09:23 +02:00
|
|
|
}
|
|
|
|
let characterLayers = query.characterLayers;
|
|
|
|
if (characterLayers === null) {
|
2021-06-24 10:09:10 +02:00
|
|
|
throw new Error("Expecting skin");
|
2020-10-06 18:09:23 +02:00
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
if (typeof characterLayers === "string") {
|
|
|
|
characterLayers = [characterLayers];
|
2020-10-06 18:09:23 +02:00
|
|
|
}
|
2020-10-06 15:37:00 +02:00
|
|
|
|
2021-07-27 16:37:01 +02:00
|
|
|
const tokenData =
|
2021-08-15 22:51:06 +02:00
|
|
|
token && typeof token === "string" ? jwtTokenManager.verifyJWTToken(token) : null;
|
2021-01-16 20:14:21 +01:00
|
|
|
|
2021-11-12 16:43:37 +01:00
|
|
|
if (DISABLE_ANONYMOUS && !tokenData) {
|
|
|
|
throw new Error("Expecting token");
|
|
|
|
}
|
|
|
|
|
2021-07-27 16:37:01 +02:00
|
|
|
const userIdentifier = tokenData ? tokenData.identifier : "";
|
2021-01-16 20:14:21 +01:00
|
|
|
|
2020-10-14 11:07:34 +02:00
|
|
|
let memberTags: string[] = [];
|
2021-06-24 10:09:10 +02:00
|
|
|
let memberVisitCardUrl: string | null = null;
|
2021-03-05 18:25:27 +01:00
|
|
|
let memberMessages: unknown;
|
2021-12-01 16:38:34 +01:00
|
|
|
let memberUserRoomToken: string | undefined;
|
2022-03-11 17:02:58 +01:00
|
|
|
let memberTextures: WokaDetail[] = [];
|
2020-10-15 17:25:16 +02:00
|
|
|
const room = await socketManager.getOrCreateRoom(roomId);
|
2021-07-27 16:37:01 +02:00
|
|
|
let userData: FetchMemberDataByUuidResponse = {
|
2021-11-16 14:46:32 +01:00
|
|
|
email: userIdentifier,
|
2021-07-27 16:37:01 +02:00
|
|
|
userUuid: userIdentifier,
|
|
|
|
tags: [],
|
|
|
|
visitCardUrl: null,
|
|
|
|
textures: [],
|
|
|
|
messages: [],
|
|
|
|
anonymous: true,
|
2021-12-01 16:38:34 +01:00
|
|
|
userRoomToken: undefined,
|
2021-07-27 16:37:01 +02:00
|
|
|
};
|
2022-03-11 17:02:58 +01:00
|
|
|
|
|
|
|
let characterLayerObjs: WokaDetail[];
|
|
|
|
|
2020-11-05 11:25:35 +01:00
|
|
|
if (ADMIN_API_URL) {
|
|
|
|
try {
|
2021-03-01 22:21:52 +01:00
|
|
|
try {
|
2022-03-11 17:02:58 +01:00
|
|
|
userData = await adminApi.fetchMemberDataByUuid(
|
|
|
|
userIdentifier,
|
|
|
|
roomId,
|
|
|
|
IPAddress,
|
|
|
|
characterLayers
|
|
|
|
);
|
2021-06-24 10:09:10 +02:00
|
|
|
} catch (err) {
|
2021-12-01 10:12:07 +01:00
|
|
|
if (Axios.isAxiosError(err)) {
|
2022-04-19 16:50:21 +02:00
|
|
|
const errorType = isErrorApiData.safeParse(err?.response?.data);
|
2022-04-19 17:11:51 +02:00
|
|
|
if (errorType.success) {
|
|
|
|
return res.upgrade(
|
|
|
|
{
|
|
|
|
rejected: true,
|
|
|
|
reason: "error",
|
|
|
|
status: err?.response?.status,
|
|
|
|
error: errorType.data,
|
|
|
|
} as UpgradeFailedData,
|
|
|
|
websocketKey,
|
|
|
|
websocketProtocol,
|
|
|
|
websocketExtensions,
|
|
|
|
context
|
|
|
|
);
|
|
|
|
}
|
2021-03-01 22:21:52 +01:00
|
|
|
}
|
2022-04-19 16:50:21 +02:00
|
|
|
throw err;
|
2021-03-01 22:21:52 +01:00
|
|
|
}
|
2021-03-05 18:25:27 +01:00
|
|
|
memberMessages = userData.messages;
|
2020-11-05 11:25:35 +01:00
|
|
|
memberTags = userData.tags;
|
2021-06-08 16:30:58 +02:00
|
|
|
memberVisitCardUrl = userData.visitCardUrl;
|
2020-11-05 11:25:35 +01:00
|
|
|
memberTextures = userData.textures;
|
2021-12-01 16:38:34 +01:00
|
|
|
memberUserRoomToken = userData.userRoomToken;
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
if (
|
|
|
|
room.policyType === GameRoomPolicyTypes.USE_TAGS_POLICY &&
|
|
|
|
(userData.anonymous === true || !room.canAccess(memberTags))
|
|
|
|
) {
|
|
|
|
throw new Error("Insufficient privileges to access this room");
|
2020-11-05 11:25:35 +01:00
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
if (
|
|
|
|
room.policyType === GameRoomPolicyTypes.MEMBERS_ONLY_POLICY &&
|
|
|
|
userData.anonymous === true
|
|
|
|
) {
|
|
|
|
throw new Error("Use the login URL to connect");
|
2021-03-01 22:21:52 +01:00
|
|
|
}
|
2022-03-11 17:02:58 +01:00
|
|
|
|
|
|
|
characterLayerObjs = memberTextures;
|
2020-11-05 11:25:35 +01:00
|
|
|
} catch (e) {
|
2021-07-27 16:37:01 +02:00
|
|
|
console.log(
|
|
|
|
"access not granted for user " +
|
|
|
|
(userIdentifier || "anonymous") +
|
|
|
|
" and room " +
|
|
|
|
roomId
|
|
|
|
);
|
2020-11-05 11:25:35 +01:00
|
|
|
console.error(e);
|
2021-06-24 10:09:10 +02:00
|
|
|
throw new Error("User cannot access this world");
|
2020-10-13 16:46:46 +02:00
|
|
|
}
|
2022-03-11 17:02:58 +01:00
|
|
|
} else {
|
|
|
|
const fetchedTextures = await localWokaService.fetchWokaDetails(characterLayers);
|
|
|
|
if (fetchedTextures === undefined) {
|
|
|
|
// The textures we want to use do not exist!
|
|
|
|
// We need to go in error.
|
|
|
|
res.upgrade(
|
|
|
|
{
|
|
|
|
rejected: true,
|
|
|
|
reason: "textureInvalid",
|
|
|
|
message: "",
|
|
|
|
roomId,
|
|
|
|
} as UpgradeFailedData,
|
|
|
|
websocketKey,
|
|
|
|
websocketProtocol,
|
|
|
|
websocketExtensions,
|
|
|
|
context
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
characterLayerObjs = fetchedTextures;
|
2020-10-09 14:53:18 +02:00
|
|
|
}
|
2020-09-15 16:21:41 +02:00
|
|
|
|
2020-10-20 16:39:23 +02:00
|
|
|
// Generate characterLayers objects from characterLayers string[]
|
2022-03-11 17:02:58 +01:00
|
|
|
/*const characterLayerObjs: CharacterLayer[] =
|
|
|
|
SocketManager.mergeCharacterLayersAndCustomTextures(characterLayers, memberTextures);*/
|
2020-10-20 16:39:23 +02:00
|
|
|
|
2020-09-30 10:12:40 +02:00
|
|
|
if (upgradeAborted.aborted) {
|
|
|
|
console.log("Ouch! Client disconnected before we could upgrade it!");
|
|
|
|
/* You must not upgrade now */
|
|
|
|
return;
|
|
|
|
}
|
2020-09-15 16:21:41 +02:00
|
|
|
|
2020-09-30 10:12:40 +02:00
|
|
|
/* This immediately calls open handler, you must not use res after this call */
|
2021-06-24 10:09:10 +02:00
|
|
|
res.upgrade(
|
|
|
|
{
|
2020-09-30 10:12:40 +02:00
|
|
|
// Data passed here is accessible on the "websocket" socket object.
|
2022-03-11 17:02:58 +01:00
|
|
|
rejected: false,
|
2020-10-06 15:37:00 +02:00
|
|
|
token,
|
2021-07-27 16:37:01 +02:00
|
|
|
userUuid: userData.userUuid,
|
2021-01-15 03:19:58 +01:00
|
|
|
IPAddress,
|
2020-10-06 18:09:23 +02:00
|
|
|
roomId,
|
|
|
|
name,
|
2021-04-02 21:21:11 +02:00
|
|
|
companion,
|
2020-10-20 16:39:23 +02:00
|
|
|
characterLayers: characterLayerObjs,
|
2021-03-05 18:25:27 +01:00
|
|
|
messages: memberMessages,
|
2020-10-14 16:00:25 +02:00
|
|
|
tags: memberTags,
|
2021-06-08 16:30:58 +02:00
|
|
|
visitCardUrl: memberVisitCardUrl,
|
2021-12-01 16:38:34 +01:00
|
|
|
userRoomToken: memberUserRoomToken,
|
2020-10-06 18:09:23 +02:00
|
|
|
position: {
|
|
|
|
x: x,
|
|
|
|
y: y,
|
2021-06-24 10:09:10 +02:00
|
|
|
direction: "down",
|
|
|
|
moving: false,
|
2020-10-06 18:09:23 +02:00
|
|
|
} as PointInterface,
|
|
|
|
viewport: {
|
|
|
|
top,
|
|
|
|
right,
|
|
|
|
bottom,
|
2021-06-24 10:09:10 +02:00
|
|
|
left,
|
|
|
|
},
|
2022-03-11 17:02:58 +01:00
|
|
|
} as UpgradeData,
|
2020-09-30 10:12:40 +02:00
|
|
|
/* Spell these correctly */
|
2020-10-09 16:18:25 +02:00
|
|
|
websocketKey,
|
|
|
|
websocketProtocol,
|
|
|
|
websocketExtensions,
|
2021-06-24 10:09:10 +02:00
|
|
|
context
|
|
|
|
);
|
2020-09-30 10:17:01 +02:00
|
|
|
} catch (e) {
|
2021-12-01 10:12:07 +01:00
|
|
|
if (e instanceof Error) {
|
|
|
|
res.upgrade(
|
|
|
|
{
|
|
|
|
rejected: true,
|
|
|
|
reason: e instanceof InvalidTokenError ? tokenInvalidException : null,
|
|
|
|
message: e.message,
|
2021-12-20 15:33:47 +01:00
|
|
|
roomId,
|
2022-03-11 17:02:58 +01:00
|
|
|
} as UpgradeFailedData,
|
2021-12-01 10:12:07 +01:00
|
|
|
websocketKey,
|
|
|
|
websocketProtocol,
|
|
|
|
websocketExtensions,
|
|
|
|
context
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
res.upgrade(
|
|
|
|
{
|
|
|
|
rejected: true,
|
|
|
|
reason: null,
|
|
|
|
message: "500 Internal Server Error",
|
2021-12-20 15:33:47 +01:00
|
|
|
roomId,
|
2022-03-11 17:02:58 +01:00
|
|
|
} as UpgradeFailedData,
|
2021-12-01 10:12:07 +01:00
|
|
|
websocketKey,
|
|
|
|
websocketProtocol,
|
|
|
|
websocketExtensions,
|
|
|
|
context
|
|
|
|
);
|
|
|
|
}
|
2020-09-15 16:21:41 +02:00
|
|
|
}
|
2020-09-30 10:12:40 +02:00
|
|
|
})();
|
|
|
|
},
|
2020-09-28 18:52:54 +02:00
|
|
|
/* Handlers */
|
2022-03-11 17:02:58 +01:00
|
|
|
open: (_ws: WebSocket) => {
|
|
|
|
const ws = _ws as WebSocket & (UpgradeData | UpgradeFailedData);
|
2021-06-24 10:09:10 +02:00
|
|
|
if (ws.rejected === true) {
|
2021-12-20 14:12:58 +01:00
|
|
|
// If there is a room in the error, let's check if we need to clean it.
|
2021-12-20 15:33:47 +01:00
|
|
|
if (ws.roomId) {
|
2022-03-11 17:02:58 +01:00
|
|
|
socketManager.deleteRoomIfEmptyFromId(ws.roomId);
|
2021-12-20 14:12:58 +01:00
|
|
|
}
|
|
|
|
|
2021-04-29 23:47:30 +02:00
|
|
|
//FIX ME to use status code
|
2021-07-27 16:37:01 +02:00
|
|
|
if (ws.reason === tokenInvalidException) {
|
|
|
|
socketManager.emitTokenExpiredMessage(ws);
|
2022-03-11 17:02:58 +01:00
|
|
|
} else if (ws.reason === "textureInvalid") {
|
|
|
|
socketManager.emitInvalidTextureMessage(ws);
|
2022-04-12 10:23:36 +02:00
|
|
|
} else if (ws.reason === "error") {
|
2022-04-19 16:50:21 +02:00
|
|
|
socketManager.emitErrorScreenMessage(ws, ws.error);
|
2021-06-24 10:09:10 +02:00
|
|
|
} else {
|
2022-03-11 17:02:58 +01:00
|
|
|
socketManager.emitConnexionErrorMessage(ws, ws.message);
|
2021-04-29 23:47:30 +02:00
|
|
|
}
|
2021-07-27 16:37:01 +02:00
|
|
|
setTimeout(() => ws.close(), 0);
|
2021-04-29 23:47:30 +02:00
|
|
|
return;
|
2021-03-05 18:25:27 +01:00
|
|
|
}
|
2021-04-30 09:33:18 +02:00
|
|
|
|
2020-10-06 18:09:23 +02:00
|
|
|
// Let's join the room
|
2021-03-11 16:14:34 +01:00
|
|
|
const client = this.initClient(ws);
|
2020-10-15 17:25:16 +02:00
|
|
|
socketManager.handleJoinRoom(client);
|
2020-10-20 09:20:00 +02:00
|
|
|
|
2020-11-05 11:25:35 +01:00
|
|
|
//get data information and show messages
|
2021-03-05 18:25:27 +01:00
|
|
|
if (client.messages && Array.isArray(client.messages)) {
|
|
|
|
client.messages.forEach((c: unknown) => {
|
2021-06-24 10:09:10 +02:00
|
|
|
const messageToSend = c as { type: string; message: string };
|
2020-12-11 12:23:50 +01:00
|
|
|
|
2021-03-05 18:25:27 +01:00
|
|
|
const sendUserMessage = new SendUserMessage();
|
|
|
|
sendUserMessage.setType(messageToSend.type);
|
|
|
|
sendUserMessage.setMessage(messageToSend.message);
|
2020-12-11 12:23:50 +01:00
|
|
|
|
2021-03-05 18:25:27 +01:00
|
|
|
const serverToClientMessage = new ServerToClientMessage();
|
|
|
|
serverToClientMessage.setSendusermessage(sendUserMessage);
|
2020-12-11 12:23:50 +01:00
|
|
|
|
2021-03-05 18:25:27 +01:00
|
|
|
if (!client.disconnecting) {
|
|
|
|
client.send(serverToClientMessage.serializeBinary().buffer, true);
|
|
|
|
}
|
2020-10-20 09:20:00 +02:00
|
|
|
});
|
2020-11-05 11:25:35 +01:00
|
|
|
}
|
2020-09-28 18:52:54 +02:00
|
|
|
},
|
2022-04-12 14:21:19 +02:00
|
|
|
message: (ws, arrayBuffer): void => {
|
2020-09-28 18:52:54 +02:00
|
|
|
const client = ws as ExSocketInterface;
|
|
|
|
const message = ClientToServerMessage.deserializeBinary(new Uint8Array(arrayBuffer));
|
|
|
|
|
2020-10-06 15:37:00 +02:00
|
|
|
if (message.hasViewportmessage()) {
|
2020-11-13 18:00:22 +01:00
|
|
|
socketManager.handleViewport(client, (message.getViewportmessage() as ViewportMessage).toObject());
|
2020-09-28 18:52:54 +02:00
|
|
|
} else if (message.hasUsermovesmessage()) {
|
2020-10-15 17:25:16 +02:00
|
|
|
socketManager.handleUserMovesMessage(client, message.getUsermovesmessage() as UserMovesMessage);
|
2020-09-28 18:52:54 +02:00
|
|
|
} else if (message.hasSetplayerdetailsmessage()) {
|
2021-06-24 10:09:10 +02:00
|
|
|
socketManager.handleSetPlayerDetails(
|
|
|
|
client,
|
|
|
|
message.getSetplayerdetailsmessage() as SetPlayerDetailsMessage
|
|
|
|
);
|
2020-09-28 18:52:54 +02:00
|
|
|
} else if (message.hasSilentmessage()) {
|
2020-10-15 17:25:16 +02:00
|
|
|
socketManager.handleSilentMessage(client, message.getSilentmessage() as SilentMessage);
|
2020-09-28 18:52:54 +02:00
|
|
|
} else if (message.hasItemeventmessage()) {
|
2020-10-15 17:25:16 +02:00
|
|
|
socketManager.handleItemEvent(client, message.getItemeventmessage() as ItemEventMessage);
|
2021-07-06 15:30:49 +02:00
|
|
|
} else if (message.hasVariablemessage()) {
|
|
|
|
socketManager.handleVariableEvent(client, message.getVariablemessage() as VariableMessage);
|
2020-09-29 16:01:22 +02:00
|
|
|
} else if (message.hasWebrtcsignaltoservermessage()) {
|
2021-06-24 10:09:10 +02:00
|
|
|
socketManager.emitVideo(
|
|
|
|
client,
|
|
|
|
message.getWebrtcsignaltoservermessage() as WebRtcSignalToServerMessage
|
|
|
|
);
|
2020-09-29 16:01:22 +02:00
|
|
|
} else if (message.hasWebrtcscreensharingsignaltoservermessage()) {
|
2021-06-24 10:09:10 +02:00
|
|
|
socketManager.emitScreenSharing(
|
|
|
|
client,
|
|
|
|
message.getWebrtcscreensharingsignaltoservermessage() as WebRtcSignalToServerMessage
|
|
|
|
);
|
2020-10-01 14:11:34 +02:00
|
|
|
} else if (message.hasPlayglobalmessage()) {
|
2020-10-15 17:25:16 +02:00
|
|
|
socketManager.emitPlayGlobalMessage(client, message.getPlayglobalmessage() as PlayGlobalMessage);
|
2021-06-24 10:09:10 +02:00
|
|
|
} else if (message.hasReportplayermessage()) {
|
2020-10-15 17:25:16 +02:00
|
|
|
socketManager.handleReportMessage(client, message.getReportplayermessage() as ReportPlayerMessage);
|
2021-06-24 10:09:10 +02:00
|
|
|
} else if (message.hasQueryjitsijwtmessage()) {
|
|
|
|
socketManager.handleQueryJitsiJwtMessage(
|
|
|
|
client,
|
|
|
|
message.getQueryjitsijwtmessage() as QueryJitsiJwtMessage
|
|
|
|
);
|
|
|
|
} else if (message.hasEmotepromptmessage()) {
|
|
|
|
socketManager.handleEmotePromptMessage(
|
|
|
|
client,
|
|
|
|
message.getEmotepromptmessage() as EmotePromptMessage
|
|
|
|
);
|
2021-12-12 16:56:26 +01:00
|
|
|
} else if (message.hasFollowrequestmessage()) {
|
|
|
|
socketManager.handleFollowRequest(
|
2021-12-12 00:24:39 +01:00
|
|
|
client,
|
2021-12-12 16:56:26 +01:00
|
|
|
message.getFollowrequestmessage() as FollowRequestMessage
|
2021-12-12 00:24:39 +01:00
|
|
|
);
|
2021-12-12 16:56:26 +01:00
|
|
|
} else if (message.hasFollowconfirmationmessage()) {
|
|
|
|
socketManager.handleFollowConfirmation(
|
|
|
|
client,
|
|
|
|
message.getFollowconfirmationmessage() as FollowConfirmationMessage
|
|
|
|
);
|
|
|
|
} else if (message.hasFollowabortmessage()) {
|
|
|
|
socketManager.handleFollowAbort(client, message.getFollowabortmessage() as FollowAbortMessage);
|
2022-03-03 12:22:16 +01:00
|
|
|
} else if (message.hasLockgrouppromptmessage()) {
|
|
|
|
socketManager.handleLockGroup(
|
|
|
|
client,
|
|
|
|
message.getLockgrouppromptmessage() as LockGroupPromptMessage
|
|
|
|
);
|
2020-04-04 17:22:02 +02:00
|
|
|
}
|
2020-06-11 23:18:06 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
/* Ok is false if backpressure was built up, wait for drain */
|
2020-09-28 18:52:54 +02:00
|
|
|
//let ok = ws.send(message, isBinary);
|
|
|
|
},
|
|
|
|
drain: (ws) => {
|
2021-06-24 10:09:10 +02:00
|
|
|
console.log("WebSocket backpressure: " + ws.getBufferedAmount());
|
2020-09-28 18:52:54 +02:00
|
|
|
},
|
2022-04-12 14:21:19 +02:00
|
|
|
close: (ws) => {
|
2021-06-24 10:09:10 +02:00
|
|
|
const Client = ws as ExSocketInterface;
|
2020-05-12 11:49:55 +02:00
|
|
|
try {
|
2020-09-28 18:52:54 +02:00
|
|
|
Client.disconnecting = true;
|
2020-05-12 11:49:55 +02:00
|
|
|
//leave room
|
2020-10-15 17:25:16 +02:00
|
|
|
socketManager.leaveRoom(Client);
|
2020-05-12 11:49:55 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error('An error occurred on "disconnect"');
|
|
|
|
console.error(e);
|
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
},
|
|
|
|
});
|
2020-09-28 18:52:54 +02:00
|
|
|
}
|
2020-08-31 14:03:40 +02:00
|
|
|
|
2020-10-15 17:25:16 +02:00
|
|
|
//eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
private initClient(ws: any): ExSocketInterface {
|
2021-06-24 10:09:10 +02:00
|
|
|
const client: ExSocketInterface = ws;
|
2020-10-15 17:25:16 +02:00
|
|
|
client.userId = this.nextUserId;
|
|
|
|
this.nextUserId++;
|
|
|
|
client.userUuid = ws.userUuid;
|
2021-01-15 03:19:58 +01:00
|
|
|
client.IPAddress = ws.IPAddress;
|
2020-10-15 17:25:16 +02:00
|
|
|
client.token = ws.token;
|
|
|
|
client.batchedMessages = new BatchMessage();
|
|
|
|
client.batchTimeout = null;
|
|
|
|
client.emitInBatch = (payload: SubMessage): void => {
|
|
|
|
emitInBatch(client, payload);
|
2021-06-24 10:09:10 +02:00
|
|
|
};
|
2020-10-15 17:25:16 +02:00
|
|
|
client.disconnecting = false;
|
|
|
|
|
2021-03-05 18:25:27 +01:00
|
|
|
client.messages = ws.messages;
|
2020-10-15 17:25:16 +02:00
|
|
|
client.name = ws.name;
|
|
|
|
client.tags = ws.tags;
|
2021-06-08 16:30:58 +02:00
|
|
|
client.visitCardUrl = ws.visitCardUrl;
|
2020-10-15 17:25:16 +02:00
|
|
|
client.characterLayers = ws.characterLayers;
|
2021-04-02 21:21:11 +02:00
|
|
|
client.companion = ws.companion;
|
2020-10-15 17:25:16 +02:00
|
|
|
client.roomId = ws.roomId;
|
2020-11-13 18:00:22 +01:00
|
|
|
client.listenedZones = new Set<Zone>();
|
2020-05-18 18:33:04 +02:00
|
|
|
return client;
|
2020-05-03 16:28:18 +02:00
|
|
|
}
|
2020-04-04 22:35:20 +02:00
|
|
|
}
|