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,
|
2022-06-05 20:04:25 +00:00
|
|
|
TelnetSpec: { Options, Commands },
|
2020-05-19 00:41:23 +00:00
|
|
|
} = require('telnet-socket');
|
|
|
|
const { inherits } = require('util');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const ModuleInfo = (exports.moduleInfo = {
|
|
|
|
name: 'Telnet',
|
|
|
|
desc: 'Telnet Server v2',
|
|
|
|
author: 'NuSkooler',
|
|
|
|
isSecure: false,
|
|
|
|
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
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.dataHandler = function (data) {
|
2020-05-19 00:41:23 +00:00
|
|
|
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 => {
|
2022-06-05 20:04:25 +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) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
this._logDebug({ command, error: err.message }, 'Command error');
|
2020-05-22 01:16:40 +00:00
|
|
|
});
|
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...
|
2022-06-05 20:04:25 +00:00
|
|
|
case Options.SGA:
|
|
|
|
case Options.ECHO:
|
|
|
|
case Options.TRANSMIT_BINARY:
|
2020-05-22 03:22:15 +00:00
|
|
|
break;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
default:
|
2020-05-19 00:41:23 +00:00
|
|
|
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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
case Options.TTYPE:
|
2020-05-19 00:41:23 +00:00
|
|
|
return this.socket.sb.send.ttype();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +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
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
default:
|
2020-05-19 00:41:23 +00:00
|
|
|
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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
case Options.TTYPE:
|
2020-05-19 00:41:23 +00:00
|
|
|
this.setTermType(command.optionData.ttype);
|
|
|
|
return this._clientReady();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case Options.NEW_ENVIRON:
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
2020-05-19 00:41:23 +00:00
|
|
|
this._logDebug(
|
2022-06-05 20:04:25 +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
|
2022-06-05 20:04:25 +00:00
|
|
|
const getValue = name => {
|
|
|
|
return (
|
|
|
|
command.optionData.vars &&
|
2021-03-27 06:04:07 +00:00
|
|
|
(command.optionData.vars.find(nv => nv.name === name) ||
|
2022-06-05 20:04:25 +00:00
|
|
|
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);
|
|
|
|
}
|
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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const updateTermSize = what => {
|
2020-05-19 00:41:23 +00:00
|
|
|
const value = parseInt(getValue(what));
|
|
|
|
if (value) {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.term[
|
|
|
|
what === 'ROWS' ? 'termHeight' : 'termWidth'
|
|
|
|
] = value;
|
2022-04-26 18:36:31 +00:00
|
|
|
|
2020-05-19 00:41:23 +00:00
|
|
|
this._logDebug(
|
2022-06-05 20:04:25 +00:00
|
|
|
{ [what]: value, source: 'NEW-ENVIRON' },
|
2020-05-19 00:41:23 +00:00
|
|
|
'Window size updated'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
updateTermSize('ROWS');
|
|
|
|
updateTermSize('COLUMNS');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +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
|
|
|
|
2022-06-05 20:04:25 +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._logDebug(
|
2022-06-05 20:04:25 +00:00
|
|
|
{ width, height, source: 'NAWS' },
|
2020-05-19 00:41:23 +00:00
|
|
|
'Windows size updated'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
default:
|
2020-05-19 00:41:23 +00:00
|
|
|
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() {
|
2022-06-05 20:04:25 +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;
|
2022-06-05 20:04:25 +00:00
|
|
|
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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.server = net.createServer(socket => {
|
2020-05-19 00:41:23 +00:00
|
|
|
const client = new TelnetClient(socket);
|
2022-06-05 20:04:25 +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 => {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.info({ error: err.message }, 'Telnet server error');
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
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);
|
2022-06-05 20:04:25 +00:00
|
|
|
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 => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!err) {
|
|
|
|
Log.info(
|
|
|
|
{ server: ModuleInfo.name, port: port },
|
|
|
|
'Listening for connections'
|
|
|
|
);
|
2018-12-27 09:46:16 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.TelnetClient = TelnetClient; // WebSockets is a wrapper on top of this
|