2018-06-23 03:26:46 +00:00
|
|
|
// 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');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// 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');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
2016-10-25 03:49:45 +00:00
|
|
|
const ModuleInfo = exports.moduleInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'Telnet',
|
2020-05-19 00:41:23 +00:00
|
|
|
desc : 'Telnet Server v2',
|
2018-06-23 03:26:46 +00:00
|
|
|
author : 'NuSkooler',
|
|
|
|
isSecure : false,
|
2020-05-19 00:41:23 +00:00
|
|
|
packageName : 'codes.l33t.enigma.telnet.server.v2',
|
2016-09-20 03:28:50 +00:00
|
|
|
};
|
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
class TelnetClient {
|
|
|
|
constructor(socket) {
|
|
|
|
Client.apply(this, socket, socket);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
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);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
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);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
this.dataHandler = function(data) {
|
|
|
|
this.emit('data', data);
|
|
|
|
}.bind(this);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
this.socket.on('data', this.dataHandler);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
this.socket.on('error', err => {
|
2020-05-22 01:16:40 +00:00
|
|
|
this._logDebug({ error : err.message }, 'Socket error');
|
2020-05-19 00:41:23 +00:00
|
|
|
return this.emit('end');
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
this.socket.on('end', () => {
|
|
|
|
this.emit('end');
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-22 01:16:40 +00:00
|
|
|
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...
|
2020-05-22 01:16:40 +00:00
|
|
|
case Options.SGA :
|
2020-05-22 03:22:15 +00:00
|
|
|
case Options.ECHO :
|
2020-05-22 01:16:40 +00:00
|
|
|
case Options.TRANSMIT_BINARY :
|
2020-05-22 03:22:15 +00:00
|
|
|
break;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
default :
|
|
|
|
return this.socket.command(Commands.WONT, command.option);
|
|
|
|
}
|
|
|
|
});
|
2016-09-20 03:28:50 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
this.socket.on('DONT', command => {
|
|
|
|
this._logTrace(command, 'DONT');
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
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();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
case Options.NEW_ENVIRON :
|
|
|
|
return this.socket.sb.send.new_environ(
|
|
|
|
[ 'ROWS', 'COLUMNS', 'TERM', 'TERM_PROGRAM' ]
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
default :
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
this.socket.on('WONT', command => {
|
2020-05-31 17:49:32 +00:00
|
|
|
return this._logTrace(command, 'WONT');
|
2020-05-19 00:41:23 +00:00
|
|
|
});
|
2018-06-22 05:15:04 +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();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
case Options.NEW_ENVIRON :
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
2020-05-19 00:41:23 +00:00
|
|
|
this._logDebug(
|
2021-10-22 16:29:47 +00:00
|
|
|
{ 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) => {
|
2021-03-27 06:04:07 +00:00
|
|
|
return command.optionData.vars &&
|
|
|
|
(command.optionData.vars.find(nv => nv.name === name) ||
|
2021-10-22 16:29:47 +00:00
|
|
|
command.optionData.uservars.find(nv => nv.name === name)
|
2021-03-27 06:04:07 +00:00
|
|
|
);
|
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);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
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');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
case Options.NAWS :
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
2020-05-19 00:41:23 +00:00
|
|
|
const { width, height } = command.optionData;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
this.term.termWidth = width;
|
|
|
|
this.term.termHeight = height;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
if (width) {
|
|
|
|
this.term.env.COLUMNS = width;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
if (height) {
|
|
|
|
this.term.env.ROWS = height;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
this.clearMciCache();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
this._logDebug(
|
|
|
|
{ width, height, source : 'NAWS' },
|
|
|
|
'Windows size updated'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
default :
|
|
|
|
return this._logTrace(command, 'SB');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2020-05-19 00:41:23 +00:00
|
|
|
});
|
2018-06-22 05:15:04 +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();
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
}
|
2019-02-02 17:20:22 +00:00
|
|
|
|
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() {
|
2019-02-02 17:20:22 +00:00
|
|
|
try {
|
2020-05-19 00:41:23 +00:00
|
|
|
return this.socket.rawSocket.end();
|
|
|
|
} catch (e) {
|
|
|
|
// ignored
|
2019-02-02 17:20:22 +00:00
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
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}`);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2017-09-01 23:42:20 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
_logDebug(info, msg) {
|
|
|
|
const log = this.log || Log;
|
|
|
|
return log.debug(info, `Telnet: ${msg}`);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
_clientReady() {
|
|
|
|
if (this.clientReadyHandled) {
|
|
|
|
return; // already processed
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
this.clientReadyHandled = true;
|
|
|
|
this.emit('ready', { firstMenu : Config().loginServers.telnet.firstMenu } );
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2020-06-16 01:08:55 +00:00
|
|
|
}
|
2016-09-20 03:28:50 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
inherits(TelnetClient, Client);
|
2016-09-20 03:28:50 +00:00
|
|
|
|
2016-10-25 03:49:45 +00:00
|
|
|
exports.getModule = class TelnetServerModule extends LoginServerModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:19:26 +00:00
|
|
|
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);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.server.on('error', err => {
|
|
|
|
Log.info( { error : err.message }, 'Telnet server error');
|
|
|
|
});
|
2018-12-27 09:19:26 +00:00
|
|
|
|
|
|
|
return cb(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:46:16 +00:00
|
|
|
listen(cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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}`));
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2016-09-20 03:28:50 +00:00
|
|
|
};
|
2020-05-19 00:41:23 +00:00
|
|
|
|
|
|
|
exports.TelnetClient = TelnetClient; // WebSockets is a wrapper on top of this
|