From d0025b42218336dd050d923d99c1819a5b93fdc1 Mon Sep 17 00:00:00 2001 From: hippoz Date: Tue, 1 Dec 2020 22:28:35 +0200 Subject: [PATCH] add forceful refresh of user clients to push updates and erase bad messages from the dom --- api/v1/gateway/index.js | 62 ++++++++++++++++++++++++++++++++++++++++- app/resources/js/app.js | 20 +++++++++++-- index.js | 11 +++++++- 3 files changed, 89 insertions(+), 4 deletions(-) diff --git a/api/v1/gateway/index.js b/api/v1/gateway/index.js index 9f50acb..e647e9d 100644 --- a/api/v1/gateway/index.js +++ b/api/v1/gateway/index.js @@ -18,9 +18,63 @@ class GatewayServer { minPoints: 0 }); this.eventSetup(); + + this._commandPrefix = '/'; } } +GatewayServer.prototype._sendSystemMessage = function(socket, message, category) { + const messageObject = { + author: { + username: '__SYSTEM', + _id: '5fc69864f15a7c5e504c9a1f' + }, + category: { + title: category.title, + _id: category._id + }, + content: message, + _id: uuid.v4() + }; + + socket.emit('message', messageObject); +}; + +GatewayServer.prototype.notifyClientsOfUpdate = function(reason) { + this._gateway.emit('refreshClient', { reason: reason || 'REFRESH' }); +}; + +GatewayServer.prototype._processCommand = function(socket, message) { + const content = message.content; + const fullCommandString = content.slice(this._commandPrefix.length, content.length); + const fullCommand = fullCommandString.split(' '); + const command = fullCommand[0] || 'INVALID_COMMAND'; + const args = fullCommand.length - 1; + + switch (command) { + case 'INVALID_COMMAND': { + this._sendSystemMessage(socket, 'Invalid command.', message.category); + break; + } + case 'admin/fr': { + if (args === 1) { + if (socket.user.permissionLevel >= config.roleMap.ADMIN) { + this._gateway.emit('refreshClient', { reason: fullCommand[1] || 'REFRESH' }); + } else { + this._sendSystemMessage(socket, 'how about no', message.category); + } + } else { + this._sendSystemMessage(socket, 'Invalid number of arguments.', message.category); + } + break; + } + default: { + this._sendSystemMessage(socket, 'That command does not exist.', message.category); + break; + } + } +}; + GatewayServer.prototype.authDisconnect = function(socket, callback) { console.log('[E] [gateway] [handshake] User disconnected due to failed authentication'); socket.isConnected = false; @@ -74,7 +128,8 @@ GatewayServer.prototype.eventSetup = function() { socket.user = { username: data.username, _id: user._id, - token // Maybe not secure + token, // Maybe not secure + permissionLevel }; console.log(`[*] [gateway] [handshake] User ${data.username} has successfully authenticated`); return callback(); @@ -123,6 +178,11 @@ GatewayServer.prototype.eventSetup = function() { _id: uuid.v4() }; + if (messageObject.content.startsWith(this._commandPrefix)) { + this._processCommand(socket, messageObject); + return; + } + this._gateway.in(category._id).emit('message', messageObject); }); diff --git a/app/resources/js/app.js b/app/resources/js/app.js index 659443f..e0631a0 100755 --- a/app/resources/js/app.js +++ b/app/resources/js/app.js @@ -177,10 +177,10 @@ const app = new Vue({ }, gateway: new GatewayConnection(), messages: { - 'X': [ { username: 'NONEXISTENT_TEST_ACCOUNT', content: 'TEST MSG' } ] + 'X': [ { username: '__SYSTEM', content: 'TEST MSG' } ] }, userLists: { - 'X': [ { username: 'NONEXISTENT_TEST_ACCOUNT', _id: 'INVALID_ID' } ] + 'X': [ { username: '__SYSTEM', _id: 'INVALID_ID' } ] }, message: { typed: '' @@ -238,6 +238,22 @@ const app = new Vue({ console.log('[*] [gateway] Client list update', e); this.processUserListUpdate(e); }); + this.gateway.socket.on('refreshClient', (e) => { + console.log('[*] [gateway] Gateway requested refresh', e); + if (e.reason === 'exit') { + this.showApp = false; + this.okNotification('The server has exited. Sit tight!'); + } else if (e.reason === 'upd') { + this.showApp = false; + this.okNotification('An update has just rolled out! To ensure everything runs smoothly, you need to refresh the page!'); + } else { + this.showApp = false; + this.okNotification('Sorry, but something happened and a refresh is required to keep using the app!'); + } + this.snackbarEditButton('Refresh', () => { + window.location.reload(); + }); + }); }, processMessage: async function(messageObject) { if (!this.messages[messageObject.category._id]) this.$set(this.messages, messageObject.category._id, []); diff --git a/index.js b/index.js index 0eff46c..f7ed2a3 100755 --- a/index.js +++ b/index.js @@ -27,12 +27,21 @@ app.use(express.static('app')); app.get('/', authenticateEndpoint((req, res, user) => { res.redirect('/app.html'); -}, `/auth.html`)); +}, '/auth.html')); app.get('/admin', (req, res) => { res.send('Keanu chungus wholesome 100 reddit moment 😀i beat up a kid that said minecraft bad 😂and my doggo bit him so i gave him snaccos😉 and we watched pewdiepie together while in elon musk’s cyber truck 😳talking about how superior reddit memers are : “haha emojis bad” 😲i said and keanu reeves came outta nowhere and said “this is wholesome 100, updoot this wholesome boy” 😗so i got alot of updoots and edit: thanks for the gold kind stranger😣. but the kind stranger revealed himself to be baby yoda eating chiccy nuggies😨 and drinking choccy milk😎 so we went to the cinema to see our (communism funny) favorite movies avengers endgame😆 but then thor played fortnite and fortnite bad😡, so then i said “reality is often dissappointing” and then baby yoda replied r/unexpectedthanos and i replied by r/expectedthanos😖 for balance and then danny devito came to pick us up from the cinema😩 and all the insta normies and gay mods stood watching😵 ,as we,superior redditors went home with danny devito to suck on his magnum dong😫 but i said no homo and started sucking,not like those gay mods😮,then the next morning we woke up to MrBeast telling us to plant 69420 million trees😌, me, baby yoda and danny said nice, and then on our way to plant 69420 million trees😊 (nice) we saw a kid doing a tiktok so keanu reeves appeared and said “we have a kid to burn” and i replied “you’re breathtaking”😄 so i said “i need a weapon” and baby yoda gave me an RPG so i blew the kid (DESTRUCTION 100)😎 and posted it on r/memes and r/dankmemes and r/pewdiepiesubmissions and got 1000000000 updoots😘,i’m sure pewds will give me a big pp, then we shat on emoji users😂😂 and started dreaming about girls that will never like me😢 and posted a lie on r/teenagers about how i got a GF after my doggo died by the hands of fortnite players😳 so i exploited his death for updoots😜, but i watched the sunset with the wholesome gang😁 (keanu,danny,Mrbeast, pewds, spongebob,stefan karl , bob ross, steve irwin, baby yoda and other artists that reddit exploits them) [Everyone liked that] WHOLESOME 100 REDDIT 100🤡'); }); +const onServerClosing = (evt) => { + gateway.notifyClientsOfUpdate('exit'); + process.exit(); +}; + +['exit', 'SIGINT', 'SIGUSR1', 'SIGUSR2', 'uncaughtException', 'SIGTERM'].forEach((eventType) => { + process.on(eventType, onServerClosing.bind(null, eventType)); +}) + httpServer.listen(config.ports.mainServerPort, () => { console.log(`[*] [server] Main server is listening on port ${config.ports.mainServerPort}`); }); \ No newline at end of file