enigma-bbs/core/servers/login/telnet.js

277 lines
8.7 KiB
JavaScript
Raw Normal View History

// ENiGMA½
2020-05-19 00:41:23 +00:00
const LoginServerModule = require('../../login_server_module');
const { Client } = require('../../client');
const Config = require('../../config').get;
const { log: Log } = require('../../logger');
2020-06-16 01:08:55 +00:00
const { Errors } = require('../../enig_error');
// deps
2020-05-19 00:41:23 +00:00
const net = require('net');
const {
TelnetSocket,
TelnetSpec: { Options, Commands }
} = require('telnet-socket');
const { inherits } = require('util');
const ModuleInfo = exports.moduleInfo = {
name : 'Telnet',
2020-05-19 00:41:23 +00:00
desc : 'Telnet Server v2',
author : 'NuSkooler',
isSecure : false,
2020-05-19 00:41:23 +00:00
packageName : 'codes.l33t.enigma.telnet.server.v2',
};
2020-05-19 00:41:23 +00:00
class TelnetClient {
constructor(socket) {
Client.apply(this, socket, socket);
2020-05-19 00:41:23 +00:00
this.socket = new TelnetSocket(socket);
2020-05-21 02:23:09 +00:00
this.setInputOutput(this.socket, this.socket);
2020-05-19 00:41:23 +00:00
//
// Wait up to 3s to hear about from our terminal type request
// then go ahead and move on...
//
setTimeout(() => {
this._clientReady();
}, 3000);
2020-05-19 00:41:23 +00:00
this.dataHandler = function(data) {
this.emit('data', data);
}.bind(this);
2020-05-19 00:41:23 +00:00
this.socket.on('data', this.dataHandler);
2020-05-19 00:41:23 +00:00
this.socket.on('error', err => {
this._logDebug({ error : err.message }, 'Socket error');
2020-05-19 00:41:23 +00:00
return this.emit('end');
});
2020-05-19 00:41:23 +00:00
this.socket.on('end', () => {
this.emit('end');
});
this.socket.on('command error', (command, err) => {
this._logDebug({ command, error : err.message }, 'Command error');
});
2020-05-21 02:23:09 +00:00
2020-05-19 00:41:23 +00:00
this.socket.on('DO', command => {
switch (command.option) {
2020-05-22 03:22:15 +00:00
// We've already stated we WILL do the following via
// the banner - some terminals will ask over and over
// if we respond to a DO with a WILL, so just don't
// do anything...
case Options.SGA :
2020-05-22 03:22:15 +00:00
case Options.ECHO :
case Options.TRANSMIT_BINARY :
2020-05-22 03:22:15 +00:00
break;
2020-05-19 00:41:23 +00:00
default :
return this.socket.command(Commands.WONT, command.option);
}
});
2020-05-19 00:41:23 +00:00
this.socket.on('DONT', command => {
this._logTrace(command, 'DONT');
});
2020-05-19 00:41:23 +00:00
this.socket.on('WILL', command => {
switch (command.option) {
case Options.TTYPE :
return this.socket.sb.send.ttype();
2020-05-19 00:41:23 +00:00
case Options.NEW_ENVIRON :
return this.socket.sb.send.new_environ(
[ 'ROWS', 'COLUMNS', 'TERM', 'TERM_PROGRAM' ]
);
2020-05-19 00:41:23 +00:00
default :
break;
}
});
2020-05-19 00:41:23 +00:00
this.socket.on('WONT', command => {
return this._logTrace(command, 'WONT');
2020-05-19 00:41:23 +00:00
});
2020-05-19 00:41:23 +00:00
this.socket.on('SB', command => {
switch (command.option) {
case Options.TTYPE :
this.setTermType(command.optionData.ttype);
return this._clientReady();
2020-05-19 00:41:23 +00:00
case Options.NEW_ENVIRON :
{
2020-05-19 00:41:23 +00:00
this._logDebug(
{ vars : command.optionData.vars, uservars : command.optionData.uservars },
2020-05-19 00:41:23 +00:00
'New environment received'
);
// get a value from vars with fallback of user vars
const getValue = (name) => {
return command.optionData.vars &&
(command.optionData.vars.find(nv => nv.name === name) ||
command.optionData.uservars.find(nv => nv.name === name)
);
2020-05-19 00:41:23 +00:00
};
if ('unknown' === this.term.termType) {
// allow from vars or user vars
const term = getValue('TERM') || getValue('TERM_PROGRAM');
if (term) {
this.setTermType(term.value);
}
}
2020-05-19 00:41:23 +00:00
if (0 === this.term.termHeight || 0 === this.term.termWidth) {
const updateTermSize = (what) => {
const value = parseInt(getValue(what));
if (value) {
this.term[what === 'ROWS' ? 'termHeight' : 'termWidth'] = value;
this.clearMciCache();
this._logDebug(
{ [ what ] : value, source : 'NEW-ENVIRON' },
'Window size updated'
);
}
};
updateTermSize('ROWS');
updateTermSize('COLUMNS');
}
}
break;
2020-05-19 00:41:23 +00:00
case Options.NAWS :
{
2020-05-19 00:41:23 +00:00
const { width, height } = command.optionData;
2020-05-19 00:41:23 +00:00
this.term.termWidth = width;
this.term.termHeight = height;
2020-05-19 00:41:23 +00:00
if (width) {
this.term.env.COLUMNS = width;
}
2020-05-19 00:41:23 +00:00
if (height) {
this.term.env.ROWS = height;
}
2020-05-19 00:41:23 +00:00
this.clearMciCache();
2020-05-19 00:41:23 +00:00
this._logDebug(
{ width, height, source : 'NAWS' },
'Windows size updated'
);
}
break;
2020-05-19 00:41:23 +00:00
default :
return this._logTrace(command, 'SB');
}
2020-05-19 00:41:23 +00:00
});
2020-05-19 00:41:23 +00:00
this.socket.on('IP', command => {
this._logDebug(command, 'Interrupt Process (IP) - Ending session');
return this.disconnect();
});
2020-06-16 01:08:55 +00:00
this.socket.on('AYT', command => {
2020-05-19 00:41:23 +00:00
this.socket.write('\b');
return this._logTrace(command, 'Are You There (AYT) - Replied');
});
}
2020-05-21 02:23:09 +00:00
get dataPassthrough() {
return this.socket.passthrough;
}
set dataPassthrough(passthrough) {
this.socket.passthrough = passthrough;
}
2020-05-19 00:41:23 +00:00
disconnect() {
try {
2020-05-19 00:41:23 +00:00
return this.socket.rawSocket.end();
} catch (e) {
// ignored
}
}
2017-08-19 19:59:09 +00:00
2020-05-19 01:19:30 +00:00
banner() {
2020-05-31 20:54:33 +00:00
this.socket.dont.echo(); // don't echo characters
this.socket.will.echo(); // ...we'll echo them back
2020-05-19 01:19:30 +00:00
this.socket.will.sga();
this.socket.do.sga();
this.socket.do.transmit_binary();
this.socket.will.transmit_binary();
this.socket.do.ttype();
this.socket.do.naws();
this.socket.do.new_environ();
}
2020-05-19 00:41:23 +00:00
_logTrace(info, msg) {
if (Config().loginServers.telnet.traceConnections) {
const log = this.log || Log;
return log.trace(info, `Telnet: ${msg}`);
}
}
2020-05-19 00:41:23 +00:00
_logDebug(info, msg) {
const log = this.log || Log;
return log.debug(info, `Telnet: ${msg}`);
}
2020-05-19 00:41:23 +00:00
_clientReady() {
if (this.clientReadyHandled) {
return; // already processed
}
2020-05-19 00:41:23 +00:00
this.clientReadyHandled = true;
this.emit('ready', { firstMenu : Config().loginServers.telnet.firstMenu } );
}
2020-06-16 01:08:55 +00:00
}
2020-05-19 00:41:23 +00:00
inherits(TelnetClient, Client);
exports.getModule = class TelnetServerModule extends LoginServerModule {
constructor() {
super();
}
createServer(cb) {
2020-05-19 00:41:23 +00:00
this.server = net.createServer( socket => {
const client = new TelnetClient(socket);
2020-05-19 01:19:30 +00:00
client.banner(); // start negotiations
2020-05-19 00:41:23 +00:00
this.handleNewClient(client, socket, ModuleInfo);
});
this.server.on('error', err => {
Log.info( { error : err.message }, 'Telnet server error');
});
return cb(null);
}
2018-12-27 09:46:16 +00:00
listen(cb) {
const config = Config();
const port = parseInt(config.loginServers.telnet.port);
if(isNaN(port)) {
Log.error( { server : ModuleInfo.name, port : config.loginServers.telnet.port }, 'Cannot load server (invalid port)' );
2018-12-27 09:46:16 +00:00
return cb(Errors.Invalid(`Invalid port: ${config.loginServers.telnet.port}`));
}
2019-04-10 02:25:14 +00:00
this.server.listen(port, config.loginServers.telnet.address, err => {
2018-12-27 09:46:16 +00:00
if(!err) {
Log.info( { server : ModuleInfo.name, port : port }, 'Listening for connections' );
}
return cb(err);
});
}
};
2020-05-19 00:41:23 +00:00
exports.TelnetClient = TelnetClient; // WebSockets is a wrapper on top of this