2021-06-24 10:09:10 +02:00
|
|
|
import { HttpRequest, HttpResponse, TemplatedApp } from "uWebSockets.js";
|
|
|
|
import { BaseController } from "./BaseController";
|
|
|
|
import { parse } from "query-string";
|
2021-07-16 09:52:51 +02:00
|
|
|
import { adminApi } from "../Services/AdminApi";
|
2021-11-16 18:38:18 +01:00
|
|
|
import { ADMIN_API_URL, DISABLE_ANONYMOUS, FRONT_URL } from "../Enum/EnvironmentVariable";
|
2021-07-13 19:09:07 +02:00
|
|
|
import { GameRoomPolicyTypes } from "../Model/PusherRoom";
|
2021-12-14 18:55:41 +01:00
|
|
|
import { isMapDetailsData, MapDetailsData } from "../Messages/JsonMessages/MapDetailsData";
|
2021-08-15 08:51:35 +02:00
|
|
|
import { socketManager } from "../Services/SocketManager";
|
2021-08-15 22:51:06 +02:00
|
|
|
import { AuthTokenData, jwtTokenManager } from "../Services/JWTTokenManager";
|
|
|
|
import { v4 } from "uuid";
|
2021-12-08 14:46:23 +01:00
|
|
|
import { InvalidTokenError } from "./InvalidTokenError";
|
2020-05-09 17:50:47 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
export class MapController extends BaseController {
|
|
|
|
constructor(private App: TemplatedApp) {
|
2020-09-28 18:52:54 +02:00
|
|
|
super();
|
2020-05-09 17:50:47 +02:00
|
|
|
this.App = App;
|
2020-10-13 15:12:24 +02:00
|
|
|
this.getMapUrl();
|
2020-05-09 17:50:47 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 14:48:34 +02:00
|
|
|
// Returns a map mapping map name to file name of the map
|
2020-10-13 15:12:24 +02:00
|
|
|
getMapUrl() {
|
|
|
|
this.App.options("/map", (res: HttpResponse, req: HttpRequest) => {
|
2020-09-28 18:52:54 +02:00
|
|
|
this.addCorsHeaders(res);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
2020-10-13 15:12:24 +02:00
|
|
|
this.App.get("/map", (res: HttpResponse, req: HttpRequest) => {
|
2020-10-13 15:29:08 +02:00
|
|
|
res.onAborted(() => {
|
2021-11-24 16:20:07 +01:00
|
|
|
console.warn("/map request was aborted");
|
2021-06-24 10:09:10 +02:00
|
|
|
});
|
2020-10-13 15:29:08 +02:00
|
|
|
|
2020-10-13 15:12:24 +02:00
|
|
|
const query = parse(req.getQuery());
|
|
|
|
|
2021-07-13 19:09:07 +02:00
|
|
|
if (typeof query.playUri !== "string") {
|
2021-11-24 16:20:07 +01:00
|
|
|
console.error("Expected playUri parameter in /map endpoint");
|
2020-10-15 17:27:40 +02:00
|
|
|
res.writeStatus("400 Bad request");
|
|
|
|
this.addCorsHeaders(res);
|
2021-07-13 19:09:07 +02:00
|
|
|
res.end("Expected playUri parameter");
|
2020-11-13 12:11:59 +01:00
|
|
|
return;
|
2020-10-13 15:12:24 +02:00
|
|
|
}
|
2021-07-13 19:09:07 +02:00
|
|
|
|
|
|
|
// If no admin URL is set, let's react on '/_/[instance]/[map url]' URLs
|
|
|
|
if (!ADMIN_API_URL) {
|
|
|
|
const roomUrl = new URL(query.playUri);
|
|
|
|
|
|
|
|
const match = /\/_\/[^/]+\/(.+)/.exec(roomUrl.pathname);
|
|
|
|
if (!match) {
|
|
|
|
res.writeStatus("404 Not Found");
|
|
|
|
this.addCorsHeaders(res);
|
2022-02-15 09:15:41 +01:00
|
|
|
res.writeHeader("Content-Type", "application/json");
|
2021-07-13 19:09:07 +02:00
|
|
|
res.end(JSON.stringify({}));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapUrl = roomUrl.protocol + "//" + match[1];
|
|
|
|
|
|
|
|
res.writeStatus("200 OK");
|
2020-10-15 17:27:40 +02:00
|
|
|
this.addCorsHeaders(res);
|
2022-02-15 09:15:41 +01:00
|
|
|
res.writeHeader("Content-Type", "application/json");
|
2021-07-13 19:09:07 +02:00
|
|
|
res.end(
|
|
|
|
JSON.stringify({
|
|
|
|
mapUrl,
|
|
|
|
policy_type: GameRoomPolicyTypes.ANONYMOUS_POLICY,
|
2021-12-21 11:24:05 +01:00
|
|
|
roomSlug: null, // Deprecated
|
2021-09-30 16:14:55 +02:00
|
|
|
group: null,
|
2021-07-13 19:09:07 +02:00
|
|
|
tags: [],
|
2021-07-16 09:52:51 +02:00
|
|
|
textures: [],
|
2021-12-21 11:24:05 +01:00
|
|
|
contactPage: null,
|
2021-11-12 16:43:37 +01:00
|
|
|
authenticationMandatory: DISABLE_ANONYMOUS,
|
2021-07-13 19:09:07 +02:00
|
|
|
} as MapDetailsData)
|
|
|
|
);
|
|
|
|
|
2020-11-13 12:11:59 +01:00
|
|
|
return;
|
2020-10-13 15:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
(async () => {
|
2020-10-13 15:29:08 +02:00
|
|
|
try {
|
2021-08-15 08:51:35 +02:00
|
|
|
let userId: string | undefined = undefined;
|
|
|
|
if (query.authToken != undefined) {
|
2021-08-15 22:51:06 +02:00
|
|
|
let authTokenData: AuthTokenData;
|
|
|
|
try {
|
|
|
|
authTokenData = jwtTokenManager.verifyJWTToken(query.authToken as string);
|
|
|
|
userId = authTokenData.identifier;
|
|
|
|
} catch (e) {
|
2021-11-16 11:14:27 +01:00
|
|
|
try {
|
|
|
|
// Decode token, in this case we don't need to create new token.
|
|
|
|
authTokenData = jwtTokenManager.verifyJWTToken(query.authToken as string, true);
|
|
|
|
userId = authTokenData.identifier;
|
2021-11-24 16:20:07 +01:00
|
|
|
console.info("JWT expire, but decoded", userId);
|
2021-11-16 11:14:27 +01:00
|
|
|
} catch (e) {
|
2021-12-08 14:46:23 +01:00
|
|
|
if (e instanceof InvalidTokenError) {
|
|
|
|
// The token was not good, redirect user on login page
|
|
|
|
res.writeStatus("401 Unauthorized");
|
|
|
|
res.writeHeader("Access-Control-Allow-Origin", FRONT_URL);
|
|
|
|
res.end("Token decrypted error");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
return this.errorToResponse(e, res);
|
|
|
|
}
|
2021-11-16 11:14:27 +01:00
|
|
|
}
|
2021-08-15 22:51:06 +02:00
|
|
|
}
|
2021-08-15 08:51:35 +02:00
|
|
|
}
|
|
|
|
const mapDetails = await adminApi.fetchMapDetails(query.playUri as string, userId);
|
2020-10-13 15:29:08 +02:00
|
|
|
|
2021-11-12 16:43:37 +01:00
|
|
|
if (isMapDetailsData(mapDetails) && DISABLE_ANONYMOUS) {
|
|
|
|
mapDetails.authenticationMandatory = true;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:27:40 +02:00
|
|
|
res.writeStatus("200 OK");
|
|
|
|
this.addCorsHeaders(res);
|
2022-02-15 09:15:41 +01:00
|
|
|
res.writeHeader("Content-Type", "application/json");
|
2020-10-15 17:27:40 +02:00
|
|
|
res.end(JSON.stringify(mapDetails));
|
2020-10-13 15:29:08 +02:00
|
|
|
} catch (e) {
|
2021-01-17 20:42:33 +01:00
|
|
|
this.errorToResponse(e, res);
|
2020-10-13 15:29:08 +02:00
|
|
|
}
|
2020-10-13 15:12:24 +02:00
|
|
|
})();
|
2020-05-09 17:50:47 +02:00
|
|
|
});
|
|
|
|
}
|
2020-05-10 14:48:34 +02:00
|
|
|
}
|