Semi-stable version with old variable names
This commit is contained in:
+180
-74
@@ -38,43 +38,64 @@ const HEARTBEAT_INTERVAL = 30000;
|
||||
const MAX_MESSAGE_SIZE = 10 * 1024; // 10KB
|
||||
|
||||
|
||||
function addToGroup(ws, access, authenticted = false) {
|
||||
if (authenticted) {
|
||||
if (!authenticatedGroups.has(access)) {
|
||||
authenticatedGroups.set(access, new Set());
|
||||
}
|
||||
authenticatedGroups.get(access).add(ws);
|
||||
ws.send("Dieser Benutzer wurde einer Authentifizierten Gruppe hinzugefügt." + access);
|
||||
} else {
|
||||
if (!groups.has(access)) {
|
||||
groups.set(access, new Set());
|
||||
}
|
||||
groups.get(access).add(ws);
|
||||
function addToGroup(ws, wkId, accesstype, access, isAuthenticated = false) {
|
||||
const targetMap = isAuthenticated ? authenticatedGroups : groups;
|
||||
|
||||
if (!targetMap.has(wkId)) {
|
||||
targetMap.set(wkId, new Map());
|
||||
}
|
||||
|
||||
const workspaceMap = targetMap.get(wkId);
|
||||
|
||||
if (!workspaceMap.has(accesstype)) {
|
||||
workspaceMap.set(accesstype, new Map());
|
||||
}
|
||||
|
||||
const accesstypeMap = workspaceMap.get(accesstype);
|
||||
|
||||
if (!accesstypeMap.has(access)) {
|
||||
accesstypeMap.set(access, new Set());
|
||||
}
|
||||
|
||||
const accessSet = accesstypeMap.get(access);
|
||||
|
||||
accessSet.add(ws);
|
||||
|
||||
if (isAuthenticated) {
|
||||
ws.send(`Dieser Benutzer wurde einer Gruppe hinzugefügt: Wettkampf-Id: ${wkId}, Zugriffstyp: ${accesstype}, Zugriff: ${access}`);
|
||||
}
|
||||
}
|
||||
|
||||
function removeFromGroup(ws, access, authenticted = false) {
|
||||
function removeFromGroup(ws, wkId, accesstype, access, authenticted = false) {
|
||||
let group;
|
||||
if (authenticted) {
|
||||
group = authenticatedGroups.get(access);
|
||||
} else {
|
||||
group = groups.get(access);
|
||||
}
|
||||
if (group) {
|
||||
group.delete(ws);
|
||||
if (group.size === 0) {
|
||||
groups.delete(access);
|
||||
|
||||
const targetMap = authenticted ? authenticatedGroups : groups;
|
||||
|
||||
const workspaceMap = targetMap.get(wkId);
|
||||
|
||||
const accesstypeMap = workspaceMap.get(accesstype);
|
||||
|
||||
const accessSet = accesstypeMap.get(access);
|
||||
|
||||
if (accessSet) {
|
||||
accessSet.delete(ws);
|
||||
if (accessSet.size === 0) {
|
||||
accessSet.delete(access);
|
||||
}
|
||||
if (accesstypeMap.size === 0) {
|
||||
accesstypeMap.delete(accesstype);
|
||||
}
|
||||
if (workspaceMap.size === 0) {
|
||||
workspaceMap.delete(wkId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function sendToGroup(access, messageObj, authenticted = false, excludeWs = null) {
|
||||
let group;
|
||||
if (authenticted) {
|
||||
group = authenticatedGroups.get(access);
|
||||
} else {
|
||||
group = groups.get(access);
|
||||
}
|
||||
function sendToAccess(wkId, accesstype, access, messageObj, authenticted = false, excludeWs = null) {
|
||||
|
||||
const targetMap = authenticted ? authenticatedGroups : groups;
|
||||
|
||||
const group = targetMap.get(wkId)?.get(accesstype)?.get(access);
|
||||
|
||||
if (!group) return;
|
||||
|
||||
@@ -87,6 +108,23 @@ function sendToGroup(access, messageObj, authenticted = false, excludeWs = null)
|
||||
}
|
||||
}
|
||||
|
||||
function sendToAccessType(wkId, accessType, messageObj, authenticated = false, excludeWs = null) {
|
||||
const targetMap = authenticated ? authenticatedGroups : groups;
|
||||
const accessTypeMap = targetMap.get(wkId)?.get(accessType);
|
||||
|
||||
if (!accessTypeMap) return;
|
||||
|
||||
const message = JSON.stringify(messageObj);
|
||||
|
||||
for (const sAccessTypeMap of accessTypeMap.values()) {
|
||||
for (const ws of sAccessTypeMap) {
|
||||
if (ws.readyState === WebSocket.OPEN && ws !== excludeWs) {
|
||||
ws.send(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function sendToSelf(ws, messageObj) {
|
||||
|
||||
const message = JSON.stringify(messageObj);
|
||||
@@ -151,23 +189,28 @@ wss.on("connection", async (ws, req) => {
|
||||
const access = typeof params.access === "string" ? params.access : "";
|
||||
let authenticatedFreigaben = null;
|
||||
ws.isAuthenticated = false;
|
||||
ws.access = access;
|
||||
if (access === 'token' && typeof params.token === "string") {
|
||||
authenticatedFreigaben = await authenticateWithToken(params.token, ws);
|
||||
|
||||
if (!authenticatedFreigaben) return;
|
||||
|
||||
ws.send("Authentifizierung mit Token erfolgreich");
|
||||
ws.isAuthenticated = true;
|
||||
ws.access = authenticatedFreigaben.access ?? authenticatedFreigaben?.type;
|
||||
if (access !== 'token' || typeof params.token !== "string") {
|
||||
ws.send("unauthorized Access");
|
||||
ws.terminate();
|
||||
return;
|
||||
}
|
||||
|
||||
ws.authenticatedFreigaben = authenticatedFreigaben;
|
||||
authenticatedFreigaben = await authenticateWithToken(params.token, ws);
|
||||
|
||||
if (!authenticatedFreigaben) return;
|
||||
|
||||
ws.send("Authentifizierung mit Token erfolgreich");
|
||||
ws.isAuthenticated = authenticatedFreigaben.authenticated;
|
||||
ws.access = String(authenticatedFreigaben.access);
|
||||
ws.accesstype = String(authenticatedFreigaben.type); // kampfrichter, wk_leitung
|
||||
ws.wkId = String(authenticatedFreigaben.wk_id);
|
||||
|
||||
ws.isAlive = true;
|
||||
|
||||
clients.add(ws);
|
||||
|
||||
addToGroup(ws, ws.access, ws.isAuthenticated);
|
||||
addToGroup(ws, ws.wkId, ws.accesstype, ws.access, ws.isAuthenticated);
|
||||
|
||||
ws.on("pong", () => {
|
||||
ws.isAlive = true;
|
||||
@@ -189,21 +232,23 @@ wss.on("connection", async (ws, req) => {
|
||||
|
||||
if (!type) return;
|
||||
|
||||
let geraetId;
|
||||
|
||||
switch (type) {
|
||||
case "DISPLAY_CONTROL":
|
||||
if (ws.authenticatedFreigaben.type === "displaycontrol") {
|
||||
sendToGroupsContaining("_display", {
|
||||
type: "UPDATE_DISPLAYCONTROL",
|
||||
payload
|
||||
});
|
||||
} else {
|
||||
if (ws.accesstype !== 'wk_leitung' || ws.access !== "displaycontrol") {
|
||||
ws.send("Unauthorized Request");
|
||||
ws.close(4003, "Unauthorized");
|
||||
} else {
|
||||
sendToAccessType(ws.wkId, "display", {
|
||||
type: "UPDATE_DISPLAYCONTROL",
|
||||
payload
|
||||
}, false);
|
||||
}
|
||||
break;
|
||||
|
||||
case "UPDATE_SCORE":
|
||||
if (ws.authenticatedFreigaben.type !== ("kampfrichter")) {
|
||||
if (ws.accesstype !== "kampfrichter") {
|
||||
ws.send("Unauthorized Request");
|
||||
ws.close(4003, "Unauthorized");
|
||||
};
|
||||
@@ -211,19 +256,65 @@ wss.on("connection", async (ws, req) => {
|
||||
if (!payload || typeof payload !== "object") return;
|
||||
|
||||
const geraetScreen = payload.geraet;
|
||||
if (!geraetScreen) return;
|
||||
geraetId = String(payload.geraetId);
|
||||
if (!geraetScreen || !geraetId) return;
|
||||
|
||||
if (ws.authenticatedFreigaben.access !== geraetScreen && ws.authenticatedFreigaben.access !== 'admin') {
|
||||
if (ws.access !== geraetId && ws.access !== 'A') {
|
||||
ws.send("Unauthorized Request");
|
||||
ws.close(4003, "Unauthorized");
|
||||
}
|
||||
|
||||
sendToGroup(`${geraetScreen}_display`, {
|
||||
sendToAccess(ws.wkId, "display", geraetId, {
|
||||
type: "UPDATE_SCORE",
|
||||
payload: payload.data
|
||||
});
|
||||
}, false);
|
||||
|
||||
break;
|
||||
|
||||
case "UPDATE_RANKLIVE_SCORE":
|
||||
if (ws.accesstype !== "kampfrichter") {
|
||||
ws.send("Unauthorized Request");
|
||||
ws.close(4003, "Unauthorized");
|
||||
};
|
||||
|
||||
if (!payload || typeof payload !== "object") return;
|
||||
|
||||
geraetId = String(payload.geraetId);
|
||||
if (!geraetId) return;
|
||||
|
||||
if (ws.access !== geraetId && ws.access !== 'A') {
|
||||
ws.send("Unauthorized Request");
|
||||
ws.close(4003, "Unauthorized");
|
||||
}
|
||||
|
||||
sendToAccess(ws.wkId, "rankLive", geraetId, {
|
||||
type: "UPDATE",
|
||||
payload: payload
|
||||
}, false);
|
||||
|
||||
sendToAccess(ws.wkId, "rankLive", 'A', {
|
||||
type: "UPDATE",
|
||||
payload: payload
|
||||
}, false);
|
||||
break;
|
||||
|
||||
case "UPDATE_RANKLIVE_C_SUBABT":
|
||||
if (ws.accesstype !== "kampfrichter") {
|
||||
ws.send("Unauthorized Request");
|
||||
ws.close(4003, "Unauthorized");
|
||||
};
|
||||
|
||||
if (ws.access !== 'A') {
|
||||
ws.send("Unauthorized Request");
|
||||
ws.close(4003, "Unauthorized");
|
||||
}
|
||||
|
||||
sendToAccessType(ws.wkId, "rankLive", {
|
||||
type: "UPDATE_RANKLIVE_C_SUBABT",
|
||||
payload: {}
|
||||
}, false);
|
||||
break;
|
||||
|
||||
case "SELF":
|
||||
sendToSelf(ws, {
|
||||
type: "SELF",
|
||||
@@ -232,39 +323,49 @@ wss.on("connection", async (ws, req) => {
|
||||
break;
|
||||
|
||||
case "AUDIO":
|
||||
if (ws.authenticatedFreigaben.type !== ("kampfrichter")) {
|
||||
ws.send("Unauthorized Request");
|
||||
ws.close(4003, "Unauthorized");
|
||||
};
|
||||
|
||||
sendToGroup("audio", {
|
||||
type: "AUDIO",
|
||||
payload
|
||||
});
|
||||
break;
|
||||
|
||||
case "KAMPFRICHTER_UPDATE":
|
||||
if (ws.authenticatedFreigaben.type !== ("kampfrichter")) {
|
||||
if (ws.accesstype !== "kampfrichter") {
|
||||
ws.send("Unauthorized Request");
|
||||
ws.close(4003, "Unauthorized");
|
||||
};
|
||||
|
||||
if (!payload || typeof payload !== "object") return;
|
||||
|
||||
const discipline = payload.discipline;
|
||||
if (!discipline) return;
|
||||
const audioDiscipline = String(payload.audioDiscipline);
|
||||
if (!audioDiscipline) return;
|
||||
|
||||
if (ws.authenticatedFreigaben.access !== discipline && ws.authenticatedFreigaben.access !== 'admin') {
|
||||
if (ws.access !== audioDiscipline && ws.access !== 'A' && audioDiscipline !== 'A') {
|
||||
ws.send("Unauthorized Request");
|
||||
ws.close(4003, "Unauthorized");
|
||||
}
|
||||
|
||||
sendToGroup(discipline, {
|
||||
sendToAccess(ws.wkId, "audio", audioDiscipline, {
|
||||
type: "AUDIO",
|
||||
payload: payload
|
||||
}, false);
|
||||
break;
|
||||
|
||||
case "KAMPFRICHTER_UPDATE":
|
||||
if (ws.accesstype !== "kampfrichter") {
|
||||
ws.send("Unauthorized Request");
|
||||
ws.close(4003, "Unauthorized");
|
||||
};
|
||||
|
||||
if (!payload || typeof payload !== "object") return;
|
||||
|
||||
const discipline = String(payload.discipline);
|
||||
if (!discipline) return;
|
||||
|
||||
if (ws.access !== discipline && ws.access !== 'A') {
|
||||
ws.send("Unauthorized Request");
|
||||
ws.close(4003, "Unauthorized");
|
||||
}
|
||||
|
||||
sendToAccess(ws.wkId, "kampfrichter", discipline, {
|
||||
type: "UPDATE",
|
||||
payload: payload
|
||||
}, true, ws);
|
||||
|
||||
sendToGroup('admin', {
|
||||
sendToAccess(ws.wkId, "kampfrichter", 'A', {
|
||||
type: "UPDATE",
|
||||
payload: payload
|
||||
}, true, ws);
|
||||
@@ -272,7 +373,7 @@ wss.on("connection", async (ws, req) => {
|
||||
|
||||
|
||||
case "EINSTELLUNGEN_DISPLAY_UPDATE":
|
||||
if (ws.authenticatedFreigaben.type !== ("einstellungen")) {
|
||||
if (ws.accesstype !== 'wk_leitung' || ws.access !== "einstellungen") {
|
||||
ws.send("Unauthorized Request");
|
||||
ws.close(4003, "Unauthorized");
|
||||
};
|
||||
@@ -282,10 +383,15 @@ wss.on("connection", async (ws, req) => {
|
||||
const { key, value } = payload;
|
||||
if (!key) return;
|
||||
|
||||
sendToGroupsContaining("_display", {
|
||||
sendToAccessType(ws.wkId, "display", {
|
||||
type: "EINSTELLUNGEN_DISPLAY_UPDATE",
|
||||
payload: { key, value }
|
||||
});
|
||||
}, false);
|
||||
|
||||
sendToAccess(ws.wkId, 'wk_leitung', "einstellungen", {
|
||||
type: "EINSTELLUNGEN_DISPLAY_UPDATE",
|
||||
payload: { key, value }
|
||||
}, true, true);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -295,12 +401,12 @@ wss.on("connection", async (ws, req) => {
|
||||
|
||||
ws.on("close", () => {
|
||||
clients.delete(ws);
|
||||
removeFromGroup(ws, ws.access, ws.isAuthenticated);
|
||||
removeFromGroup(ws, ws.wkId, ws.accesstype, ws.access, ws.isAuthenticated);
|
||||
});
|
||||
|
||||
ws.on("error", () => {
|
||||
clients.delete(ws);
|
||||
removeFromGroup(ws, ws.access, ws.isAuthenticated);
|
||||
removeFromGroup(ws, ws.wkId, ws.accesstype, ws.access, ws.isAuthenticated);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -313,7 +419,7 @@ setInterval(() => {
|
||||
if (!ws.isAlive) {
|
||||
ws.terminate();
|
||||
clients.delete(ws);
|
||||
removeFromGroup(ws, ws.access, ws.isAuthenticated);
|
||||
removeFromGroup(ws, ws.wkId, ws.accesstype, ws.access, ws.isAuthenticated);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user