From 34206b40ad8377986af08ed2bc648a520a0da2ab Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Sat, 12 Mar 2022 13:53:39 -0800 Subject: [PATCH] Fix session rebind and share between https and wss Signed-off-by: James Ketrenos --- client/src/MediaControl.js | 1 + server/app.js | 1 + server/routes/games.js | 43 +++++++++++++++++++++++++------------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/client/src/MediaControl.js b/client/src/MediaControl.js index 534fcec..d04db3c 100644 --- a/client/src/MediaControl.js +++ b/client/src/MediaControl.js @@ -285,6 +285,7 @@ const MediaAgent = () => { update = true; peers[name] = { local: true, + muted: true, attributes: { } }; diff --git a/server/app.js b/server/app.js index 4b09ef0..531cd10 100755 --- a/server/app.js +++ b/server/app.js @@ -80,6 +80,7 @@ const sessionParser = session({ }); app.use(sessionParser); +app.locals.sessionParser = sessionParser; const index = require("./routes/index"); diff --git a/server/routes/games.js b/server/routes/games.js index 2661af2..b08b844 100755 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -19,7 +19,7 @@ const debug = { audio: false, get: true, set: true, - update: true + update: false//true }; let gameDB; @@ -496,7 +496,7 @@ const loadGame = async (id) => { if (game) { try { game = JSON.parse(game); - console.log(`${info} Creating backup of games/${id}`); + console.log(`${info}: Creating backup of games/${id}`); await writeFile(`games/${id}.bk`, JSON.stringify(game)); } catch (error) { console.log(`Attempting to load backup from games/${id}.bk`); @@ -2822,7 +2822,7 @@ const part = (peers, session, id) => { const getName = (session) => { - return session.name ? session.name : "Unnamed"; + return session.name ? session.name : session.id; } const saveGame = async (game) => { @@ -2989,21 +2989,34 @@ const sendWarning = (session, warning) => { session.ws.send(JSON.stringify({ type: 'warning', warning })); } -router.ws("/ws/:id", async (ws, req) => { +router.ws("/ws/:id", (ws, req) => { + /* Connect the WebSocket to the app's sessionParser */ + req.app.locals.sessionParser(req, {}, () => { + if (!req.session.player_id) { + req.session.player_id = crypto.randomBytes(16).toString('hex'); + console.log(`[${req.session.player_id.substring(0, 8)}]: wss - New session connected`); + } else { + console.log(`[${req.session.player_id.substring(0, 8)}]: wss - Existing session being used`); + } + wsConnect(ws, req); + }); +}); + +const wsConnect = async (ws, req) => { const { id } = req.params; const gameId = id; if (!req.session.player_id) { - req.session.player_id = crypto.randomBytes(16).toString('hex'); + throw new Error(`player_id not set from http load`); } const short = `[${req.session.player_id.substring(0, 8)}]`; ws.id = short; - console.log(`${short}:${gameId} - New connection from client.`); + console.log(`${short}: Game ${gameId} - New connection from client.`); if (!(id in audio)) { audio[id] = {}; /* List of peer sockets using session.name as index. */ - console.log(`${short}:${id} - New Game Audio`); + console.log(`${short}: Game ${id} - New Game Audio`); } else { - console.log(`${short}:${id} - Already has Audio`); + console.log(`${short}: Game ${id} - Already has Audio`); } /* Setup WebSocket event handlers prior to performing any async calls or @@ -3121,7 +3134,7 @@ router.ws("/ws/:id", async (ws, req) => { break; case 'game-update': - console.log(`${short}:${id}:${getName(session)} - full game update.`); + console.log(`${short}: <- game-update ${getName(session)} - full game update.`); sendGameToPlayer(game, session); break; @@ -3182,7 +3195,7 @@ router.ws("/ws/:id", async (ws, req) => { update.players = game.players; break; case 'color': - console.log(`${session.id}: -> Returning color as ${session.color} for ${session.name}`); + console.log(`${session.id}: -> Returning color as ${session.color} for ${getName(session)}`); update.color = session.color; break; case 'timestamp': @@ -3276,13 +3289,13 @@ router.ws("/ws/:id", async (ws, req) => { } resetDisconnectCheck(game, req); - console.log(`${short}:WebSocket connect from game ${id}:${getName(session)}`); + console.log(`${short}: Game ${id} - WebSocket connect from ${getName(session)}`); if (session.keepAlive) { clearTimeout(session.keepAlive); } session.keepAlive = setTimeout(() => { ping(session); }, 2500); -}); +}; const debugChat = (game, preamble) => { preamble = `Degug ${preamble.trim()}`; @@ -3695,7 +3708,6 @@ const createGame = (id) => { /* Look for a new game with random words that does not already exist */ while (!id) { id = randomWords(4).join('-'); - console.log(`Looking for ${id}`); try { /* If file can be read, it already exists so look for a new name */ accessSync(`games/${id}`, fs.F_OK); @@ -3704,6 +3716,7 @@ const createGame = (id) => { break; } } + console.log(`${info} creating ${id}`); const game = { id: id, @@ -3737,9 +3750,11 @@ router.post("/", (req, res/*, next*/) => { const game = createGame(); if (!req.session.player_id) { req.session.player_id = crypto.randomBytes(16).toString('hex'); + console.log(`[${req.session.player_id.substring(0, 8)}]: https - New session connected`); + } else { + console.log(`[${req.session.player_id.substring(0, 8)}]: https - Existing session being used`); } const session = getSession(game, req.session); - console.log(`${session.id}: - load game via http`); saveGame(game); return res.status(200).send(getFilteredGameForPlayer(game, session)); });