Cleanup of useless files
This commit is contained in:
parent
10ee00e08a
commit
5d188e51b2
@ -1,26 +1,16 @@
|
||||
// lib/app.ts
|
||||
import {AuthenticateController} from "./Controller/AuthenticateController"; //TODO fix import by "_Controller/..."
|
||||
import {MapController} from "./Controller/MapController";
|
||||
import {PrometheusController} from "./Controller/PrometheusController";
|
||||
import {FileController} from "./Controller/FileController";
|
||||
import {DebugController} from "./Controller/DebugController";
|
||||
import {App as uwsApp} from "./Server/sifrr.server";
|
||||
|
||||
class App {
|
||||
public app: uwsApp;
|
||||
//public authenticateController: AuthenticateController;
|
||||
//public fileController: FileController;
|
||||
//public mapController: MapController;
|
||||
public prometheusController: PrometheusController;
|
||||
private debugController: DebugController;
|
||||
|
||||
constructor() {
|
||||
this.app = new uwsApp();
|
||||
|
||||
//create socket controllers
|
||||
//this.authenticateController = new AuthenticateController(this.app);
|
||||
//this.fileController = new FileController(this.app);
|
||||
//this.mapController = new MapController(this.app);
|
||||
this.prometheusController = new PrometheusController(this.app);
|
||||
this.debugController = new DebugController(this.app);
|
||||
}
|
||||
|
@ -1,135 +0,0 @@
|
||||
import { v4 } from 'uuid';
|
||||
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
|
||||
import {BaseController} from "./BaseController";
|
||||
import {adminApi} from "../Services/AdminApi";
|
||||
import {jwtTokenManager} from "../Services/JWTTokenManager";
|
||||
import {parse} from "query-string";
|
||||
|
||||
export interface TokenInterface {
|
||||
userUuid: string
|
||||
}
|
||||
|
||||
export class AuthenticateController extends BaseController {
|
||||
|
||||
constructor(private App : TemplatedApp) {
|
||||
super();
|
||||
this.register();
|
||||
this.verify();
|
||||
this.anonymLogin();
|
||||
}
|
||||
|
||||
//Try to login with an admin token
|
||||
private register(){
|
||||
this.App.options("/register", (res: HttpResponse, req: HttpRequest) => {
|
||||
this.addCorsHeaders(res);
|
||||
|
||||
res.end();
|
||||
});
|
||||
|
||||
this.App.post("/register", (res: HttpResponse, req: HttpRequest) => {
|
||||
(async () => {
|
||||
res.onAborted(() => {
|
||||
console.warn('Login request was aborted');
|
||||
})
|
||||
const param = await res.json();
|
||||
|
||||
//todo: what to do if the organizationMemberToken is already used?
|
||||
const organizationMemberToken:string|null = param.organizationMemberToken;
|
||||
|
||||
try {
|
||||
if (typeof organizationMemberToken != 'string') throw new Error('No organization token');
|
||||
const data = await adminApi.fetchMemberDataByToken(organizationMemberToken);
|
||||
const userUuid = data.userUuid;
|
||||
const organizationSlug = data.organizationSlug;
|
||||
const worldSlug = data.worldSlug;
|
||||
const roomSlug = data.roomSlug;
|
||||
const mapUrlStart = data.mapUrlStart;
|
||||
const textures = data.textures;
|
||||
|
||||
const authToken = jwtTokenManager.createJWTToken(userUuid);
|
||||
res.writeStatus("200 OK");
|
||||
this.addCorsHeaders(res);
|
||||
res.end(JSON.stringify({
|
||||
authToken,
|
||||
userUuid,
|
||||
organizationSlug,
|
||||
worldSlug,
|
||||
roomSlug,
|
||||
mapUrlStart,
|
||||
textures
|
||||
}));
|
||||
|
||||
} catch (e) {
|
||||
console.error("An error happened", e)
|
||||
res.writeStatus(e.status || "500 Internal Server Error");
|
||||
this.addCorsHeaders(res);
|
||||
res.end('An error happened');
|
||||
}
|
||||
|
||||
|
||||
})();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private verify(){
|
||||
this.App.options("/verify", (res: HttpResponse, req: HttpRequest) => {
|
||||
this.addCorsHeaders(res);
|
||||
|
||||
res.end();
|
||||
});
|
||||
|
||||
this.App.get("/verify", (res: HttpResponse, req: HttpRequest) => {
|
||||
(async () => {
|
||||
const query = parse(req.getQuery());
|
||||
|
||||
res.onAborted(() => {
|
||||
console.warn('verify request was aborted');
|
||||
})
|
||||
|
||||
try {
|
||||
await jwtTokenManager.getUserUuidFromToken(query.token as string);
|
||||
} catch (e) {
|
||||
res.writeStatus("400 Bad Request");
|
||||
this.addCorsHeaders(res);
|
||||
res.end(JSON.stringify({
|
||||
"success": false,
|
||||
"message": "Invalid JWT token"
|
||||
}));
|
||||
return;
|
||||
}
|
||||
res.writeStatus("200 OK");
|
||||
this.addCorsHeaders(res);
|
||||
res.end(JSON.stringify({
|
||||
"success": true
|
||||
}));
|
||||
})();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
//permit to login on application. Return token to connect on Websocket IO.
|
||||
private anonymLogin(){
|
||||
this.App.options("/anonymLogin", (res: HttpResponse, req: HttpRequest) => {
|
||||
this.addCorsHeaders(res);
|
||||
|
||||
res.end();
|
||||
});
|
||||
|
||||
this.App.post("/anonymLogin", (res: HttpResponse, req: HttpRequest) => {
|
||||
|
||||
res.onAborted(() => {
|
||||
console.warn('Login request was aborted');
|
||||
})
|
||||
|
||||
const userUuid = v4();
|
||||
const authToken = jwtTokenManager.createJWTToken(userUuid);
|
||||
res.writeStatus("200 OK");
|
||||
this.addCorsHeaders(res);
|
||||
res.end(JSON.stringify({
|
||||
authToken,
|
||||
userUuid,
|
||||
}));
|
||||
});
|
||||
}
|
||||
}
|
@ -1,161 +0,0 @@
|
||||
import {App} from "../Server/sifrr.server";
|
||||
|
||||
import {v4} from "uuid";
|
||||
import {HttpRequest, HttpResponse} from "uWebSockets.js";
|
||||
import {BaseController} from "./BaseController";
|
||||
import { Readable } from 'stream'
|
||||
|
||||
interface UploadedFileBuffer {
|
||||
buffer: Buffer,
|
||||
expireDate: Date
|
||||
}
|
||||
|
||||
export class FileController extends BaseController {
|
||||
private uploadedFileBuffers: Map<string, UploadedFileBuffer> = new Map<string, UploadedFileBuffer>();
|
||||
|
||||
constructor(private App : App) {
|
||||
super();
|
||||
this.App = App;
|
||||
this.uploadAudioMessage();
|
||||
this.downloadAudioMessage();
|
||||
|
||||
// Cleanup every 1 minute
|
||||
setInterval(this.cleanup.bind(this), 60000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean memory from old files
|
||||
*/
|
||||
cleanup(): void {
|
||||
const now = new Date();
|
||||
for (const [id, file] of this.uploadedFileBuffers) {
|
||||
if (file.expireDate < now) {
|
||||
this.uploadedFileBuffers.delete(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uploadAudioMessage(){
|
||||
this.App.options("/upload-audio-message", (res: HttpResponse, req: HttpRequest) => {
|
||||
this.addCorsHeaders(res);
|
||||
|
||||
res.end();
|
||||
});
|
||||
|
||||
this.App.post("/upload-audio-message", (res: HttpResponse, req: HttpRequest) => {
|
||||
(async () => {
|
||||
res.onAborted(() => {
|
||||
console.warn('upload-audio-message request was aborted');
|
||||
})
|
||||
|
||||
try {
|
||||
const audioMessageId = v4();
|
||||
|
||||
const params = await res.formData({
|
||||
onFile: (fieldname: string,
|
||||
file: NodeJS.ReadableStream,
|
||||
filename: string,
|
||||
encoding: string,
|
||||
mimetype: string) => {
|
||||
(async () => {
|
||||
console.log('READING FILE', fieldname)
|
||||
|
||||
const chunks: Buffer[] = []
|
||||
for await (const chunk of file) {
|
||||
if (!(chunk instanceof Buffer)) {
|
||||
throw new Error('Unexpected chunk');
|
||||
}
|
||||
chunks.push(chunk)
|
||||
}
|
||||
// Let's expire in 1 minute.
|
||||
const expireDate = new Date();
|
||||
expireDate.setMinutes(expireDate.getMinutes() + 1);
|
||||
this.uploadedFileBuffers.set(audioMessageId, {
|
||||
buffer: Buffer.concat(chunks),
|
||||
expireDate
|
||||
});
|
||||
})();
|
||||
}
|
||||
});
|
||||
|
||||
res.writeStatus("200 OK");
|
||||
this.addCorsHeaders(res);
|
||||
res.end(JSON.stringify({
|
||||
id: audioMessageId,
|
||||
path: `/download-audio-message/${audioMessageId}`
|
||||
}));
|
||||
|
||||
} catch (e) {
|
||||
console.log("An error happened", e)
|
||||
res.writeStatus(e.status || "500 Internal Server Error");
|
||||
this.addCorsHeaders(res);
|
||||
res.end('An error happened');
|
||||
}
|
||||
})();
|
||||
});
|
||||
}
|
||||
|
||||
downloadAudioMessage(){
|
||||
this.App.options("/download-audio-message/*", (res: HttpResponse, req: HttpRequest) => {
|
||||
this.addCorsHeaders(res);
|
||||
|
||||
res.end();
|
||||
});
|
||||
|
||||
this.App.get("/download-audio-message/:id", (res: HttpResponse, req: HttpRequest) => {
|
||||
|
||||
res.onAborted(() => {
|
||||
console.warn('upload-audio-message request was aborted');
|
||||
})
|
||||
|
||||
const id = req.getParameter(0);
|
||||
|
||||
const file = this.uploadedFileBuffers.get(id);
|
||||
if (file === undefined) {
|
||||
res.writeStatus("404 Not found");
|
||||
this.addCorsHeaders(res);
|
||||
res.end("Cannot find file");
|
||||
return;
|
||||
}
|
||||
|
||||
const readable = new Readable()
|
||||
readable._read = () => {} // _read is required but you can noop it
|
||||
readable.push(file.buffer);
|
||||
readable.push(null);
|
||||
|
||||
const size = file.buffer.byteLength;
|
||||
|
||||
res.writeStatus("200 OK");
|
||||
|
||||
readable.on('data', buffer => {
|
||||
const chunk = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength),
|
||||
lastOffset = res.getWriteOffset();
|
||||
|
||||
// First try
|
||||
const [ok, done] = res.tryEnd(chunk, size);
|
||||
|
||||
if (done) {
|
||||
readable.destroy();
|
||||
} else if (!ok) {
|
||||
// pause because backpressure
|
||||
readable.pause();
|
||||
|
||||
// Save unsent chunk for later
|
||||
res.ab = chunk;
|
||||
res.abOffset = lastOffset;
|
||||
|
||||
// Register async handlers for drainage
|
||||
res.onWritable(offset => {
|
||||
const [ok, done] = res.tryEnd(res.ab.slice(offset - res.abOffset), size);
|
||||
if (done) {
|
||||
readable.destroy();
|
||||
} else if (ok) {
|
||||
readable.resume();
|
||||
}
|
||||
return ok;
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
import {OK} from "http-status-codes";
|
||||
import {URL_ROOM_STARTED} from "../Enum/EnvironmentVariable";
|
||||
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
|
||||
import {BaseController} from "./BaseController";
|
||||
import {parse} from "query-string";
|
||||
import {adminApi} from "../Services/AdminApi";
|
||||
|
||||
//todo: delete this
|
||||
export class MapController extends BaseController{
|
||||
|
||||
constructor(private App : TemplatedApp) {
|
||||
super();
|
||||
this.App = App;
|
||||
this.getMapUrl();
|
||||
}
|
||||
|
||||
|
||||
// Returns a map mapping map name to file name of the map
|
||||
getMapUrl() {
|
||||
this.App.options("/map", (res: HttpResponse, req: HttpRequest) => {
|
||||
this.addCorsHeaders(res);
|
||||
|
||||
res.end();
|
||||
});
|
||||
|
||||
this.App.get("/map", (res: HttpResponse, req: HttpRequest) => {
|
||||
|
||||
res.onAborted(() => {
|
||||
console.warn('/map request was aborted');
|
||||
})
|
||||
|
||||
const query = parse(req.getQuery());
|
||||
|
||||
if (typeof query.organizationSlug !== 'string') {
|
||||
console.error('Expected organizationSlug parameter');
|
||||
res.writeStatus("400 Bad request");
|
||||
this.addCorsHeaders(res);
|
||||
res.end("Expected organizationSlug parameter");
|
||||
}
|
||||
if (typeof query.worldSlug !== 'string') {
|
||||
console.error('Expected worldSlug parameter');
|
||||
res.writeStatus("400 Bad request");
|
||||
this.addCorsHeaders(res);
|
||||
res.end("Expected worldSlug parameter");
|
||||
}
|
||||
if (typeof query.roomSlug !== 'string' && query.roomSlug !== undefined) {
|
||||
console.error('Expected only one roomSlug parameter');
|
||||
res.writeStatus("400 Bad request");
|
||||
this.addCorsHeaders(res);
|
||||
res.end("Expected only one roomSlug parameter");
|
||||
}
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const mapDetails = await adminApi.fetchMapDetails(query.organizationSlug as string, query.worldSlug as string, query.roomSlug as string|undefined);
|
||||
|
||||
res.writeStatus("200 OK");
|
||||
this.addCorsHeaders(res);
|
||||
res.end(JSON.stringify(mapDetails));
|
||||
} catch (e) {
|
||||
console.error(e.message || e);
|
||||
res.writeStatus("500 Internal Server Error")
|
||||
this.addCorsHeaders(res);
|
||||
res.end("An error occurred");
|
||||
}
|
||||
})();
|
||||
|
||||
});
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
import {MessageUserPosition} from "../Model/Websocket/MessageUserPosition";
|
||||
|
||||
export interface Distance {
|
||||
distance: number,
|
||||
first: MessageUserPosition,
|
||||
second: MessageUserPosition,
|
||||
}
|
@ -1,12 +1,9 @@
|
||||
import {PointInterface} from "./Websocket/PointInterface";
|
||||
import {Group} from "./Group";
|
||||
import {User, UserSocket} from "./User";
|
||||
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
||||
import {PositionInterface} from "_Model/PositionInterface";
|
||||
import {Identificable} from "_Model/Websocket/Identificable";
|
||||
import {EntersCallback, LeavesCallback, MovesCallback} from "_Model/Zone";
|
||||
import {PositionNotifier} from "./PositionNotifier";
|
||||
import {ViewportInterface} from "_Model/Websocket/ViewportMessage";
|
||||
import {Movable} from "_Model/Movable";
|
||||
import {extractDataFromPrivateRoomId, extractRoomSlugPublicRoomId, isRoomAnonymous} from "./RoomIdentifier";
|
||||
import {arrayIntersect} from "../Services/ArrayHelper";
|
||||
|
@ -3,10 +3,9 @@ import { PointInterface } from "./Websocket/PointInterface";
|
||||
import {Zone} from "_Model/Zone";
|
||||
import {Movable} from "_Model/Movable";
|
||||
import {PositionNotifier} from "_Model/PositionNotifier";
|
||||
import {ServerDuplexStream, ServerWritableStream} from "grpc";
|
||||
import {ServerDuplexStream} from "grpc";
|
||||
import {BatchMessage, PusherToBackMessage, ServerToClientMessage, SubMessage} from "../Messages/generated/messages_pb";
|
||||
import {ProtobufUtils} from "_Model/Websocket/ProtobufUtils";
|
||||
import {CharacterLayer, ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
||||
import {CharacterLayer} from "_Model/Websocket/CharacterLayer";
|
||||
|
||||
export type UserSocket = ServerDuplexStream<PusherToBackMessage, ServerToClientMessage>;
|
||||
|
||||
|
4
back/src/Model/Websocket/CharacterLayer.ts
Normal file
4
back/src/Model/Websocket/CharacterLayer.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export interface CharacterLayer {
|
||||
name: string,
|
||||
url: string|undefined
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
import {PointInterface} from "./PointInterface";
|
||||
import {Identificable} from "./Identificable";
|
||||
import {ViewportInterface} from "_Model/Websocket/ViewportMessage";
|
||||
import {BatchMessage, SubMessage} from "../../Messages/generated/messages_pb";
|
||||
import {WebSocket} from "uWebSockets.js"
|
||||
import {CharacterTexture} from "../../Services/AdminApi";
|
||||
|
||||
export interface CharacterLayer {
|
||||
name: string,
|
||||
url: string|undefined
|
||||
}
|
||||
|
||||
export interface ExSocketInterface extends WebSocket, Identificable {
|
||||
token: string;
|
||||
roomId: string;
|
||||
//userId: number; // A temporary (autoincremented) identifier for this user
|
||||
userUuid: string; // A unique identifier for this user
|
||||
name: string;
|
||||
characterLayers: CharacterLayer[];
|
||||
position: PointInterface;
|
||||
viewport: ViewportInterface;
|
||||
/**
|
||||
* Pushes an event that will be sent in the next batch of events
|
||||
*/
|
||||
emitInBatch: (payload: SubMessage) => void;
|
||||
batchedMessages: BatchMessage;
|
||||
batchTimeout: NodeJS.Timeout|null;
|
||||
disconnecting: boolean,
|
||||
tags: string[],
|
||||
textures: CharacterTexture[],
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
import {PositionInterface} from "_Model/PositionInterface";
|
||||
|
||||
export interface GroupUpdateInterface {
|
||||
position: PositionInterface,
|
||||
groupId: number,
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
import * as tg from "generic-type-guard";
|
||||
import {isPointInterface} from "./PointInterface";
|
||||
import {isViewport} from "./ViewportMessage";
|
||||
|
||||
export const isJoinRoomMessageInterface =
|
||||
new tg.IsInterface().withProperties({
|
||||
roomId: tg.isString,
|
||||
position: isPointInterface,
|
||||
viewport: isViewport
|
||||
}).get();
|
||||
export type JoinRoomMessageInterface = tg.GuardedType<typeof isJoinRoomMessageInterface>;
|
@ -1,6 +0,0 @@
|
||||
import {PointInterface} from "_Model/Websocket/PointInterface";
|
||||
|
||||
export class MessageUserJoined {
|
||||
constructor(public userId: number, public name: string, public characterLayers: string[], public position: PointInterface) {
|
||||
}
|
||||
}
|
@ -5,7 +5,3 @@ export class Point implements PointInterface{
|
||||
}
|
||||
}
|
||||
|
||||
export class MessageUserPosition {
|
||||
constructor(public userId: number, public name: string, public characterLayers: string[], public position: PointInterface) {
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import {
|
||||
PointMessage,
|
||||
PositionMessage
|
||||
} from "../../Messages/generated/messages_pb";
|
||||
import {CharacterLayer, ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
||||
import {CharacterLayer} from "_Model/Websocket/CharacterLayer";
|
||||
import Direction = PositionMessage.Direction;
|
||||
import {ItemEventMessageInterface} from "_Model/Websocket/ItemEventMessage";
|
||||
import {PositionInterface} from "_Model/PositionInterface";
|
||||
|
@ -1,8 +0,0 @@
|
||||
import * as tg from "generic-type-guard";
|
||||
|
||||
export const isSetPlayerDetailsMessage =
|
||||
new tg.IsInterface().withProperties({
|
||||
name: tg.isString,
|
||||
characterLayers: tg.isArray(tg.isString)
|
||||
}).get();
|
||||
export type SetPlayerDetailsMessage = tg.GuardedType<typeof isSetPlayerDetailsMessage>;
|
@ -1,5 +0,0 @@
|
||||
export interface UserInGroupInterface {
|
||||
userId: number,
|
||||
name: string,
|
||||
initiator: boolean
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
import * as tg from "generic-type-guard";
|
||||
|
||||
export const isViewport =
|
||||
new tg.IsInterface().withProperties({
|
||||
left: tg.isNumber,
|
||||
top: tg.isNumber,
|
||||
right: tg.isNumber,
|
||||
bottom: tg.isNumber,
|
||||
}).get();
|
||||
export type ViewportInterface = tg.GuardedType<typeof isViewport>;
|
@ -1,18 +0,0 @@
|
||||
import * as tg from "generic-type-guard";
|
||||
|
||||
export const isSignalData =
|
||||
new tg.IsInterface().withProperties({
|
||||
type: tg.isOptional(tg.isString)
|
||||
}).get();
|
||||
|
||||
export const isWebRtcSignalMessageInterface =
|
||||
new tg.IsInterface().withProperties({
|
||||
receiverId: tg.isNumber,
|
||||
signal: isSignalData
|
||||
}).get();
|
||||
export const isWebRtcScreenSharingStartMessageInterface =
|
||||
new tg.IsInterface().withProperties({
|
||||
userId: tg.isNumber,
|
||||
roomId: tg.isString
|
||||
}).get();
|
||||
export type WebRtcSignalMessageInterface = tg.GuardedType<typeof isWebRtcSignalMessageInterface>;
|
@ -8,7 +8,7 @@ import {
|
||||
import grpc, {ServerWritableStream} from "grpc";
|
||||
import {Empty} from "google-protobuf/google/protobuf/empty_pb";
|
||||
import {socketManager} from "./Services/SocketManager";
|
||||
import {emitError} from "./Services/IoSocketHelpers";
|
||||
import {emitError} from "./Services/MessageHelpers";
|
||||
import {User, UserSocket} from "./Model/User";
|
||||
import {GameRoom} from "./Model/GameRoom";
|
||||
import Debug from "debug";
|
||||
|
@ -1,39 +0,0 @@
|
||||
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
||||
import {BatchMessage, ErrorMessage, ServerToClientMessage, SubMessage} from "../Messages/generated/messages_pb";
|
||||
import {UserSocket} from "_Model/User";
|
||||
|
||||
/**
|
||||
* @deprecated use User.emitInBatch instead
|
||||
*/
|
||||
export function emitInBatch(socket: ExSocketInterface, payload: SubMessage): void {
|
||||
socket.batchedMessages.addPayload(payload);
|
||||
|
||||
if (socket.batchTimeout === null) {
|
||||
socket.batchTimeout = setTimeout(() => {
|
||||
if (socket.disconnecting) {
|
||||
return;
|
||||
}
|
||||
|
||||
const serverToClientMessage = new ServerToClientMessage();
|
||||
serverToClientMessage.setBatchmessage(socket.batchedMessages);
|
||||
|
||||
socket.send(serverToClientMessage.serializeBinary().buffer, true);
|
||||
socket.batchedMessages = new BatchMessage();
|
||||
socket.batchTimeout = null;
|
||||
}, 100);
|
||||
}
|
||||
}
|
||||
|
||||
export function emitError(Client: UserSocket, message: string): void {
|
||||
const errorMessage = new ErrorMessage();
|
||||
errorMessage.setMessage(message);
|
||||
|
||||
const serverToClientMessage = new ServerToClientMessage();
|
||||
serverToClientMessage.setErrormessage(errorMessage);
|
||||
|
||||
//if (!Client.disconnecting) {
|
||||
Client.write(serverToClientMessage);
|
||||
//}
|
||||
console.warn(message);
|
||||
}
|
||||
|
@ -1,76 +0,0 @@
|
||||
import {ADMIN_API_URL, ALLOW_ARTILLERY, SECRET_KEY} from "../Enum/EnvironmentVariable";
|
||||
import {uuid} from "uuidv4";
|
||||
import Jwt from "jsonwebtoken";
|
||||
import {TokenInterface} from "../Controller/AuthenticateController";
|
||||
import {adminApi, AdminApiData} from "../Services/AdminApi";
|
||||
|
||||
class JWTTokenManager {
|
||||
|
||||
public createJWTToken(userUuid: string) {
|
||||
return Jwt.sign({userUuid: userUuid}, SECRET_KEY, {expiresIn: '200d'}); //todo: add a mechanic to refresh or recreate token
|
||||
}
|
||||
|
||||
public async getUserUuidFromToken(token: unknown): Promise<string> {
|
||||
|
||||
if (!token) {
|
||||
throw new Error('An authentication error happened, a user tried to connect without a token.');
|
||||
}
|
||||
if (typeof(token) !== "string") {
|
||||
throw new Error('Token is expected to be a string');
|
||||
}
|
||||
|
||||
|
||||
if(token === 'test') {
|
||||
if (ALLOW_ARTILLERY) {
|
||||
return uuid();
|
||||
} 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'");
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
Jwt.verify(token, SECRET_KEY, {},(err, tokenDecoded) => {
|
||||
const tokenInterface = tokenDecoded as TokenInterface;
|
||||
if (err) {
|
||||
console.error('An authentication error happened, invalid JsonWebToken.', err);
|
||||
reject(new Error('An authentication error happened, invalid JsonWebToken. ' + err.message));
|
||||
return;
|
||||
}
|
||||
if (tokenDecoded === undefined) {
|
||||
console.error('Empty token found.');
|
||||
reject(new Error('Empty token found.'));
|
||||
return;
|
||||
}
|
||||
|
||||
//verify token
|
||||
if (!this.isValidToken(tokenInterface)) {
|
||||
reject(new Error('Authentication error, invalid token structure.'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (ADMIN_API_URL) {
|
||||
//verify user in admin
|
||||
adminApi.fetchCheckUserByToken(tokenInterface.userUuid).then(() => {
|
||||
resolve(tokenInterface.userUuid);
|
||||
}).catch((err) => {
|
||||
//anonymous user
|
||||
if(err.response && err.response.status && err.response.status === 404){
|
||||
resolve(tokenInterface.userUuid);
|
||||
return;
|
||||
}
|
||||
reject(err);
|
||||
});
|
||||
} else {
|
||||
resolve(tokenInterface.userUuid);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private isValidToken(token: object): token is TokenInterface {
|
||||
return !(typeof((token as TokenInterface).userUuid) !== 'string');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const jwtTokenManager = new JWTTokenManager();
|
15
back/src/Services/MessageHelpers.ts
Normal file
15
back/src/Services/MessageHelpers.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import {ErrorMessage, ServerToClientMessage} from "../Messages/generated/messages_pb";
|
||||
import {UserSocket} from "_Model/User";
|
||||
|
||||
export function emitError(Client: UserSocket, message: string): void {
|
||||
const errorMessage = new ErrorMessage();
|
||||
errorMessage.setMessage(message);
|
||||
|
||||
const serverToClientMessage = new ServerToClientMessage();
|
||||
serverToClientMessage.setErrormessage(errorMessage);
|
||||
|
||||
//if (!Client.disconnecting) {
|
||||
Client.write(serverToClientMessage);
|
||||
//}
|
||||
console.warn(message);
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
import {GameRoom} from "../Model/GameRoom";
|
||||
import {CharacterLayer, ExSocketInterface} from "../Model/Websocket/ExSocketInterface";
|
||||
import {CharacterLayer} from "_Model/Websocket/CharacterLayer";
|
||||
import {
|
||||
GroupDeleteMessage,
|
||||
GroupUpdateMessage,
|
||||
@ -33,23 +33,18 @@ import {
|
||||
SubToPusherMessage,
|
||||
UserJoinedZoneMessage, GroupUpdateZoneMessage, GroupLeftZoneMessage, UserLeftZoneMessage
|
||||
} from "../Messages/generated/messages_pb";
|
||||
import {PointInterface} from "../Model/Websocket/PointInterface";
|
||||
import {User, UserSocket} from "../Model/User";
|
||||
import {ProtobufUtils} from "../Model/Websocket/ProtobufUtils";
|
||||
import {Group} from "../Model/Group";
|
||||
import {cpuTracker} from "./CpuTracker";
|
||||
import {isSetPlayerDetailsMessage} from "../Model/Websocket/SetPlayerDetailsMessage";
|
||||
import {ADMIN_API_URL, GROUP_RADIUS, JITSI_ISS, MINIMUM_DISTANCE, SECRET_JITSI_KEY} from "../Enum/EnvironmentVariable";
|
||||
import {Movable} from "../Model/Movable";
|
||||
import {PositionInterface} from "../Model/PositionInterface";
|
||||
import {adminApi, CharacterTexture, FetchMemberDataByUuidResponse} from "./AdminApi";
|
||||
import Direction = PositionMessage.Direction;
|
||||
import {emitError, emitInBatch} from "./IoSocketHelpers";
|
||||
import Jwt from "jsonwebtoken";
|
||||
import {JITSI_URL} from "../Enum/EnvironmentVariable";
|
||||
import {clientEventsEmitter} from "./ClientEventsEmitter";
|
||||
import {gaugeManager} from "./GaugeManager";
|
||||
import {ServerWritableStream} from "grpc";
|
||||
import {ZoneSocket} from "../RoomManager";
|
||||
import {Zone} from "_Model/Zone";
|
||||
import Debug from "debug";
|
||||
@ -78,7 +73,6 @@ function emitZoneMessage(subMessage: SubToPusherMessage, socket: ZoneSocket): vo
|
||||
|
||||
export class SocketManager {
|
||||
private rooms: Map<string, GameRoom> = new Map<string, GameRoom>();
|
||||
//private sockets: Map<number, ExSocketInterface> = new Map<number, ExSocketInterface>();
|
||||
|
||||
constructor() {
|
||||
clientEventsEmitter.registerToClientJoin((clientUUid: string, roomId: string) => {
|
||||
@ -647,7 +641,7 @@ export class SocketManager {
|
||||
user.socket.write(serverToClientMessage);
|
||||
}
|
||||
|
||||
public emitSendUserMessage(messageToSend: {userUuid: string, message: string, type: string}): ExSocketInterface {
|
||||
public emitSendUserMessage(messageToSend: {userUuid: string, message: string, type: string}): void {
|
||||
// TODO: move this to room (findByUuid)
|
||||
throw new Error("Not yet reimplemented");
|
||||
/*const socket = this.searchClientByUuid(messageToSend.userUuid);
|
||||
|
@ -2,7 +2,7 @@ import "jasmine";
|
||||
import {GameRoom, ConnectCallback, DisconnectCallback } from "../src/Model/GameRoom";
|
||||
import {Point} from "../src/Model/Websocket/MessageUserPosition";
|
||||
import { Group } from "../src/Model/Group";
|
||||
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
||||
import {ExSocketInterface} from "_Model/Websocket/CharacterLayer";
|
||||
import {User} from "_Model/User";
|
||||
|
||||
function createMockUser(userId: number): ExSocketInterface {
|
||||
|
@ -8,7 +8,7 @@ import {PointInterface} from "../src/Model/Websocket/PointInterface";
|
||||
import {Zone} from "_Model/Zone";
|
||||
import {Movable} from "_Model/Movable";
|
||||
import {PositionInterface} from "_Model/PositionInterface";
|
||||
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
||||
import {ExSocketInterface} from "_Model/Websocket/CharacterLayer";
|
||||
|
||||
|
||||
describe("PositionNotifier", () => {
|
||||
|
@ -1,7 +0,0 @@
|
||||
import {MessageUserPosition} from "../Model/Websocket/MessageUserPosition";
|
||||
|
||||
export interface Distance {
|
||||
distance: number,
|
||||
first: MessageUserPosition,
|
||||
second: MessageUserPosition,
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
import {PositionInterface} from "_Model/PositionInterface";
|
||||
|
||||
export interface GroupUpdateInterface {
|
||||
position: PositionInterface,
|
||||
groupId: number,
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
import * as tg from "generic-type-guard";
|
||||
import {isPointInterface} from "./PointInterface";
|
||||
import {isViewport} from "./ViewportMessage";
|
||||
|
||||
export const isJoinRoomMessageInterface =
|
||||
new tg.IsInterface().withProperties({
|
||||
roomId: tg.isString,
|
||||
position: isPointInterface,
|
||||
viewport: isViewport
|
||||
}).get();
|
||||
export type JoinRoomMessageInterface = tg.GuardedType<typeof isJoinRoomMessageInterface>;
|
@ -1,6 +0,0 @@
|
||||
import {PointInterface} from "_Model/Websocket/PointInterface";
|
||||
|
||||
export class MessageUserJoined {
|
||||
constructor(public userId: number, public name: string, public characterLayers: string[], public position: PointInterface) {
|
||||
}
|
||||
}
|
@ -4,8 +4,3 @@ export class Point implements PointInterface{
|
||||
constructor(public x : number, public y : number, public direction : string = "none", public moving : boolean = false) {
|
||||
}
|
||||
}
|
||||
|
||||
export class MessageUserPosition {
|
||||
constructor(public userId: number, public name: string, public characterLayers: string[], public position: PointInterface) {
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
import * as tg from "generic-type-guard";
|
||||
|
||||
export const isSetPlayerDetailsMessage =
|
||||
new tg.IsInterface().withProperties({
|
||||
name: tg.isString,
|
||||
characterLayers: tg.isArray(tg.isString)
|
||||
}).get();
|
||||
export type SetPlayerDetailsMessage = tg.GuardedType<typeof isSetPlayerDetailsMessage>;
|
@ -1,5 +0,0 @@
|
||||
export interface UserInGroupInterface {
|
||||
userId: number,
|
||||
name: string,
|
||||
initiator: boolean
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
import * as tg from "generic-type-guard";
|
||||
|
||||
export const isSignalData =
|
||||
new tg.IsInterface().withProperties({
|
||||
type: tg.isOptional(tg.isString)
|
||||
}).get();
|
||||
|
||||
export const isWebRtcSignalMessageInterface =
|
||||
new tg.IsInterface().withProperties({
|
||||
receiverId: tg.isNumber,
|
||||
signal: isSignalData
|
||||
}).get();
|
||||
export const isWebRtcScreenSharingStartMessageInterface =
|
||||
new tg.IsInterface().withProperties({
|
||||
userId: tg.isNumber,
|
||||
roomId: tg.isString
|
||||
}).get();
|
||||
export type WebRtcSignalMessageInterface = tg.GuardedType<typeof isWebRtcSignalMessageInterface>;
|
@ -30,7 +30,6 @@ import {User} from "../Model/User";
|
||||
import {ProtobufUtils} from "../Model/Websocket/ProtobufUtils";
|
||||
import {Group} from "../Model/Group";
|
||||
import {cpuTracker} from "./CpuTracker";
|
||||
import {isSetPlayerDetailsMessage} from "../Model/Websocket/SetPlayerDetailsMessage";
|
||||
import {GROUP_RADIUS, JITSI_ISS, MINIMUM_DISTANCE, SECRET_JITSI_KEY} from "../Enum/EnvironmentVariable";
|
||||
import {Movable} from "../Model/Movable";
|
||||
import {PositionInterface} from "../Model/PositionInterface";
|
||||
|
@ -1,6 +1,6 @@
|
||||
import "jasmine";
|
||||
import {PusherRoom, ConnectCallback, DisconnectCallback } from "_Model/PusherRoom";
|
||||
import {Point} from "../src/Model/Websocket/MessageUserPosition";
|
||||
import {Point} from "_Model/Websocket/Point";
|
||||
import { Group } from "../src/Model/Group";
|
||||
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
||||
import {User} from "_Model/User";
|
||||
|
@ -1,6 +1,6 @@
|
||||
import "jasmine";
|
||||
import {PusherRoom, ConnectCallback, DisconnectCallback } from "_Model/PusherRoom";
|
||||
import {Point} from "../src/Model/Websocket/MessageUserPosition";
|
||||
import {Point} from "_Model/Websocket/Point";
|
||||
import { Group } from "../src/Model/Group";
|
||||
import {PositionDispatcher} from "_Model/PositionDispatcher";
|
||||
import {User} from "../src/Model/User";
|
||||
|
Loading…
Reference in New Issue
Block a user