2019-05-17 23:25:35 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
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
|
2022-06-05 20:04:25 +00:00
|
|
|
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
|
2022-06-05 20:04:25 +00:00
|
|
|
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();
|
2019-05-31 00:16:32 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.log = Log.child({ server: 'MRC' });
|
2019-05-31 21:00:48 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const config = Config();
|
|
|
|
this.boardName = config.general.prettyBoardName || config.general.boardName;
|
2019-05-31 00:16:32 +00:00
|
|
|
this.mrcConnectOpts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
host: config.chatServers.mrc.serverHostname || 'mrc.bottomlessabyss.net',
|
|
|
|
port: config.chatServers.mrc.serverPort || 5000,
|
|
|
|
retryDelay: config.chatServers.mrc.retryDelay || 10000,
|
2019-05-31 00:16:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_connectionHandler() {
|
2019-05-31 19:32:16 +00:00
|
|
|
const enigmaVersion = 'ENiGMA½-BBS_' + require('../../../package.json').version;
|
2019-05-31 00:16:32 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const handshake = `${
|
|
|
|
this.boardName
|
|
|
|
}~${enigmaVersion}/${os.platform()}.${os.arch()}/${protocolVersion}`;
|
|
|
|
this.log.debug({ handshake: handshake }, 'Handshaking with MRC server');
|
2019-05-31 00:16:32 +00:00
|
|
|
|
2019-05-31 19:32:16 +00:00
|
|
|
this.sendRaw(handshake);
|
2019-05-31 00:16:32 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-05-22 22:43:41 +00:00
|
|
|
|
2019-05-31 00:16:32 +00:00
|
|
|
this.connectToMrc();
|
|
|
|
this.createLocalListener();
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
listen(cb) {
|
|
|
|
if (!this.enabled) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
2019-05-22 22:43:41 +00:00
|
|
|
|
2019-05-17 23:25:35 +00:00
|
|
|
const config = Config();
|
|
|
|
|
2019-05-31 00:16:32 +00:00
|
|
|
const port = parseInt(config.chatServers.mrc.multiplexerPort);
|
2022-06-05 20:04:25 +00:00
|
|
|
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}`)
|
|
|
|
);
|
2019-05-31 00:16:32 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.info(
|
|
|
|
{ server: ModuleInfo.name, port: config.chatServers.mrc.multiplexerPort },
|
|
|
|
'MRC multiplexer starting up'
|
|
|
|
);
|
2019-05-31 00:16:32 +00:00
|
|
|
return this.server.listen(port, cb);
|
|
|
|
}
|
2019-05-17 23:25:35 +00:00
|
|
|
|
2019-05-31 00:16:32 +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
|
2022-06-05 20:04:25 +00:00
|
|
|
this.mrcClient = net.createConnection(
|
|
|
|
this.mrcConnectOpts,
|
|
|
|
self._connectionHandler.bind(self)
|
|
|
|
);
|
2019-05-31 00:16:32 +00:00
|
|
|
|
|
|
|
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('');
|
2019-05-31 00:16:32 +00:00
|
|
|
|
|
|
|
function handleData(chunk) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(chunk)) {
|
2019-05-31 00:16:32 +00:00
|
|
|
buffer += chunk;
|
|
|
|
} else {
|
|
|
|
buffer = Buffer.concat([buffer, chunk]);
|
|
|
|
}
|
|
|
|
|
2019-06-09 15:19:34 +00:00
|
|
|
let lines = buffer.toString().split(lineDelimiter);
|
2019-05-31 00:16:32 +00:00
|
|
|
|
|
|
|
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('');
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
lines.forEach(line => {
|
2019-05-31 00:16:32 +00:00
|
|
|
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
|
|
|
});
|
2019-05-31 00:16:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.mrcClient.on('data', data => {
|
2019-05-31 00:16:32 +00:00
|
|
|
handleData(data);
|
2019-05-17 23:25:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.mrcClient.on('end', () => {
|
2019-05-31 00:16:32 +00:00
|
|
|
this.log.info(this.mrcConnectOpts, 'Disconnected from MRC server');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.mrcClient.on('close', () => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.mrcClient && this.mrcClient.requestedDisconnect) return;
|
2019-05-31 00:16:32 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.log.info(
|
|
|
|
this.mrcConnectOpts,
|
|
|
|
'Disconnected from MRC server, reconnecting'
|
|
|
|
);
|
|
|
|
this.log.debug(
|
|
|
|
'Waiting ' + this.mrcConnectOpts.retryDelay + 'ms before retrying'
|
|
|
|
);
|
2019-05-31 00:16:32 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
setTimeout(function () {
|
2019-05-31 00:16:32 +00:00
|
|
|
self.connectToMrc();
|
|
|
|
}, this.mrcConnectOpts.retryDelay);
|
2019-05-17 23:25:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.mrcClient.on('error', err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.log.info({ error: err.message }, 'MRC server error');
|
2019-05-17 23:25:35 +00:00
|
|
|
});
|
2019-05-31 00:16:32 +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
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.server = net.createServer(socket => {
|
2019-05-17 23:25:35 +00:00
|
|
|
socket.setEncoding('ascii');
|
2019-05-22 22:43:41 +00:00
|
|
|
|
2019-05-17 23:25:35 +00:00
|
|
|
socket.on('data', data => {
|
|
|
|
// split on \n to deal with getting messages in batches
|
2022-06-05 20:04:25 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
socket.on('end', function () {
|
2019-05-17 23:25:35 +00:00
|
|
|
connectedSockets.delete(socket);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('error', err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
connectedSockets.forEach(client => {
|
|
|
|
if (
|
|
|
|
message.to_user == '' ||
|
2023-03-31 17:46:07 +00:00
|
|
|
// Fix PrivMSG delivery on case mismatch
|
|
|
|
message.to_user.toUpperCase() == client.username.toUpperCase() ||
|
2022-06-05 20:04:25 +00:00
|
|
|
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
|
|
|
/**
|
2019-05-31 00:16:32 +00:00
|
|
|
* Processes messages received from the central MRC server
|
2019-05-25 23:14:36 +00:00
|
|
|
*/
|
2019-05-31 00:16:32 +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
|
2022-06-05 20:04:25 +00:00
|
|
|
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
|
2022-06-05 20:04:25 +00:00
|
|
|
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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
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
|
|
|
|
*/
|
2019-05-22 22:43:41 +00:00
|
|
|
receiveFromClient(username, message) {
|
|
|
|
try {
|
|
|
|
message = JSON.parse(message);
|
2022-06-05 20:04:25 +00:00
|
|
|
this.sendToMrcServer(
|
|
|
|
message.from_user,
|
|
|
|
message.from_room,
|
|
|
|
message.to_user,
|
|
|
|
message.to_site,
|
|
|
|
message.to_room,
|
|
|
|
message.body
|
|
|
|
);
|
2019-05-22 22:43:41 +00:00
|
|
|
} catch (e) {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.debug(
|
|
|
|
{ server: 'MRC', user: username, message: message },
|
|
|
|
'Dodgy message received from client'
|
|
|
|
);
|
2019-05-22 22:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 23:14:36 +00:00
|
|
|
/**
|
|
|
|
* Converts a message back into the MRC format and sends it to the central MRC server
|
|
|
|
*/
|
2019-05-31 00:16:32 +00:00
|
|
|
sendToMrcServer(fromUser, fromRoom, toUser, toSite, toRoom, messageBody) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const line =
|
|
|
|
[
|
|
|
|
fromUser,
|
|
|
|
this.boardName,
|
|
|
|
sanitiseRoomName(fromRoom || ''),
|
|
|
|
sanitiseName(toUser || ''),
|
|
|
|
sanitiseName(toSite || ''),
|
|
|
|
sanitiseRoomName(toRoom || ''),
|
|
|
|
sanitiseMessage(messageBody || ''),
|
|
|
|
].join('~') + '~';
|
2019-05-22 22:43:41 +00:00
|
|
|
|
2019-06-05 22:41:28 +00:00
|
|
|
// Log.debug({ server : 'MRC', data : line }, 'Sending data');
|
2019-05-31 00:16:32 +00:00
|
|
|
this.sendRaw(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendRaw(message) {
|
|
|
|
// optionally log messages here
|
2019-05-31 19:32:16 +00:00
|
|
|
this.mrcClient.write(message + '\n');
|
2019-05-22 22:43:41 +00:00
|
|
|
}
|
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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
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, '');
|
|
|
|
}
|