enigma-bbs/core/servers/chat/mrc_multiplexer.js

382 lines
11 KiB
JavaScript
Raw Normal View History

2019-05-17 23:25:35 +00:00
/* jslint node: true */
'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');
2019-05-17 23:25:35 +00:00
// deps
const net = require('net');
const _ = require('lodash');
const os = require('os');
2019-05-20 22:37:32 +00:00
2019-05-17 23:25:35 +00:00
// MRC
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',
});
2019-05-17 23:25:35 +00:00
const connectedSockets = new Set();
exports.getModule = class MrcModule extends ServerModule {
constructor() {
super();
this.log = Log.child({ server: 'MRC' });
2019-05-31 21:00:48 +00:00
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,
};
}
_connectionHandler() {
2019-05-31 19:32:16 +00:00
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');
2019-05-31 19:32:16 +00:00
this.sendRaw(handshake);
this.log.info(this.mrcConnectOpts, 'Connected to MRC server');
2019-05-17 23:25:35 +00:00
}
createServer(cb) {
if (!this.enabled) {
return cb(null);
}
this.connectToMrc();
this.createLocalListener();
return cb(null);
}
listen(cb) {
if (!this.enabled) {
return cb(null);
}
2019-05-17 23:25:35 +00:00
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}`)
);
}
Log.info(
{ server: ModuleInfo.name, port: config.chatServers.mrc.multiplexerPort },
'MRC multiplexer starting up'
);
return this.server.listen(port, cb);
}
2019-05-17 23:25:35 +00:00
/**
* Handles connecting to to the MRC server
*/
connectToMrc() {
const self = this;
2019-05-17 23:25:35 +00:00
// create connection to MRC server
this.mrcClient = net.createConnection(
this.mrcConnectOpts,
self._connectionHandler.bind(self)
);
this.mrcClient.requestedDisconnect = false;
2019-05-17 23:25:35 +00:00
// do things when we get data from MRC central
2019-05-31 19:32:16 +00:00
let buffer = new Buffer.from('');
function handleData(chunk) {
if (_.isString(chunk)) {
buffer += chunk;
} else {
buffer = Buffer.concat([buffer, chunk]);
}
2019-06-09 15:19:34 +00:00
let lines = buffer.toString().split(lineDelimiter);
if (lines.pop()) {
// if buffer is not ended with \r\n, there's more chunks.
return;
} else {
// else, initialize the buffer.
buffer = new Buffer.from('');
}
lines.forEach(line => {
if (line.length) {
let message = self.parseMessage(line);
if (message) {
self.receiveFromMRC(message);
}
2019-05-20 22:37:32 +00:00
}
2019-05-17 23:25:35 +00:00
});
}
this.mrcClient.on('data', data => {
handleData(data);
2019-05-17 23:25:35 +00:00
});
this.mrcClient.on('end', () => {
this.log.info(this.mrcConnectOpts, 'Disconnected from MRC server');
});
this.mrcClient.on('close', () => {
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'
);
setTimeout(function () {
self.connectToMrc();
}, this.mrcConnectOpts.retryDelay);
2019-05-17 23:25:35 +00:00
});
this.mrcClient.on('error', err => {
this.log.info({ error: err.message }, 'MRC server error');
2019-05-17 23:25:35 +00:00
});
}
createLocalListener() {
2019-05-17 23:25:35 +00:00
// start a local server for clients to connect to
2019-05-31 19:32:16 +00:00
this.server = net.createServer(socket => {
2019-05-17 23:25:35 +00:00
socket.setEncoding('ascii');
2019-05-17 23:25:35 +00:00
socket.on('data', data => {
// split on \n to deal with getting messages in batches
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);
}
});
2019-05-17 23:25:35 +00:00
});
socket.on('end', function () {
2019-05-17 23:25:35 +00:00
connectedSockets.delete(socket);
});
socket.on('error', err => {
if ('ECONNRESET' !== err.code) {
// normal
this.log.error({ error: err.message }, 'MRC error');
2019-05-17 23:25:35 +00:00
}
});
});
}
get enabled() {
return _.get(Config(), 'chatServers.mrc.enabled', false) && this.isConfigured();
}
isConfigured() {
const config = Config();
return _.isNumber(_.get(config, 'chatServers.mrc.multiplexerPort'));
}
2019-05-25 23:14:36 +00:00
/**
* Sends received messages to local clients
*/
2019-05-20 22:37:32 +00:00
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'
) {
2019-05-31 21:00:48 +00:00
// this.log.debug({ server : 'MRC', username : client.username, message : message }, 'Forwarding message to connected user');
2019-05-20 22:37:32 +00:00
client.write(JSON.stringify(message) + '\n');
}
2019-05-17 23:25:35 +00:00
});
}
2019-05-25 23:14:36 +00:00
/**
* Processes messages received from the central MRC server
2019-05-25 23:14:36 +00:00
*/
receiveFromMRC(message) {
2019-05-17 23:25:35 +00:00
const config = Config();
if (message.from_user == 'SERVER' && message.body == 'HELLO') {
2019-05-25 23:14:36 +00:00
// 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'
) {
2019-05-17 23:25:35 +00:00
// reply to heartbeat
this.sendToMrcServer(
'CLIENT',
'',
'SERVER',
'ALL',
'',
`IMALIVE:${this.boardName}`
);
2019-05-17 23:25:35 +00:00
} else {
// if not a heartbeat, and we have clients then we need to send something to them
2019-05-20 22:37:32 +00:00
this.sendToClient(message);
2019-05-17 23:25:35 +00:00
}
}
2019-05-25 23:14:36 +00:00
/**
* Takes an MRC message and parses it into something usable
*/
2019-05-17 23:25:35 +00:00
parseMessage(line) {
const [from_user, from_site, from_room, to_user, to_site, to_room, body] =
line.split('~');
2019-05-31 19:32:16 +00:00
// const msg = line.split('~');
// if (msg.length < 7) {
// return;
// }
return { from_user, from_site, from_room, to_user, to_site, to_room, body };
2019-05-17 23:25:35 +00:00
}
2019-05-25 23:14:36 +00:00
/**
* Receives a message from a local client and sanity checks before sending on to the central MRC server
*/
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
);
} catch (e) {
Log.debug(
{ server: 'MRC', user: username, message: message },
'Dodgy message received from client'
);
}
}
2019-05-25 23:14:36 +00:00
/**
* 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('~') + '~';
// Log.debug({ server : 'MRC', data : line }, 'Sending data');
this.sendRaw(line);
}
sendRaw(message) {
// optionally log messages here
2019-05-31 19:32:16 +00:00
this.mrcClient.write(message + '\n');
}
2019-05-17 23:25:35 +00:00
};
2019-05-25 23:14:36 +00:00
/**
* User / site name must be ASCII 33-125, no MCI, 30 chars max, underscores
*/
2019-05-17 23:25:35 +00:00
function sanitiseName(str) {
return str
.replace(/\s/g, '_')
.replace(
/[^\x21-\x7D]|(\|\w\w)/g,
'' // Non-printable & MCI
)
.substr(0, 30);
2019-05-17 23:25:35 +00:00
}
function sanitiseRoomName(message) {
return message.replace(/[^\x21-\x7D]|(\|\w\w)/g, '').substr(0, 30);
}
function sanitiseMessage(message) {
return message.replace(/[^\x20-\x7D]/g, '');
}