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/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 ++--------------- 7 files changed, 88 insertions(+), 49 deletions(-) create mode 100644 server/src/commands/core/morestats.js (limited to 'server/src/commands/core') 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' }; -- cgit v1.2.1