First pass formatting with Prettier
* Added .prettierrc.json * Added .prettierignore * Formatted
This commit is contained in:
@@ -2,30 +2,29 @@
|
||||
'use strict';
|
||||
|
||||
// ENiGMA½
|
||||
const Log = require('../../logger.js').log;
|
||||
const { ServerModule } = require('../../server_module.js');
|
||||
const Config = require('../../config.js').get;
|
||||
const { Errors } = require('../../enig_error.js');
|
||||
const SysProps = require('../../system_property.js');
|
||||
const StatLog = require('../../stat_log.js');
|
||||
const Log = require('../../logger.js').log;
|
||||
const { ServerModule } = require('../../server_module.js');
|
||||
const Config = require('../../config.js').get;
|
||||
const { Errors } = require('../../enig_error.js');
|
||||
const SysProps = require('../../system_property.js');
|
||||
const StatLog = require('../../stat_log.js');
|
||||
|
||||
// deps
|
||||
const net = require('net');
|
||||
const _ = require('lodash');
|
||||
const os = require('os');
|
||||
|
||||
const net = require('net');
|
||||
const _ = require('lodash');
|
||||
const os = require('os');
|
||||
|
||||
// MRC
|
||||
const protocolVersion = '1.2.9';
|
||||
const lineDelimiter = new RegExp('\r\n|\r|\n'); // eslint-disable-line no-control-regex
|
||||
const protocolVersion = '1.2.9';
|
||||
const lineDelimiter = new RegExp('\r\n|\r|\n'); // eslint-disable-line no-control-regex
|
||||
|
||||
const ModuleInfo = exports.moduleInfo = {
|
||||
name : 'MRC',
|
||||
desc : 'An MRC Chat Multiplexer',
|
||||
author : 'RiPuk',
|
||||
packageName : 'codes.l33t.enigma.mrc.server',
|
||||
notes : 'https://bbswiki.bottomlessabyss.net/index.php?title=MRC_Chat_platform',
|
||||
};
|
||||
const ModuleInfo = (exports.moduleInfo = {
|
||||
name: 'MRC',
|
||||
desc: 'An MRC Chat Multiplexer',
|
||||
author: 'RiPuk',
|
||||
packageName: 'codes.l33t.enigma.mrc.server',
|
||||
notes: 'https://bbswiki.bottomlessabyss.net/index.php?title=MRC_Chat_platform',
|
||||
});
|
||||
|
||||
const connectedSockets = new Set();
|
||||
|
||||
@@ -33,29 +32,30 @@ exports.getModule = class MrcModule extends ServerModule {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.log = Log.child( { server : 'MRC' } );
|
||||
this.log = Log.child({ server: 'MRC' });
|
||||
|
||||
const config = Config();
|
||||
this.boardName = config.general.prettyBoardName || config.general.boardName;
|
||||
const config = Config();
|
||||
this.boardName = config.general.prettyBoardName || config.general.boardName;
|
||||
this.mrcConnectOpts = {
|
||||
host : config.chatServers.mrc.serverHostname || 'mrc.bottomlessabyss.net',
|
||||
port : config.chatServers.mrc.serverPort || 5000,
|
||||
retryDelay : config.chatServers.mrc.retryDelay || 10000
|
||||
host: config.chatServers.mrc.serverHostname || 'mrc.bottomlessabyss.net',
|
||||
port: config.chatServers.mrc.serverPort || 5000,
|
||||
retryDelay: config.chatServers.mrc.retryDelay || 10000,
|
||||
};
|
||||
}
|
||||
|
||||
_connectionHandler() {
|
||||
const enigmaVersion = 'ENiGMA½-BBS_' + require('../../../package.json').version;
|
||||
|
||||
const handshake = `${this.boardName}~${enigmaVersion}/${os.platform()}.${os.arch()}/${protocolVersion}`;
|
||||
this.log.debug({ handshake : handshake }, 'Handshaking with MRC server');
|
||||
const handshake = `${
|
||||
this.boardName
|
||||
}~${enigmaVersion}/${os.platform()}.${os.arch()}/${protocolVersion}`;
|
||||
this.log.debug({ handshake: handshake }, 'Handshaking with MRC server');
|
||||
|
||||
this.sendRaw(handshake);
|
||||
this.log.info(this.mrcConnectOpts, 'Connected to MRC server');
|
||||
}
|
||||
|
||||
createServer(cb) {
|
||||
|
||||
if (!this.enabled) {
|
||||
return cb(null);
|
||||
}
|
||||
@@ -74,11 +74,19 @@ exports.getModule = class MrcModule extends ServerModule {
|
||||
const config = Config();
|
||||
|
||||
const port = parseInt(config.chatServers.mrc.multiplexerPort);
|
||||
if(isNaN(port)) {
|
||||
this.log.warn( { port : config.chatServers.mrc.multiplexerPort, server : ModuleInfo.name }, 'Invalid port' );
|
||||
return cb(Errors.Invalid(`Invalid port: ${config.chatServers.mrc.multiplexerPort}`));
|
||||
if (isNaN(port)) {
|
||||
this.log.warn(
|
||||
{ port: config.chatServers.mrc.multiplexerPort, server: ModuleInfo.name },
|
||||
'Invalid port'
|
||||
);
|
||||
return cb(
|
||||
Errors.Invalid(`Invalid port: ${config.chatServers.mrc.multiplexerPort}`)
|
||||
);
|
||||
}
|
||||
Log.info( { server : ModuleInfo.name, port : config.chatServers.mrc.multiplexerPort }, 'MRC multiplexer starting up');
|
||||
Log.info(
|
||||
{ server: ModuleInfo.name, port: config.chatServers.mrc.multiplexerPort },
|
||||
'MRC multiplexer starting up'
|
||||
);
|
||||
return this.server.listen(port, cb);
|
||||
}
|
||||
|
||||
@@ -89,7 +97,10 @@ exports.getModule = class MrcModule extends ServerModule {
|
||||
const self = this;
|
||||
|
||||
// create connection to MRC server
|
||||
this.mrcClient = net.createConnection(this.mrcConnectOpts, self._connectionHandler.bind(self));
|
||||
this.mrcClient = net.createConnection(
|
||||
this.mrcConnectOpts,
|
||||
self._connectionHandler.bind(self)
|
||||
);
|
||||
|
||||
this.mrcClient.requestedDisconnect = false;
|
||||
|
||||
@@ -97,7 +108,7 @@ exports.getModule = class MrcModule extends ServerModule {
|
||||
let buffer = new Buffer.from('');
|
||||
|
||||
function handleData(chunk) {
|
||||
if(_.isString(chunk)) {
|
||||
if (_.isString(chunk)) {
|
||||
buffer += chunk;
|
||||
} else {
|
||||
buffer = Buffer.concat([buffer, chunk]);
|
||||
@@ -113,7 +124,7 @@ exports.getModule = class MrcModule extends ServerModule {
|
||||
buffer = new Buffer.from('');
|
||||
}
|
||||
|
||||
lines.forEach( line => {
|
||||
lines.forEach(line => {
|
||||
if (line.length) {
|
||||
let message = self.parseMessage(line);
|
||||
if (message) {
|
||||
@@ -123,7 +134,7 @@ exports.getModule = class MrcModule extends ServerModule {
|
||||
});
|
||||
}
|
||||
|
||||
this.mrcClient.on('data', (data) => {
|
||||
this.mrcClient.on('data', data => {
|
||||
handleData(data);
|
||||
});
|
||||
|
||||
@@ -132,52 +143,61 @@ exports.getModule = class MrcModule extends ServerModule {
|
||||
});
|
||||
|
||||
this.mrcClient.on('close', () => {
|
||||
if (this.mrcClient && this.mrcClient.requestedDisconnect) return;
|
||||
|
||||
if (this.mrcClient && this.mrcClient.requestedDisconnect)
|
||||
return;
|
||||
this.log.info(
|
||||
this.mrcConnectOpts,
|
||||
'Disconnected from MRC server, reconnecting'
|
||||
);
|
||||
this.log.debug(
|
||||
'Waiting ' + this.mrcConnectOpts.retryDelay + 'ms before retrying'
|
||||
);
|
||||
|
||||
this.log.info(this.mrcConnectOpts, 'Disconnected from MRC server, reconnecting');
|
||||
this.log.debug('Waiting ' + this.mrcConnectOpts.retryDelay + 'ms before retrying');
|
||||
|
||||
setTimeout(function() {
|
||||
setTimeout(function () {
|
||||
self.connectToMrc();
|
||||
}, this.mrcConnectOpts.retryDelay);
|
||||
});
|
||||
|
||||
this.mrcClient.on('error', err => {
|
||||
this.log.info( { error : err.message }, 'MRC server error');
|
||||
this.log.info({ error: err.message }, 'MRC server error');
|
||||
});
|
||||
}
|
||||
|
||||
createLocalListener() {
|
||||
// start a local server for clients to connect to
|
||||
|
||||
this.server = net.createServer( socket => {
|
||||
this.server = net.createServer(socket => {
|
||||
socket.setEncoding('ascii');
|
||||
|
||||
socket.on('data', data => {
|
||||
// split on \n to deal with getting messages in batches
|
||||
data.toString().split(lineDelimiter).forEach( item => {
|
||||
if (item == '') return;
|
||||
data.toString()
|
||||
.split(lineDelimiter)
|
||||
.forEach(item => {
|
||||
if (item == '') return;
|
||||
|
||||
// save username with socket
|
||||
if(item.startsWith('--DUDE-ITS--')) {
|
||||
connectedSockets.add(socket);
|
||||
socket.username = item.split('|')[1];
|
||||
Log.debug( { server : 'MRC', user: socket.username } , 'User connected');
|
||||
} else {
|
||||
this.receiveFromClient(socket.username, item);
|
||||
}
|
||||
});
|
||||
// save username with socket
|
||||
if (item.startsWith('--DUDE-ITS--')) {
|
||||
connectedSockets.add(socket);
|
||||
socket.username = item.split('|')[1];
|
||||
Log.debug(
|
||||
{ server: 'MRC', user: socket.username },
|
||||
'User connected'
|
||||
);
|
||||
} else {
|
||||
this.receiveFromClient(socket.username, item);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('end', function() {
|
||||
socket.on('end', function () {
|
||||
connectedSockets.delete(socket);
|
||||
});
|
||||
|
||||
socket.on('error', err => {
|
||||
if('ECONNRESET' !== err.code) { // normal
|
||||
this.log.error( { error: err.message }, 'MRC error' );
|
||||
if ('ECONNRESET' !== err.code) {
|
||||
// normal
|
||||
this.log.error({ error: err.message }, 'MRC error');
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -196,8 +216,14 @@ exports.getModule = class MrcModule extends ServerModule {
|
||||
* Sends received messages to local clients
|
||||
*/
|
||||
sendToClient(message) {
|
||||
connectedSockets.forEach( (client) => {
|
||||
if (message.to_user == '' || message.to_user == client.username || message.to_user == 'CLIENT' || message.from_user == client.username || message.to_user == 'NOTME' ) {
|
||||
connectedSockets.forEach(client => {
|
||||
if (
|
||||
message.to_user == '' ||
|
||||
message.to_user == client.username ||
|
||||
message.to_user == 'CLIENT' ||
|
||||
message.from_user == client.username ||
|
||||
message.to_user == 'NOTME'
|
||||
) {
|
||||
// this.log.debug({ server : 'MRC', username : client.username, message : message }, 'Forwarding message to connected user');
|
||||
client.write(JSON.stringify(message) + '\n');
|
||||
}
|
||||
@@ -212,16 +238,59 @@ exports.getModule = class MrcModule extends ServerModule {
|
||||
|
||||
if (message.from_user == 'SERVER' && message.body == 'HELLO') {
|
||||
// reply with extra bbs info
|
||||
this.sendToMrcServer('CLIENT', '', 'SERVER', 'ALL', '', `INFOSYS:${StatLog.getSystemStat(SysProps.SysOpUsername)}`);
|
||||
this.sendToMrcServer('CLIENT', '', 'SERVER', 'ALL', '', `INFOWEB:${config.general.website}`);
|
||||
this.sendToMrcServer('CLIENT', '', 'SERVER', 'ALL', '', `INFOTEL:${config.general.telnetHostname}`);
|
||||
this.sendToMrcServer('CLIENT', '', 'SERVER', 'ALL', '', `INFOSSH:${config.general.sshHostname}`);
|
||||
this.sendToMrcServer('CLIENT', '', 'SERVER', 'ALL', '', `INFODSC:${config.general.description}`);
|
||||
|
||||
} else if (message.from_user == 'SERVER' && message.body.toUpperCase() == 'PING') {
|
||||
this.sendToMrcServer(
|
||||
'CLIENT',
|
||||
'',
|
||||
'SERVER',
|
||||
'ALL',
|
||||
'',
|
||||
`INFOSYS:${StatLog.getSystemStat(SysProps.SysOpUsername)}`
|
||||
);
|
||||
this.sendToMrcServer(
|
||||
'CLIENT',
|
||||
'',
|
||||
'SERVER',
|
||||
'ALL',
|
||||
'',
|
||||
`INFOWEB:${config.general.website}`
|
||||
);
|
||||
this.sendToMrcServer(
|
||||
'CLIENT',
|
||||
'',
|
||||
'SERVER',
|
||||
'ALL',
|
||||
'',
|
||||
`INFOTEL:${config.general.telnetHostname}`
|
||||
);
|
||||
this.sendToMrcServer(
|
||||
'CLIENT',
|
||||
'',
|
||||
'SERVER',
|
||||
'ALL',
|
||||
'',
|
||||
`INFOSSH:${config.general.sshHostname}`
|
||||
);
|
||||
this.sendToMrcServer(
|
||||
'CLIENT',
|
||||
'',
|
||||
'SERVER',
|
||||
'ALL',
|
||||
'',
|
||||
`INFODSC:${config.general.description}`
|
||||
);
|
||||
} else if (
|
||||
message.from_user == 'SERVER' &&
|
||||
message.body.toUpperCase() == 'PING'
|
||||
) {
|
||||
// reply to heartbeat
|
||||
this.sendToMrcServer('CLIENT', '', 'SERVER', 'ALL', '', `IMALIVE:${this.boardName}`);
|
||||
|
||||
this.sendToMrcServer(
|
||||
'CLIENT',
|
||||
'',
|
||||
'SERVER',
|
||||
'ALL',
|
||||
'',
|
||||
`IMALIVE:${this.boardName}`
|
||||
);
|
||||
} else {
|
||||
// if not a heartbeat, and we have clients then we need to send something to them
|
||||
this.sendToClient(message);
|
||||
@@ -232,8 +301,8 @@ exports.getModule = class MrcModule extends ServerModule {
|
||||
* Takes an MRC message and parses it into something usable
|
||||
*/
|
||||
parseMessage(line) {
|
||||
|
||||
const [from_user, from_site, from_room, to_user, to_site, to_room, body ] = line.split('~');
|
||||
const [from_user, from_site, from_room, to_user, to_site, to_room, body] =
|
||||
line.split('~');
|
||||
|
||||
// const msg = line.split('~');
|
||||
// if (msg.length < 7) {
|
||||
@@ -249,9 +318,19 @@ exports.getModule = class MrcModule extends ServerModule {
|
||||
receiveFromClient(username, message) {
|
||||
try {
|
||||
message = JSON.parse(message);
|
||||
this.sendToMrcServer(message.from_user, message.from_room, message.to_user, message.to_site, message.to_room, message.body);
|
||||
this.sendToMrcServer(
|
||||
message.from_user,
|
||||
message.from_room,
|
||||
message.to_user,
|
||||
message.to_site,
|
||||
message.to_room,
|
||||
message.body
|
||||
);
|
||||
} catch (e) {
|
||||
Log.debug({ server : 'MRC', user : username, message : message }, 'Dodgy message received from client');
|
||||
Log.debug(
|
||||
{ server: 'MRC', user: username, message: message },
|
||||
'Dodgy message received from client'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,16 +338,16 @@ exports.getModule = class MrcModule extends ServerModule {
|
||||
* Converts a message back into the MRC format and sends it to the central MRC server
|
||||
*/
|
||||
sendToMrcServer(fromUser, fromRoom, toUser, toSite, toRoom, messageBody) {
|
||||
|
||||
const line = [
|
||||
fromUser,
|
||||
this.boardName,
|
||||
sanitiseRoomName(fromRoom || ''),
|
||||
sanitiseName(toUser || ''),
|
||||
sanitiseName(toSite || ''),
|
||||
sanitiseRoomName(toRoom || ''),
|
||||
sanitiseMessage(messageBody || '')
|
||||
].join('~') + '~';
|
||||
const line =
|
||||
[
|
||||
fromUser,
|
||||
this.boardName,
|
||||
sanitiseRoomName(fromRoom || ''),
|
||||
sanitiseName(toUser || ''),
|
||||
sanitiseName(toSite || ''),
|
||||
sanitiseRoomName(toRoom || ''),
|
||||
sanitiseMessage(messageBody || ''),
|
||||
].join('~') + '~';
|
||||
|
||||
// Log.debug({ server : 'MRC', data : line }, 'Sending data');
|
||||
this.sendRaw(line);
|
||||
@@ -284,13 +363,13 @@ exports.getModule = class MrcModule extends ServerModule {
|
||||
* User / site name must be ASCII 33-125, no MCI, 30 chars max, underscores
|
||||
*/
|
||||
function sanitiseName(str) {
|
||||
return str.replace(
|
||||
/\s/g, '_'
|
||||
).replace(
|
||||
/[^\x21-\x7D]|(\|\w\w)/g, '' // Non-printable & MCI
|
||||
).substr(
|
||||
0, 30
|
||||
);
|
||||
return str
|
||||
.replace(/\s/g, '_')
|
||||
.replace(
|
||||
/[^\x21-\x7D]|(\|\w\w)/g,
|
||||
'' // Non-printable & MCI
|
||||
)
|
||||
.substr(0, 30);
|
||||
}
|
||||
|
||||
function sanitiseRoomName(message) {
|
||||
@@ -300,4 +379,3 @@ function sanitiseRoomName(message) {
|
||||
function sanitiseMessage(message) {
|
||||
return message.replace(/[^\x20-\x7D]/g, '');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user