Merge pull request #1226 from thecodingmachine/apply_prettier_on_push_and_back

Applying prettier in CI for pusher and back containers
This commit is contained in:
David Négrier 2021-06-24 10:29:21 +02:00 committed by GitHub
commit 52a90e52d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
74 changed files with 1872 additions and 1654 deletions

View File

@ -64,6 +64,11 @@ jobs:
run: yarn test run: yarn test
working-directory: "front" working-directory: "front"
# We will enable prettier checks on front in a few month, when most PRs without prettier have been merged
# - name: "Prettier"
# run: yarn run pretty-check
# working-directory: "front"
continuous-integration-pusher: continuous-integration-pusher:
name: "Continuous Integration Pusher" name: "Continuous Integration Pusher"
@ -107,6 +112,10 @@ jobs:
run: yarn test run: yarn test
working-directory: "pusher" working-directory: "pusher"
- name: "Prettier"
run: yarn run pretty-check
working-directory: "pusher"
continuous-integration-back: continuous-integration-back:
name: "Continuous Integration Back" name: "Continuous Integration Back"
@ -150,3 +159,7 @@ jobs:
run: yarn test run: yarn test
working-directory: "back" working-directory: "back"
- name: "Prettier"
run: yarn run pretty-check
working-directory: "back"

View File

@ -46,6 +46,15 @@ $ yarn run install
$ yarn run prepare $ yarn run prepare
``` ```
If you don't have the precommit hook installed (or if you committed code before installing the precommit hook), you will need
to run code linting manually:
```console
$ docker-compose exec front yarn run pretty
$ docker-compose exec pusher yarn run pretty
$ docker-compose exec back yarn run pretty
```
### Providing tests ### Providing tests
WorkAdventure is based on a video game engine (Phaser), and video games are not the easiest programs to unit test. WorkAdventure is based on a video game engine (Phaser), and video games are not the easiest programs to unit test.

View File

@ -10,8 +10,8 @@
"runprod": "node --max-old-space-size=4096 ./dist/server.js", "runprod": "node --max-old-space-size=4096 ./dist/server.js",
"profile": "tsc && node --prof ./dist/server.js", "profile": "tsc && node --prof ./dist/server.js",
"test": "ts-node node_modules/jasmine/bin/jasmine --config=jasmine.json", "test": "ts-node node_modules/jasmine/bin/jasmine --config=jasmine.json",
"lint": "node_modules/.bin/eslint src/ . --ext .ts", "lint": "DEBUG= node_modules/.bin/eslint src/ . --ext .ts",
"fix": "node_modules/.bin/eslint --fix src/ . --ext .ts", "fix": "DEBUG= node_modules/.bin/eslint --fix src/ . --ext .ts",
"precommit": "lint-staged", "precommit": "lint-staged",
"pretty": "yarn prettier --write 'src/**/*.{ts,tsx}'", "pretty": "yarn prettier --write 'src/**/*.{ts,tsx}'",
"pretty-check": "yarn prettier --check 'src/**/*.{ts,tsx}'" "pretty-check": "yarn prettier --check 'src/**/*.{ts,tsx}'"

View File

@ -1,10 +1,9 @@
import { HttpResponse } from "uWebSockets.js"; import { HttpResponse } from "uWebSockets.js";
export class BaseController { export class BaseController {
protected addCorsHeaders(res: HttpResponse): void { protected addCorsHeaders(res: HttpResponse): void {
res.writeHeader('access-control-allow-headers', 'Origin, X-Requested-With, Content-Type, Accept'); res.writeHeader("access-control-allow-headers", "Origin, X-Requested-With, Content-Type, Accept");
res.writeHeader('access-control-allow-methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); res.writeHeader("access-control-allow-methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
res.writeHeader('access-control-allow-origin', '*'); res.writeHeader("access-control-allow-origin", "*");
} }
} }

View File

@ -1,7 +1,7 @@
import { ADMIN_API_TOKEN } from "../Enum/EnvironmentVariable"; import { ADMIN_API_TOKEN } from "../Enum/EnvironmentVariable";
import { stringify } from "circular-json"; import { stringify } from "circular-json";
import { HttpRequest, HttpResponse } from "uWebSockets.js"; import { HttpRequest, HttpResponse } from "uWebSockets.js";
import { parse } from 'query-string'; import { parse } from "query-string";
import { App } from "../Server/sifrr.server"; import { App } from "../Server/sifrr.server";
import { socketManager } from "../Services/SocketManager"; import { socketManager } from "../Services/SocketManager";
@ -10,26 +10,27 @@ export class DebugController {
this.getDump(); this.getDump();
} }
getDump() { getDump() {
this.App.get("/dump", (res: HttpResponse, req: HttpRequest) => { this.App.get("/dump", (res: HttpResponse, req: HttpRequest) => {
const query = parse(req.getQuery()); const query = parse(req.getQuery());
if (query.token !== ADMIN_API_TOKEN) { if (query.token !== ADMIN_API_TOKEN) {
return res.status(401).send('Invalid token sent!'); return res.status(401).send("Invalid token sent!");
} }
return res.writeStatus('200 OK').writeHeader('Content-Type', 'application/json').end(stringify( return res
socketManager.getWorlds(), .writeStatus("200 OK")
(key: unknown, value: unknown) => { .writeHeader("Content-Type", "application/json")
if (key === 'listeners') { .end(
return 'Listeners'; stringify(socketManager.getWorlds(), (key: unknown, value: unknown) => {
if (key === "listeners") {
return "Listeners";
} }
if (key === 'socket') { if (key === "socket") {
return 'Socket'; return "Socket";
} }
if (key === 'batchedMessages') { if (key === "batchedMessages") {
return 'BatchedMessages'; return "BatchedMessages";
} }
if (value instanceof Map) { if (value instanceof Map) {
const obj: any = {}; // eslint-disable-line @typescript-eslint/no-explicit-any const obj: any = {}; // eslint-disable-line @typescript-eslint/no-explicit-any
@ -46,8 +47,8 @@ export class DebugController {
} else { } else {
return value; return value;
} }
} })
)); );
}); });
} }
} }

View File

