From 39ec02d3c4cb5f5980b172d30404210de2479f0f Mon Sep 17 00:00:00 2001 From: marzavec Date: Tue, 13 Mar 2018 22:26:53 -0700 Subject: Streamlined modules, server tweaks, better feedback --- server/src/commands/admin/addmod.js | 14 +++++---- server/src/commands/admin/listusers.js | 2 +- server/src/commands/admin/reload.js | 6 ++-- server/src/commands/admin/saveconfig.js | 2 +- server/src/commands/admin/shout.js | 2 +- server/src/commands/core/chat.js | 6 ++-- server/src/commands/core/help.js | 5 ++- server/src/commands/core/invite.js | 8 ++--- server/src/commands/core/join.js | 17 ++++++----- server/src/commands/core/morestats.js | 54 +++++++++++++++++++++++++++++++++ server/src/commands/core/showcase.js | 21 ++++++++----- server/src/commands/core/stats.js | 26 ++-------------- server/src/commands/mod/ban.js | 26 ++++++++-------- server/src/commands/mod/kick.js | 18 ++++------- server/src/commands/mod/unban.js | 27 +++++++++++++---- 15 files changed, 143 insertions(+), 91 deletions(-) create mode 100644 server/src/commands/core/morestats.js (limited to 'server/src/commands') diff --git a/server/src/commands/admin/addmod.js b/server/src/commands/admin/addmod.js index dba5aba..e9dde2c 100644 --- a/server/src/commands/admin/addmod.js +++ b/server/src/commands/admin/addmod.js @@ -1,5 +1,5 @@ /* - + Description: Adds the target trip to the mod list then elevates the uType */ 'use strict'; @@ -16,14 +16,16 @@ exports.run = async (core, server, socket, data) => { core.config.mods.push(mod); // purposely not using `config.set()` to avoid auto-save - for (let client of server.clients) { - if (typeof client.trip !== 'undefined' && client.trip === data.trip) { - client.uType = 'mod'; + let newMod = server.findSockets({ trip: data.trip }); + + if (newMod.length !== 0) { + for (let i = 0, l = newMod.length; i < l; i++) { + newMod[i].uType = 'mod'; - server.reply({ + server.send({ cmd: 'info', text: 'You are now a mod.' - }, client); + }, newMod[i]); } } diff --git a/server/src/commands/admin/listusers.js b/server/src/commands/admin/listusers.js index 1ddb16b..226b000 100644 --- a/server/src/commands/admin/listusers.js +++ b/server/src/commands/admin/listusers.js @@ -1,5 +1,5 @@ /* - + Description: Outputs all current channels and their user nicks */ 'use strict'; diff --git a/server/src/commands/admin/reload.js b/server/src/commands/admin/reload.js index 88e40c7..387ae97 100644 --- a/server/src/commands/admin/reload.js +++ b/server/src/commands/admin/reload.js @@ -1,5 +1,5 @@ /* - + Description: Clears and resets the command modules, outputting any errors */ 'use strict'; @@ -14,7 +14,9 @@ exports.run = async (core, server, socket, data) => { loadResult += core.commands.loadCommands(); if (loadResult == '') { - loadResult = 'Commands reloaded without errors!'; + loadResult = `Loaded ${core.commands._commands.length} commands, 0 errors`; + } else { + loadResult = `Loaded ${core.commands._commands.length} commands, error(s): ${loadResult}`; } server.reply({ diff --git a/server/src/commands/admin/saveconfig.js b/server/src/commands/admin/saveconfig.js index 4c6dff8..0c4e0c9 100644 --- a/server/src/commands/admin/saveconfig.js +++ b/server/src/commands/admin/saveconfig.js @@ -1,5 +1,5 @@ /* - + Description: Writes any changes to the config to the disk */ 'use strict'; diff --git a/server/src/commands/admin/shout.js b/server/src/commands/admin/shout.js index c3cfded..736134a 100644 --- a/server/src/commands/admin/shout.js +++ b/server/src/commands/admin/shout.js @@ -1,5 +1,5 @@ /* - + Description: Emmits a server-wide message as `info` */ 'use strict'; diff --git a/server/src/commands/core/chat.js b/server/src/commands/core/chat.js index c086077..ee45425 100644 --- a/server/src/commands/core/chat.js +++ b/server/src/commands/core/chat.js @@ -1,10 +1,10 @@ /* - + Description: Rebroadcasts any `text` to all clients in a `channel` */ 'use strict'; -function parseText(text) { +const parseText = (text) => { if (typeof text !== 'string') { return false; } @@ -15,7 +15,7 @@ function parseText(text) { text = text.replace(/\n{3,}/g, "\n\n"); return text; -} +}; exports.run = async (core, server, socket, data) => { let text = parseText(data.text); diff --git a/server/src/commands/core/help.js b/server/src/commands/core/help.js index 7b5237c..71cc745 100644 --- a/server/src/commands/core/help.js +++ b/server/src/commands/core/help.js @@ -1,5 +1,5 @@ /* - + Description: Outputs the current command module list or command categories */ 'use strict'; @@ -44,9 +44,8 @@ exports.run = async (core, server, socket, data) => { }, socket); }; -// optional parameters are marked, all others are required exports.info = { - name: 'help', // actual command name + name: 'help', usage: 'help ([ type:categories] | [category: | command: ])', description: 'Outputs information about the servers current protocol' }; diff --git a/server/src/commands/core/invite.js b/server/src/commands/core/invite.js index 7d162d6..5889562 100644 --- a/server/src/commands/core/invite.js +++ b/server/src/commands/core/invite.js @@ -1,12 +1,12 @@ /* - + Description: Generates a semi-unique channel name then broadcasts it to each client */ 'use strict'; -function verifyNickname(nick) { +const verifyNickname = (nick) => { return /^[a-zA-Z0-9_]{1,24}$/.test(nick); -} +}; exports.run = async (core, server, socket, data) => { if (typeof data.nick !== 'string') { @@ -19,7 +19,7 @@ exports.run = async (core, server, socket, data) => { } if (data.nick == socket.nick) { - // TODO: reply with something witty? They invited themself + // They invited themself return; } diff --git a/server/src/commands/core/join.js b/server/src/commands/core/join.js index 609e39d..e896361 100644 --- a/server/src/commands/core/join.js +++ b/server/src/commands/core/join.js @@ -1,20 +1,20 @@ /* - + Description: Initial entry point, applies `channel` and `nick` to the calling socket */ 'use strict'; const crypto = require('crypto'); -function hash(password) { - var sha = crypto.createHash('sha256'); +const hash = (password) => { + let sha = crypto.createHash('sha256'); sha.update(password); return sha.digest('base64').substr(0, 6); -} +}; -function verifyNickname(nick) { +const verifyNickname = (nick) => { return /^[a-zA-Z0-9_]{1,24}$/.test(nick); -} +}; exports.run = async (core, server, socket, data) => { if (server._police.frisk(socket.remoteAddress, 3)) { @@ -53,7 +53,7 @@ exports.run = async (core, server, socket, data) => { text: 'Nickname must consist of up to 24 letters, numbers, and underscores' }, socket); - return + return; } for (let client of server.clients) { @@ -99,7 +99,8 @@ exports.run = async (core, server, socket, data) => { server.broadcast({ cmd: 'onlineAdd', nick: nick, - trip: trip || 'null' + trip: trip || 'null', + hash: server.getSocketHash(socket) }, { channel: channel }); socket.uType = uType; diff --git a/server/src/commands/core/morestats.js b/server/src/commands/core/morestats.js new file mode 100644 index 0000000..887d663 --- /dev/null +++ b/server/src/commands/core/morestats.js @@ -0,0 +1,54 @@ +/* + Description: Outputs more info than the legacy stats command +*/ + +'use strict'; + +const stripIndents = require('common-tags').stripIndents; + +const formatTime = (time) => { + let seconds = time[0] + time[1] / 1e9; + + let minutes = Math.floor(seconds / 60); + seconds = seconds % 60; + + let hours = Math.floor(minutes / 60); + minutes = minutes % 60; + return `${hours.toFixed(0)}h ${minutes.toFixed(0)}m ${seconds.toFixed(0)}s`; +}; + +exports.run = async (core, server, socket, data) => { + let ips = {}; + let channels = {}; + for (let client of server.clients) { + if (client.channel) { + channels[client.channel] = true; + ips[client.remoteAddress] = true; + } + } + + let uniqueClientCount = Object.keys(ips).length; + let uniqueChannels = Object.keys(channels).length; + + ips = null; + channels = null; + + server.reply({ + cmd: 'info', + text: stripIndents`current-connections: ${uniqueClientCount} + current-channels: ${uniqueChannels} + users-joined: ${(core.managers.stats.get('users-joined') || 0)} + invites-sent: ${(core.managers.stats.get('invites-sent') || 0)} + messages-sent: ${(core.managers.stats.get('messages-sent') || 0)} + users-banned: ${(core.managers.stats.get('users-banned') || 0)} + stats-requested: ${(core.managers.stats.get('stats-requested') || 0)} + server-uptime: ${formatTime(process.hrtime(core.managers.stats.get('start-time')))}` + }, socket); + + core.managers.stats.increment('stats-requested'); +}; + +exports.info = { + name: 'morestats', + description: 'Sends back current server stats to the calling client' +}; diff --git a/server/src/commands/core/showcase.js b/server/src/commands/core/showcase.js index 5d15ea2..5eca4a2 100644 --- a/server/src/commands/core/showcase.js +++ b/server/src/commands/core/showcase.js @@ -1,5 +1,5 @@ /* - + Description: This is a template module that should not be on prod */ 'use strict'; @@ -14,21 +14,26 @@ const createReply = (echoInput) => { return `You want me to echo: ${echoInput}?` }; -// `exports.run()` is required and will always be passed (core, server, socket, data) -// be sure it's asyn too -// this is the main function +/* + `exports.run()` is required and will always be passed (core, server, socket, data) + + be sure it's async too + this is the main function that will run when called +*/ exports.run = async (core, server, socket, data) => { server.reply({ cmd: 'info', - text: `SHOWCASE MODULE: ${core.showcase} - ${this.createReply(data.echo)}` + text: `SHOWCASE MODULE: ${core.showcase} - ${createReply(data.echo)}` }, socket); }; -// `exports.init()` is optional, and will only be run when the module is loaded into memory -// it will always be passed a reference to the global core class -// note: this will fire again if a reload is issued, keep that in mind +/* + `exports.init()` is optional, and will only be run when the module is loaded into memory + it will always be passed a reference to the global core class + note: this will fire again if a reload is issued, keep that in mind +*/ exports.init = (core) => { if (typeof core.showcase === 'undefined') { core.showcase = 'init is a handy place to put global data by assigning it to `core`'; diff --git a/server/src/commands/core/stats.js b/server/src/commands/core/stats.js index ba28319..a1abddb 100644 --- a/server/src/commands/core/stats.js +++ b/server/src/commands/core/stats.js @@ -1,22 +1,9 @@ /* - + Description: Legacy stats output, kept for compatibility, outputs user and channel count */ 'use strict'; -const stripIndents = require('common-tags').stripIndents; - -const formatTime = (time) => { - let seconds = time[0] + time[1] / 1e9; - - let minutes = Math.floor(seconds / 60); - seconds = seconds % 60; - - let hours = Math.floor(minutes / 60); - minutes = minutes % 60; - return `${hours.toFixed(0)}h ${minutes.toFixed(0)}m ${seconds.toFixed(0)}s`; -}; - exports.run = async (core, server, socket, data) => { let ips = {}; let channels = {}; @@ -35,14 +22,7 @@ exports.run = async (core, server, socket, data) => { server.reply({ cmd: 'info', - text: stripIndents`current-connections: ${uniqueClientCount} - current-channels: ${uniqueChannels} - users-joined: ${(core.managers.stats.get('users-joined') || 0)} - invites-sent: ${(core.managers.stats.get('invites-sent') || 0)} - messages-sent: ${(core.managers.stats.get('messages-sent') || 0)} - users-banned: ${(core.managers.stats.get('users-banned') || 0)} - stats-requested: ${(core.managers.stats.get('stats-requested') || 0)} - server-uptime: ${formatTime(process.hrtime(core.managers.stats.get('start-time')))}` + text: `${uniqueClientCount} unique IPs in ${uniqueChannels} channels` }, socket); core.managers.stats.increment('stats-requested'); @@ -50,5 +30,5 @@ exports.run = async (core, server, socket, data) => { exports.info = { name: 'stats', - description: 'Sends back current server stats to the calling client' + description: 'Sends back legacy server stats to the calling client' }; diff --git a/server/src/commands/mod/ban.js b/server/src/commands/mod/ban.js index 1880ef3..5ee77b6 100644 --- a/server/src/commands/mod/ban.js +++ b/server/src/commands/mod/ban.js @@ -1,5 +1,5 @@ /* - + Description: Adds the target socket's ip to the ratelimiter */ 'use strict'; @@ -15,16 +15,9 @@ exports.run = async (core, server, socket, data) => { } let targetNick = data.nick; - let badClient = null; - for (let client of server.clients) { - // Find badClient's socket - if (client.channel == socket.channel && client.nick == targetNick) { - badClient = client; - break; - } - } + let badClient = server.findSockets({ channel: socket.channel, nick: targetNick }); - if (!badClient) { + if (badClient.length === 0) { server.reply({ cmd: 'warn', text: 'Could not find user in channel' @@ -33,6 +26,8 @@ exports.run = async (core, server, socket, data) => { return; } + badClient = badClient[0]; + if (badClient.uType !== 'user') { server.reply({ cmd: 'warn', @@ -42,16 +37,21 @@ exports.run = async (core, server, socket, data) => { return; } - // TODO: add reference to banned users nick or unban by nick cmd + // TODO unban by hash server._police.arrest(badClient.remoteAddress); - // TODO: add event to log? console.log(`${socket.nick} [${socket.trip}] banned ${targetNick} in ${socket.channel}`); server.broadcast({ cmd: 'info', text: `Banned ${targetNick}` - }, { channel: socket.channel }); + }, { channel: socket.channel, uType: 'user' }); + + server.broadcast({ + cmd: 'info', + text: `${socket.nick} banned ${targetNick} in ${socket.channel}, userhash: ${server.getSocketHash(badClient)}` + }, { uType: 'mod' }); + badClient.close(); core.managers.stats.increment('users-banned'); diff --git a/server/src/commands/mod/kick.js b/server/src/commands/mod/kick.js index a730caf..f51d576 100644 --- a/server/src/commands/mod/kick.js +++ b/server/src/commands/mod/kick.js @@ -1,5 +1,5 @@ /* - + Description: Forces a change on the target socket's channel, then broadcasts event */ 'use strict'; @@ -15,16 +15,9 @@ exports.run = async (core, server, socket, data) => { } let targetNick = data.nick; - let badClient = null; - for (let client of server.clients) { - // Find badClient's socket - if (client.channel == socket.channel && client.nick == targetNick) { - badClient = client; - break; - } - } + let badClient = server.findSockets({ channel: socket.channel, nick: targetNick }); - if (!badClient) { + if (badClient.length === 0) { server.reply({ cmd: 'warn', text: 'Could not find user in channel' @@ -33,6 +26,8 @@ exports.run = async (core, server, socket, data) => { return; } + badClient = badClient[0]; + if (badClient.uType !== 'user') { server.reply({ cmd: 'warn', @@ -42,7 +37,6 @@ exports.run = async (core, server, socket, data) => { return; } - // TODO: add event to log? let newChannel = Math.random().toString(36).substr(2, 8); badClient.channel = newChannel; @@ -54,7 +48,7 @@ exports.run = async (core, server, socket, data) => { nick: targetNick }, { channel: socket.channel }); - // publicly broadcast event (TODO: should this be supressed?) + // publicly broadcast event server.broadcast({ cmd: 'info', text: `Kicked ${targetNick}` diff --git a/server/src/commands/mod/unban.js b/server/src/commands/mod/unban.js index 193b614..ee028d2 100644 --- a/server/src/commands/mod/unban.js +++ b/server/src/commands/mod/unban.js @@ -1,5 +1,5 @@ /* - + Description: Removes a target ip from the ratelimiter */ 'use strict'; @@ -15,17 +15,32 @@ exports.run = async (core, server, socket, data) => { } let ip = data.ip; - let nick = data.nick; // for future upgrade + let hash = data.hash; // TODO unban by hash + + // TODO unban by hash + let recordFound = server._police.pardon(data.ip); + + if (!recordFound) { + server.reply({ + cmd: 'warn', + text: 'Could not find target in records' + }, socket); + + return; + } - // TODO: support remove by nick future upgrade - server._police.pardon(badClient.remoteAddress); - console.log(`${socket.nick} [${socket.trip}] unbanned ${/*nick || */ip} in ${socket.channel}`); + console.log(`${socket.nick} [${socket.trip}] unbanned ${/*hash || */ip} in ${socket.channel}`); server.reply({ cmd: 'info', - text: `Unbanned ${/*nick || */ip}` + text: `${socket.nick} unbanned a userhash: ${server.getSocketHash(ip)}` }, socket); + server.broadcast({ + cmd: 'info', + text: `${socket.nick} unbanned a userhash: ${server.getSocketHash(ip)}` + }, { uType: 'mod' }); + core.managers.stats.decrement('users-banned'); }; -- cgit v1.2.1