@ -1,7 +1,7 @@
import { App } from "../Server/sifrr.server"; import { App } from "../Server/sifrr.server";
import { HttpRequest, HttpResponse } from "uWebSockets.js"; import { HttpRequest, HttpResponse } from "uWebSockets.js";
const register = require('prom-client').register; const register = require("prom-client").register;
const collectDefaultMetrics = require('prom-client').collectDefaultMetrics; const collectDefaultMetrics = require("prom-client").collectDefaultMetrics;
export class PrometheusController { export class PrometheusController {
constructor(private App: App) { constructor(private App: App) {
@ -14,7 +14,7 @@ export class PrometheusController {
} }
private metrics(res: HttpResponse, req: HttpRequest): void { private metrics(res: HttpResponse, req: HttpRequest): void {
res.writeHeader('Content-Type', register.contentType); res.writeHeader("Content-Type", register.contentType);
res.end(register.metrics()); res.end(register.metrics());
} }
} }

View File

@ -1,17 +1,17 @@
const MINIMUM_DISTANCE = process.env.MINIMUM_DISTANCE ? Number(process.env.MINIMUM_DISTANCE) : 64; const MINIMUM_DISTANCE = process.env.MINIMUM_DISTANCE ? Number(process.env.MINIMUM_DISTANCE) : 64;
const GROUP_RADIUS = process.env.GROUP_RADIUS ? Number(process.env.GROUP_RADIUS) : 48; const GROUP_RADIUS = process.env.GROUP_RADIUS ? Number(process.env.GROUP_RADIUS) : 48;
const ALLOW_ARTILLERY = process.env.ALLOW_ARTILLERY ? process.env.ALLOW_ARTILLERY == 'true' : false; const ALLOW_ARTILLERY = process.env.ALLOW_ARTILLERY ? process.env.ALLOW_ARTILLERY == "true" : false;
const ADMIN_API_URL = process.env.ADMIN_API_URL || ''; const ADMIN_API_URL = process.env.ADMIN_API_URL || "";
const ADMIN_API_TOKEN = process.env.ADMIN_API_TOKEN || 'myapitoken'; const ADMIN_API_TOKEN = process.env.ADMIN_API_TOKEN || "myapitoken";
const CPU_OVERHEAT_THRESHOLD = Number(process.env.CPU_OVERHEAT_THRESHOLD) || 80; const CPU_OVERHEAT_THRESHOLD = Number(process.env.CPU_OVERHEAT_THRESHOLD) || 80;
const JITSI_URL : string|undefined = (process.env.JITSI_URL === '') ? undefined : process.env.JITSI_URL; const JITSI_URL: string | undefined = process.env.JITSI_URL === "" ? undefined : process.env.JITSI_URL;
const JITSI_ISS = process.env.JITSI_ISS || ''; const JITSI_ISS = process.env.JITSI_ISS || "";
const SECRET_JITSI_KEY = process.env.SECRET_JITSI_KEY || ''; const SECRET_JITSI_KEY = process.env.SECRET_JITSI_KEY || "";
const HTTP_PORT = parseInt(process.env.HTTP_PORT || '8080') || 8080; const HTTP_PORT = parseInt(process.env.HTTP_PORT || "8080") || 8080;
const GRPC_PORT = parseInt(process.env.GRPC_PORT || '50051') || 50051; const GRPC_PORT = parseInt(process.env.GRPC_PORT || "50051") || 50051;
export const SOCKET_IDLE_TIMER = parseInt(process.env.SOCKET_IDLE_TIMER as string) || 30; // maximum time (in second) without activity before a socket is closed export const SOCKET_IDLE_TIMER = parseInt(process.env.SOCKET_IDLE_TIMER as string) || 30; // maximum time (in second) without activity before a socket is closed
export const TURN_STATIC_AUTH_SECRET = process.env.TURN_STATIC_AUTH_SECRET || ''; export const TURN_STATIC_AUTH_SECRET = process.env.TURN_STATIC_AUTH_SECRET || "";
export const MAX_PER_GROUP = parseInt(process.env.MAX_PER_GROUP || '4'); export const MAX_PER_GROUP = parseInt(process.env.MAX_PER_GROUP || "4");
export { export {
MINIMUM_DISTANCE, MINIMUM_DISTANCE,
@ -24,5 +24,5 @@ export {
CPU_OVERHEAT_THRESHOLD, CPU_OVERHEAT_THRESHOLD,
JITSI_URL, JITSI_URL,
JITSI_ISS, JITSI_ISS,
SECRET_JITSI_KEY SECRET_JITSI_KEY,
} };

View File

@ -1,15 +1,12 @@
import { import {
ServerToAdminClientMessage, ServerToAdminClientMessage,
UserJoinedRoomMessage, UserLeftRoomMessage UserJoinedRoomMessage,
UserLeftRoomMessage,
} from "../Messages/generated/messages_pb"; } from "../Messages/generated/messages_pb";
import { AdminSocket } from "../RoomManager"; import { AdminSocket } from "../RoomManager";
export class Admin { export class Admin {
public constructor( public constructor(private readonly socket: AdminSocket) {}
private readonly socket: AdminSocket
) {
}
public sendUserJoin(uuid: string, name: string, ip: string): void { public sendUserJoin(uuid: string, name: string, ip: string): void {
const serverToAdminClientMessage = new ServerToAdminClientMessage(); const serverToAdminClientMessage = new ServerToAdminClientMessage();

View File

@ -39,12 +39,13 @@ export class GameRoom {
private readonly positionNotifier: PositionNotifier; private readonly positionNotifier: PositionNotifier;
public readonly roomId: string; public readonly roomId: string;
public readonly roomSlug: string; public readonly roomSlug: string;
public readonly worldSlug: string = ''; public readonly worldSlug: string = "";
public readonly organizationSlug: string = ''; public readonly organizationSlug: string = "";
private versionNumber: number = 1; private versionNumber: number = 1;
private nextUserId: number = 1; private nextUserId: number = 1;
constructor(roomId: string, constructor(
roomId: string,
connectCallback: ConnectCallback, connectCallback: ConnectCallback,
disconnectCallback: DisconnectCallback, disconnectCallback: DisconnectCallback,
minDistance: number, minDistance: number,
@ -52,7 +53,7 @@ export class GameRoom {
onEnters: EntersCallback, onEnters: EntersCallback,
onMoves: MovesCallback, onMoves: MovesCallback,
onLeaves: LeavesCallback, onLeaves: LeavesCallback,
onEmote: EmoteCallback, onEmote: EmoteCallback
) { ) {
this.roomId = roomId; this.roomId = roomId;
@ -65,7 +66,6 @@ export class GameRoom {
this.worldSlug = worldSlug; this.worldSlug = worldSlug;
} }
this.users = new Map<number, User>(); this.users = new Map<number, User>();
this.usersByUuid = new Map<string, User>(); this.usersByUuid = new Map<string, User>();
this.admins = new Set<Admin>(); this.admins = new Set<Admin>();
@ -96,11 +96,12 @@ export class GameRoom {
public join(socket: UserSocket, joinRoomMessage: JoinRoomMessage): User { public join(socket: UserSocket, joinRoomMessage: JoinRoomMessage): User {
const positionMessage = joinRoomMessage.getPositionmessage(); const positionMessage = joinRoomMessage.getPositionmessage();
if (positionMessage === undefined) { if (positionMessage === undefined) {
throw new Error('Missing position message'); throw new Error("Missing position message");
} }
const position = ProtobufUtils.toPointInterface(positionMessage); const position = ProtobufUtils.toPointInterface(positionMessage);
const user = new User(this.nextUserId, const user = new User(
this.nextUserId,
joinRoomMessage.getUseruuid(), joinRoomMessage.getUseruuid(),
joinRoomMessage.getIpaddress(), joinRoomMessage.getIpaddress(),
position, position,
@ -129,9 +130,9 @@ export class GameRoom {
public leave(user: User) { public leave(user: User) {
const userObj = this.users.get(user.id); const userObj = this.users.get(user.id);
if (userObj === undefined) { if (userObj === undefined) {
console.warn('User ', user.id, 'does not belong to this game room! It should!'); console.warn("User ", user.id, "does not belong to this game room! It should!");
} }
if (userObj !== undefined && typeof userObj.group !== 'undefined') { if (userObj !== undefined && typeof userObj.group !== "undefined") {
this.leaveGroup(userObj); this.leaveGroup(userObj);
} }
this.users.delete(user.id); this.users.delete(user.id);
@ -181,14 +182,16 @@ export class GameRoom {
closestItem.join(user); closestItem.join(user);
} else { } else {
const closestUser: User = closestItem; const closestUser: User = closestItem;
const group: Group = new Group(this.roomId,[ const group: Group = new Group(
user, this.roomId,
closestUser [user, closestUser],
], this.connectCallback, this.disconnectCallback, this.positionNotifier); this.connectCallback,
this.disconnectCallback,
this.positionNotifier
);
this.groups.add(group); this.groups.add(group);
} }
} }
} else { } else {
// If the user is part of a group: // If the user is part of a group:
// should he leave the group? // should he leave the group?
@ -229,7 +232,9 @@ export class GameRoom {
this.positionNotifier.leave(group); this.positionNotifier.leave(group);
group.destroy(); group.destroy();
if (!this.groups.has(group)) { if (!this.groups.has(group)) {
throw new Error("Could not find group "+group.getId()+" referenced by user "+user.id+" in World."); throw new Error(
"Could not find group " + group.getId() + " referenced by user " + user.id + " in World."
);
} }
this.groups.delete(group); this.groups.delete(group);
//todo: is the group garbage collected? //todo: is the group garbage collected?
@ -247,13 +252,12 @@ export class GameRoom {
* OR * OR
* - close enough to a group (distance <= groupRadius) * - close enough to a group (distance <= groupRadius)
*/ */
private searchClosestAvailableUserOrGroup(user: User): User|Group|null private searchClosestAvailableUserOrGroup(user: User): User | Group | null {
{
let minimumDistanceFound: number = Math.max(this.minDistance, this.groupRadius); let minimumDistanceFound: number = Math.max(this.minDistance, this.groupRadius);
let matchingItem: User | Group | null = null; let matchingItem: User | Group | null = null;
this.users.forEach((currentUser, userId) => { this.users.forEach((currentUser, userId) => {
// Let's only check users that are not part of a group // Let's only check users that are not part of a group
if (typeof currentUser.group !== 'undefined') { if (typeof currentUser.group !== "undefined") {
return; return;
} }
if (currentUser === user) { if (currentUser === user) {
@ -285,15 +289,15 @@ export class GameRoom {
return matchingItem; return matchingItem;
} }
public static computeDistance(user1: User, user2: User): number public static computeDistance(user1: User, user2: User): number {
{
const user1Position = user1.getPosition(); const user1Position = user1.getPosition();
const user2Position = user2.getPosition(); const user2Position = user2.getPosition();
return Math.sqrt(Math.pow(user2Position.x - user1Position.x, 2) + Math.pow(user2Position.y - user1Position.y, 2)); return Math.sqrt(
Math.pow(user2Position.x - user1Position.x, 2) + Math.pow(user2Position.y - user1Position.y, 2)
);
} }
public static computeDistanceBetweenPositions(position1: PositionInterface, position2: PositionInterface): number public static computeDistanceBetweenPositions(position1: PositionInterface, position2: PositionInterface): number {
{
return Math.sqrt(Math.pow(position2.x - position1.x, 2) + Math.pow(position2.y - position1.y, 2)); return Math.sqrt(Math.pow(position2.x - position1.x, 2) + Math.pow(position2.y - position1.y, 2));
} }
@ -327,7 +331,7 @@ export class GameRoom {
} }
public incrementVersion(): number { public incrementVersion(): number {
this.versionNumber++ this.versionNumber++;
return this.versionNumber; return this.versionNumber;
} }

View File

@ -7,7 +7,6 @@ import {gaugeManager} from "../Services/GaugeManager";
import { MAX_PER_GROUP } from "../Enum/EnvironmentVariable"; import { MAX_PER_GROUP } from "../Enum/EnvironmentVariable";
export class Group implements Movable { export class Group implements Movable {
private static nextId: number = 1; private static nextId: number = 1;
private id: number; private id: number;
@ -18,8 +17,13 @@ export class Group implements Movable {
private wasDestroyed: boolean = false; private wasDestroyed: boolean = false;
private roomId: string; private roomId: string;
constructor(
constructor(roomId: string, users: User[], private connectCallback: ConnectCallback, private disconnectCallback: DisconnectCallback, private positionNotifier: PositionNotifier) { roomId: string,
users: User[],
private connectCallback: ConnectCallback,
private disconnectCallback: DisconnectCallback,
private positionNotifier: PositionNotifier
) {
this.roomId = roomId; this.roomId = roomId;
this.users = new Set<User>(); this.users = new Set<User>();
this.id = Group.nextId; this.id = Group.nextId;
@ -53,7 +57,7 @@ export class Group implements Movable {
getPosition(): PositionInterface { getPosition(): PositionInterface {
return { return {
x: this.x, x: this.x,
y: this.y y: this.y,
}; };
} }
@ -95,16 +99,14 @@ export class Group implements Movable {
return this.users.size <= 1; return this.users.size <= 1;
} }
join(user: User): void join(user: User): void {
{
// Broadcast on the right event // Broadcast on the right event
this.connectCallback(user, this); this.connectCallback(user, this);
this.users.add(user); this.users.add(user);
user.group = this; user.group = this;
} }
leave(user: User): void leave(user: User): void {
{
const success = this.users.delete(user); const success = this.users.delete(user);
if (success === false) { if (success === false) {
throw new Error("Could not find user " + user.id + " in the group " + this.id); throw new Error("Could not find user " + user.id + " in the group " + this.id);
@ -123,8 +125,7 @@ export class Group implements Movable {
* Let's kick everybody out. * Let's kick everybody out.
* Usually used when there is only one user left. * Usually used when there is only one user left.
*/ */
destroy(): void destroy(): void {
{
if (this.hasEditedGauge) gaugeManager.decNbGroupsPerRoomGauge(this.roomId); if (this.hasEditedGauge) gaugeManager.decNbGroupsPerRoomGauge(this.roomId);
for (const user of this.users) { for (const user of this.users) {
this.leave(user); this.leave(user);

View File

@ -4,5 +4,5 @@ import {PositionInterface} from "_Model/PositionInterface";
* A physical object that can be placed into a Zone * A physical object that can be placed into a Zone
*/ */
export interface Movable { export interface Movable {
getPosition(): PositionInterface getPosition(): PositionInterface;
} }

View File

@ -1,4 +1,4 @@
export interface PositionInterface { export interface PositionInterface {
x: number, x: number;
y: number y: number;
} }

View File

@ -21,19 +21,24 @@ interface ZoneDescriptor {
} }
export class PositionNotifier { export class PositionNotifier {
// TODO: we need a way to clean the zones if noone is in the zone and noone listening (to free memory!) // TODO: we need a way to clean the zones if noone is in the zone and noone listening (to free memory!)
private zones: Zone[][] = []; private zones: Zone[][] = [];
constructor(private zoneWidth: number, private zoneHeight: number, private onUserEnters: EntersCallback, private onUserMoves: MovesCallback, private onUserLeaves: LeavesCallback, private onEmote: EmoteCallback) { constructor(
} private zoneWidth: number,
private zoneHeight: number,
private onUserEnters: EntersCallback,
private onUserMoves: MovesCallback,
private onUserLeaves: LeavesCallback,
private onEmote: EmoteCallback
) {}
private getZoneDescriptorFromCoordinates(x: number, y: number): ZoneDescriptor { private getZoneDescriptorFromCoordinates(x: number, y: number): ZoneDescriptor {
return { return {
i: Math.floor(x / this.zoneWidth), i: Math.floor(x / this.zoneWidth),
j: Math.floor(y / this.zoneHeight), j: Math.floor(y / this.zoneHeight),
} };
} }
public enter(thing: Movable): void { public enter(thing: Movable): void {
@ -100,6 +105,5 @@ export class PositionNotifier {
const zoneDesc = this.getZoneDescriptorFromCoordinates(user.getPosition().x, user.getPosition().y); const zoneDesc = this.getZoneDescriptorFromCoordinates(user.getPosition().x, user.getPosition().y);
const zone = this.getZone(zoneDesc.i, zoneDesc.j); const zone = this.getZone(zoneDesc.i, zoneDesc.j);
zone.emitEmoteEvent(emoteEventMessage); zone.emitEmoteEvent(emoteEventMessage);
} }
} }

View File

@ -1,30 +1,30 @@
//helper functions to parse room IDs //helper functions to parse room IDs
export const isRoomAnonymous = (roomID: string): boolean => { export const isRoomAnonymous = (roomID: string): boolean => {
if (roomID.startsWith('_/')) { if (roomID.startsWith("_/")) {
return true; return true;
} else if(roomID.startsWith('@/')) { } else if (roomID.startsWith("@/")) {
return false; return false;
} else { } else {
throw new Error('Incorrect room ID: '+roomID); throw new Error("Incorrect room ID: " + roomID);
}
} }
};
export const extractRoomSlugPublicRoomId = (roomId: string): string => { export const extractRoomSlugPublicRoomId = (roomId: string): string => {
const idParts = roomId.split('/'); const idParts = roomId.split("/");
if (idParts.length < 3) throw new Error('Incorrect roomId: '+roomId); if (idParts.length < 3) throw new Error("Incorrect roomId: " + roomId);
return idParts.slice(2).join('/'); return idParts.slice(2).join("/");
} };
export interface extractDataFromPrivateRoomIdResponse { export interface extractDataFromPrivateRoomIdResponse {
organizationSlug: string; organizationSlug: string;
worldSlug: string; worldSlug: string;
roomSlug: string; roomSlug: string;
} }
export const extractDataFromPrivateRoomId = (roomId: string): extractDataFromPrivateRoomIdResponse => { export const extractDataFromPrivateRoomId = (roomId: string): extractDataFromPrivateRoomIdResponse => {
const idParts = roomId.split('/'); const idParts = roomId.split("/");
if (idParts.length < 4) throw new Error('Incorrect roomId: '+roomId); if (idParts.length < 4) throw new Error("Incorrect roomId: " + roomId);
const organizationSlug = idParts[1]; const organizationSlug = idParts[1];
const worldSlug = idParts[2]; const worldSlug = idParts[2];
const roomSlug = idParts[3]; const roomSlug = idParts[3];
return {organizationSlug, worldSlug, roomSlug} return { organizationSlug, worldSlug, roomSlug };
} };

View File

@ -4,7 +4,13 @@ import {Zone} from "_Model/Zone";
import { Movable } from "_Model/Movable"; import { Movable } from "_Model/Movable";
import { PositionNotifier } from "_Model/PositionNotifier"; import { PositionNotifier } from "_Model/PositionNotifier";
import { ServerDuplexStream } from "grpc"; import { ServerDuplexStream } from "grpc";
import {BatchMessage, CompanionMessage, PusherToBackMessage, ServerToClientMessage, SubMessage} from "../Messages/generated/messages_pb"; import {
BatchMessage,
CompanionMessage,
PusherToBackMessage,
ServerToClientMessage,
SubMessage,
} from "../Messages/generated/messages_pb";
import { CharacterLayer } from "_Model/Websocket/CharacterLayer"; import { CharacterLayer } from "_Model/Websocket/CharacterLayer";
export type UserSocket = ServerDuplexStream<PusherToBackMessage, ServerToClientMessage>; export type UserSocket = ServerDuplexStream<PusherToBackMessage, ServerToClientMessage>;
@ -42,7 +48,6 @@ export class User implements Movable {
this.positionNotifier.updatePosition(this, position, oldPosition); this.positionNotifier.updatePosition(this, position, oldPosition);
} }
private batchedMessages: BatchMessage = new BatchMessage(); private batchedMessages: BatchMessage = new BatchMessage();
private batchTimeout: NodeJS.Timeout | null = null; private batchTimeout: NodeJS.Timeout | null = null;

View File

@ -1,4 +1,4 @@
export interface CharacterLayer { export interface CharacterLayer {
name: string, name: string;
url: string|undefined url: string | undefined;
} }

View File

@ -1,10 +1,11 @@
import * as tg from "generic-type-guard"; import * as tg from "generic-type-guard";
export const isItemEventMessageInterface = export const isItemEventMessageInterface = new tg.IsInterface()
new tg.IsInterface().withProperties({ .withProperties({
itemId: tg.isNumber, itemId: tg.isNumber,
event: tg.isString, event: tg.isString,
state: tg.isUnknown, state: tg.isUnknown,
parameters: tg.isUnknown, parameters: tg.isUnknown,
}).get(); })
.get();
export type ItemEventMessageInterface = tg.GuardedType<typeof isItemEventMessageInterface>; export type ItemEventMessageInterface = tg.GuardedType<typeof isItemEventMessageInterface>;

View File

@ -1,7 +1,10 @@
import { PointInterface } from "./PointInterface"; import { PointInterface } from "./PointInterface";
export class Point implements PointInterface { export class Point implements PointInterface {
constructor(public x : number, public y : number, public direction : string = "none", public moving : boolean = false) { constructor(
public x: number,
public y: number,
public direction: string = "none",
public moving: boolean = false
) {}
} }
}

View File

@ -7,11 +7,12 @@ import * as tg from "generic-type-guard";
readonly moving: boolean; readonly moving: boolean;
}*/ }*/
export const isPointInterface = export const isPointInterface = new tg.IsInterface()
new tg.IsInterface().withProperties({ .withProperties({
x: tg.isNumber, x: tg.isNumber,
y: tg.isNumber, y: tg.isNumber,
direction: tg.isString, direction: tg.isString,
moving: tg.isBoolean moving: tg.isBoolean,
}).get(); })
.get();
export type PointInterface = tg.GuardedType<typeof isPointInterface>; export type PointInterface = tg.GuardedType<typeof isPointInterface>;

View File

@ -3,7 +3,7 @@ import {
CharacterLayerMessage, CharacterLayerMessage,
ItemEventMessage, ItemEventMessage,
PointMessage, PointMessage,
PositionMessage PositionMessage,
} from "../../Messages/generated/messages_pb"; } from "../../Messages/generated/messages_pb";
import { CharacterLayer } from "_Model/Websocket/CharacterLayer"; import { CharacterLayer } from "_Model/Websocket/CharacterLayer";
import Direction = PositionMessage.Direction; import Direction = PositionMessage.Direction;
@ -11,24 +11,23 @@ import {ItemEventMessageInterface} from "_Model/Websocket/ItemEventMessage";
import { PositionInterface } from "_Model/PositionInterface"; import { PositionInterface } from "_Model/PositionInterface";
export class ProtobufUtils { export class ProtobufUtils {
public static toPositionMessage(point: PointInterface): PositionMessage { public static toPositionMessage(point: PointInterface): PositionMessage {
let direction: Direction; let direction: Direction;
switch (point.direction) { switch (point.direction) {
case 'up': case "up":
direction = Direction.UP; direction = Direction.UP;
break; break;
case 'down': case "down":
direction = Direction.DOWN; direction = Direction.DOWN;
break; break;
case 'left': case "left":
direction = Direction.LEFT; direction = Direction.LEFT;
break; break;
case 'right': case "right":
direction = Direction.RIGHT; direction = Direction.RIGHT;
break; break;
default: default:
throw new Error('unexpected direction'); throw new Error("unexpected direction");
} }
const position = new PositionMessage(); const position = new PositionMessage();
@ -44,16 +43,16 @@ export class ProtobufUtils {
let direction: string; let direction: string;
switch (position.getDirection()) { switch (position.getDirection()) {
case Direction.UP: case Direction.UP:
direction = 'up'; direction = "up";
break; break;
case Direction.DOWN: case Direction.DOWN:
direction = 'down'; direction = "down";
break; break;
case Direction.LEFT: case Direction.LEFT:
direction = 'left'; direction = "left";
break; break;
case Direction.RIGHT: case Direction.RIGHT:
direction = 'right'; direction = "right";
break; break;
default: default:
throw new Error("Unexpected direction"); throw new Error("Unexpected direction");
@ -82,7 +81,7 @@ export class ProtobufUtils {
event: itemEventMessage.getEvent(), event: itemEventMessage.getEvent(),
parameters: JSON.parse(itemEventMessage.getParametersjson()), parameters: JSON.parse(itemEventMessage.getParametersjson()),
state: JSON.parse(itemEventMessage.getStatejson()), state: JSON.parse(itemEventMessage.getStatejson()),
} };
} }
public static toItemEventProtobuf(itemEvent: ItemEventMessageInterface): ItemEventMessage { public static toItemEventProtobuf(itemEvent: ItemEventMessageInterface): ItemEventMessage {

View File

@ -14,8 +14,14 @@ export class Zone {
private things: Set<Movable> = new Set<Movable>(); private things: Set<Movable> = new Set<Movable>();
private listeners: Set<ZoneSocket> = new Set<ZoneSocket>(); private listeners: Set<ZoneSocket> = new Set<ZoneSocket>();
constructor(
constructor(private onEnters: EntersCallback, private onMoves: MovesCallback, private onLeaves: LeavesCallback, private onEmote: EmoteCallback, public readonly x: number, public readonly y: number) { } private onEnters: EntersCallback,
private onMoves: MovesCallback,
private onLeaves: LeavesCallback,
private onEmote: EmoteCallback,
public readonly x: number,
public readonly y: number
) {}
/** /**
* A user/thing leaves the zone * A user/thing leaves the zone
@ -24,12 +30,23 @@ export class Zone {
const result = this.things.delete(thing); const result = this.things.delete(thing);
if (!result) { if (!result) {
if (thing instanceof User) { if (thing instanceof User) {
throw new Error('Could not find user in zone '+thing.id); throw new Error("Could not find user in zone " + thing.id);
} }
if (thing instanceof Group) { if (thing instanceof Group) {
throw new Error('Could not find group '+thing.getId()+' in zone ('+this.x+','+this.y+'). Position of group: ('+thing.getPosition().x+','+thing.getPosition().y+')'); throw new Error(
"Could not find group " +
thing.getId() +
" in zone (" +
this.x +
"," +
this.y +
"). Position of group: (" +
thing.getPosition().x +
"," +
thing.getPosition().y +
")"
);
} }
} }
this.notifyLeft(thing, newZone); this.notifyLeft(thing, newZone);
} }
@ -57,7 +74,6 @@ export class Zone {
} }
} }
public move(thing: Movable, position: PositionInterface) { public move(thing: Movable, position: PositionInterface) {
if (!this.things.has(thing)) { if (!this.things.has(thing)) {
this.things.add(thing); this.things.add(thing);
@ -89,6 +105,5 @@ export class Zone {
for (const listener of this.listeners) { for (const listener of this.listeners) {
this.onEmote(emoteEventMessage, listener); this.onEmote(emoteEventMessage, listener);
} }
} }
} }

View File

@ -11,13 +11,15 @@ import {
JoinRoomMessage, JoinRoomMessage,
PlayGlobalMessage, PlayGlobalMessage,
PusherToBackMessage, PusherToBackMessage,
QueryJitsiJwtMessage, RefreshRoomPromptMessage, QueryJitsiJwtMessage,
RefreshRoomPromptMessage,
ServerToAdminClientMessage, ServerToAdminClientMessage,
ServerToClientMessage, ServerToClientMessage,
SilentMessage, SilentMessage,
UserMovesMessage, UserMovesMessage,
WebRtcSignalToServerMessage, WorldFullWarningToRoomMessage, WebRtcSignalToServerMessage,
ZoneMessage WorldFullWarningToRoomMessage,
ZoneMessage,
} from "./Messages/generated/messages_pb"; } from "./Messages/generated/messages_pb";
import { sendUnaryData, ServerDuplexStream, ServerUnaryCall, ServerWritableStream } from "grpc"; import { sendUnaryData, ServerDuplexStream, ServerUnaryCall, ServerWritableStream } from "grpc";
import { socketManager } from "./Services/SocketManager"; import { socketManager } from "./Services/SocketManager";
@ -27,23 +29,25 @@ import {GameRoom} from "./Model/GameRoom";
import Debug from "debug"; import Debug from "debug";
import { Admin } from "./Model/Admin"; import { Admin } from "./Model/Admin";
const debug = Debug('roommanager'); const debug = Debug("roommanager");
export type AdminSocket = ServerDuplexStream<AdminPusherToBackMessage, ServerToAdminClientMessage>; export type AdminSocket = ServerDuplexStream<AdminPusherToBackMessage, ServerToAdminClientMessage>;
export type ZoneSocket = ServerWritableStream<ZoneMessage, ServerToClientMessage>; export type ZoneSocket = ServerWritableStream<ZoneMessage, ServerToClientMessage>;
const roomManager: IRoomManagerServer = { const roomManager: IRoomManagerServer = {
joinRoom: (call: UserSocket): void => { joinRoom: (call: UserSocket): void => {
console.log('joinRoom called'); console.log("joinRoom called");
let room: GameRoom | null = null; let room: GameRoom | null = null;
let user: User | null = null; let user: User | null = null;
call.on('data', (message: PusherToBackMessage) => { call.on("data", (message: PusherToBackMessage) => {
try { try {
if (room === null || user === null) { if (room === null || user === null) {
if (message.hasJoinroommessage()) { if (message.hasJoinroommessage()) {
socketManager.handleJoinRoom(call, message.getJoinroommessage() as JoinRoomMessage).then(({room: gameRoom, user: myUser}) => { socketManager
.handleJoinRoom(call, message.getJoinroommessage() as JoinRoomMessage)
.then(({ room: gameRoom, user: myUser }) => {
if (call.writable) { if (call.writable) {
room = gameRoom; room = gameRoom;
user = myUser; user = myUser;
@ -53,27 +57,46 @@ const roomManager: IRoomManagerServer = {
} }
}); });
} else { } else {
throw new Error('The first message sent MUST be of type JoinRoomMessage'); throw new Error("The first message sent MUST be of type JoinRoomMessage");
} }
} else { } else {
if (message.hasJoinroommessage()) { if (message.hasJoinroommessage()) {
throw new Error('Cannot call JoinRoomMessage twice!'); throw new Error("Cannot call JoinRoomMessage twice!");
} else if (message.hasUsermovesmessage()) { } else if (message.hasUsermovesmessage()) {
socketManager.handleUserMovesMessage(room, user, message.getUsermovesmessage() as UserMovesMessage); socketManager.handleUserMovesMessage(
room,
user,
message.getUsermovesmessage() as UserMovesMessage
);
} else if (message.hasSilentmessage()) { } else if (message.hasSilentmessage()) {
socketManager.handleSilentMessage(room, user, message.getSilentmessage() as SilentMessage); socketManager.handleSilentMessage(room, user, message.getSilentmessage() as SilentMessage);
} else if (message.hasItemeventmessage()) { } else if (message.hasItemeventmessage()) {
socketManager.handleItemEvent(room, user, message.getItemeventmessage() as ItemEventMessage); socketManager.handleItemEvent(room, user, message.getItemeventmessage() as ItemEventMessage);
} else if (message.hasWebrtcsignaltoservermessage()) { } else if (message.hasWebrtcsignaltoservermessage()) {
socketManager.emitVideo(room, user, message.getWebrtcsignaltoservermessage() as WebRtcSignalToServerMessage); socketManager.emitVideo(
room,
user,
message.getWebrtcsignaltoservermessage() as WebRtcSignalToServerMessage
);
} else if (message.hasWebrtcscreensharingsignaltoservermessage()) { } else if (message.hasWebrtcscreensharingsignaltoservermessage()) {
socketManager.emitScreenSharing(room, user, message.getWebrtcscreensharingsignaltoservermessage() as WebRtcSignalToServerMessage); socketManager.emitScreenSharing(
room,
user,
message.getWebrtcscreensharingsignaltoservermessage() as WebRtcSignalToServerMessage
);
} else if (message.hasPlayglobalmessage()) { } else if (message.hasPlayglobalmessage()) {
socketManager.emitPlayGlobalMessage(room, message.getPlayglobalmessage() as PlayGlobalMessage); socketManager.emitPlayGlobalMessage(room, message.getPlayglobalmessage() as PlayGlobalMessage);
} else if (message.hasQueryjitsijwtmessage()) { } else if (message.hasQueryjitsijwtmessage()) {
socketManager.handleQueryJitsiJwtMessage(user, message.getQueryjitsijwtmessage() as QueryJitsiJwtMessage); socketManager.handleQueryJitsiJwtMessage(
user,
message.getQueryjitsijwtmessage() as QueryJitsiJwtMessage
);
} else if (message.hasEmotepromptmessage()) { } else if (message.hasEmotepromptmessage()) {
socketManager.handleEmoteEventMessage(room, user, message.getEmotepromptmessage() as EmotePromptMessage); socketManager.handleEmoteEventMessage(
room,
user,
message.getEmotepromptmessage() as EmotePromptMessage
);
} else if (message.hasSendusermessage()) { } else if (message.hasSendusermessage()) {
const sendUserMessage = message.getSendusermessage(); const sendUserMessage = message.getSendusermessage();
if (sendUserMessage !== undefined) { if (sendUserMessage !== undefined) {
@ -85,18 +108,17 @@ const roomManager: IRoomManagerServer = {
socketManager.handlerBanUserMessage(room, user, banUserMessage); socketManager.handlerBanUserMessage(room, user, banUserMessage);
} }
} else { } else {
throw new Error('Unhandled message type'); throw new Error("Unhandled message type");
} }
} }
} catch (e) { } catch (e) {
emitError(call, e); emitError(call, e);
call.end(); call.end();
} }
}); });
call.on('end', () => { call.on("end", () => {
debug('joinRoom ended'); debug("joinRoom ended");
if (user !== null && room !== null) { if (user !== null && room !== null) {
socketManager.leaveRoom(room, user); socketManager.leaveRoom(room, user);
} }
@ -105,41 +127,40 @@ const roomManager: IRoomManagerServer = {
user = null; user = null;
}); });
call.on('error', (err: Error) => { call.on("error", (err: Error) => {
console.error('An error occurred in joinRoom stream:', err); console.error("An error occurred in joinRoom stream:", err);
}); });
}, },
listenZone(call: ZoneSocket): void { listenZone(call: ZoneSocket): void {
debug('listenZone called'); debug("listenZone called");
const zoneMessage = call.request; const zoneMessage = call.request;
socketManager.addZoneListener(call, zoneMessage.getRoomid(), zoneMessage.getX(), zoneMessage.getY()); socketManager.addZoneListener(call, zoneMessage.getRoomid(), zoneMessage.getX(), zoneMessage.getY());
call.on('cancelled', () => { call.on("cancelled", () => {
debug('listenZone cancelled'); debug("listenZone cancelled");
socketManager.removeZoneListener(call, zoneMessage.getRoomid(), zoneMessage.getX(), zoneMessage.getY()); socketManager.removeZoneListener(call, zoneMessage.getRoomid(), zoneMessage.getX(), zoneMessage.getY());
call.end(); call.end();
}) });
call.on('close', () => { call.on("close", () => {
debug('listenZone connection closed'); debug("listenZone connection closed");
socketManager.removeZoneListener(call, zoneMessage.getRoomid(), zoneMessage.getX(), zoneMessage.getY()); socketManager.removeZoneListener(call, zoneMessage.getRoomid(), zoneMessage.getX(), zoneMessage.getY());
}).on('error', (e) => { }).on("error", (e) => {
console.error('An error occurred in listenZone stream:', e); console.error("An error occurred in listenZone stream:", e);
socketManager.removeZoneListener(call, zoneMessage.getRoomid(), zoneMessage.getX(), zoneMessage.getY()); socketManager.removeZoneListener(call, zoneMessage.getRoomid(), zoneMessage.getX(), zoneMessage.getY());
call.end(); call.end();
}); });
}, },
adminRoom(call: AdminSocket): void { adminRoom(call: AdminSocket): void {
console.log('adminRoom called'); console.log("adminRoom called");
const admin = new Admin(call); const admin = new Admin(call);
let room: GameRoom | null = null; let room: GameRoom | null = null;
call.on('data', (message: AdminPusherToBackMessage) => { call.on("data", (message: AdminPusherToBackMessage) => {
try { try {
if (room === null) { if (room === null) {
if (message.hasSubscribetoroom()) { if (message.hasSubscribetoroom()) {
@ -148,18 +169,17 @@ const roomManager: IRoomManagerServer = {
room = gameRoom; room = gameRoom;
}); });
} else { } else {
throw new Error('The first message sent MUST be of type JoinRoomMessage'); throw new Error("The first message sent MUST be of type JoinRoomMessage");
} }
} }
} catch (e) { } catch (e) {
emitError(call, e); emitError(call, e);
call.end(); call.end();
} }
}); });
call.on('end', () => { call.on("end", () => {
debug('joinRoom ended'); debug("joinRoom ended");
if (room !== null) { if (room !== null) {
socketManager.leaveAdminRoom(room, admin); socketManager.leaveAdminRoom(room, admin);
} }
@ -167,18 +187,21 @@ const roomManager: IRoomManagerServer = {
room = null; room = null;
}); });
call.on('error', (err: Error) => { call.on("error", (err: Error) => {
console.error('An error occurred in joinAdminRoom stream:', err); console.error("An error occurred in joinAdminRoom stream:", err);
}); });
}, },
sendAdminMessage(call: ServerUnaryCall<AdminMessage>, callback: sendUnaryData<EmptyMessage>): void { sendAdminMessage(call: ServerUnaryCall<AdminMessage>, callback: sendUnaryData<EmptyMessage>): void {
socketManager.sendAdminMessage(
socketManager.sendAdminMessage(call.request.getRoomid(), call.request.getRecipientuuid(), call.request.getMessage()); call.request.getRoomid(),
call.request.getRecipientuuid(),
call.request.getMessage()
);
callback(null, new EmptyMessage()); callback(null, new EmptyMessage());
}, },
sendGlobalAdminMessage(call: ServerUnaryCall<AdminGlobalMessage>, callback: sendUnaryData<EmptyMessage>): void { sendGlobalAdminMessage(call: ServerUnaryCall<AdminGlobalMessage>, callback: sendUnaryData<EmptyMessage>): void {
throw new Error('Not implemented yet'); throw new Error("Not implemented yet");
// TODO // TODO
callback(null, new EmptyMessage()); callback(null, new EmptyMessage());
}, },
@ -192,11 +215,17 @@ const roomManager: IRoomManagerServer = {
socketManager.sendAdminRoomMessage(call.request.getRoomid(), call.request.getMessage()); socketManager.sendAdminRoomMessage(call.request.getRoomid(), call.request.getMessage());
callback(null, new EmptyMessage()); callback(null, new EmptyMessage());
}, },
sendWorldFullWarningToRoom(call: ServerUnaryCall<WorldFullWarningToRoomMessage>, callback: sendUnaryData<EmptyMessage>): void { sendWorldFullWarningToRoom(
call: ServerUnaryCall<WorldFullWarningToRoomMessage>,
callback: sendUnaryData<EmptyMessage>
): void {
socketManager.dispatchWorlFullWarning(call.request.getRoomid()); socketManager.dispatchWorlFullWarning(call.request.getRoomid());
callback(null, new EmptyMessage()); callback(null, new EmptyMessage());
}, },
sendRefreshRoomPrompt(call: ServerUnaryCall<RefreshRoomPromptMessage>, callback: sendUnaryData<EmptyMessage>): void { sendRefreshRoomPrompt(
call: ServerUnaryCall<RefreshRoomPromptMessage>,
callback: sendUnaryData<EmptyMessage>
): void {
socketManager.dispatchRoomRefresh(call.request.getRoomid()); socketManager.dispatchRoomRefresh(call.request.getRoomid());
callback(null, new EmptyMessage()); callback(null, new EmptyMessage());
}, },

View File

@ -1,7 +1,7 @@
import { App as _App, AppOptions } from 'uWebSockets.js'; import { App as _App, AppOptions } from "uWebSockets.js";
import BaseApp from './baseapp'; import BaseApp from "./baseapp";
import { extend } from './utils'; import { extend } from "./utils";
import { UwsApp } from './types'; import { UwsApp } from "./types";
class App extends (<UwsApp>_App) { class App extends (<UwsApp>_App) {
constructor(options: AppOptions = {}) { constructor(options: AppOptions = {}) {

View File

@ -1,16 +1,16 @@
import { Readable } from 'stream'; import { Readable } from "stream";
import { us_listen_socket_close, TemplatedApp, HttpResponse, HttpRequest } from 'uWebSockets.js'; import { us_listen_socket_close, TemplatedApp, HttpResponse, HttpRequest } from "uWebSockets.js";
import formData from './formdata'; import formData from "./formdata";
import { stob } from './utils'; import { stob } from "./utils";
import { Handler } from './types'; import { Handler } from "./types";
import { join } from "path"; import { join } from "path";
const contTypes = ['application/x-www-form-urlencoded', 'multipart/form-data']; const contTypes = ["application/x-www-form-urlencoded", "multipart/form-data"];
const noOp = () => true; const noOp = () => true;
const handleBody = (res: HttpResponse, req: HttpRequest) => { const handleBody = (res: HttpResponse, req: HttpRequest) => {
const contType = req.getHeader('content-type'); const contType = req.getHeader("content-type");
res.bodyStream = function () { res.bodyStream = function () {
const stream = new Readable(); const stream = new Readable();
@ -29,24 +29,21 @@ const handleBody = (res: HttpResponse, req: HttpRequest) => {
res.body = () => stob(res.bodyStream()); res.body = () => stob(res.bodyStream());
if (contType.includes('application/json')) if (contType.includes("application/json")) res.json = async () => JSON.parse(await res.body());
res.json = async () => JSON.parse(await res.body()); if (contTypes.map((t) => contType.includes(t)).includes(true)) res.formData = formData.bind(res, contType);
if (contTypes.map(t => contType.includes(t)).includes(true))
res.formData = formData.bind(res, contType);
}; };
class BaseApp { class BaseApp {
_sockets = new Map(); _sockets = new Map();
ws!: TemplatedApp['ws']; ws!: TemplatedApp["ws"];
get!: TemplatedApp['get']; get!: TemplatedApp["get"];
_post!: TemplatedApp['post']; _post!: TemplatedApp["post"];
_put!: TemplatedApp['put']; _put!: TemplatedApp["put"];
_patch!: TemplatedApp['patch']; _patch!: TemplatedApp["patch"];
_listen!: TemplatedApp['listen']; _listen!: TemplatedApp["listen"];
post(pattern: string, handler: Handler) { post(pattern: string, handler: Handler) {
if (typeof handler !== 'function') if (typeof handler !== "function") throw Error(`handler should be a function, given ${typeof handler}.`);
throw Error(`handler should be a function, given ${typeof handler}.`);
this._post(pattern, (res, req) => { this._post(pattern, (res, req) => {
handleBody(res, req); handleBody(res, req);
handler(res, req); handler(res, req);
@ -55,8 +52,7 @@ class BaseApp {
} }
put(pattern: string, handler: Handler) { put(pattern: string, handler: Handler) {
if (typeof handler !== 'function') if (typeof handler !== "function") throw Error(`handler should be a function, given ${typeof handler}.`);
throw Error(`handler should be a function, given ${typeof handler}.`);
this._put(pattern, (res, req) => { this._put(pattern, (res, req) => {
handleBody(res, req); handleBody(res, req);
@ -66,8 +62,7 @@ class BaseApp {
} }
patch(pattern: string, handler: Handler) { patch(pattern: string, handler: Handler) {
if (typeof handler !== 'function') if (typeof handler !== "function") throw Error(`handler should be a function, given ${typeof handler}.`);
throw Error(`handler should be a function, given ${typeof handler}.`);
this._patch(pattern, (res, req) => { this._patch(pattern, (res, req) => {
handleBody(res, req); handleBody(res, req);
@ -77,23 +72,21 @@ class BaseApp {
} }
listen(h: string | number, p: Function | number = noOp, cb?: Function) { listen(h: string | number, p: Function | number = noOp, cb?: Function) {
if (typeof p === 'number' && typeof h === 'string') { if (typeof p === "number" && typeof h === "string") {
this._listen(h, p, socket => { this._listen(h, p, (socket) => {
this._sockets.set(p, socket); this._sockets.set(p, socket);
if (cb === undefined) { if (cb === undefined) {
throw new Error('cb undefined'); throw new Error("cb undefined");
} }
cb(socket); cb(socket);
}); });
} else if (typeof h === 'number' && typeof p === 'function') { } else if (typeof h === "number" && typeof p === "function") {
this._listen(h, socket => { this._listen(h, (socket) => {
this._sockets.set(h, socket); this._sockets.set(h, socket);
p(socket); p(socket);
}); });
} else { } else {
throw Error( throw Error("Argument types: (host: string, port: number, cb?: Function) | (port: number, cb?: Function)");
'Argument types: (host: string, port: number, cb?: Function) | (port: number, cb?: Function)'
);
} }
return this; return this;
@ -104,7 +97,7 @@ class BaseApp {
this._sockets.has(port) && us_listen_socket_close(this._sockets.get(port)); this._sockets.has(port) && us_listen_socket_close(this._sockets.get(port));
this._sockets.delete(port); this._sockets.delete(port);
} else { } else {
this._sockets.forEach(app => { this._sockets.forEach((app) => {
us_listen_socket_close(app); us_listen_socket_close(app);
}); });
this._sockets.clear(); this._sockets.clear();

View File

@ -1,7 +1,7 @@
import { createWriteStream } from 'fs'; import { createWriteStream } from "fs";
import { join, dirname } from 'path'; import { join, dirname } from "path";
import Busboy from 'busboy'; import Busboy from "busboy";
import mkdirp from 'mkdirp'; import mkdirp from "mkdirp";
function formData( function formData(
contType: string, contType: string,
@ -19,9 +19,9 @@ function formData(
filename?: (oldName: string) => string; filename?: (oldName: string) => string;
} = {} } = {}
) { ) {
console.log('Enter form data'); console.log("Enter form data");
options.headers = { options.headers = {
'content-type': contType "content-type": contType,
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -30,47 +30,46 @@ function formData(
this.bodyStream().pipe(busb); this.bodyStream().pipe(busb);
busb.on('limit', () => { busb.on("limit", () => {
if (options.abortOnLimit) { if (options.abortOnLimit) {
reject(Error('limit')); reject(Error("limit"));
} }
}); });
busb.on('file', function(fieldname, file, filename, encoding, mimetype) { busb.on("file", function (fieldname, file, filename, encoding, mimetype) {
const value: { filePath: string|undefined, filename: string, encoding:string, mimetype: string } = { const value: { filePath: string | undefined; filename: string; encoding: string; mimetype: string } = {
filename, filename,
encoding, encoding,
mimetype, mimetype,
filePath: undefined filePath: undefined,
}; };
if (typeof options.tmpDir === 'string') { if (typeof options.tmpDir === "string") {
if (typeof options.filename === 'function') filename = options.filename(filename); if (typeof options.filename === "function") filename = options.filename(filename);
const fileToSave = join(options.tmpDir, filename); const fileToSave = join(options.tmpDir, filename);
mkdirp(dirname(fileToSave)); mkdirp(dirname(fileToSave));
file.pipe(createWriteStream(fileToSave)); file.pipe(createWriteStream(fileToSave));
value.filePath = fileToSave; value.filePath = fileToSave;
} }
if (typeof options.onFile === 'function') { if (typeof options.onFile === "function") {
value.filePath = value.filePath = options.onFile(fieldname, file, filename, encoding, mimetype) || value.filePath;
options.onFile(fieldname, file, filename, encoding, mimetype) || value.filePath;
} }
setRetValue(ret, fieldname, value); setRetValue(ret, fieldname, value);
}); });
busb.on('field', function(fieldname, value) { busb.on("field", function (fieldname, value) {
if (typeof options.onField === 'function') options.onField(fieldname, value); if (typeof options.onField === "function") options.onField(fieldname, value);
setRetValue(ret, fieldname, value); setRetValue(ret, fieldname, value);
}); });
busb.on('finish', function() { busb.on("finish", function () {
resolve(ret); resolve(ret);
}); });
busb.on('error', reject); busb.on("error", reject);
}); });
} }
@ -79,7 +78,7 @@ function setRetValue(
fieldname: string, fieldname: string,
value: { filename: string; encoding: string; mimetype: string; filePath?: string } | any // eslint-disable-line @typescript-eslint/no-explicit-any value: { filename: string; encoding: string; mimetype: string; filePath?: string } | any // eslint-disable-line @typescript-eslint/no-explicit-any
) { ) {
if (fieldname.endsWith('[]')) { if (fieldname.endsWith("[]")) {
fieldname = fieldname.slice(0, fieldname.length - 2); fieldname = fieldname.slice(0, fieldname.length - 2);
if (Array.isArray(ret[fieldname])) { if (Array.isArray(ret[fieldname])) {
ret[fieldname].push(value); ret[fieldname].push(value);

View File

@ -1,7 +1,7 @@
import { SSLApp as _SSLApp, AppOptions } from 'uWebSockets.js'; import { SSLApp as _SSLApp, AppOptions } from "uWebSockets.js";
import BaseApp from './baseapp'; import BaseApp from "./baseapp";
import { extend } from './utils'; import { extend } from "./utils";
import { UwsApp } from './types'; import { UwsApp } from "./types";
class SSLApp extends (<UwsApp>_SSLApp) { class SSLApp extends (<UwsApp>_SSLApp) {
constructor(options: AppOptions) { constructor(options: AppOptions) {

View File

@ -1,4 +1,4 @@
import { AppOptions, TemplatedApp, HttpResponse, HttpRequest } from 'uWebSockets.js'; import { AppOptions, TemplatedApp, HttpResponse, HttpRequest } from "uWebSockets.js";
export type UwsApp = { export type UwsApp = {
(options: AppOptions): TemplatedApp; (options: AppOptions): TemplatedApp;

View File

@ -1,25 +1,24 @@
import { ReadStream } from 'fs'; import { ReadStream } from "fs";
function extend(who: any, from: any, overwrite = true) { // eslint-disable-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const ownProps = Object.getOwnPropertyNames(Object.getPrototypeOf(from)).concat( function extend(who: any, from: any, overwrite = true) {
Object.keys(from) const ownProps = Object.getOwnPropertyNames(Object.getPrototypeOf(from)).concat(Object.keys(from));
); ownProps.forEach((prop) => {
ownProps.forEach(prop => { if (prop === "constructor" || from[prop] === undefined) return;
if (prop === 'constructor' || from[prop] === undefined) return;
if (who[prop] && overwrite) { if (who[prop] && overwrite) {
who[`_${prop}`] = who[prop]; who[`_${prop}`] = who[prop];
} }
if (typeof from[prop] === 'function') who[prop] = from[prop].bind(who); if (typeof from[prop] === "function") who[prop] = from[prop].bind(who);
else who[prop] = from[prop]; else who[prop] = from[prop];
}); });
} }
function stob(stream: ReadStream): Promise<Buffer> { function stob(stream: ReadStream): Promise<Buffer> {
return new Promise(resolve => { return new Promise((resolve) => {
const buffers: Buffer[] = []; const buffers: Buffer[] = [];
stream.on('data', buffers.push.bind(buffers)); stream.on("data", buffers.push.bind(buffers));
stream.on('end', () => { stream.on("end", () => {
switch (buffers.length) { switch (buffers.length) {
case 0: case 0:
resolve(Buffer.allocUnsafe(0)); resolve(Buffer.allocUnsafe(0));

View File

@ -1,19 +1,19 @@
import { parse } from 'query-string'; import { parse } from "query-string";
import { HttpRequest } from 'uWebSockets.js'; import { HttpRequest } from "uWebSockets.js";
import App from './server/app'; import App from "./server/app";
import SSLApp from './server/sslapp'; import SSLApp from "./server/sslapp";
import * as types from './server/types'; import * as types from "./server/types";
const getQuery = (req: HttpRequest) => { const getQuery = (req: HttpRequest) => {
return parse(req.getQuery()); return parse(req.getQuery());
}; };
export { App, SSLApp, getQuery }; export { App, SSLApp, getQuery };
export * from './server/types'; export * from "./server/types";
export default { export default {
App, App,
SSLApp, SSLApp,
getQuery, getQuery,
...types ...types,
}; };

View File

@ -1,3 +1,3 @@
export const arrayIntersect = (array1: string[], array2: string[]): boolean => { export const arrayIntersect = (array1: string[], array2: string[]): boolean => {
return array1.filter(value => array2.includes(value)).length > 0; return array1.filter((value) => array2.includes(value)).length > 0;
} };

View File

@ -1,7 +1,7 @@
const EventEmitter = require('events'); const EventEmitter = require("events");
const clientJoinEvent = 'clientJoin'; const clientJoinEvent = "clientJoin";
const clientLeaveEvent = 'clientLeave'; const clientLeaveEvent = "clientLeave";
class ClientEventsEmitter extends EventEmitter { class ClientEventsEmitter extends EventEmitter {
emitClientJoin(clientUUid: string, roomId: string): void { emitClientJoin(clientUUid: string, roomId: string): void {

View File

@ -12,17 +12,17 @@ class CpuTracker {
private overHeating: boolean = false; private overHeating: boolean = false;
constructor() { constructor() {
let time = process.hrtime.bigint() let time = process.hrtime.bigint();
let usage = process.cpuUsage() let usage = process.cpuUsage();
setInterval(() => { setInterval(() => {
const elapTime = process.hrtime.bigint(); const elapTime = process.hrtime.bigint();
const elapUsage = process.cpuUsage(usage) const elapUsage = process.cpuUsage(usage);
usage = process.cpuUsage() usage = process.cpuUsage();
const elapTimeMS = elapTime - time; const elapTimeMS = elapTime - time;
const elapUserMS = secNSec2ms(elapUsage.user) const elapUserMS = secNSec2ms(elapUsage.user);
const elapSystMS = secNSec2ms(elapUsage.system) const elapSystMS = secNSec2ms(elapUsage.system);
this.cpuPercent = Math.round(100 * (elapUserMS + elapSystMS) / Number(elapTimeMS) * 1000000) this.cpuPercent = Math.round(((100 * (elapUserMS + elapSystMS)) / Number(elapTimeMS)) * 1000000);
time = elapTime; time = elapTime;

View File

@ -10,29 +10,29 @@ class GaugeManager {
constructor() { constructor() {
this.nbRoomsGauge = new Gauge({ this.nbRoomsGauge = new Gauge({
name: 'workadventure_nb_rooms', name: "workadventure_nb_rooms",
help: 'Number of active rooms' help: "Number of active rooms",
}); });
this.nbClientsGauge = new Gauge({ this.nbClientsGauge = new Gauge({
name: 'workadventure_nb_sockets', name: "workadventure_nb_sockets",
help: 'Number of connected sockets', help: "Number of connected sockets",
labelNames: [ ] labelNames: [],
}); });
this.nbClientsPerRoomGauge = new Gauge({ this.nbClientsPerRoomGauge = new Gauge({
name: 'workadventure_nb_clients_per_room', name: "workadventure_nb_clients_per_room",
help: 'Number of clients per room', help: "Number of clients per room",
labelNames: [ 'room' ] labelNames: ["room"],
}); });
this.nbGroupsPerRoomCounter = new Counter({ this.nbGroupsPerRoomCounter = new Counter({
name: 'workadventure_counter_groups_per_room', name: "workadventure_counter_groups_per_room",
help: 'Counter of groups per room', help: "Counter of groups per room",
labelNames: [ 'room' ] labelNames: ["room"],
}); });
this.nbGroupsPerRoomGauge = new Gauge({ this.nbGroupsPerRoomGauge = new Gauge({
name: 'workadventure_nb_groups_per_room', name: "workadventure_nb_groups_per_room",
help: 'Number of groups per room', help: "Number of groups per room",
labelNames: [ 'room' ] labelNames: ["room"],
}); });
} }
@ -54,12 +54,12 @@ class GaugeManager {
} }
incNbGroupsPerRoomGauge(roomId: string): void { incNbGroupsPerRoomGauge(roomId: string): void {
this.nbGroupsPerRoomCounter.inc({ room: roomId }) this.nbGroupsPerRoomCounter.inc({ room: roomId });
this.nbGroupsPerRoomGauge.inc({ room: roomId }) this.nbGroupsPerRoomGauge.inc({ room: roomId });
} }
decNbGroupsPerRoomGauge(roomId: string): void { decNbGroupsPerRoomGauge(roomId: string): void {
this.nbGroupsPerRoomGauge.dec({ room: roomId }) this.nbGroupsPerRoomGauge.dec({ room: roomId });
} }
} }

View File

@ -27,7 +27,9 @@ import {
WorldFullWarningMessage, WorldFullWarningMessage,
UserLeftZoneMessage, UserLeftZoneMessage,
EmoteEventMessage, EmoteEventMessage,
BanUserMessage, RefreshRoomMessage, EmotePromptMessage, BanUserMessage,
RefreshRoomMessage,
EmotePromptMessage,
} from "../Messages/generated/messages_pb"; } from "../Messages/generated/messages_pb";
import { User, UserSocket } from "../Model/User"; import { User, UserSocket } from "../Model/User";
import { ProtobufUtils } from "../Model/Websocket/ProtobufUtils"; import { ProtobufUtils } from "../Model/Websocket/ProtobufUtils";
@ -38,7 +40,7 @@ import {
JITSI_ISS, JITSI_ISS,
MINIMUM_DISTANCE, MINIMUM_DISTANCE,
SECRET_JITSI_KEY, SECRET_JITSI_KEY,
TURN_STATIC_AUTH_SECRET TURN_STATIC_AUTH_SECRET,
} from "../Enum/EnvironmentVariable"; } from "../Enum/EnvironmentVariable";
import { Movable } from "../Model/Movable"; import { Movable } from "../Model/Movable";
import { PositionInterface } from "../Model/PositionInterface"; import { PositionInterface } from "../Model/PositionInterface";
@ -52,15 +54,13 @@ import Debug from "debug";
import { Admin } from "_Model/Admin"; import { Admin } from "_Model/Admin";
import crypto from "crypto"; import crypto from "crypto";
const debug = Debug("sockermanager");
const debug = Debug('sockermanager');
function emitZoneMessage(subMessage: SubToPusherMessage, socket: ZoneSocket): void { function emitZoneMessage(subMessage: SubToPusherMessage, socket: ZoneSocket): void {
// TODO: should we batch those every 100ms? // TODO: should we batch those every 100ms?
const batchMessage = new BatchToPusherMessage(); const batchMessage = new BatchToPusherMessage();
batchMessage.addPayload(subMessage); batchMessage.addPayload(subMessage);
socket.write(batchMessage); socket.write(batchMessage);
} }
@ -68,7 +68,6 @@ export class SocketManager {
private rooms: Map<string, GameRoom> = new Map<string, GameRoom>(); private rooms: Map<string, GameRoom> = new Map<string, GameRoom>();
constructor() { constructor() {
clientEventsEmitter.registerToClientJoin((clientUUid: string, roomId: string) => { clientEventsEmitter.registerToClientJoin((clientUUid: string, roomId: string) => {
gaugeManager.incNbClientPerRoomGauge(roomId); gaugeManager.incNbClientPerRoomGauge(roomId);
}); });
@ -77,16 +76,18 @@ export class SocketManager {
}); });
} }
public async handleJoinRoom(socket: UserSocket, joinRoomMessage: JoinRoomMessage): Promise<{ room: GameRoom; user: User }> { public async handleJoinRoom(
socket: UserSocket,
joinRoomMessage: JoinRoomMessage
): Promise<{ room: GameRoom; user: User }> {
//join new previous room //join new previous room
const { room, user } = await this.joinRoom(socket, joinRoomMessage); const { room, user } = await this.joinRoom(socket, joinRoomMessage);
if (!socket.writable) { if (!socket.writable) {
console.warn('Socket was aborted'); console.warn("Socket was aborted");
return { return {
room, room,
user user,
}; };
} }
const roomJoinedMessage = new RoomJoinedMessage(); const roomJoinedMessage = new RoomJoinedMessage();
@ -108,9 +109,8 @@ export class SocketManager {
return { return {
room, room,
user user,
}; };
} }
handleUserMovesMessage(room: GameRoom, user: User, userMovesMessage: UserMovesMessage) { handleUserMovesMessage(room: GameRoom, user: User, userMovesMessage: UserMovesMessage) {
@ -124,14 +124,13 @@ export class SocketManager {
} }
if (position === undefined) { if (position === undefined) {
throw new Error('Position not found in message'); throw new Error("Position not found in message");
} }
const viewport = userMoves.viewport; const viewport = userMoves.viewport;
if (viewport === undefined) { if (viewport === undefined) {
throw new Error('Viewport not found in message'); throw new Error("Viewport not found in message");
} }
// update position in the world // update position in the world
room.updatePosition(user, ProtobufUtils.toPointInterface(position)); room.updatePosition(user, ProtobufUtils.toPointInterface(position));
//room.setViewport(client, client.viewport); //room.setViewport(client, client.viewport);
@ -189,7 +188,11 @@ export class SocketManager {
//send only at user //send only at user
const remoteUser = room.getUsers().get(data.getReceiverid()); const remoteUser = room.getUsers().get(data.getReceiverid());
if (remoteUser === undefined) { if (remoteUser === undefined) {
console.warn("While exchanging a WebRTC signal: client with id ", data.getReceiverid(), " does not exist. This might be a race condition."); console.warn(
"While exchanging a WebRTC signal: client with id ",
data.getReceiverid(),
" does not exist. This might be a race condition."
);
return; return;
} }
@ -197,8 +200,8 @@ export class SocketManager {
webrtcSignalToClient.setUserid(user.id); webrtcSignalToClient.setUserid(user.id);
webrtcSignalToClient.setSignal(data.getSignal()); webrtcSignalToClient.setSignal(data.getSignal());
// TODO: only compute credentials if data.signal.type === "offer" // TODO: only compute credentials if data.signal.type === "offer"
if (TURN_STATIC_AUTH_SECRET !== '') { if (TURN_STATIC_AUTH_SECRET !== "") {
const {username, password} = this.getTURNCredentials(''+user.id, TURN_STATIC_AUTH_SECRET); const { username, password } = this.getTURNCredentials("" + user.id, TURN_STATIC_AUTH_SECRET);
webrtcSignalToClient.setWebrtcusername(username); webrtcSignalToClient.setWebrtcusername(username);
webrtcSignalToClient.setWebrtcpassword(password); webrtcSignalToClient.setWebrtcpassword(password);
} }
@ -215,7 +218,11 @@ export class SocketManager {
//send only at user //send only at user
const remoteUser = room.getUsers().get(data.getReceiverid()); const remoteUser = room.getUsers().get(data.getReceiverid());
if (remoteUser === undefined) { if (remoteUser === undefined) {
console.warn("While exchanging a WEBRTC_SCREEN_SHARING signal: client with id ", data.getReceiverid(), " does not exist. This might be a race condition."); console.warn(
"While exchanging a WEBRTC_SCREEN_SHARING signal: client with id ",
data.getReceiverid(),
" does not exist. This might be a race condition."
);
return; return;
} }
@ -223,8 +230,8 @@ export class SocketManager {
webrtcSignalToClient.setUserid(user.id); webrtcSignalToClient.setUserid(user.id);
webrtcSignalToClient.setSignal(data.getSignal()); webrtcSignalToClient.setSignal(data.getSignal());
// TODO: only compute credentials if data.signal.type === "offer" // TODO: only compute credentials if data.signal.type === "offer"
if (TURN_STATIC_AUTH_SECRET !== '') { if (TURN_STATIC_AUTH_SECRET !== "") {
const {username, password} = this.getTURNCredentials(''+user.id, TURN_STATIC_AUTH_SECRET); const { username, password } = this.getTURNCredentials("" + user.id, TURN_STATIC_AUTH_SECRET);
webrtcSignalToClient.setWebrtcusername(username); webrtcSignalToClient.setWebrtcusername(username);
webrtcSignalToClient.setWebrtcpassword(password); webrtcSignalToClient.setWebrtcpassword(password);
} }
@ -249,13 +256,13 @@ export class SocketManager {
} }
} finally { } finally {
clientEventsEmitter.emitClientLeave(user.uuid, room.roomId); clientEventsEmitter.emitClientLeave(user.uuid, room.roomId);
console.log('A user left'); console.log("A user left");
} }
} }
async getOrCreateRoom(roomId: string): Promise<GameRoom> { async getOrCreateRoom(roomId: string): Promise<GameRoom> {
//check and create new world for a room //check and create new world for a room
let world = this.rooms.get(roomId) let world = this.rooms.get(roomId);
if (world === undefined) { if (world === undefined) {
world = new GameRoom( world = new GameRoom(
roomId, roomId,
@ -263,19 +270,25 @@ export class SocketManager {
(user: User, group: Group) => this.disConnectedUser(user, group), (user: User, group: Group) => this.disConnectedUser(user, group),
MINIMUM_DISTANCE, MINIMUM_DISTANCE,
GROUP_RADIUS, GROUP_RADIUS,
(thing: Movable, fromZone: Zone|null, listener: ZoneSocket) => this.onZoneEnter(thing, fromZone, listener), (thing: Movable, fromZone: Zone | null, listener: ZoneSocket) =>
(thing: Movable, position:PositionInterface, listener: ZoneSocket) => this.onClientMove(thing, position, listener), this.onZoneEnter(thing, fromZone, listener),
(thing: Movable, newZone: Zone|null, listener: ZoneSocket) => this.onClientLeave(thing, newZone, listener), (thing: Movable, position: PositionInterface, listener: ZoneSocket) =>
(emoteEventMessage:EmoteEventMessage, listener: ZoneSocket) => this.onEmote(emoteEventMessage, listener), this.onClientMove(thing, position, listener),
(thing: Movable, newZone: Zone | null, listener: ZoneSocket) =>
this.onClientLeave(thing, newZone, listener),
(emoteEventMessage: EmoteEventMessage, listener: ZoneSocket) =>
this.onEmote(emoteEventMessage, listener)
); );
gaugeManager.incNbRoomGauge(); gaugeManager.incNbRoomGauge();
this.rooms.set(roomId, world); this.rooms.set(roomId, world);
} }
return Promise.resolve(world) return Promise.resolve(world);
} }
private async joinRoom(socket: UserSocket, joinRoomMessage: JoinRoomMessage): Promise<{ room: GameRoom; user: User }> { private async joinRoom(
socket: UserSocket,
joinRoomMessage: JoinRoomMessage
): Promise<{ room: GameRoom; user: User }> {
const roomId = joinRoomMessage.getRoomid(); const roomId = joinRoomMessage.getRoomid();
const room = await socketManager.getOrCreateRoom(roomId); const room = await socketManager.getOrCreateRoom(roomId);
@ -284,7 +297,7 @@ export class SocketManager {
const user = room.join(socket, joinRoomMessage); const user = room.join(socket, joinRoomMessage);
clientEventsEmitter.emitClientJoin(user.uuid, roomId); clientEventsEmitter.emitClientJoin(user.uuid, roomId);
console.log(new Date().toISOString() + ' A user joined'); console.log(new Date().toISOString() + " A user joined");
return { room, user }; return { room, user };
} }
@ -292,7 +305,7 @@ export class SocketManager {
if (thing instanceof User) { if (thing instanceof User) {
const userJoinedZoneMessage = new UserJoinedZoneMessage(); const userJoinedZoneMessage = new UserJoinedZoneMessage();
if (!Number.isInteger(thing.id)) { if (!Number.isInteger(thing.id)) {
throw new Error('clientUser.userId is not an integer '+thing.id); throw new Error("clientUser.userId is not an integer " + thing.id);
} }
userJoinedZoneMessage.setUserid(thing.id); userJoinedZoneMessage.setUserid(thing.id);
userJoinedZoneMessage.setName(thing.name); userJoinedZoneMessage.setName(thing.name);
@ -312,7 +325,7 @@ export class SocketManager {
} else if (thing instanceof Group) { } else if (thing instanceof Group) {
this.emitCreateUpdateGroupEvent(listener, fromZone, thing); this.emitCreateUpdateGroupEvent(listener, fromZone, thing);
} else { } else {
console.error('Unexpected type for Movable.'); console.error("Unexpected type for Movable.");
} }
} }
@ -331,7 +344,7 @@ export class SocketManager {
} else if (thing instanceof Group) { } else if (thing instanceof Group) {
this.emitCreateUpdateGroupEvent(listener, null, thing); this.emitCreateUpdateGroupEvent(listener, null, thing);
} else { } else {
console.error('Unexpected type for Movable.'); console.error("Unexpected type for Movable.");
} }
} }
@ -341,11 +354,10 @@ export class SocketManager {
} else if (thing instanceof Group) { } else if (thing instanceof Group) {
this.emitDeleteGroupEvent(listener, thing.getId(), newZone); this.emitDeleteGroupEvent(listener, thing.getId(), newZone);
} else { } else {
console.error('Unexpected type for Movable.'); console.error("Unexpected type for Movable.");
} }
} }
private onEmote(emoteEventMessage: EmoteEventMessage, client: ZoneSocket) { private onEmote(emoteEventMessage: EmoteEventMessage, client: ZoneSocket) {
const subMessage = new SubToPusherMessage(); const subMessage = new SubToPusherMessage();
subMessage.setEmoteeventmessage(emoteEventMessage); subMessage.setEmoteeventmessage(emoteEventMessage);
@ -405,7 +417,6 @@ export class SocketManager {
} }
private joinWebRtcRoom(user: User, group: Group) { private joinWebRtcRoom(user: User, group: Group) {
for (const otherUser of group.getUsers()) { for (const otherUser of group.getUsers()) {
if (user === otherUser) { if (user === otherUser) {
continue; continue;
@ -416,8 +427,8 @@ export class SocketManager {
webrtcStartMessage1.setUserid(otherUser.id); webrtcStartMessage1.setUserid(otherUser.id);
webrtcStartMessage1.setName(otherUser.name); webrtcStartMessage1.setName(otherUser.name);
webrtcStartMessage1.setInitiator(true); webrtcStartMessage1.setInitiator(true);
if (TURN_STATIC_AUTH_SECRET !== '') { if (TURN_STATIC_AUTH_SECRET !== "") {
const {username, password} = this.getTURNCredentials(''+otherUser.id, TURN_STATIC_AUTH_SECRET); const { username, password } = this.getTURNCredentials("" + otherUser.id, TURN_STATIC_AUTH_SECRET);
webrtcStartMessage1.setWebrtcusername(username); webrtcStartMessage1.setWebrtcusername(username);
webrtcStartMessage1.setWebrtcpassword(password); webrtcStartMessage1.setWebrtcpassword(password);
} }
@ -434,8 +445,8 @@ export class SocketManager {
webrtcStartMessage2.setUserid(user.id); webrtcStartMessage2.setUserid(user.id);
webrtcStartMessage2.setName(user.name); webrtcStartMessage2.setName(user.name);
webrtcStartMessage2.setInitiator(false); webrtcStartMessage2.setInitiator(false);
if (TURN_STATIC_AUTH_SECRET !== '') { if (TURN_STATIC_AUTH_SECRET !== "") {
const {username, password} = this.getTURNCredentials(''+user.id, TURN_STATIC_AUTH_SECRET); const { username, password } = this.getTURNCredentials("" + user.id, TURN_STATIC_AUTH_SECRET);
webrtcStartMessage2.setWebrtcusername(username); webrtcStartMessage2.setWebrtcusername(username);
webrtcStartMessage2.setWebrtcpassword(password); webrtcStartMessage2.setWebrtcpassword(password);
} }
@ -447,7 +458,6 @@ export class SocketManager {
otherUser.socket.write(serverToClientMessage2); otherUser.socket.write(serverToClientMessage2);
//console.log('Sending webrtcstart to '+otherUser.socket.userId) //console.log('Sending webrtcstart to '+otherUser.socket.userId)
//} //}
} }
} }
@ -456,17 +466,17 @@ export class SocketManager {
* and the Coturn server. * and the Coturn server.
* The Coturn server should be initialized with parameters: `--use-auth-secret --static-auth-secret=MySecretKey` * The Coturn server should be initialized with parameters: `--use-auth-secret --static-auth-secret=MySecretKey`
*/ */
private getTURNCredentials(name: string, secret: string): {username: string, password: string} { private getTURNCredentials(name: string, secret: string): { username: string; password: string } {
const unixTimeStamp = Math.floor(Date.now() / 1000) + 4 * 3600; // this credential would be valid for the next 4 hours const unixTimeStamp = Math.floor(Date.now() / 1000) + 4 * 3600; // this credential would be valid for the next 4 hours
const username = [unixTimeStamp, name].join(':'); const username = [unixTimeStamp, name].join(":");
const hmac = crypto.createHmac('sha1', secret); const hmac = crypto.createHmac("sha1", secret);
hmac.setEncoding('base64'); hmac.setEncoding("base64");
hmac.write(username); hmac.write(username);
hmac.end(); hmac.end();
const password = hmac.read(); const password = hmac.read();
return { return {
username: username, username: username,
password: password password: password,
}; };
} }
@ -492,7 +502,6 @@ export class SocketManager {
otherUser.socket.write(serverToClientMessage1); otherUser.socket.write(serverToClientMessage1);
//} //}
const webrtcDisconnectMessage2 = new WebRtcDisconnectMessage(); const webrtcDisconnectMessage2 = new WebRtcDisconnectMessage();
webrtcDisconnectMessage2.setUserid(otherUser.id); webrtcDisconnectMessage2.setUserid(otherUser.id);
@ -517,40 +526,41 @@ export class SocketManager {
console.error('An error occurred on "emitPlayGlobalMessage" event'); console.error('An error occurred on "emitPlayGlobalMessage" event');
console.error(e); console.error(e);
} }
} }
public getWorlds(): Map<string, GameRoom> { public getWorlds(): Map<string, GameRoom> {
return this.rooms; return this.rooms;
} }
public handleQueryJitsiJwtMessage(user: User, queryJitsiJwtMessage: QueryJitsiJwtMessage) { public handleQueryJitsiJwtMessage(user: User, queryJitsiJwtMessage: QueryJitsiJwtMessage) {
const room = queryJitsiJwtMessage.getJitsiroom(); const room = queryJitsiJwtMessage.getJitsiroom();
const tag = queryJitsiJwtMessage.getTag(); // FIXME: this is not secure. We should load the JSON for the current room and check rights associated to room instead. const tag = queryJitsiJwtMessage.getTag(); // FIXME: this is not secure. We should load the JSON for the current room and check rights associated to room instead.
if (SECRET_JITSI_KEY === '') { if (SECRET_JITSI_KEY === "") {
throw new Error('You must set the SECRET_JITSI_KEY key to the secret to generate JWT tokens for Jitsi.'); throw new Error("You must set the SECRET_JITSI_KEY key to the secret to generate JWT tokens for Jitsi.");
} }
// Let's see if the current client has // Let's see if the current client has
const isAdmin = user.tags.includes(tag); const isAdmin = user.tags.includes(tag);
const jwt = Jwt.sign({ const jwt = Jwt.sign(
"aud": "jitsi",
"iss": JITSI_ISS,
"sub": JITSI_URL,
"room": room,
"moderator": isAdmin
}, SECRET_JITSI_KEY, {
expiresIn: '1d',
algorithm: "HS256",
header:
{ {
"alg": "HS256", aud: "jitsi",
"typ": "JWT" iss: JITSI_ISS,
sub: JITSI_URL,
room: room,
moderator: isAdmin,
},
SECRET_JITSI_KEY,
{
expiresIn: "1d",
algorithm: "HS256",
header: {
alg: "HS256",
typ: "JWT",
},
} }
}); );
const sendJitsiJwtMessage = new SendJitsiJwtMessage(); const sendJitsiJwtMessage = new SendJitsiJwtMessage();
sendJitsiJwtMessage.setJitsiroom(room); sendJitsiJwtMessage.setJitsiroom(room);
@ -663,19 +673,27 @@ export class SocketManager {
public sendAdminMessage(roomId: string, recipientUuid: string, message: string): void { public sendAdminMessage(roomId: string, recipientUuid: string, message: string): void {
const room = this.rooms.get(roomId); const room = this.rooms.get(roomId);
if (!room) { if (!room) {
console.error("In sendAdminMessage, could not find room with id '" + roomId + "'. Maybe the room was closed a few milliseconds ago and there was a race condition?"); console.error(
"In sendAdminMessage, could not find room with id '" +
roomId +
"'. Maybe the room was closed a few milliseconds ago and there was a race condition?"
);
return; return;
} }
const recipient = room.getUserByUuid(recipientUuid); const recipient = room.getUserByUuid(recipientUuid);
if (recipient === undefined) { if (recipient === undefined) {
console.error("In sendAdminMessage, could not find user with id '" + recipientUuid + "'. Maybe the user left the room a few milliseconds ago and there was a race condition?"); console.error(
"In sendAdminMessage, could not find user with id '" +
recipientUuid +
"'. Maybe the user left the room a few milliseconds ago and there was a race condition?"
);
return; return;
} }
const sendUserMessage = new SendUserMessage(); const sendUserMessage = new SendUserMessage();
sendUserMessage.setMessage(message); sendUserMessage.setMessage(message);
sendUserMessage.setType('ban'); //todo: is the type correct? sendUserMessage.setType("ban"); //todo: is the type correct?
const serverToClientMessage = new ServerToClientMessage(); const serverToClientMessage = new ServerToClientMessage();
serverToClientMessage.setSendusermessage(sendUserMessage); serverToClientMessage.setSendusermessage(sendUserMessage);
@ -686,13 +704,21 @@ export class SocketManager {
public banUser(roomId: string, recipientUuid: string, message: string): void { public banUser(roomId: string, recipientUuid: string, message: string): void {
const room = this.rooms.get(roomId); const room = this.rooms.get(roomId);
if (!room) { if (!room) {
console.error("In banUser, could not find room with id '" + roomId + "'. Maybe the room was closed a few milliseconds ago and there was a race condition?"); console.error(
"In banUser, could not find room with id '" +
roomId +
"'. Maybe the room was closed a few milliseconds ago and there was a race condition?"
);
return; return;
} }
const recipient = room.getUserByUuid(recipientUuid); const recipient = room.getUserByUuid(recipientUuid);
if (recipient === undefined) { if (recipient === undefined) {
console.error("In banUser, could not find user with id '" + recipientUuid + "'. Maybe the user left the room a few milliseconds ago and there was a race condition?"); console.error(
"In banUser, could not find user with id '" +
recipientUuid +
"'. Maybe the user left the room a few milliseconds ago and there was a race condition?"
);
return; return;
} }
@ -701,7 +727,7 @@ export class SocketManager {
const banUserMessage = new BanUserMessage(); const banUserMessage = new BanUserMessage();
banUserMessage.setMessage(message); banUserMessage.setMessage(message);
banUserMessage.setType('banned'); banUserMessage.setType("banned");
const serverToClientMessage = new ServerToClientMessage(); const serverToClientMessage = new ServerToClientMessage();
serverToClientMessage.setBanusermessage(banUserMessage); serverToClientMessage.setBanusermessage(banUserMessage);
@ -711,19 +737,22 @@ export class SocketManager {
recipient.socket.end(); recipient.socket.end();
} }
sendAdminRoomMessage(roomId: string, message: string) { sendAdminRoomMessage(roomId: string, message: string) {
const room = this.rooms.get(roomId); const room = this.rooms.get(roomId);
if (!room) { if (!room) {
//todo: this should cause the http call to return a 500 //todo: this should cause the http call to return a 500
console.error("In sendAdminRoomMessage, could not find room with id '" + roomId + "'. Maybe the room was closed a few milliseconds ago and there was a race condition?"); console.error(
"In sendAdminRoomMessage, could not find room with id '" +
roomId +
"'. Maybe the room was closed a few milliseconds ago and there was a race condition?"
);
return; return;
} }
room.getUsers().forEach((recipient) => { room.getUsers().forEach((recipient) => {
const sendUserMessage = new SendUserMessage(); const sendUserMessage = new SendUserMessage();
sendUserMessage.setMessage(message); sendUserMessage.setMessage(message);
sendUserMessage.setType('message'); sendUserMessage.setType("message");
const clientMessage = new ServerToClientMessage(); const clientMessage = new ServerToClientMessage();
clientMessage.setSendusermessage(sendUserMessage); clientMessage.setSendusermessage(sendUserMessage);
@ -732,11 +761,15 @@ export class SocketManager {
}); });
} }
dispatchWorlFullWarning(roomId: string,): void { dispatchWorlFullWarning(roomId: string): void {
const room = this.rooms.get(roomId); const room = this.rooms.get(roomId);
if (!room) { if (!room) {
//todo: this should cause the http call to return a 500 //todo: this should cause the http call to return a 500
console.error("In sendAdminRoomMessage, could not find room with id '" + roomId + "'. Maybe the room was closed a few milliseconds ago and there was a race condition?"); console.error(
"In sendAdminRoomMessage, could not find room with id '" +
roomId +
"'. Maybe the room was closed a few milliseconds ago and there was a race condition?"
);
return; return;
} }
@ -750,7 +783,7 @@ export class SocketManager {
}); });
} }
dispatchRoomRefresh(roomId: string,): void { dispatchRoomRefresh(roomId: string): void {
const room = this.rooms.get(roomId); const room = this.rooms.get(roomId);
if (!room) { if (!room) {
return; return;
@ -759,8 +792,8 @@ export class SocketManager {
const versionNumber = room.incrementVersion(); const versionNumber = room.incrementVersion();
room.getUsers().forEach((recipient) => { room.getUsers().forEach((recipient) => {
const worldFullMessage = new RefreshRoomMessage(); const worldFullMessage = new RefreshRoomMessage();
worldFullMessage.setRoomid(roomId) worldFullMessage.setRoomid(roomId);
worldFullMessage.setVersionnumber(versionNumber) worldFullMessage.setVersionnumber(versionNumber);
const clientMessage = new ServerToClientMessage(); const clientMessage = new ServerToClientMessage();
clientMessage.setRefreshroommessage(worldFullMessage); clientMessage.setRefreshroommessage(worldFullMessage);

View File

@ -2,11 +2,13 @@ import {BaseController} from "./BaseController";
import { HttpRequest, HttpResponse, TemplatedApp } from "uWebSockets.js"; import { HttpRequest, HttpResponse, TemplatedApp } from "uWebSockets.js";
import { ADMIN_API_TOKEN } from "../Enum/EnvironmentVariable"; import { ADMIN_API_TOKEN } from "../Enum/EnvironmentVariable";
import { apiClientRepository } from "../Services/ApiClientRepository"; import { apiClientRepository } from "../Services/ApiClientRepository";
import {AdminRoomMessage, WorldFullWarningToRoomMessage, RefreshRoomPromptMessage} from "../Messages/generated/messages_pb"; import {
AdminRoomMessage,
WorldFullWarningToRoomMessage,
RefreshRoomPromptMessage,
} from "../Messages/generated/messages_pb";
export class AdminController extends BaseController { export class AdminController extends BaseController {
constructor(private App: TemplatedApp) { constructor(private App: TemplatedApp) {
super(); super();
this.App = App; this.App = App;
@ -23,21 +25,21 @@ export class AdminController extends BaseController{
// eslint-disable-next-line @typescript-eslint/no-misused-promises // eslint-disable-next-line @typescript-eslint/no-misused-promises
this.App.post("/room/refresh", async (res: HttpResponse, req: HttpRequest) => { this.App.post("/room/refresh", async (res: HttpResponse, req: HttpRequest) => {
res.onAborted(() => { res.onAborted(() => {
console.warn('/message request was aborted'); console.warn("/message request was aborted");
}) });
const token = req.getHeader('admin-token'); const token = req.getHeader("admin-token");
const body = await res.json(); const body = await res.json();
if (token !== ADMIN_API_TOKEN) { if (token !== ADMIN_API_TOKEN) {
console.error('Admin access refused for token: '+token) console.error("Admin access refused for token: " + token);
res.writeStatus("401 Unauthorized").end('Incorrect token'); res.writeStatus("401 Unauthorized").end("Incorrect token");
return; return;
} }
try { try {
if (typeof body.roomId !== 'string') { if (typeof body.roomId !== "string") {
throw 'Incorrect roomId parameter' throw "Incorrect roomId parameter";
} }
const roomId: string = body.roomId; const roomId: string = body.roomId;
@ -57,9 +59,7 @@ export class AdminController extends BaseController{
} }
res.writeStatus("200"); res.writeStatus("200");
res.end('ok'); res.end("ok");
}); });
} }
@ -71,39 +71,38 @@ export class AdminController extends BaseController{
// eslint-disable-next-line @typescript-eslint/no-misused-promises // eslint-disable-next-line @typescript-eslint/no-misused-promises
this.App.post("/message", async (res: HttpResponse, req: HttpRequest) => { this.App.post("/message", async (res: HttpResponse, req: HttpRequest) => {
res.onAborted(() => { res.onAborted(() => {
console.warn('/message request was aborted'); console.warn("/message request was aborted");
}) });
const token = req.getHeader("admin-token");
const token = req.getHeader('admin-token');
const body = await res.json(); const body = await res.json();
if (token !== ADMIN_API_TOKEN) { if (token !== ADMIN_API_TOKEN) {
console.error('Admin access refused for token: '+token) console.error("Admin access refused for token: " + token);
res.writeStatus("401 Unauthorized").end('Incorrect token'); res.writeStatus("401 Unauthorized").end("Incorrect token");
return; return;
} }
try { try {
if (typeof body.text !== 'string') { if (typeof body.text !== "string") {
throw 'Incorrect text parameter' throw "Incorrect text parameter";
} }
if (body.type !== 'capacity' && body.type !== 'message') { if (body.type !== "capacity" && body.type !== "message") {
throw 'Incorrect type parameter' throw "Incorrect type parameter";
} }
if (!body.targets || typeof body.targets !== 'object') { if (!body.targets || typeof body.targets !== "object") {
throw 'Incorrect targets parameter' throw "Incorrect targets parameter";
} }
const text: string = body.text; const text: string = body.text;
const type: string = body.type; const type: string = body.type;
const targets: string[] = body.targets; const targets: string[] = body.targets;
await Promise.all(targets.map((roomId) => { await Promise.all(
targets.map((roomId) => {
return apiClientRepository.getClient(roomId).then((roomClient) => { return apiClientRepository.getClient(roomId).then((roomClient) => {
return new Promise((res, rej) => { return new Promise((res, rej) => {
if (type === 'message') { if (type === "message") {
const roomMessage = new AdminRoomMessage(); const roomMessage = new AdminRoomMessage();
roomMessage.setMessage(text); roomMessage.setMessage(text);
roomMessage.setRoomid(roomId); roomMessage.setRoomid(roomId);
@ -111,7 +110,7 @@ export class AdminController extends BaseController{
roomClient.sendAdminMessageToRoom(roomMessage, (err) => { roomClient.sendAdminMessageToRoom(roomMessage, (err) => {
err ? rej(err) : res(); err ? rej(err) : res();
}); });
} else if (type === 'capacity') { } else if (type === "capacity") {
const roomMessage = new WorldFullWarningToRoomMessage(); const roomMessage = new WorldFullWarningToRoomMessage();
roomMessage.setRoomid(roomId); roomMessage.setRoomid(roomId);
@ -119,11 +118,10 @@ export class AdminController extends BaseController{
err ? rej(err) : res(); err ? rej(err) : res();
}); });
} }
}); });
}); });
})); })
);
} catch (err) { } catch (err) {
this.errorToResponse(err, res); this.errorToResponse(err, res);
return; return;
@ -131,7 +129,7 @@ export class AdminController extends BaseController{
res.writeStatus("200"); res.writeStatus("200");
this.addCorsHeaders(res); this.addCorsHeaders(res);
res.end('ok'); res.end("ok");
}); });
} }
} }

View File

@ -1,4 +1,4 @@
import { v4 } from 'uuid'; import { v4 } from "uuid";
import { HttpRequest, HttpResponse, TemplatedApp } from "uWebSockets.js"; import { HttpRequest, HttpResponse, TemplatedApp } from "uWebSockets.js";
import { BaseController } from "./BaseController"; import { BaseController } from "./BaseController";
import { adminApi } from "../Services/AdminApi"; import { adminApi } from "../Services/AdminApi";
@ -6,11 +6,10 @@ import {jwtTokenManager} from "../Services/JWTTokenManager";
import { parse } from "query-string"; import { parse } from "query-string";
export interface TokenInterface { export interface TokenInterface {
userUuid: string userUuid: string;
} }
export class AuthenticateController extends BaseController { export class AuthenticateController extends BaseController {
constructor(private App: TemplatedApp) { constructor(private App: TemplatedApp) {
super(); super();
this.register(); this.register();
@ -29,15 +28,15 @@ export class AuthenticateController extends BaseController {
this.App.post("/register", (res: HttpResponse, req: HttpRequest) => { this.App.post("/register", (res: HttpResponse, req: HttpRequest) => {
(async () => { (async () => {
res.onAborted(() => { res.onAborted(() => {
console.warn('Login request was aborted'); console.warn("Login request was aborted");
}) });
const param = await res.json(); const param = await res.json();
//todo: what to do if the organizationMemberToken is already used? //todo: what to do if the organizationMemberToken is already used?
const organizationMemberToken: string | null = param.organizationMemberToken; const organizationMemberToken: string | null = param.organizationMemberToken;
try { try {
if (typeof organizationMemberToken != 'string') throw new Error('No organization token'); if (typeof organizationMemberToken != "string") throw new Error("No organization token");
const data = await adminApi.fetchMemberDataByToken(organizationMemberToken); const data = await adminApi.fetchMemberDataByToken(organizationMemberToken);
const userUuid = data.userUuid; const userUuid = data.userUuid;
const organizationSlug = data.organizationSlug; const organizationSlug = data.organizationSlug;
@ -49,7 +48,8 @@ export class AuthenticateController extends BaseController {
const authToken = jwtTokenManager.createJWTToken(userUuid); const authToken = jwtTokenManager.createJWTToken(userUuid);
res.writeStatus("200 OK"); res.writeStatus("200 OK");
this.addCorsHeaders(res); this.addCorsHeaders(res);
res.end(JSON.stringify({ res.end(
JSON.stringify({
authToken, authToken,
userUuid, userUuid,
organizationSlug, organizationSlug,
@ -57,17 +57,14 @@ export class AuthenticateController extends BaseController {
roomSlug, roomSlug,
mapUrlStart, mapUrlStart,
organizationMemberToken, organizationMemberToken,
textures textures,
})); })
);
} catch (e) { } catch (e) {
this.errorToResponse(e, res); this.errorToResponse(e, res);
} }
})(); })();
}); });
} }
private verify() { private verify() {
@ -82,28 +79,31 @@ export class AuthenticateController extends BaseController {
const query = parse(req.getQuery()); const query = parse(req.getQuery());
res.onAborted(() => { res.onAborted(() => {
console.warn('verify request was aborted'); console.warn("verify request was aborted");
}) });
try { try {
await jwtTokenManager.getUserUuidFromToken(query.token as string); await jwtTokenManager.getUserUuidFromToken(query.token as string);
} catch (e) { } catch (e) {
res.writeStatus("400 Bad Request"); res.writeStatus("400 Bad Request");
this.addCorsHeaders(res); this.addCorsHeaders(res);
res.end(JSON.stringify({ res.end(
"success": false, JSON.stringify({
"message": "Invalid JWT token" success: false,
})); message: "Invalid JWT token",
})
);
return; return;
} }
res.writeStatus("200 OK"); res.writeStatus("200 OK");
this.addCorsHeaders(res); this.addCorsHeaders(res);
res.end(JSON.stringify({ res.end(
"success": true JSON.stringify({
})); success: true,
})
);
})(); })();
}); });
} }
//permit to login on application. Return token to connect on Websocket IO. //permit to login on application. Return token to connect on Websocket IO.
@ -115,17 +115,19 @@ export class AuthenticateController extends BaseController {
this.App.post("/anonymLogin", (res: HttpResponse, req: HttpRequest) => { this.App.post("/anonymLogin", (res: HttpResponse, req: HttpRequest) => {
res.onAborted(() => { res.onAborted(() => {
console.warn('Login request was aborted'); console.warn("Login request was aborted");
}) });
const userUuid = v4(); const userUuid = v4();
const authToken = jwtTokenManager.createJWTToken(userUuid); const authToken = jwtTokenManager.createJWTToken(userUuid);
res.writeStatus("200 OK"); res.writeStatus("200 OK");
this.addCorsHeaders(res); this.addCorsHeaders(res);
res.end(JSON.stringify({ res.end(
JSON.stringify({
authToken, authToken,
userUuid, userUuid,
})); })
);
}); });
} }
} }

View File

@ -1,11 +1,10 @@
import { HttpResponse } from "uWebSockets.js"; import { HttpResponse } from "uWebSockets.js";
export class BaseController { export class BaseController {
protected addCorsHeaders(res: HttpResponse): void { protected addCorsHeaders(res: HttpResponse): void {
res.writeHeader('access-control-allow-headers', 'Origin, X-Requested-With, Content-Type, Accept'); res.writeHeader("access-control-allow-headers", "Origin, X-Requested-With, Content-Type, Accept");
res.writeHeader('access-control-allow-methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); res.writeHeader("access-control-allow-methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
res.writeHeader('access-control-allow-origin', '*'); res.writeHeader("access-control-allow-origin", "*");
} }
/** /**
@ -16,12 +15,12 @@ export class BaseController {
if (e && e.message) { if (e && e.message) {
let url = e?.config?.url; let url = e?.config?.url;
if (url !== undefined) { if (url !== undefined) {
url = ' for URL: '+url; url = " for URL: " + url;
} else { } else {
url = ''; url = "";
} }
console.error('ERROR: '+e.message+url); console.error("ERROR: " + e.message + url);
} else if (typeof(e) === 'string') { } else if (typeof e === "string") {
console.error(e); console.error(e);
} }
if (e.stack) { if (e.stack) {
@ -32,7 +31,7 @@ export class BaseController {
this.addCorsHeaders(res); this.addCorsHeaders(res);
res.end("An error occurred: " + e.response.status + " " + e.response.statusText); res.end("An error occurred: " + e.response.status + " " + e.response.statusText);
} else { } else {
res.writeStatus("500 Internal Server Error") res.writeStatus("500 Internal Server Error");
this.addCorsHeaders(res); this.addCorsHeaders(res);
res.end("An error occurred"); res.end("An error occurred");
} }

View File

@ -2,7 +2,7 @@ import {ADMIN_API_TOKEN} from "../Enum/EnvironmentVariable";
import { IoSocketController } from "_Controller/IoSocketController"; import { IoSocketController } from "_Controller/IoSocketController";
import { stringify } from "circular-json"; import { stringify } from "circular-json";
import { HttpRequest, HttpResponse } from "uWebSockets.js"; import { HttpRequest, HttpResponse } from "uWebSockets.js";
import { parse } from 'query-string'; import { parse } from "query-string";
import { App } from "../Server/sifrr.server"; import { App } from "../Server/sifrr.server";
import { socketManager } from "../Services/SocketManager"; import { socketManager } from "../Services/SocketManager";
@ -11,18 +11,19 @@ export class DebugController {
this.getDump(); this.getDump();
} }
getDump() { getDump() {
this.App.get("/dump", (res: HttpResponse, req: HttpRequest) => { this.App.get("/dump", (res: HttpResponse, req: HttpRequest) => {
const query = parse(req.getQuery()); const query = parse(req.getQuery());
if (query.token !== ADMIN_API_TOKEN) { if (query.token !== ADMIN_API_TOKEN) {
return res.status(401).send('Invalid token sent!'); return res.status(401).send("Invalid token sent!");
} }
return res.writeStatus('200 OK').writeHeader('Content-Type', 'application/json').end(stringify( return res
socketManager.getWorlds(), .writeStatus("200 OK")
(key: unknown, value: unknown) => { .writeHeader("Content-Type", "application/json")
.end(
stringify(socketManager.getWorlds(), (key: unknown, value: unknown) => {
if (value instanceof Map) { if (value instanceof Map) {
const obj: any = {}; // eslint-disable-line @typescript-eslint/no-explicit-any const obj: any = {}; // eslint-disable-line @typescript-eslint/no-explicit-any
for (const [mapKey, mapValue] of value.entries()) { for (const [mapKey, mapValue] of value.entries()) {
@ -38,8 +39,8 @@ export class DebugController {
} else { } else {
return value; return value;
} }
} })
)); );
}); });
} }
} }

View File

@ -19,7 +19,7 @@ import {
EmotePromptMessage, EmotePromptMessage,
} from "../Messages/generated/messages_pb"; } from "../Messages/generated/messages_pb";
import { UserMovesMessage } from "../Messages/generated/messages_pb"; import { UserMovesMessage } from "../Messages/generated/messages_pb";
import {TemplatedApp} from "uWebSockets.js" import { TemplatedApp } from "uWebSockets.js";
import { parse } from "query-string"; import { parse } from "query-string";
import { jwtTokenManager } from "../Services/JWTTokenManager"; import { jwtTokenManager } from "../Services/JWTTokenManager";
import { adminApi, CharacterTexture, FetchMemberDataByUuidResponse } from "../Services/AdminApi"; import { adminApi, CharacterTexture, FetchMemberDataByUuidResponse } from "../Services/AdminApi";
@ -39,32 +39,29 @@ export class IoSocketController {
} }
adminRoomSocket() { adminRoomSocket() {
this.app.ws('/admin/rooms', { this.app.ws("/admin/rooms", {
upgrade: (res, req, context) => { upgrade: (res, req, context) => {
const query = parse(req.getQuery()); const query = parse(req.getQuery());
const websocketKey = req.getHeader('sec-websocket-key'); const websocketKey = req.getHeader("sec-websocket-key");
const websocketProtocol = req.getHeader('sec-websocket-protocol'); const websocketProtocol = req.getHeader("sec-websocket-protocol");
const websocketExtensions = req.getHeader('sec-websocket-extensions'); const websocketExtensions = req.getHeader("sec-websocket-extensions");
const token = query.token; const token = query.token;
if (token !== ADMIN_API_TOKEN) { if (token !== ADMIN_API_TOKEN) {
console.log('Admin access refused for token: '+token) console.log("Admin access refused for token: " + token);
res.writeStatus("401 Unauthorized").end('Incorrect token'); res.writeStatus("401 Unauthorized").end("Incorrect token");
return; return;
} }
const roomId = query.roomId; const roomId = query.roomId;
if (typeof roomId !== 'string') { if (typeof roomId !== "string") {
console.error('Received') console.error("Received");
res.writeStatus("400 Bad Request").end('Missing room id'); res.writeStatus("400 Bad Request").end("Missing room id");
return; return;
} }
res.upgrade( res.upgrade({ roomId }, websocketKey, websocketProtocol, websocketExtensions, context);
{roomId},
websocketKey, websocketProtocol, websocketExtensions, context,
);
}, },
open: (ws) => { open: (ws) => {
console.log('Admin socket connect for room: '+ws.roomId); console.log("Admin socket connect for room: " + ws.roomId);
ws.disconnecting = false; ws.disconnecting = false;
socketManager.handleAdminRoom(ws as ExAdminSocketInterface, ws.roomId as string); socketManager.handleAdminRoom(ws as ExAdminSocketInterface, ws.roomId as string);
@ -74,16 +71,26 @@ export class IoSocketController {
const roomId = ws.roomId as string; const roomId = ws.roomId as string;
//TODO refactor message type and data //TODO refactor message type and data
const message: {event: string, message: {type: string, message: unknown, userUuid: string}} = const message: { event: string; message: { type: string; message: unknown; userUuid: string } } =
JSON.parse(new TextDecoder("utf-8").decode(new Uint8Array(arrayBuffer))); JSON.parse(new TextDecoder("utf-8").decode(new Uint8Array(arrayBuffer)));
if(message.event === 'user-message') { if (message.event === "user-message") {
const messageToEmit = (message.message as { message: string, type: string, userUuid: string }); const messageToEmit = message.message as { message: string; type: string; userUuid: string };
if(messageToEmit.type === 'banned'){ if (messageToEmit.type === "banned") {
socketManager.emitBan(messageToEmit.userUuid, messageToEmit.message, messageToEmit.type, ws.roomId as string); socketManager.emitBan(
messageToEmit.userUuid,
messageToEmit.message,
messageToEmit.type,
ws.roomId as string
);
} }
if(messageToEmit.type === 'ban') { if (messageToEmit.type === "ban") {
socketManager.emitSendUserMessage(messageToEmit.userUuid, messageToEmit.message, messageToEmit.type, ws.roomId as string); socketManager.emitSendUserMessage(
messageToEmit.userUuid,
messageToEmit.message,
messageToEmit.type,
ws.roomId as string
);
} }
} }
} catch (err) { } catch (err) {
@ -91,7 +98,7 @@ export class IoSocketController {
} }
}, },
close: (ws, code, message) => { close: (ws, code, message) => {
const Client = (ws as ExAdminSocketInterface); const Client = ws as ExAdminSocketInterface;
try { try {
Client.disconnecting = true; Client.disconnecting = true;
socketManager.leaveAdminRoom(Client); socketManager.leaveAdminRoom(Client);
@ -99,12 +106,12 @@ export class IoSocketController {
console.error('An error occurred on admin "disconnect"'); console.error('An error occurred on admin "disconnect"');
console.error(e); console.error(e);
} }
} },
}) });
} }
ioConnection() { ioConnection() {
this.app.ws('/room', { this.app.ws("/room", {
/* Options */ /* Options */
//compression: uWS.SHARED_COMPRESSOR, //compression: uWS.SHARED_COMPRESSOR,
idleTimeout: SOCKET_IDLE_TIMER, idleTimeout: SOCKET_IDLE_TIMER,
@ -123,15 +130,15 @@ export class IoSocketController {
const url = req.getUrl(); const url = req.getUrl();
const query = parse(req.getQuery()); const query = parse(req.getQuery());
const websocketKey = req.getHeader('sec-websocket-key'); const websocketKey = req.getHeader("sec-websocket-key");
const websocketProtocol = req.getHeader('sec-websocket-protocol'); const websocketProtocol = req.getHeader("sec-websocket-protocol");
const websocketExtensions = req.getHeader('sec-websocket-extensions'); const websocketExtensions = req.getHeader("sec-websocket-extensions");
const IPAddress = req.getHeader('x-forwarded-for'); const IPAddress = req.getHeader("x-forwarded-for");
const roomId = query.roomId; const roomId = query.roomId;
try { try {
if (typeof roomId !== 'string') { if (typeof roomId !== "string") {
throw new Error('Undefined room ID: '); throw new Error("Undefined room ID: ");
} }
const token = query.token; const token = query.token;
@ -145,22 +152,22 @@ export class IoSocketController {
let companion: CompanionMessage | undefined = undefined; let companion: CompanionMessage | undefined = undefined;
if (typeof query.companion === 'string') { if (typeof query.companion === "string") {
companion = new CompanionMessage(); companion = new CompanionMessage();
companion.setName(query.companion); companion.setName(query.companion);
} }
if (typeof name !== 'string') { if (typeof name !== "string") {
throw new Error('Expecting name'); throw new Error("Expecting name");
} }
if (name === '') { if (name === "") {
throw new Error('No empty name'); throw new Error("No empty name");
} }
let characterLayers = query.characterLayers; let characterLayers = query.characterLayers;
if (characterLayers === null) { if (characterLayers === null) {
throw new Error('Expecting skin'); throw new Error("Expecting skin");
} }
if (typeof characterLayers === 'string') { if (typeof characterLayers === "string") {
characterLayers = [characterLayers]; characterLayers = [characterLayers];
} }
@ -179,25 +186,32 @@ export class IoSocketController {
visitCardUrl: null, visitCardUrl: null,
textures: [], textures: [],
messages: [], messages: [],
anonymous: true anonymous: true,
}; };
try { try {
userData = await adminApi.fetchMemberDataByUuid(userUuid, roomId); userData = await adminApi.fetchMemberDataByUuid(userUuid, roomId);
} catch (err) { } catch (err) {
if (err?.response?.status == 404) { if (err?.response?.status == 404) {
// If we get an HTTP 404, the token is invalid. Let's perform an anonymous login! // If we get an HTTP 404, the token is invalid. Let's perform an anonymous login!
console.warn('Cannot find user with uuid "'+userUuid+'". Performing an anonymous login instead.'); console.warn(
'Cannot find user with uuid "' +
userUuid +
'". Performing an anonymous login instead.'
);
} else if (err?.response?.status == 403) { } else if (err?.response?.status == 403) {
// If we get an HTTP 403, the world is full. We need to broadcast a special error to the client. // If we get an HTTP 403, the world is full. We need to broadcast a special error to the client.
// we finish immediately the upgrade then we will close the socket as soon as it starts opening. // we finish immediately the upgrade then we will close the socket as soon as it starts opening.
return res.upgrade({ return res.upgrade(
{
rejected: true, rejected: true,
message: err?.response?.data.message, message: err?.response?.data.message,
status: err?.response?.status status: err?.response?.status,
}, websocketKey, },
websocketKey,
websocketProtocol, websocketProtocol,
websocketExtensions, websocketExtensions,
context); context
);
} else { } else {
throw err; throw err;
} }
@ -206,21 +220,30 @@ export class IoSocketController {
memberTags = userData.tags; memberTags = userData.tags;
memberVisitCardUrl = userData.visitCardUrl; memberVisitCardUrl = userData.visitCardUrl;
memberTextures = userData.textures; memberTextures = userData.textures;
if (!room.public && room.policyType === GameRoomPolicyTypes.USE_TAGS_POLICY && (userData.anonymous === true || !room.canAccess(memberTags))) { if (
throw new Error('Insufficient privileges to access this room') !room.public &&
room.policyType === GameRoomPolicyTypes.USE_TAGS_POLICY &&
(userData.anonymous === true || !room.canAccess(memberTags))
) {
throw new Error("Insufficient privileges to access this room");
} }
if (!room.public && room.policyType === GameRoomPolicyTypes.MEMBERS_ONLY_POLICY && userData.anonymous === true) { if (
throw new Error('Use the login URL to connect') !room.public &&
room.policyType === GameRoomPolicyTypes.MEMBERS_ONLY_POLICY &&
userData.anonymous === true
) {
throw new Error("Use the login URL to connect");
} }
} catch (e) { } catch (e) {
console.log('access not granted for user '+userUuid+' and room '+roomId); console.log("access not granted for user " + userUuid + " and room " + roomId);
console.error(e); console.error(e);
throw new Error('User cannot access this world') throw new Error("User cannot access this world");
} }
} }
// Generate characterLayers objects from characterLayers string[] // Generate characterLayers objects from characterLayers string[]
const characterLayerObjs: CharacterLayer[] = SocketManager.mergeCharacterLayersAndCustomTextures(characterLayers, memberTextures); const characterLayerObjs: CharacterLayer[] =
SocketManager.mergeCharacterLayersAndCustomTextures(characterLayers, memberTextures);
if (upgradeAborted.aborted) { if (upgradeAborted.aborted) {
console.log("Ouch! Client disconnected before we could upgrade it!"); console.log("Ouch! Client disconnected before we could upgrade it!");
@ -229,7 +252,8 @@ export class IoSocketController {
} }
/* This immediately calls open handler, you must not use res after this call */ /* This immediately calls open handler, you must not use res after this call */
res.upgrade({ res.upgrade(
{
// Data passed here is accessible on the "websocket" socket object. // Data passed here is accessible on the "websocket" socket object.
url, url,
token, token,
@ -246,22 +270,22 @@ export class IoSocketController {
position: { position: {
x: x, x: x,
y: y, y: y,
direction: 'down', direction: "down",
moving: false moving: false,
} as PointInterface, } as PointInterface,
viewport: { viewport: {
top, top,
right, right,
bottom, bottom,
left left,
} },
}, },
/* Spell these correctly */ /* Spell these correctly */
websocketKey, websocketKey,
websocketProtocol, websocketProtocol,
websocketExtensions, websocketExtensions,
context); context
);
} catch (e) { } catch (e) {
/*if (e instanceof Error) { /*if (e instanceof Error) {
console.log(e.message); console.log(e.message);
@ -269,13 +293,16 @@ export class IoSocketController {
} else { } else {
res.writeStatus("500 Internal Server Error").end('An error occurred'); res.writeStatus("500 Internal Server Error").end('An error occurred');
}*/ }*/
return res.upgrade({ return res.upgrade(
{
rejected: true, rejected: true,
message: e.message ? e.message : '500 Internal Server Error' message: e.message ? e.message : "500 Internal Server Error",
}, websocketKey, },
websocketKey,
websocketProtocol, websocketProtocol,
websocketExtensions, websocketExtensions,
context); context
);
} }
})(); })();
}, },
@ -283,7 +310,7 @@ export class IoSocketController {
open: (ws) => { open: (ws) => {
if (ws.rejected === true) { if (ws.rejected === true) {
//FIX ME to use status code //FIX ME to use status code
if(ws.message === 'World is full'){ if (ws.message === "World is full") {
socketManager.emitWorldFullMessage(ws); socketManager.emitWorldFullMessage(ws);
} else { } else {
socketManager.emitConnexionErrorMessage(ws, ws.message as string); socketManager.emitConnexionErrorMessage(ws, ws.message as string);
@ -299,7 +326,7 @@ export class IoSocketController {
//get data information and show messages //get data information and show messages
if (client.messages && Array.isArray(client.messages)) { if (client.messages && Array.isArray(client.messages)) {
client.messages.forEach((c: unknown) => { client.messages.forEach((c: unknown) => {
const messageToSend = c as { type: string, message: string }; const messageToSend = c as { type: string; message: string };
const sendUserMessage = new SendUserMessage(); const sendUserMessage = new SendUserMessage();
sendUserMessage.setType(messageToSend.type); sendUserMessage.setType(messageToSend.type);
@ -323,33 +350,48 @@ export class IoSocketController {
} else if (message.hasUsermovesmessage()) { } else if (message.hasUsermovesmessage()) {
socketManager.handleUserMovesMessage(client, message.getUsermovesmessage() as UserMovesMessage); socketManager.handleUserMovesMessage(client, message.getUsermovesmessage() as UserMovesMessage);
} else if (message.hasSetplayerdetailsmessage()) { } else if (message.hasSetplayerdetailsmessage()) {
socketManager.handleSetPlayerDetails(client, message.getSetplayerdetailsmessage() as SetPlayerDetailsMessage); socketManager.handleSetPlayerDetails(
client,
message.getSetplayerdetailsmessage() as SetPlayerDetailsMessage
);
} else if (message.hasSilentmessage()) { } else if (message.hasSilentmessage()) {
socketManager.handleSilentMessage(client, message.getSilentmessage() as SilentMessage); socketManager.handleSilentMessage(client, message.getSilentmessage() as SilentMessage);
} else if (message.hasItemeventmessage()) { } else if (message.hasItemeventmessage()) {
socketManager.handleItemEvent(client, message.getItemeventmessage() as ItemEventMessage); socketManager.handleItemEvent(client, message.getItemeventmessage() as ItemEventMessage);
} else if (message.hasWebrtcsignaltoservermessage()) { } else if (message.hasWebrtcsignaltoservermessage()) {
socketManager.emitVideo(client, message.getWebrtcsignaltoservermessage() as WebRtcSignalToServerMessage); socketManager.emitVideo(
client,
message.getWebrtcsignaltoservermessage() as WebRtcSignalToServerMessage
);
} else if (message.hasWebrtcscreensharingsignaltoservermessage()) { } else if (message.hasWebrtcscreensharingsignaltoservermessage()) {
socketManager.emitScreenSharing(client, message.getWebrtcscreensharingsignaltoservermessage() as WebRtcSignalToServerMessage); socketManager.emitScreenSharing(
client,
message.getWebrtcscreensharingsignaltoservermessage() as WebRtcSignalToServerMessage
);
} else if (message.hasPlayglobalmessage()) { } else if (message.hasPlayglobalmessage()) {
socketManager.emitPlayGlobalMessage(client, message.getPlayglobalmessage() as PlayGlobalMessage); socketManager.emitPlayGlobalMessage(client, message.getPlayglobalmessage() as PlayGlobalMessage);
} else if (message.hasReportplayermessage()) { } else if (message.hasReportplayermessage()) {
socketManager.handleReportMessage(client, message.getReportplayermessage() as ReportPlayerMessage); socketManager.handleReportMessage(client, message.getReportplayermessage() as ReportPlayerMessage);
} else if (message.hasQueryjitsijwtmessage()) { } else if (message.hasQueryjitsijwtmessage()) {
socketManager.handleQueryJitsiJwtMessage(client, message.getQueryjitsijwtmessage() as QueryJitsiJwtMessage); socketManager.handleQueryJitsiJwtMessage(
client,
message.getQueryjitsijwtmessage() as QueryJitsiJwtMessage
);
} else if (message.hasEmotepromptmessage()) { } else if (message.hasEmotepromptmessage()) {
socketManager.handleEmotePromptMessage(client, message.getEmotepromptmessage() as EmotePromptMessage); socketManager.handleEmotePromptMessage(
client,
message.getEmotepromptmessage() as EmotePromptMessage
);
} }
/* Ok is false if backpressure was built up, wait for drain */ /* Ok is false if backpressure was built up, wait for drain */
//let ok = ws.send(message, isBinary); //let ok = ws.send(message, isBinary);
}, },
drain: (ws) => { drain: (ws) => {
console.log('WebSocket backpressure: ' + ws.getBufferedAmount()); console.log("WebSocket backpressure: " + ws.getBufferedAmount());
}, },
close: (ws, code, message) => { close: (ws, code, message) => {
const Client = (ws as ExSocketInterface); const Client = ws as ExSocketInterface;
try { try {
Client.disconnecting = true; Client.disconnecting = true;
//leave room //leave room
@ -358,8 +400,8 @@ export class IoSocketController {
console.error('An error occurred on "disconnect"'); console.error('An error occurred on "disconnect"');
console.error(e); console.error(e);
} }
} },
}) });
} }
//eslint-disable-next-line @typescript-eslint/no-explicit-any //eslint-disable-next-line @typescript-eslint/no-explicit-any
@ -374,7 +416,7 @@ export class IoSocketController {
client.batchTimeout = null; client.batchTimeout = null;
client.emitInBatch = (payload: SubMessage): void => { client.emitInBatch = (payload: SubMessage): void => {
emitInBatch(client, payload); emitInBatch(client, payload);
} };
client.disconnecting = false; client.disconnecting = false;
client.messages = ws.messages; client.messages = ws.messages;

View File

@ -3,16 +3,13 @@ import {BaseController} from "./BaseController";
import { parse } from "query-string"; import { parse } from "query-string";
import { adminApi } from "../Services/AdminApi"; import { adminApi } from "../Services/AdminApi";
export class MapController extends BaseController { export class MapController extends BaseController {
constructor(private App: TemplatedApp) { constructor(private App: TemplatedApp) {
super(); super();
this.App = App; this.App = App;
this.getMapUrl(); this.getMapUrl();
} }
// Returns a map mapping map name to file name of the map // Returns a map mapping map name to file name of the map
getMapUrl() { getMapUrl() {
this.App.options("/map", (res: HttpResponse, req: HttpRequest) => { this.App.options("/map", (res: HttpResponse, req: HttpRequest) => {
@ -22,29 +19,28 @@ export class MapController extends BaseController{
}); });
this.App.get("/map", (res: HttpResponse, req: HttpRequest) => { this.App.get("/map", (res: HttpResponse, req: HttpRequest) => {
res.onAborted(() => { res.onAborted(() => {
console.warn('/map request was aborted'); console.warn("/map request was aborted");
}) });
const query = parse(req.getQuery()); const query = parse(req.getQuery());
if (typeof query.organizationSlug !== 'string') { if (typeof query.organizationSlug !== "string") {
console.error('Expected organizationSlug parameter'); console.error("Expected organizationSlug parameter");
res.writeStatus("400 Bad request"); res.writeStatus("400 Bad request");
this.addCorsHeaders(res); this.addCorsHeaders(res);
res.end("Expected organizationSlug parameter"); res.end("Expected organizationSlug parameter");
return; return;
} }
if (typeof query.worldSlug !== 'string') { if (typeof query.worldSlug !== "string") {
console.error('Expected worldSlug parameter'); console.error("Expected worldSlug parameter");
res.writeStatus("400 Bad request"); res.writeStatus("400 Bad request");
this.addCorsHeaders(res); this.addCorsHeaders(res);
res.end("Expected worldSlug parameter"); res.end("Expected worldSlug parameter");
return; return;
} }
if (typeof query.roomSlug !== 'string' && query.roomSlug !== undefined) { if (typeof query.roomSlug !== "string" && query.roomSlug !== undefined) {
console.error('Expected only one roomSlug parameter'); console.error("Expected only one roomSlug parameter");
res.writeStatus("400 Bad request"); res.writeStatus("400 Bad request");
this.addCorsHeaders(res); this.addCorsHeaders(res);
res.end("Expected only one roomSlug parameter"); res.end("Expected only one roomSlug parameter");
@ -53,7 +49,11 @@ export class MapController extends BaseController{
(async () => { (async () => {
try { try {
const mapDetails = await adminApi.fetchMapDetails(query.organizationSlug as string, query.worldSlug as string, query.roomSlug as string|undefined); const mapDetails = await adminApi.fetchMapDetails(
query.organizationSlug as string,
query.worldSlug as string,
query.roomSlug as string | undefined
);
res.writeStatus("200 OK"); res.writeStatus("200 OK");
this.addCorsHeaders(res); this.addCorsHeaders(res);
@ -62,7 +62,6 @@ export class MapController extends BaseController{
this.errorToResponse(e, res); this.errorToResponse(e, res);
} }
})(); })();
}); });
} }
} }

View File

@ -1,7 +1,7 @@
import { App } from "../Server/sifrr.server"; import { App } from "../Server/sifrr.server";
import { HttpRequest, HttpResponse } from "uWebSockets.js"; import { HttpRequest, HttpResponse } from "uWebSockets.js";
const register = require('prom-client').register; const register = require("prom-client").register;
const collectDefaultMetrics = require('prom-client').collectDefaultMetrics; const collectDefaultMetrics = require("prom-client").collectDefaultMetrics;
export class PrometheusController { export class PrometheusController {
constructor(private App: App) { constructor(private App: App) {
@ -14,7 +14,7 @@ export class PrometheusController {
} }
private metrics(res: HttpResponse, req: HttpRequest): void { private metrics(res: HttpResponse, req: HttpRequest): void {
res.writeHeader('Content-Type', register.contentType); res.writeHeader("Content-Type", register.contentType);
res.end(register.metrics()); res.end(register.metrics());
} }
} }

View File

@ -1,16 +1,16 @@
const SECRET_KEY = process.env.SECRET_KEY || "THECODINGMACHINE_SECRET_KEY"; const SECRET_KEY = process.env.SECRET_KEY || "THECODINGMACHINE_SECRET_KEY";
const MINIMUM_DISTANCE = process.env.MINIMUM_DISTANCE ? Number(process.env.MINIMUM_DISTANCE) : 64; const MINIMUM_DISTANCE = process.env.MINIMUM_DISTANCE ? Number(process.env.MINIMUM_DISTANCE) : 64;
const GROUP_RADIUS = process.env.GROUP_RADIUS ? Number(process.env.GROUP_RADIUS) : 48; const GROUP_RADIUS = process.env.GROUP_RADIUS ? Number(process.env.GROUP_RADIUS) : 48;
const ALLOW_ARTILLERY = process.env.ALLOW_ARTILLERY ? process.env.ALLOW_ARTILLERY == 'true' : false; const ALLOW_ARTILLERY = process.env.ALLOW_ARTILLERY ? process.env.ALLOW_ARTILLERY == "true" : false;
const API_URL = process.env.API_URL || ''; const API_URL = process.env.API_URL || "";
const ADMIN_API_URL = process.env.ADMIN_API_URL || ''; const ADMIN_API_URL = process.env.ADMIN_API_URL || "";
const ADMIN_API_TOKEN = process.env.ADMIN_API_TOKEN || 'myapitoken'; const ADMIN_API_TOKEN = process.env.ADMIN_API_TOKEN || "myapitoken";
const MAX_USERS_PER_ROOM = parseInt(process.env.MAX_USERS_PER_ROOM || '') || 600; const MAX_USERS_PER_ROOM = parseInt(process.env.MAX_USERS_PER_ROOM || "") || 600;
const CPU_OVERHEAT_THRESHOLD = Number(process.env.CPU_OVERHEAT_THRESHOLD) || 80; const CPU_OVERHEAT_THRESHOLD = Number(process.env.CPU_OVERHEAT_THRESHOLD) || 80;
const JITSI_URL : string|undefined = (process.env.JITSI_URL === '') ? undefined : process.env.JITSI_URL; const JITSI_URL: string | undefined = process.env.JITSI_URL === "" ? undefined : process.env.JITSI_URL;
const JITSI_ISS = process.env.JITSI_ISS || ''; const JITSI_ISS = process.env.JITSI_ISS || "";
const SECRET_JITSI_KEY = process.env.SECRET_JITSI_KEY || ''; const SECRET_JITSI_KEY = process.env.SECRET_JITSI_KEY || "";
const PUSHER_HTTP_PORT = parseInt(process.env.PUSHER_HTTP_PORT || '8080') || 8080 const PUSHER_HTTP_PORT = parseInt(process.env.PUSHER_HTTP_PORT || "8080") || 8080;
export const SOCKET_IDLE_TIMER = parseInt(process.env.SOCKET_IDLE_TIMER as string) || 30; // maximum time (in second) without activity before a socket is closed export const SOCKET_IDLE_TIMER = parseInt(process.env.SOCKET_IDLE_TIMER as string) || 30; // maximum time (in second) without activity before a socket is closed
export { export {
@ -26,5 +26,5 @@ export {
JITSI_URL, JITSI_URL,
JITSI_ISS, JITSI_ISS,
SECRET_JITSI_KEY, SECRET_JITSI_KEY,
PUSHER_HTTP_PORT PUSHER_HTTP_PORT,
} };

View File

@ -4,5 +4,5 @@ import {PositionInterface} from "_Model/PositionInterface";
* A physical object that can be placed into a Zone * A physical object that can be placed into a Zone
*/ */
export interface Movable { export interface Movable {
getPosition(): PositionInterface getPosition(): PositionInterface;
} }

View File

@ -21,19 +21,22 @@ interface ZoneDescriptor {
} }
export class PositionDispatcher { export class PositionDispatcher {
// TODO: we need a way to clean the zones if noone is in the zone and noone listening (to free memory!) // TODO: we need a way to clean the zones if noone is in the zone and noone listening (to free memory!)
private zones: Zone[][] = []; private zones: Zone[][] = [];
constructor(public readonly roomId: string, private zoneWidth: number, private zoneHeight: number, private socketListener: ZoneEventListener) { constructor(
} public readonly roomId: string,
private zoneWidth: number,
private zoneHeight: number,
private socketListener: ZoneEventListener
) {}
private getZoneDescriptorFromCoordinates(x: number, y: number): ZoneDescriptor { private getZoneDescriptorFromCoordinates(x: number, y: number): ZoneDescriptor {
return { return {
i: Math.floor(x / this.zoneWidth), i: Math.floor(x / this.zoneWidth),
j: Math.floor(y / this.zoneHeight), j: Math.floor(y / this.zoneHeight),
} };
} }
/** /**
@ -41,7 +44,7 @@ export class PositionDispatcher {
*/ */
public setViewport(socket: ExSocketInterface, viewport: ViewportInterface): void { public setViewport(socket: ExSocketInterface, viewport: ViewportInterface): void {
if (viewport.left > viewport.right || viewport.top > viewport.bottom) { if (viewport.left > viewport.right || viewport.top > viewport.bottom) {
console.warn('Invalid viewport received: ', viewport); console.warn("Invalid viewport received: ", viewport);
return; return;
} }
@ -57,8 +60,8 @@ export class PositionDispatcher {
} }
} }
const addedZones = [...newZones].filter(x => !oldZones.has(x)); const addedZones = [...newZones].filter((x) => !oldZones.has(x));
const removedZones = [...oldZones].filter(x => !newZones.has(x)); const removedZones = [...oldZones].filter((x) => !newZones.has(x));
for (const zone of addedZones) { for (const zone of addedZones) {
zone.startListening(socket); zone.startListening(socket);

View File

@ -1,4 +1,4 @@
export interface PositionInterface { export interface PositionInterface {
x: number, x: number;
y: number y: number;
} }

View File

@ -17,13 +17,11 @@ export class PusherRoom {
public tags: string[]; public tags: string[];
public policyType: GameRoomPolicyTypes; public policyType: GameRoomPolicyTypes;
public readonly roomSlug: string; public readonly roomSlug: string;
public readonly worldSlug: string = ''; public readonly worldSlug: string = "";
public readonly organizationSlug: string = ''; public readonly organizationSlug: string = "";
private versionNumber: number = 1; private versionNumber: number = 1;
constructor(public readonly roomId: string, constructor(public readonly roomId: string, private socketListener: ZoneEventListener) {
private socketListener: ZoneEventListener)
{
this.public = isRoomAnonymous(roomId); this.public = isRoomAnonymous(roomId);
this.tags = []; this.tags = [];
this.policyType = GameRoomPolicyTypes.ANONYMUS_POLICY; this.policyType = GameRoomPolicyTypes.ANONYMUS_POLICY;

View File

@ -1,30 +1,30 @@
//helper functions to parse room IDs //helper functions to parse room IDs
export const isRoomAnonymous = (roomID: string): boolean => { export const isRoomAnonymous = (roomID: string): boolean => {
if (roomID.startsWith('_/')) { if (roomID.startsWith("_/")) {
return true; return true;
} else if(roomID.startsWith('@/')) { } else if (roomID.startsWith("@/")) {
return false; return false;
} else { } else {
throw new Error('Incorrect room ID: '+roomID); throw new Error("Incorrect room ID: " + roomID);
}
} }
};
export const extractRoomSlugPublicRoomId = (roomId: string): string => { export const extractRoomSlugPublicRoomId = (roomId: string): string => {
const idParts = roomId.split('/'); const idParts = roomId.split("/");
if (idParts.length < 3) throw new Error('Incorrect roomId: '+roomId); if (idParts.length < 3) throw new Error("Incorrect roomId: " + roomId);
return idParts.slice(2).join('/'); return idParts.slice(2).join("/");
} };
export interface extractDataFromPrivateRoomIdResponse { export interface extractDataFromPrivateRoomIdResponse {
organizationSlug: string; organizationSlug: string;
worldSlug: string; worldSlug: string;
roomSlug: string; roomSlug: string;
} }
export const extractDataFromPrivateRoomId = (roomId: string): extractDataFromPrivateRoomIdResponse => { export const extractDataFromPrivateRoomId = (roomId: string): extractDataFromPrivateRoomIdResponse => {
const idParts = roomId.split('/'); const idParts = roomId.split("/");
if (idParts.length < 4) throw new Error('Incorrect roomId: '+roomId); if (idParts.length < 4) throw new Error("Incorrect roomId: " + roomId);
const organizationSlug = idParts[1]; const organizationSlug = idParts[1];
const worldSlug = idParts[2]; const worldSlug = idParts[2];
const roomSlug = idParts[3]; const roomSlug = idParts[3];
return {organizationSlug, worldSlug, roomSlug} return { organizationSlug, worldSlug, roomSlug };
} };

View File

@ -4,11 +4,12 @@ import {ViewportInterface} from "_Model/Websocket/ViewportMessage";
import { import {
AdminPusherToBackMessage, AdminPusherToBackMessage,
BatchMessage, BatchMessage,
PusherToBackMessage, ServerToAdminClientMessage, PusherToBackMessage,
ServerToAdminClientMessage,
ServerToClientMessage, ServerToClientMessage,
SubMessage SubMessage,
} from "../../Messages/generated/messages_pb"; } from "../../Messages/generated/messages_pb";
import {WebSocket} from "uWebSockets.js" import { WebSocket } from "uWebSockets.js";
import { CharacterTexture } from "../../Services/AdminApi"; import { CharacterTexture } from "../../Services/AdminApi";
import { ClientDuplexStream } from "grpc"; import { ClientDuplexStream } from "grpc";
import { Zone } from "_Model/Zone"; import { Zone } from "_Model/Zone";
@ -16,6 +17,6 @@ import {Zone} from "_Model/Zone";
export type AdminConnection = ClientDuplexStream<AdminPusherToBackMessage, ServerToAdminClientMessage>; export type AdminConnection = ClientDuplexStream<AdminPusherToBackMessage, ServerToAdminClientMessage>;
export interface ExAdminSocketInterface extends WebSocket { export interface ExAdminSocketInterface extends WebSocket {
adminConnection: AdminConnection, adminConnection: AdminConnection;
disconnecting: boolean, disconnecting: boolean;
} }

View File

@ -6,9 +6,9 @@ import {
CompanionMessage, CompanionMessage,
PusherToBackMessage, PusherToBackMessage,
ServerToClientMessage, ServerToClientMessage,
SubMessage SubMessage,
} from "../../Messages/generated/messages_pb"; } from "../../Messages/generated/messages_pb";
import {WebSocket} from "uWebSockets.js" import { WebSocket } from "uWebSockets.js";
import { CharacterTexture } from "../../Services/AdminApi"; import { CharacterTexture } from "../../Services/AdminApi";
import { ClientDuplexStream } from "grpc"; import { ClientDuplexStream } from "grpc";
import { Zone } from "_Model/Zone"; import { Zone } from "_Model/Zone";
@ -16,8 +16,8 @@ import {Zone} from "_Model/Zone";
export type BackConnection = ClientDuplexStream<PusherToBackMessage, ServerToClientMessage>; export type BackConnection = ClientDuplexStream<PusherToBackMessage, ServerToClientMessage>;
export interface CharacterLayer { export interface CharacterLayer {
name: string, name: string;
url: string|undefined url: string | undefined;
} }
export interface ExSocketInterface extends WebSocket, Identificable { export interface ExSocketInterface extends WebSocket, Identificable {
@ -37,11 +37,11 @@ export interface ExSocketInterface extends WebSocket, Identificable {
emitInBatch: (payload: SubMessage) => void; emitInBatch: (payload: SubMessage) => void;
batchedMessages: BatchMessage; batchedMessages: BatchMessage;
batchTimeout: NodeJS.Timeout | null; batchTimeout: NodeJS.Timeout | null;
disconnecting: boolean, disconnecting: boolean;
messages: unknown, messages: unknown;
tags: string[], tags: string[];
visitCardUrl: string|null, visitCardUrl: string | null;
textures: CharacterTexture[], textures: CharacterTexture[];
backConnection: BackConnection, backConnection: BackConnection;
listenedZones: Set<Zone>; listenedZones: Set<Zone>;
} }

View File

@ -1,10 +1,11 @@
import * as tg from "generic-type-guard"; import * as tg from "generic-type-guard";
export const isItemEventMessageInterface = export const isItemEventMessageInterface = new tg.IsInterface()
new tg.IsInterface().withProperties({ .withProperties({
itemId: tg.isNumber, itemId: tg.isNumber,
event: tg.isString, event: tg.isString,
state: tg.isUnknown, state: tg.isUnknown,
parameters: tg.isUnknown, parameters: tg.isUnknown,
}).get(); })
.get();
export type ItemEventMessageInterface = tg.GuardedType<typeof isItemEventMessageInterface>; export type ItemEventMessageInterface = tg.GuardedType<typeof isItemEventMessageInterface>;

View File

@ -1,6 +1,10 @@
import { PointInterface } from "./PointInterface"; import { PointInterface } from "./PointInterface";
export class Point implements PointInterface { export class Point implements PointInterface {
constructor(public x : number, public y : number, public direction : string = "none", public moving : boolean = false) { constructor(
} public x: number,
public y: number,
public direction: string = "none",
public moving: boolean = false
) {}
} }

View File

@ -7,11 +7,12 @@ import * as tg from "generic-type-guard";
readonly moving: boolean; readonly moving: boolean;
}*/ }*/
export const isPointInterface = export const isPointInterface = new tg.IsInterface()
new tg.IsInterface().withProperties({ .withProperties({
x: tg.isNumber, x: tg.isNumber,
y: tg.isNumber, y: tg.isNumber,
direction: tg.isString, direction: tg.isString,
moving: tg.isBoolean moving: tg.isBoolean,
}).get(); })
.get();
export type PointInterface = tg.GuardedType<typeof isPointInterface>; export type PointInterface = tg.GuardedType<typeof isPointInterface>;

View File

@ -3,7 +3,7 @@ import {
CharacterLayerMessage, CharacterLayerMessage,
ItemEventMessage, ItemEventMessage,
PointMessage, PointMessage,
PositionMessage PositionMessage,
} from "../../Messages/generated/messages_pb"; } from "../../Messages/generated/messages_pb";
import { CharacterLayer, ExSocketInterface } from "_Model/Websocket/ExSocketInterface"; import { CharacterLayer, ExSocketInterface } from "_Model/Websocket/ExSocketInterface";
import Direction = PositionMessage.Direction; import Direction = PositionMessage.Direction;
@ -11,24 +11,23 @@ import {ItemEventMessageInterface} from "_Model/Websocket/ItemEventMessage";
import { PositionInterface } from "_Model/PositionInterface"; import { PositionInterface } from "_Model/PositionInterface";
export class ProtobufUtils { export class ProtobufUtils {
public static toPositionMessage(point: PointInterface): PositionMessage { public static toPositionMessage(point: PointInterface): PositionMessage {
let direction: Direction; let direction: Direction;
switch (point.direction) { switch (point.direction) {
case 'up': case "up":
direction = Direction.UP; direction = Direction.UP;
break; break;
case 'down': case "down":
direction = Direction.DOWN; direction = Direction.DOWN;
break; break;
case 'left': case "left":
direction = Direction.LEFT; direction = Direction.LEFT;
break; break;
case 'right': case "right":
direction = Direction.RIGHT; direction = Direction.RIGHT;
break; break;
default: default:
throw new Error('unexpected direction'); throw new Error("unexpected direction");
} }
const position = new PositionMessage(); const position = new PositionMessage();
@ -44,16 +43,16 @@ export class ProtobufUtils {
let direction: string; let direction: string;
switch (position.getDirection()) { switch (position.getDirection()) {
case Direction.UP: case Direction.UP:
direction = 'up'; direction = "up";
break; break;
case Direction.DOWN: case Direction.DOWN:
direction = 'down'; direction = "down";
break; break;
case Direction.LEFT: case Direction.LEFT:
direction = 'left'; direction = "left";
break; break;
case Direction.RIGHT: case Direction.RIGHT:
direction = 'right'; direction = "right";
break; break;
default: default:
throw new Error("Unexpected direction"); throw new Error("Unexpected direction");
@ -82,7 +81,7 @@ export class ProtobufUtils {
event: itemEventMessage.getEvent(), event: itemEventMessage.getEvent(),
parameters: JSON.parse(itemEventMessage.getParametersjson()), parameters: JSON.parse(itemEventMessage.getParametersjson()),
state: JSON.parse(itemEventMessage.getStatejson()), state: JSON.parse(itemEventMessage.getStatejson()),
} };
} }
public static toItemEventProtobuf(itemEvent: ItemEventMessageInterface): ItemEventMessage { public static toItemEventProtobuf(itemEvent: ItemEventMessageInterface): ItemEventMessage {

View File

@ -1,10 +1,11 @@
import * as tg from "generic-type-guard"; import * as tg from "generic-type-guard";
export const isViewport = export const isViewport = new tg.IsInterface()
new tg.IsInterface().withProperties({ .withProperties({
left: tg.isNumber, left: tg.isNumber,
top: tg.isNumber, top: tg.isNumber,
right: tg.isNumber, right: tg.isNumber,
bottom: tg.isNumber, bottom: tg.isNumber,
}).get(); })
.get();
export type ViewportInterface = tg.GuardedType<typeof isViewport>; export type ViewportInterface = tg.GuardedType<typeof isViewport>;

View File

@ -2,12 +2,19 @@ import {ExSocketInterface} from "./Websocket/ExSocketInterface";
import { apiClientRepository } from "../Services/ApiClientRepository"; import { apiClientRepository } from "../Services/ApiClientRepository";
import { import {
BatchToPusherMessage, BatchToPusherMessage,
CharacterLayerMessage, GroupLeftZoneMessage, GroupUpdateMessage, GroupUpdateZoneMessage, CharacterLayerMessage,
PointMessage, PositionMessage, UserJoinedMessage, GroupLeftZoneMessage,
UserJoinedZoneMessage, UserLeftZoneMessage, UserMovedMessage, GroupUpdateMessage,
GroupUpdateZoneMessage,
PointMessage,
PositionMessage,
UserJoinedMessage,
UserJoinedZoneMessage,
UserLeftZoneMessage,
UserMovedMessage,
ZoneMessage, ZoneMessage,
EmoteEventMessage, EmoteEventMessage,
CompanionMessage CompanionMessage,
} from "../Messages/generated/messages_pb"; } from "../Messages/generated/messages_pb";
import { ClientReadableStream } from "grpc"; import { ClientReadableStream } from "grpc";
import { PositionDispatcher } from "_Model/PositionDispatcher"; import { PositionDispatcher } from "_Model/PositionDispatcher";
@ -30,24 +37,38 @@ export type MovesCallback = (thing: Movable, position: PositionInterface, listen
export type LeavesCallback = (thing: Movable, listener: User) => void;*/ export type LeavesCallback = (thing: Movable, listener: User) => void;*/
export class UserDescriptor { export class UserDescriptor {
private constructor(public readonly userId: number, private name: string, private characterLayers: CharacterLayerMessage[], private position: PositionMessage, private visitCardUrl: string | null, private companion?: CompanionMessage) { private constructor(
public readonly userId: number,
private name: string,
private characterLayers: CharacterLayerMessage[],
private position: PositionMessage,
private visitCardUrl: string | null,
private companion?: CompanionMessage
) {
if (!Number.isInteger(this.userId)) { if (!Number.isInteger(this.userId)) {
throw new Error('UserDescriptor.userId is not an integer: '+this.userId); throw new Error("UserDescriptor.userId is not an integer: " + this.userId);
} }
} }
public static createFromUserJoinedZoneMessage(message: UserJoinedZoneMessage): UserDescriptor { public static createFromUserJoinedZoneMessage(message: UserJoinedZoneMessage): UserDescriptor {
const position = message.getPosition(); const position = message.getPosition();
if (position === undefined) { if (position === undefined) {
throw new Error('Missing position'); throw new Error("Missing position");
} }
return new UserDescriptor(message.getUserid(), message.getName(), message.getCharacterlayersList(), position, message.getVisitcardurl(), message.getCompanion()); return new UserDescriptor(
message.getUserid(),
message.getName(),
message.getCharacterlayersList(),
position,
message.getVisitcardurl(),
message.getCompanion()
);
} }
public update(userMovedMessage: UserMovedMessage) { public update(userMovedMessage: UserMovedMessage) {
const position = userMovedMessage.getPosition(); const position = userMovedMessage.getPosition();
if (position === undefined) { if (position === undefined) {
throw new Error('Missing position'); throw new Error("Missing position");
} }
this.position = position; this.position = position;
} }
@ -78,13 +99,12 @@ export class UserDescriptor {
} }
export class GroupDescriptor { export class GroupDescriptor {
private constructor(public readonly groupId: number, private groupSize: number, private position: PointMessage) { private constructor(public readonly groupId: number, private groupSize: number, private position: PointMessage) {}
}
public static createFromGroupUpdateZoneMessage(message: GroupUpdateZoneMessage): GroupDescriptor { public static createFromGroupUpdateZoneMessage(message: GroupUpdateZoneMessage): GroupDescriptor {
const position = message.getPosition(); const position = message.getPosition();
if (position === undefined) { if (position === undefined) {
throw new Error('Missing position'); throw new Error("Missing position");
} }
return new GroupDescriptor(message.getGroupid(), message.getGroupsize(), position); return new GroupDescriptor(message.getGroupid(), message.getGroupsize(), position);
} }
@ -97,7 +117,7 @@ export class GroupDescriptor {
public toGroupUpdateMessage(): GroupUpdateMessage { public toGroupUpdateMessage(): GroupUpdateMessage {
const groupUpdateMessage = new GroupUpdateMessage(); const groupUpdateMessage = new GroupUpdateMessage();
if (!Number.isInteger(this.groupId)) { if (!Number.isInteger(this.groupId)) {
throw new Error('GroupDescriptor.groupId is not an integer: '+this.groupId); throw new Error("GroupDescriptor.groupId is not an integer: " + this.groupId);
} }
groupUpdateMessage.setGroupid(this.groupId); groupUpdateMessage.setGroupid(this.groupId);
groupUpdateMessage.setGroupsize(this.groupSize); groupUpdateMessage.setGroupsize(this.groupSize);
@ -108,8 +128,8 @@ export class GroupDescriptor {
} }
interface ZoneDescriptor { interface ZoneDescriptor {
x: number, x: number;
y: number y: number;
} }
export class Zone { export class Zone {
@ -120,21 +140,26 @@ export class Zone {
private backConnection!: ClientReadableStream<BatchToPusherMessage>; private backConnection!: ClientReadableStream<BatchToPusherMessage>;
private isClosing: boolean = false; private isClosing: boolean = false;
constructor(private positionDispatcher: PositionDispatcher, private socketListener: ZoneEventListener, public readonly x: number, public readonly y: number, private onBackFailure: (e: Error|null, zone: Zone) => void) { constructor(
} private positionDispatcher: PositionDispatcher,
private socketListener: ZoneEventListener,
public readonly x: number,
public readonly y: number,
private onBackFailure: (e: Error | null, zone: Zone) => void
) {}
/** /**
* Creates a connection to the back server to track the users. * Creates a connection to the back server to track the users.
*/ */
public async init(): Promise<void> { public async init(): Promise<void> {
debug('Opening connection to zone %d, %d on back server', this.x, this.y); debug("Opening connection to zone %d, %d on back server", this.x, this.y);
const apiClient = await apiClientRepository.getClient(this.positionDispatcher.roomId); const apiClient = await apiClientRepository.getClient(this.positionDispatcher.roomId);
const zoneMessage = new ZoneMessage(); const zoneMessage = new ZoneMessage();
zoneMessage.setRoomid(this.positionDispatcher.roomId); zoneMessage.setRoomid(this.positionDispatcher.roomId);
zoneMessage.setX(this.x); zoneMessage.setX(this.x);
zoneMessage.setY(this.y); zoneMessage.setY(this.y);
this.backConnection = apiClient.listenZone(zoneMessage); this.backConnection = apiClient.listenZone(zoneMessage);
this.backConnection.on('data', (batch: BatchToPusherMessage) => { this.backConnection.on("data", (batch: BatchToPusherMessage) => {
for (const message of batch.getPayloadList()) { for (const message of batch.getPayloadList()) {
if (message.hasUserjoinedzonemessage()) { if (message.hasUserjoinedzonemessage()) {
const userJoinedZoneMessage = message.getUserjoinedzonemessage() as UserJoinedZoneMessage; const userJoinedZoneMessage = message.getUserjoinedzonemessage() as UserJoinedZoneMessage;
@ -190,22 +215,21 @@ export class Zone {
const emoteEventMessage = message.getEmoteeventmessage() as EmoteEventMessage; const emoteEventMessage = message.getEmoteeventmessage() as EmoteEventMessage;
this.notifyEmote(emoteEventMessage); this.notifyEmote(emoteEventMessage);
} else { } else {
throw new Error('Unexpected message'); throw new Error("Unexpected message");
} }
} }
}); });
this.backConnection.on('error', (e) => { this.backConnection.on("error", (e) => {
if (!this.isClosing) { if (!this.isClosing) {
debug('Error on back connection') debug("Error on back connection");
this.close(); this.close();
this.onBackFailure(e, this); this.onBackFailure(e, this);
} }
}); });
this.backConnection.on('close', () => { this.backConnection.on("close", () => {
if (!this.isClosing) { if (!this.isClosing) {
debug('Close on back connection') debug("Close on back connection");
this.close(); this.close();
this.onBackFailure(null, this); this.onBackFailure(null, this);
} }
@ -213,7 +237,7 @@ export class Zone {
} }
public close(): void { public close(): void {
debug('Closing connection to zone %d, %d on back server', this.x, this.y); debug("Closing connection to zone %d, %d on back server", this.x, this.y);
this.isClosing = true; this.isClosing = true;
this.backConnection.cancel(); this.backConnection.cancel();
} }

View File

@ -1,7 +1,7 @@
import { App as _App, AppOptions } from 'uWebSockets.js'; import { App as _App, AppOptions } from "uWebSockets.js";
import BaseApp from './baseapp'; import BaseApp from "./baseapp";
import { extend } from './utils'; import { extend } from "./utils";
import { UwsApp } from './types'; import { UwsApp } from "./types";
class App extends (<UwsApp>_App) { class App extends (<UwsApp>_App) {
constructor(options: AppOptions = {}) { constructor(options: AppOptions = {}) {

View File

@ -1,16 +1,16 @@
import { Readable } from 'stream'; import { Readable } from "stream";
import { us_listen_socket_close, TemplatedApp, HttpResponse, HttpRequest } from 'uWebSockets.js'; import { us_listen_socket_close, TemplatedApp, HttpResponse, HttpRequest } from "uWebSockets.js";
import formData from './formdata'; import formData from "./formdata";
import { stob } from './utils'; import { stob } from "./utils";
import { Handler } from './types'; import { Handler } from "./types";
import { join } from "path"; import { join } from "path";
const contTypes = ['application/x-www-form-urlencoded', 'multipart/form-data']; const contTypes = ["application/x-www-form-urlencoded", "multipart/form-data"];
const noOp = () => true; const noOp = () => true;
const handleBody = (res: HttpResponse, req: HttpRequest) => { const handleBody = (res: HttpResponse, req: HttpRequest) => {
const contType = req.getHeader('content-type'); const contType = req.getHeader("content-type");
res.bodyStream = function () { res.bodyStream = function () {
const stream = new Readable(); const stream = new Readable();
@ -29,24 +29,21 @@ const handleBody = (res: HttpResponse, req: HttpRequest) => {
res.body = () => stob(res.bodyStream()); res.body = () => stob(res.bodyStream());
if (contType.includes('application/json')) if (contType.includes("application/json")) res.json = async () => JSON.parse(await res.body());
res.json = async () => JSON.parse(await res.body()); if (contTypes.map((t) => contType.includes(t)).includes(true)) res.formData = formData.bind(res, contType);
if (contTypes.map(t => contType.includes(t)).includes(true))
res.formData = formData.bind(res, contType);
}; };
class BaseApp { class BaseApp {
_sockets = new Map(); _sockets = new Map();
ws!: TemplatedApp['ws']; ws!: TemplatedApp["ws"];
get!: TemplatedApp['get']; get!: TemplatedApp["get"];
_post!: TemplatedApp['post']; _post!: TemplatedApp["post"];
_put!: TemplatedApp['put']; _put!: TemplatedApp["put"];
_patch!: TemplatedApp['patch']; _patch!: TemplatedApp["patch"];
_listen!: TemplatedApp['listen']; _listen!: TemplatedApp["listen"];
post(pattern: string, handler: Handler) { post(pattern: string, handler: Handler) {
if (typeof handler !== 'function') if (typeof handler !== "function") throw Error(`handler should be a function, given ${typeof handler}.`);
throw Error(`handler should be a function, given ${typeof handler}.`);
this._post(pattern, (res, req) => { this._post(pattern, (res, req) => {
handleBody(res, req); handleBody(res, req);
handler(res, req); handler(res, req);
@ -55,8 +52,7 @@ class BaseApp {
} }
put(pattern: string, handler: Handler) { put(pattern: string, handler: Handler) {
if (typeof handler !== 'function') if (typeof handler !== "function") throw Error(`handler should be a function, given ${typeof handler}.`);
throw Error(`handler should be a function, given ${typeof handler}.`);
this._put(pattern, (res, req) => { this._put(pattern, (res, req) => {
handleBody(res, req); handleBody(res, req);
@ -66,8 +62,7 @@ class BaseApp {
} }
patch(pattern: string, handler: Handler) { patch(pattern: string, handler: Handler) {
if (typeof handler !== 'function') if (typeof handler !== "function") throw Error(`handler should be a function, given ${typeof handler}.`);
throw Error(`handler should be a function, given ${typeof handler}.`);
this._patch(pattern, (res, req) => { this._patch(pattern, (res, req) => {
handleBody(res, req); handleBody(res, req);
@ -77,23 +72,21 @@ class BaseApp {
} }
listen(h: string | number, p: Function | number = noOp, cb?: Function) { listen(h: string | number, p: Function | number = noOp, cb?: Function) {
if (typeof p === 'number' && typeof h === 'string') { if (typeof p === "number" && typeof h === "string") {
this._listen(h, p, socket => { this._listen(h, p, (socket) => {
this._sockets.set(p, socket); this._sockets.set(p, socket);
if (cb === undefined) { if (cb === undefined) {
throw new Error('cb undefined'); throw new Error("cb undefined");
} }
cb(socket); cb(socket);
}); });
} else if (typeof h === 'number' && typeof p === 'function') { } else if (typeof h === "number" && typeof p === "function") {
this._listen(h, socket => { this._listen(h, (socket) => {
this._sockets.set(h, socket); this._sockets.set(h, socket);
p(socket); p(socket);
}); });
} else { } else {
throw Error( throw Error("Argument types: (host: string, port: number, cb?: Function) | (port: number, cb?: Function)");
'Argument types: (host: string, port: number, cb?: Function) | (port: number, cb?: Function)'
);
} }
return this; return this;
@ -104,7 +97,7 @@ class BaseApp {
this._sockets.has(port) && us_listen_socket_close(this._sockets.get(port)); this._sockets.has(port) && us_listen_socket_close(this._sockets.get(port));
this._sockets.delete(port); this._sockets.delete(port);
} else { } else {
this._sockets.forEach(app => { this._sockets.forEach((app) => {
us_listen_socket_close(app); us_listen_socket_close(app);
}); });
this._sockets.clear(); this._sockets.clear();

View File

@ -1,7 +1,7 @@
import { createWriteStream } from 'fs'; import { createWriteStream } from "fs";
import { join, dirname } from 'path'; import { join, dirname } from "path";
import Busboy from 'busboy'; import Busboy from "busboy";
import mkdirp from 'mkdirp'; import mkdirp from "mkdirp";
function formData( function formData(
contType: string, contType: string,
@ -19,9 +19,9 @@ function formData(
filename?: (oldName: string) => string; filename?: (oldName: string) => string;
} = {} } = {}
) { ) {
console.log('Enter form data'); console.log("Enter form data");
options.headers = { options.headers = {
'content-type': contType "content-type": contType,
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -30,47 +30,46 @@ function formData(
this.bodyStream().pipe(busb); this.bodyStream().pipe(busb);
busb.on('limit', () => { busb.on("limit", () => {
if (options.abortOnLimit) { if (options.abortOnLimit) {
reject(Error('limit')); reject(Error("limit"));
} }
}); });
busb.on('file', function(fieldname, file, filename, encoding, mimetype) { busb.on("file", function (fieldname, file, filename, encoding, mimetype) {
const value: { filePath: string|undefined, filename: string, encoding:string, mimetype: string } = { const value: { filePath: string | undefined; filename: string; encoding: string; mimetype: string } = {
filename, filename,
encoding, encoding,
mimetype, mimetype,
filePath: undefined filePath: undefined,
}; };
if (typeof options.tmpDir === 'string') { if (typeof options.tmpDir === "string") {
if (typeof options.filename === 'function') filename = options.filename(filename); if (typeof options.filename === "function") filename = options.filename(filename);
const fileToSave = join(options.tmpDir, filename); const fileToSave = join(options.tmpDir, filename);
mkdirp(dirname(fileToSave)); mkdirp(dirname(fileToSave));
file.pipe(createWriteStream(fileToSave)); file.pipe(createWriteStream(fileToSave));
value.filePath = fileToSave; value.filePath = fileToSave;
} }
if (typeof options.onFile === 'function') { if (typeof options.onFile === "function") {
value.filePath = value.filePath = options.onFile(fieldname, file, filename, encoding, mimetype) || value.filePath;
options.onFile(fieldname, file, filename, encoding, mimetype) || value.filePath;
} }
setRetValue(ret, fieldname, value); setRetValue(ret, fieldname, value);
}); });
busb.on('field', function(fieldname, value) { busb.on("field", function (fieldname, value) {
if (typeof options.onField === 'function') options.onField(fieldname, value); if (typeof options.onField === "function") options.onField(fieldname, value);
setRetValue(ret, fieldname, value); setRetValue(ret, fieldname, value);
}); });
busb.on('finish', function() { busb.on("finish", function () {
resolve(ret); resolve(ret);
}); });
busb.on('error', reject); busb.on("error", reject);
}); });
} }
@ -79,7 +78,7 @@ function setRetValue(
fieldname: string, fieldname: string,
value: { filename: string; encoding: string; mimetype: string; filePath?: string } | any // eslint-disable-line @typescript-eslint/no-explicit-any value: { filename: string; encoding: string; mimetype: string; filePath?: string } | any // eslint-disable-line @typescript-eslint/no-explicit-any
) { ) {
if (fieldname.endsWith('[]')) { if (fieldname.endsWith("[]")) {
fieldname = fieldname.slice(0, fieldname.length - 2); fieldname = fieldname.slice(0, fieldname.length - 2);
if (Array.isArray(ret[fieldname])) { if (Array.isArray(ret[fieldname])) {
ret[fieldname].push(value); ret[fieldname].push(value);

View File

@ -1,7 +1,7 @@
import { SSLApp as _SSLApp, AppOptions } from 'uWebSockets.js'; import { SSLApp as _SSLApp, AppOptions } from "uWebSockets.js";
import BaseApp from './baseapp'; import BaseApp from "./baseapp";
import { extend } from './utils'; import { extend } from "./utils";
import { UwsApp } from './types'; import { UwsApp } from "./types";
class SSLApp extends (<UwsApp>_SSLApp) { class SSLApp extends (<UwsApp>_SSLApp) {
constructor(options: AppOptions) { constructor(options: AppOptions) {

View File

@ -1,4 +1,4 @@
import { AppOptions, TemplatedApp, HttpResponse, HttpRequest } from 'uWebSockets.js'; import { AppOptions, TemplatedApp, HttpResponse, HttpRequest } from "uWebSockets.js";
export type UwsApp = { export type UwsApp = {
(options: AppOptions): TemplatedApp; (options: AppOptions): TemplatedApp;

View File

@ -1,25 +1,24 @@
import { ReadStream } from 'fs'; import { ReadStream } from "fs";
function extend(who: any, from: any, overwrite = true) { // eslint-disable-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const ownProps = Object.getOwnPropertyNames(Object.getPrototypeOf(from)).concat( function extend(who: any, from: any, overwrite = true) {
Object.keys(from) const ownProps = Object.getOwnPropertyNames(Object.getPrototypeOf(from)).concat(Object.keys(from));
); ownProps.forEach((prop) => {
ownProps.forEach(prop => { if (prop === "constructor" || from[prop] === undefined) return;
if (prop === 'constructor' || from[prop] === undefined) return;
if (who[prop] && overwrite) { if (who[prop] && overwrite) {
who[`_${prop}`] = who[prop]; who[`_${prop}`] = who[prop];
} }
if (typeof from[prop] === 'function') who[prop] = from[prop].bind(who); if (typeof from[prop] === "function") who[prop] = from[prop].bind(who);
else who[prop] = from[prop]; else who[prop] = from[prop];
}); });
} }
function stob(stream: ReadStream): Promise<Buffer> { function stob(stream: ReadStream): Promise<Buffer> {
return new Promise(resolve => { return new Promise((resolve) => {
const buffers: Buffer[] = []; const buffers: Buffer[] = [];
stream.on('data', buffers.push.bind(buffers)); stream.on("data", buffers.push.bind(buffers));
stream.on('end', () => { stream.on("end", () => {
switch (buffers.length) { switch (buffers.length) {
case 0: case 0:
resolve(Buffer.allocUnsafe(0)); resolve(Buffer.allocUnsafe(0));

View File

@ -1,19 +1,19 @@
import { parse } from 'query-string'; import { parse } from "query-string";
import { HttpRequest } from 'uWebSockets.js'; import { HttpRequest } from "uWebSockets.js";
import App from './server/app'; import App from "./server/app";
import SSLApp from './server/sslapp'; import SSLApp from "./server/sslapp";
import * as types from './server/types'; import * as types from "./server/types";
const getQuery = (req: HttpRequest) => { const getQuery = (req: HttpRequest) => {
return parse(req.getQuery()); return parse(req.getQuery());
}; };
export { App, SSLApp, getQuery }; export { App, SSLApp, getQuery };
export * from './server/types'; export * from "./server/types";
export default { export default {
App, App,
SSLApp, SSLApp,
getQuery, getQuery,
...types ...types,
}; };

View File

@ -3,34 +3,34 @@ import Axios from "axios";
import { GameRoomPolicyTypes } from "_Model/PusherRoom"; import { GameRoomPolicyTypes } from "_Model/PusherRoom";
export interface AdminApiData { export interface AdminApiData {
organizationSlug: string organizationSlug: string;
worldSlug: string worldSlug: string;
roomSlug: string roomSlug: string;
mapUrlStart: string mapUrlStart: string;
tags: string[] tags: string[];
policy_type: number policy_type: number;
userUuid: string userUuid: string;
messages?: unknown[], messages?: unknown[];
textures: CharacterTexture[] textures: CharacterTexture[];
} }
export interface MapDetailsData { export interface MapDetailsData {
roomSlug: string, roomSlug: string;
mapUrl: string, mapUrl: string;
policy_type: GameRoomPolicyTypes, policy_type: GameRoomPolicyTypes;
tags: string[], tags: string[];
} }
export interface AdminBannedData { export interface AdminBannedData {
is_banned: boolean, is_banned: boolean;
message: string message: string;
} }
export interface CharacterTexture { export interface CharacterTexture {
id: number, id: number;
level: number, level: number;
url: string, url: string;
rights: string rights: string;
} }
export interface FetchMemberDataByUuidResponse { export interface FetchMemberDataByUuidResponse {
@ -43,81 +43,105 @@ export interface FetchMemberDataByUuidResponse {
} }
class AdminApi { class AdminApi {
async fetchMapDetails(
async fetchMapDetails(organizationSlug: string, worldSlug: string, roomSlug: string|undefined): Promise<MapDetailsData> { organizationSlug: string,
worldSlug: string,
roomSlug: string | undefined
): Promise<MapDetailsData> {
if (!ADMIN_API_URL) { if (!ADMIN_API_URL) {
return Promise.reject(new Error('No admin backoffice set!')); return Promise.reject(new Error("No admin backoffice set!"));
} }
const params: { organizationSlug: string, worldSlug: string, roomSlug?: string } = { const params: { organizationSlug: string; worldSlug: string; roomSlug?: string } = {
organizationSlug, organizationSlug,
worldSlug worldSlug,
}; };
if (roomSlug) { if (roomSlug) {
params.roomSlug = roomSlug; params.roomSlug = roomSlug;
} }
const res = await Axios.get(ADMIN_API_URL + '/api/map', const res = await Axios.get(ADMIN_API_URL + "/api/map", {
{ headers: { Authorization: `${ADMIN_API_TOKEN}` },
headers: {"Authorization": `${ADMIN_API_TOKEN}`}, params,
params });
}
)
return res.data; return res.data;
} }
async fetchMemberDataByUuid(uuid: string, roomId: string): Promise<FetchMemberDataByUuidResponse> { async fetchMemberDataByUuid(uuid: string, roomId: string): Promise<FetchMemberDataByUuidResponse> {
if (!ADMIN_API_URL) { if (!ADMIN_API_URL) {
return Promise.reject(new Error('No admin backoffice set!')); return Promise.reject(new Error("No admin backoffice set!"));
} }
const res = await Axios.get(ADMIN_API_URL+'/api/room/access', const res = await Axios.get(ADMIN_API_URL + "/api/room/access", {
{ params: {uuid, roomId}, headers: {"Authorization" : `${ADMIN_API_TOKEN}`} } params: { uuid, roomId },
) headers: { Authorization: `${ADMIN_API_TOKEN}` },
});
return res.data; return res.data;
} }
async fetchMemberDataByToken(organizationMemberToken: string): Promise<AdminApiData> { async fetchMemberDataByToken(organizationMemberToken: string): Promise<AdminApiData> {
if (!ADMIN_API_URL) { if (!ADMIN_API_URL) {
return Promise.reject(new Error('No admin backoffice set!')); return Promise.reject(new Error("No admin backoffice set!"));
} }
//todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case. //todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case.
const res = await Axios.get(ADMIN_API_URL+'/api/login-url/'+organizationMemberToken, const res = await Axios.get(ADMIN_API_URL + "/api/login-url/" + organizationMemberToken, {
{ headers: {"Authorization" : `${ADMIN_API_TOKEN}`} } headers: { Authorization: `${ADMIN_API_TOKEN}` },
) });
return res.data; return res.data;
} }
async fetchCheckUserByToken(organizationMemberToken: string): Promise<AdminApiData> { async fetchCheckUserByToken(organizationMemberToken: string): Promise<AdminApiData> {
if (!ADMIN_API_URL) { if (!ADMIN_API_URL) {
return Promise.reject(new Error('No admin backoffice set!')); return Promise.reject(new Error("No admin backoffice set!"));
} }
//todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case. //todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case.
const res = await Axios.get(ADMIN_API_URL+'/api/check-user/'+organizationMemberToken, const res = await Axios.get(ADMIN_API_URL + "/api/check-user/" + organizationMemberToken, {
{ headers: {"Authorization" : `${ADMIN_API_TOKEN}`} } headers: { Authorization: `${ADMIN_API_TOKEN}` },
) });
return res.data; return res.data;
} }
reportPlayer(reportedUserUuid: string, reportedUserComment: string, reporterUserUuid: string, reportWorldSlug: string) { reportPlayer(
return Axios.post(`${ADMIN_API_URL}/api/report`, { reportedUserUuid: string,
reportedUserComment: string,
reporterUserUuid: string,
reportWorldSlug: string
) {
return Axios.post(
`${ADMIN_API_URL}/api/report`,
{
reportedUserUuid, reportedUserUuid,
reportedUserComment, reportedUserComment,
reporterUserUuid, reporterUserUuid,
reportWorldSlug reportWorldSlug,
}, },
{ {
headers: {"Authorization": `${ADMIN_API_TOKEN}`} headers: { Authorization: `${ADMIN_API_TOKEN}` },
}); }
);
} }
async verifyBanUser(organizationMemberToken: string, ipAddress: string, organization: string, world: string): Promise<AdminBannedData> { async verifyBanUser(
organizationMemberToken: string,
ipAddress: string,
organization: string,
world: string
): Promise<AdminBannedData> {
if (!ADMIN_API_URL) { if (!ADMIN_API_URL) {
return Promise.reject(new Error('No admin backoffice set!')); return Promise.reject(new Error("No admin backoffice set!"));
} }
//todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case. //todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case.
return Axios.get(ADMIN_API_URL + '/api/check-moderate-user/'+organization+'/'+world+'?ipAddress='+ipAddress+'&token='+organizationMemberToken, return Axios.get(
{headers: {"Authorization": `${ADMIN_API_TOKEN}`}} ADMIN_API_URL +
"/api/check-moderate-user/" +
organization +
"/" +
world +
"?ipAddress=" +
ipAddress +
"&token=" +
organizationMemberToken,
{ headers: { Authorization: `${ADMIN_API_TOKEN}` } }
).then((data) => { ).then((data) => {
return data.data; return data.data;
}); });

View File

@ -2,13 +2,13 @@
* A class to get connections to the correct "api" server given a room name. * A class to get connections to the correct "api" server given a room name.
*/ */
import { RoomManagerClient } from "../Messages/generated/messages_grpc_pb"; import { RoomManagerClient } from "../Messages/generated/messages_grpc_pb";
import grpc from 'grpc'; import grpc from "grpc";
import crypto from 'crypto'; import crypto from "crypto";
import { API_URL } from "../Enum/EnvironmentVariable"; import { API_URL } from "../Enum/EnvironmentVariable";
import Debug from "debug"; import Debug from "debug";
const debug = Debug('apiClientRespository'); const debug = Debug("apiClientRespository");
class ApiClientRepository { class ApiClientRepository {
private roomManagerClients: RoomManagerClient[] = []; private roomManagerClients: RoomManagerClient[] = [];
@ -16,23 +16,26 @@ class ApiClientRepository {
public constructor(private apiUrls: string[]) {} public constructor(private apiUrls: string[]) {}
public async getClient(roomId: string): Promise<RoomManagerClient> { public async getClient(roomId: string): Promise<RoomManagerClient> {
const array = new Uint32Array(crypto.createHash('md5').update(roomId).digest()); const array = new Uint32Array(crypto.createHash("md5").update(roomId).digest());
const index = array[0] % this.apiUrls.length; const index = array[0] % this.apiUrls.length;
let client = this.roomManagerClients[index]; let client = this.roomManagerClients[index];
if (client === undefined) { if (client === undefined) {
this.roomManagerClients[index] = client = new RoomManagerClient(this.apiUrls[index], grpc.credentials.createInsecure()); this.roomManagerClients[index] = client = new RoomManagerClient(
debug('Mapping room %s to API server %s', roomId, this.apiUrls[index]) this.apiUrls[index],
grpc.credentials.createInsecure()
);
debug("Mapping room %s to API server %s", roomId, this.apiUrls[index]);
} }
return Promise.resolve(client); return Promise.resolve(client);
} }
public async getAllClients(): Promise<RoomManagerClient[]> { public async getAllClients(): Promise<RoomManagerClient[]> {
return [await this.getClient('')]; return [await this.getClient("")];
} }
} }
const apiClientRepository = new ApiClientRepository(API_URL.split(',')); const apiClientRepository = new ApiClientRepository(API_URL.split(","));
export { apiClientRepository }; export { apiClientRepository };

View File

@ -1,3 +1,3 @@
export const arrayIntersect = (array1: string[], array2: string[]): boolean => { export const arrayIntersect = (array1: string[], array2: string[]): boolean => {
return array1.filter(value => array2.includes(value)).length > 0; return array1.filter((value) => array2.includes(value)).length > 0;
} };

View File

@ -1,7 +1,7 @@
const EventEmitter = require('events'); const EventEmitter = require("events");
const clientJoinEvent = 'clientJoin'; const clientJoinEvent = "clientJoin";
const clientLeaveEvent = 'clientLeave'; const clientLeaveEvent = "clientLeave";
class ClientEventsEmitter extends EventEmitter { class ClientEventsEmitter extends EventEmitter {
emitClientJoin(clientUUid: string, roomId: string): void { emitClientJoin(clientUUid: string, roomId: string): void {

View File

@ -12,17 +12,17 @@ class CpuTracker {
private overHeating: boolean = false; private overHeating: boolean = false;
constructor() { constructor() {
let time = process.hrtime.bigint() let time = process.hrtime.bigint();
let usage = process.cpuUsage() let usage = process.cpuUsage();
setInterval(() => { setInterval(() => {
const elapTime = process.hrtime.bigint(); const elapTime = process.hrtime.bigint();
const elapUsage = process.cpuUsage(usage) const elapUsage = process.cpuUsage(usage);
usage = process.cpuUsage() usage = process.cpuUsage();
const elapTimeMS = elapTime - time; const elapTimeMS = elapTime - time;
const elapUserMS = secNSec2ms(elapUsage.user) const elapUserMS = secNSec2ms(elapUsage.user);
const elapSystMS = secNSec2ms(elapUsage.system) const elapSystMS = secNSec2ms(elapUsage.system);
this.cpuPercent = Math.round(100 * (elapUserMS + elapSystMS) / Number(elapTimeMS) * 1000000) this.cpuPercent = Math.round(((100 * (elapUserMS + elapSystMS)) / Number(elapTimeMS)) * 1000000);
time = elapTime; time = elapTime;

View File

@ -9,25 +9,25 @@ class GaugeManager {
constructor() { constructor() {
this.nbClientsGauge = new Gauge({ this.nbClientsGauge = new Gauge({
name: 'workadventure_nb_sockets', name: "workadventure_nb_sockets",
help: 'Number of connected sockets', help: "Number of connected sockets",
labelNames: [ ] labelNames: [],
}); });
this.nbClientsPerRoomGauge = new Gauge({ this.nbClientsPerRoomGauge = new Gauge({
name: 'workadventure_nb_clients_per_room', name: "workadventure_nb_clients_per_room",
help: 'Number of clients per room', help: "Number of clients per room",
labelNames: [ 'room' ] labelNames: ["room"],
}); });
this.nbGroupsPerRoomCounter = new Counter({ this.nbGroupsPerRoomCounter = new Counter({
name: 'workadventure_counter_groups_per_room', name: "workadventure_counter_groups_per_room",
help: 'Counter of groups per room', help: "Counter of groups per room",
labelNames: [ 'room' ] labelNames: ["room"],
}); });
this.nbGroupsPerRoomGauge = new Gauge({ this.nbGroupsPerRoomGauge = new Gauge({
name: 'workadventure_nb_groups_per_room', name: "workadventure_nb_groups_per_room",
help: 'Number of groups per room', help: "Number of groups per room",
labelNames: [ 'room' ] labelNames: ["room"],
}); });
} }
@ -42,12 +42,12 @@ class GaugeManager {
} }
incNbGroupsPerRoomGauge(roomId: string): void { incNbGroupsPerRoomGauge(roomId: string): void {
this.nbGroupsPerRoomCounter.inc({ room: roomId }) this.nbGroupsPerRoomCounter.inc({ room: roomId });
this.nbGroupsPerRoomGauge.inc({ room: roomId }) this.nbGroupsPerRoomGauge.inc({ room: roomId });
} }
decNbGroupsPerRoomGauge(roomId: string): void { decNbGroupsPerRoomGauge(roomId: string): void {
this.nbGroupsPerRoomGauge.dec({ room: roomId }) this.nbGroupsPerRoomGauge.dec({ room: roomId });
} }
} }

View File

@ -33,4 +33,3 @@ export function emitError(Client: WebSocket, message: string): void {
} }
console.warn(message); console.warn(message);
} }

View File

@ -5,26 +5,25 @@ import {TokenInterface} from "../Controller/AuthenticateController";
import { adminApi, AdminBannedData } from "../Services/AdminApi"; import { adminApi, AdminBannedData } from "../Services/AdminApi";
class JWTTokenManager { class JWTTokenManager {
public createJWTToken(userUuid: string) { public createJWTToken(userUuid: string) {
return Jwt.sign({userUuid: userUuid}, SECRET_KEY, {expiresIn: '200d'}); //todo: add a mechanic to refresh or recreate token return Jwt.sign({ userUuid: userUuid }, SECRET_KEY, { expiresIn: "200d" }); //todo: add a mechanic to refresh or recreate token
} }
public async getUserUuidFromToken(token: unknown, ipAddress?: string, room?: string): Promise<string> { public async getUserUuidFromToken(token: unknown, ipAddress?: string, room?: string): Promise<string> {
if (!token) { if (!token) {
throw new Error('An authentication error happened, a user tried to connect without a token.'); throw new Error("An authentication error happened, a user tried to connect without a token.");
} }
if (typeof(token) !== "string") { if (typeof token !== "string") {
throw new Error('Token is expected to be a string'); throw new Error("Token is expected to be a string");
} }
if (token === "test") {
if(token === 'test') {
if (ALLOW_ARTILLERY) { if (ALLOW_ARTILLERY) {
return uuid(); return uuid();
} else { } else {
throw new Error("In order to perform a load-testing test on this environment, you must set the ALLOW_ARTILLERY environment variable to 'true'"); throw new Error(
"In order to perform a load-testing test on this environment, you must set the ALLOW_ARTILLERY environment variable to 'true'"
);
} }
} }
@ -32,19 +31,19 @@ class JWTTokenManager {
Jwt.verify(token, SECRET_KEY, {}, (err, tokenDecoded) => { Jwt.verify(token, SECRET_KEY, {}, (err, tokenDecoded) => {
const tokenInterface = tokenDecoded as TokenInterface; const tokenInterface = tokenDecoded as TokenInterface;
if (err) { if (err) {
console.error('An authentication error happened, invalid JsonWebToken.', err); console.error("An authentication error happened, invalid JsonWebToken.", err);
reject(new Error('An authentication error happened, invalid JsonWebToken. ' + err.message)); reject(new Error("An authentication error happened, invalid JsonWebToken. " + err.message));
return; return;
} }
if (tokenDecoded === undefined) { if (tokenDecoded === undefined) {
console.error('Empty token found.'); console.error("Empty token found.");
reject(new Error('Empty token found.')); reject(new Error("Empty token found."));
return; return;
} }
//verify token //verify token
if (!this.isValidToken(tokenInterface)) { if (!this.isValidToken(tokenInterface)) {
reject(new Error('Authentication error, invalid token structure.')); reject(new Error("Authentication error, invalid token structure."));
return; return;
} }
@ -54,10 +53,14 @@ class JWTTokenManager {
if (ipAddress && room) { if (ipAddress && room) {
promise = this.verifyBanUser(tokenInterface.userUuid, ipAddress, room); promise = this.verifyBanUser(tokenInterface.userUuid, ipAddress, room);
} }
promise.then(() => { promise
adminApi.fetchCheckUserByToken(tokenInterface.userUuid).then(() => { .then(() => {
adminApi
.fetchCheckUserByToken(tokenInterface.userUuid)
.then(() => {
resolve(tokenInterface.userUuid); resolve(tokenInterface.userUuid);
}).catch((err) => { })
.catch((err) => {
//anonymous user //anonymous user
if (err.response && err.response.status && err.response.status === 404) { if (err.response && err.response.status && err.response.status === 404) {
resolve(tokenInterface.userUuid); resolve(tokenInterface.userUuid);
@ -65,7 +68,8 @@ class JWTTokenManager {
} }
reject(err); reject(err);
}); });
}).catch((err) => { })
.catch((err) => {
reject(err); reject(err);
}); });
} else { } else {
@ -76,30 +80,32 @@ class JWTTokenManager {
} }
private verifyBanUser(userUuid: string, ipAddress: string, room: string): Promise<AdminBannedData> { private verifyBanUser(userUuid: string, ipAddress: string, room: string): Promise<AdminBannedData> {
const parts = room.split('/'); const parts = room.split("/");
if (parts.length < 3 || parts[0] !== '@') { if (parts.length < 3 || parts[0] !== "@") {
return Promise.resolve({ return Promise.resolve({
is_banned: false, is_banned: false,
message: '' message: "",
}); });
} }
const organization = parts[1]; const organization = parts[1];
const world = parts[2]; const world = parts[2];
return adminApi.verifyBanUser(userUuid, ipAddress, organization, world).then((data: AdminBannedData) => { return adminApi
.verifyBanUser(userUuid, ipAddress, organization, world)
.then((data: AdminBannedData) => {
if (data && data.is_banned) { if (data && data.is_banned) {
throw new Error('User was banned'); throw new Error("User was banned");
} }
return data; return data;
}).catch((err) => { })
.catch((err) => {
throw err; throw err;
}); });
} }
private isValidToken(token: object): token is TokenInterface { private isValidToken(token: object): token is TokenInterface {
return !(typeof((token as TokenInterface).userUuid) !== 'string'); return !(typeof (token as TokenInterface).userUuid !== "string");
} }
} }
export const jwtTokenManager = new JWTTokenManager(); export const jwtTokenManager = new JWTTokenManager();

View File

@ -45,7 +45,7 @@ import Debug from "debug";
import { ExAdminSocketInterface } from "_Model/Websocket/ExAdminSocketInterface"; import { ExAdminSocketInterface } from "_Model/Websocket/ExAdminSocketInterface";
import { WebSocket } from "uWebSockets.js"; import { WebSocket } from "uWebSockets.js";
const debug = Debug('socket'); const debug = Debug("socket");
interface AdminSocketRoomsList { interface AdminSocketRoomsList {
[index: string]: number; [index: string]: number;
@ -55,12 +55,11 @@ interface AdminSocketUsersList {
} }
export interface AdminSocketData { export interface AdminSocketData {
rooms: AdminSocketRoomsList, rooms: AdminSocketRoomsList;
users: AdminSocketUsersList, users: AdminSocketUsersList;
} }
export class SocketManager implements ZoneEventListener { export class SocketManager implements ZoneEventListener {
private rooms: Map<string, PusherRoom> = new Map<string, PusherRoom>(); private rooms: Map<string, PusherRoom> = new Map<string, PusherRoom>();
private sockets: Map<number, ExSocketInterface> = new Map<number, ExSocketInterface>(); private sockets: Map<number, ExSocketInterface> = new Map<number, ExSocketInterface>();
@ -78,45 +77,51 @@ export class SocketManager implements ZoneEventListener {
const adminRoomStream = apiClient.adminRoom(); const adminRoomStream = apiClient.adminRoom();
client.adminConnection = adminRoomStream; client.adminConnection = adminRoomStream;
adminRoomStream.on('data', (message: ServerToAdminClientMessage) => { adminRoomStream
.on("data", (message: ServerToAdminClientMessage) => {
if (message.hasUserjoinedroom()) { if (message.hasUserjoinedroom()) {
const userJoinedRoomMessage = message.getUserjoinedroom() as UserJoinedRoomMessage; const userJoinedRoomMessage = message.getUserjoinedroom() as UserJoinedRoomMessage;
if (!client.disconnecting) { if (!client.disconnecting) {
client.send(JSON.stringify({ client.send(
type: 'MemberJoin', JSON.stringify({
type: "MemberJoin",
data: { data: {
uuid: userJoinedRoomMessage.getUuid(), uuid: userJoinedRoomMessage.getUuid(),
name: userJoinedRoomMessage.getName(), name: userJoinedRoomMessage.getName(),
ipAddress: userJoinedRoomMessage.getIpaddress(), ipAddress: userJoinedRoomMessage.getIpaddress(),
roomId: roomId, roomId: roomId,
} },
})); })
);
} }
} else if (message.hasUserleftroom()) { } else if (message.hasUserleftroom()) {
const userLeftRoomMessage = message.getUserleftroom() as UserLeftRoomMessage; const userLeftRoomMessage = message.getUserleftroom() as UserLeftRoomMessage;
if (!client.disconnecting) { if (!client.disconnecting) {
client.send(JSON.stringify({ client.send(
type: 'MemberLeave', JSON.stringify({
type: "MemberLeave",
data: { data: {
uuid: userLeftRoomMessage.getUuid() uuid: userLeftRoomMessage.getUuid(),
} },
})); })
);
} }
} else { } else {
throw new Error('Unexpected admin message'); throw new Error("Unexpected admin message");
} }
}).on('end', () => { })
console.warn('Admin connection lost to back server'); .on("end", () => {
console.warn("Admin connection lost to back server");
// Let's close the front connection if the back connection is closed. This way, we can retry connecting from the start. // Let's close the front connection if the back connection is closed. This way, we can retry connecting from the start.
if (!client.disconnecting) { if (!client.disconnecting) {
this.closeWebsocketConnection(client, 1011, 'Connection lost to back server'); this.closeWebsocketConnection(client, 1011, "Connection lost to back server");
} }
console.log('A user left'); console.log("A user left");
}).on('error', (err: Error) => { })
console.error('Error in connection to back server:', err); .on("error", (err: Error) => {
console.error("Error in connection to back server:", err);
if (!client.disconnecting) { if (!client.disconnecting) {
this.closeWebsocketConnection(client, 1011, 'Error while connecting to back server'); this.closeWebsocketConnection(client, 1011, "Error while connecting to back server");
} }
}); });
@ -133,7 +138,7 @@ export class SocketManager implements ZoneEventListener {
} }
getAdminSocketDataFor(roomId: string): AdminSocketData { getAdminSocketDataFor(roomId: string): AdminSocketData {
throw new Error('Not reimplemented yet'); throw new Error("Not reimplemented yet");
/*const data:AdminSocketData = { /*const data:AdminSocketData = {
rooms: {}, rooms: {},
users: {}, users: {},
@ -153,7 +158,6 @@ export class SocketManager implements ZoneEventListener {
async handleJoinRoom(client: ExSocketInterface): Promise<void> { async handleJoinRoom(client: ExSocketInterface): Promise<void> {
const viewport = client.viewport; const viewport = client.viewport;
try { try {
const joinRoomMessage = new JoinRoomMessage(); const joinRoomMessage = new JoinRoomMessage();
joinRoomMessage.setUseruuid(client.userUuid); joinRoomMessage.setUseruuid(client.userUuid);
joinRoomMessage.setIpaddress(client.IPAddress); joinRoomMessage.setIpaddress(client.IPAddress);
@ -176,15 +180,15 @@ export class SocketManager implements ZoneEventListener {
joinRoomMessage.addCharacterlayer(characterLayerMessage); joinRoomMessage.addCharacterlayer(characterLayerMessage);
} }
console.log("Calling joinRoom");
console.log('Calling joinRoom')
const apiClient = await apiClientRepository.getClient(client.roomId); const apiClient = await apiClientRepository.getClient(client.roomId);
const streamToPusher = apiClient.joinRoom(); const streamToPusher = apiClient.joinRoom();
clientEventsEmitter.emitClientJoin(client.userUuid, client.roomId); clientEventsEmitter.emitClientJoin(client.userUuid, client.roomId);
client.backConnection = streamToPusher; client.backConnection = streamToPusher;
streamToPusher.on('data', (message: ServerToClientMessage) => { streamToPusher
.on("data", (message: ServerToClientMessage) => {
if (message.hasRoomjoinedmessage()) { if (message.hasRoomjoinedmessage()) {
client.userId = (message.getRoomjoinedmessage() as RoomJoinedMessage).getCurrentuserid(); client.userId = (message.getRoomjoinedmessage() as RoomJoinedMessage).getCurrentuserid();
// TODO: do we need this.sockets anymore? // TODO: do we need this.sockets anymore?
@ -195,25 +199,28 @@ export class SocketManager implements ZoneEventListener {
} }
if (message.hasRefreshroommessage()) { if (message.hasRefreshroommessage()) {
const refreshMessage:RefreshRoomMessage = message.getRefreshroommessage() as unknown as RefreshRoomMessage; const refreshMessage: RefreshRoomMessage =
this.refreshRoomData(refreshMessage.getRoomid(), refreshMessage.getVersionnumber()) message.getRefreshroommessage() as unknown as RefreshRoomMessage;
this.refreshRoomData(refreshMessage.getRoomid(), refreshMessage.getVersionnumber());
} }
// Let's pass data over from the back to the client. // Let's pass data over from the back to the client.
if (!client.disconnecting) { if (!client.disconnecting) {
client.send(message.serializeBinary().buffer, true); client.send(message.serializeBinary().buffer, true);
} }
}).on('end', () => { })
console.warn('Connection lost to back server'); .on("end", () => {
console.warn("Connection lost to back server");
// Let's close the front connection if the back connection is closed. This way, we can retry connecting from the start. // Let's close the front connection if the back connection is closed. This way, we can retry connecting from the start.
if (!client.disconnecting) { if (!client.disconnecting) {
this.closeWebsocketConnection(client, 1011, 'Connection lost to back server'); this.closeWebsocketConnection(client, 1011, "Connection lost to back server");
} }
console.log('A user left'); console.log("A user left");
}).on('error', (err: Error) => { })
console.error('Error in connection to back server:', err); .on("error", (err: Error) => {
console.error("Error in connection to back server:", err);
if (!client.disconnecting) { if (!client.disconnecting) {
this.closeWebsocketConnection(client, 1011, 'Error while connecting to back server'); this.closeWebsocketConnection(client, 1011, "Error while connecting to back server");
} }
}); });
@ -257,15 +264,13 @@ export class SocketManager implements ZoneEventListener {
const viewport = userMovesMessage.getViewport(); const viewport = userMovesMessage.getViewport();
if (viewport === undefined) { if (viewport === undefined) {
throw new Error('Missing viewport in UserMovesMessage'); throw new Error("Missing viewport in UserMovesMessage");
} }
// Now, we need to listen to the correct viewport. // Now, we need to listen to the correct viewport.
this.handleViewport(client, viewport.toObject()) this.handleViewport(client, viewport.toObject());
} }
onEmote(emoteMessage: EmoteEventMessage, listener: ExSocketInterface): void { onEmote(emoteMessage: EmoteEventMessage, listener: ExSocketInterface): void {
const subMessage = new SubMessage(); const subMessage = new SubMessage();
subMessage.setEmoteeventmessage(emoteMessage); subMessage.setEmoteeventmessage(emoteMessage);
@ -299,11 +304,16 @@ export class SocketManager implements ZoneEventListener {
try { try {
const reportedSocket = this.sockets.get(reportPlayerMessage.getReporteduserid()); const reportedSocket = this.sockets.get(reportPlayerMessage.getReporteduserid());
if (!reportedSocket) { if (!reportedSocket) {
throw 'reported socket user not found'; throw "reported socket user not found";
} }
//TODO report user on admin application //TODO report user on admin application
//todo: move to back because this fail if the reported player is in another pusher. //todo: move to back because this fail if the reported player is in another pusher.
await adminApi.reportPlayer(reportedSocket.userUuid, reportPlayerMessage.getReportcomment(), client.userUuid, client.roomId.split('/')[2]) await adminApi.reportPlayer(
reportedSocket.userUuid,
reportPlayerMessage.getReportcomment(),
client.userUuid,
client.roomId.split("/")[2]
);
} catch (e) { } catch (e) {
console.error('An error occurred on "handleReportMessage"'); console.error('An error occurred on "handleReportMessage"');
console.error(e); console.error(e);
@ -340,15 +350,15 @@ export class SocketManager implements ZoneEventListener {
//user leaves room //user leaves room
const room: PusherRoom | undefined = this.rooms.get(socket.roomId); const room: PusherRoom | undefined = this.rooms.get(socket.roomId);
if (room) { if (room) {
debug('Leaving room %s.', socket.roomId); debug("Leaving room %s.", socket.roomId);
room.leave(socket); room.leave(socket);
if (room.isEmpty()) { if (room.isEmpty()) {
this.rooms.delete(socket.roomId); this.rooms.delete(socket.roomId);
debug('Room %s is empty. Deleting.', socket.roomId); debug("Room %s is empty. Deleting.", socket.roomId);
} }
} else { } else {
console.error('Could not find the GameRoom the user is leaving!'); console.error("Could not find the GameRoom the user is leaving!");
} }
//user leave previous room //user leave previous room
//Client.leave(Client.roomId); //Client.leave(Client.roomId);
@ -356,7 +366,7 @@ export class SocketManager implements ZoneEventListener {
//delete Client.roomId; //delete Client.roomId;
this.sockets.delete(socket.userId); this.sockets.delete(socket.userId);
clientEventsEmitter.emitClientLeave(socket.userUuid, socket.roomId); clientEventsEmitter.emitClientLeave(socket.userUuid, socket.roomId);
console.log('A user left (', this.sockets.size, ' connected users)'); console.log("A user left (", this.sockets.size, " connected users)");
} }
} }
} finally { } finally {
@ -368,7 +378,7 @@ export class SocketManager implements ZoneEventListener {
async getOrCreateRoom(roomId: string): Promise<PusherRoom> { async getOrCreateRoom(roomId: string): Promise<PusherRoom> {
//check and create new world for a room //check and create new world for a room
let world = this.rooms.get(roomId) let world = this.rooms.get(roomId);
if (world === undefined) { if (world === undefined) {
world = new PusherRoom(roomId, this); world = new PusherRoom(roomId, this);
if (!world.public) { if (!world.public) {
@ -376,19 +386,19 @@ export class SocketManager implements ZoneEventListener {
} }
this.rooms.set(roomId, world); this.rooms.set(roomId, world);
} }
return Promise.resolve(world) return Promise.resolve(world);
} }
public async updateRoomWithAdminData(world: PusherRoom): Promise<void> { public async updateRoomWithAdminData(world: PusherRoom): Promise<void> {
const data = await adminApi.fetchMapDetails(world.organizationSlug, world.worldSlug, world.roomSlug) const data = await adminApi.fetchMapDetails(world.organizationSlug, world.worldSlug, world.roomSlug);
world.tags = data.tags; world.tags = data.tags;
world.policyType = Number(data.policy_type); world.policyType = Number(data.policy_type);
} }
emitPlayGlobalMessage(client: ExSocketInterface, playglobalmessage: PlayGlobalMessage) { emitPlayGlobalMessage(client: ExSocketInterface, playglobalmessage: PlayGlobalMessage) {
if (!client.tags.includes('admin')) { if (!client.tags.includes("admin")) {
//In case of xss injection, we just kill the connection. //In case of xss injection, we just kill the connection.
throw 'Client is not an admin!'; throw "Client is not an admin!";
} }
const pusherToBackMessage = new PusherToBackMessage(); const pusherToBackMessage = new PusherToBackMessage();
pusherToBackMessage.setPlayglobalmessage(playglobalmessage); pusherToBackMessage.setPlayglobalmessage(playglobalmessage);
@ -409,34 +419,38 @@ export class SocketManager implements ZoneEventListener {
return null; return null;
} }
public handleQueryJitsiJwtMessage(client: ExSocketInterface, queryJitsiJwtMessage: QueryJitsiJwtMessage) { public handleQueryJitsiJwtMessage(client: ExSocketInterface, queryJitsiJwtMessage: QueryJitsiJwtMessage) {
try { try {
const room = queryJitsiJwtMessage.getJitsiroom(); const room = queryJitsiJwtMessage.getJitsiroom();
const tag = queryJitsiJwtMessage.getTag(); // FIXME: this is not secure. We should load the JSON for the current room and check rights associated to room instead. const tag = queryJitsiJwtMessage.getTag(); // FIXME: this is not secure. We should load the JSON for the current room and check rights associated to room instead.
if (SECRET_JITSI_KEY === '') { if (SECRET_JITSI_KEY === "") {
throw new Error('You must set the SECRET_JITSI_KEY key to the secret to generate JWT tokens for Jitsi.'); throw new Error(
"You must set the SECRET_JITSI_KEY key to the secret to generate JWT tokens for Jitsi."
);
} }
// Let's see if the current client has // Let's see if the current client has
const isAdmin = client.tags.includes(tag); const isAdmin = client.tags.includes(tag);
const jwt = Jwt.sign({ const jwt = Jwt.sign(
"aud": "jitsi",
"iss": JITSI_ISS,
"sub": JITSI_URL,
"room": room,
"moderator": isAdmin
}, SECRET_JITSI_KEY, {
expiresIn: '1d',
algorithm: "HS256",
header:
{ {
"alg": "HS256", aud: "jitsi",
"typ": "JWT" iss: JITSI_ISS,
sub: JITSI_URL,
room: room,
moderator: isAdmin,
},
SECRET_JITSI_KEY,
{
expiresIn: "1d",
algorithm: "HS256",
header: {
alg: "HS256",
typ: "JWT",
},
} }
}); );
const sendJitsiJwtMessage = new SendJitsiJwtMessage(); const sendJitsiJwtMessage = new SendJitsiJwtMessage();
sendJitsiJwtMessage.setJitsiroom(room); sendJitsiJwtMessage.setJitsiroom(room);
@ -447,7 +461,7 @@ export class SocketManager implements ZoneEventListener {
client.send(serverToClientMessage.serializeBinary().buffer, true); client.send(serverToClientMessage.serializeBinary().buffer, true);
} catch (e) { } catch (e) {
console.error('An error occured while generating the Jitsi JWT token: ', e); console.error("An error occured while generating the Jitsi JWT token: ", e);
} }
} }
@ -471,7 +485,7 @@ export class SocketManager implements ZoneEventListener {
backAdminMessage.setType(type); backAdminMessage.setType(type);
backConnection.sendAdminMessage(backAdminMessage, (error) => { backConnection.sendAdminMessage(backAdminMessage, (error) => {
if (error !== null) { if (error !== null) {
console.error('Error while sending admin message', error); console.error("Error while sending admin message", error);
} }
}); });
} }
@ -496,7 +510,7 @@ export class SocketManager implements ZoneEventListener {
banMessage.setType(type); banMessage.setType(type);
backConnection.ban(banMessage, (error) => { backConnection.ban(banMessage, (error) => {
if (error !== null) { if (error !== null) {
console.error('Error while sending admin message', error); console.error("Error while sending admin message", error);
} }
}); });
} }
@ -504,25 +518,28 @@ export class SocketManager implements ZoneEventListener {
/** /**
* Merges the characterLayers received from the front (as an array of string) with the custom textures from the back. * Merges the characterLayers received from the front (as an array of string) with the custom textures from the back.
*/ */
static mergeCharacterLayersAndCustomTextures(characterLayers: string[], memberTextures: CharacterTexture[]): CharacterLayer[] { static mergeCharacterLayersAndCustomTextures(
characterLayers: string[],
memberTextures: CharacterTexture[]
): CharacterLayer[] {
const characterLayerObjs: CharacterLayer[] = []; const characterLayerObjs: CharacterLayer[] = [];
for (const characterLayer of characterLayers) { for (const characterLayer of characterLayers) {
if (characterLayer.startsWith('customCharacterTexture')) { if (characterLayer.startsWith("customCharacterTexture")) {
const customCharacterLayerId: number = +characterLayer.substr(22); const customCharacterLayerId: number = +characterLayer.substr(22);
for (const memberTexture of memberTextures) { for (const memberTexture of memberTextures) {
if (memberTexture.id == customCharacterLayerId) { if (memberTexture.id == customCharacterLayerId) {
characterLayerObjs.push({ characterLayerObjs.push({
name: characterLayer, name: characterLayer,
url: memberTexture.url url: memberTexture.url,
}) });
break; break;
} }
} }
} else { } else {
characterLayerObjs.push({ characterLayerObjs.push({
name: characterLayer, name: characterLayer,
url: undefined url: undefined,
}) });
} }
} }
return characterLayerObjs; return characterLayerObjs;