2016-06-26 04:45:49 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const resetScreen = require('./ansi_term.js').resetScreen;
|
|
|
|
const setSyncTermFontWithAlias = require('./ansi_term.js').setSyncTermFontWithAlias;
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const net = require('net');
|
|
|
|
const EventEmitter = require('events');
|
2020-05-28 03:05:19 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
TelnetSocket,
|
2022-06-05 20:04:25 +00:00
|
|
|
TelnetSpec: { Commands, Options, SubNegotiationCommands },
|
2020-05-28 03:05:19 +00:00
|
|
|
} = require('telnet-socket');
|
2016-06-26 04:45:49 +00:00
|
|
|
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
Expected configuration block:
|
|
|
|
|
|
|
|
{
|
|
|
|
module: telnet_bridge
|
|
|
|
...
|
|
|
|
config: {
|
|
|
|
host: somehost.net
|
|
|
|
port: 23
|
|
|
|
}
|
|
|
|
}
|
2016-06-26 04:45:49 +00:00
|
|
|
*/
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: ENH: Support nodeMax and tooManyArt
|
2016-06-26 04:45:49 +00:00
|
|
|
exports.moduleInfo = {
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'Telnet Bridge',
|
|
|
|
desc: 'Connect to other Telnet Systems',
|
|
|
|
author: 'Andrew Pamment',
|
2016-06-26 04:45:49 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const IAC_DO_TERM_TYPE = TelnetSocket.commandBuffer(Commands.DO, Options.TTYPE);
|
2016-07-11 04:14:51 +00:00
|
|
|
|
2016-07-10 02:09:39 +00:00
|
|
|
class TelnetClientConnection extends EventEmitter {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(client) {
|
|
|
|
super();
|
|
|
|
|
2020-07-12 22:12:26 +00:00
|
|
|
this.client = client;
|
|
|
|
|
|
|
|
this.dataHits = 0;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-12 22:12:26 +00:00
|
|
|
updateActivity() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === this.dataHits++ % 4) {
|
2020-07-12 22:12:26 +00:00
|
|
|
this.client.explicitActivityTimeUpdate();
|
|
|
|
}
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
restorePipe() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!this.pipeRestored) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.pipeRestored = true;
|
2020-07-12 22:12:26 +00:00
|
|
|
|
|
|
|
this.client.restoreDataHandler();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// client may have bailed
|
2022-06-05 20:04:25 +00:00
|
|
|
if (null !== _.get(this, 'client.term.output', null)) {
|
|
|
|
if (this.bridgeConnection) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client.term.output.unpipe(this.bridgeConnection);
|
|
|
|
}
|
|
|
|
this.client.term.output.resume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(connectOpts) {
|
|
|
|
this.bridgeConnection = net.createConnection(connectOpts, () => {
|
|
|
|
this.emit('connected');
|
|
|
|
|
|
|
|
this.pipeRestored = false;
|
2020-07-12 22:12:26 +00:00
|
|
|
this.client.setTemporaryDirectDataHandler(data => {
|
|
|
|
this.updateActivity();
|
|
|
|
this.bridgeConnection.write(data);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.bridgeConnection.on('data', data => {
|
2020-07-12 22:12:26 +00:00
|
|
|
this.updateActivity();
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client.term.rawWrite(data);
|
|
|
|
|
|
|
|
//
|
2020-05-28 03:05:19 +00:00
|
|
|
// Wait for a terminal type request, and send it exactly once.
|
2018-06-23 03:26:46 +00:00
|
|
|
// This is enough (in additional to other negotiations handled in telnet.js)
|
|
|
|
// to get us in on most systems
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!this.termSent && data.indexOf(IAC_DO_TERM_TYPE) > -1) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.termSent = true;
|
|
|
|
this.bridgeConnection.write(this.getTermTypeNegotiationBuffer());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.bridgeConnection.once('end', () => {
|
|
|
|
this.restorePipe();
|
|
|
|
this.emit('end');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.bridgeConnection.once('error', err => {
|
|
|
|
this.restorePipe();
|
|
|
|
this.emit('end', err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.bridgeConnection) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.bridgeConnection.end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.bridgeConnection) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.bridgeConnection.destroy();
|
|
|
|
this.bridgeConnection.removeAllListeners();
|
|
|
|
this.restorePipe();
|
|
|
|
this.emit('end');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getTermTypeNegotiationBuffer() {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Create a TERMINAL-TYPE sub negotiation buffer using the
|
|
|
|
// actual/current terminal type.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
const sendTermType = TelnetSocket.commandBuffer(Commands.SB, Options.TTYPE, [
|
|
|
|
SubNegotiationCommands.IS,
|
|
|
|
...Buffer.from(this.client.term.termType), // e.g. "ansi"
|
|
|
|
Commands.IAC,
|
|
|
|
Commands.SE,
|
|
|
|
]);
|
2020-05-28 03:05:19 +00:00
|
|
|
return sendTermType;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2016-07-10 02:09:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.getModule = class TelnetBridgeModule extends MenuModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2016-06-26 04:45:49 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.config = Object.assign(
|
|
|
|
{},
|
|
|
|
_.get(options, 'menuConfig.config'),
|
|
|
|
options.extraArgs
|
|
|
|
);
|
|
|
|
this.config.port = this.config.port || 23;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
initSequence() {
|
|
|
|
let clientTerminated;
|
|
|
|
const self = this;
|
2016-06-26 04:45:49 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function validateConfig(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(self.config.host) && _.isNumber(self.config.port)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
callback(null);
|
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
callback(
|
|
|
|
new Error('Configuration is missing required option(s)')
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
function createTelnetBridge(callback) {
|
|
|
|
const connectOpts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
port: self.config.port,
|
|
|
|
host: self.config.host,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.client.term.write(resetScreen());
|
|
|
|
self.client.term.write(
|
|
|
|
` Connecting to ${connectOpts.host}, please wait...\n (Press ESC to cancel)\n`
|
|
|
|
);
|
|
|
|
|
|
|
|
const telnetConnection = new TelnetClientConnection(self.client);
|
|
|
|
|
|
|
|
const connectionKeyPressHandler = (ch, key) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('escape' === key.name) {
|
|
|
|
self.client.removeListener(
|
|
|
|
'key press',
|
|
|
|
connectionKeyPressHandler
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
telnetConnection.destroy();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.client.on('key press', connectionKeyPressHandler);
|
|
|
|
|
|
|
|
telnetConnection.on('connected', () => {
|
2022-06-05 20:04:25 +00:00
|
|
|
self.client.removeListener(
|
|
|
|
'key press',
|
|
|
|
connectionKeyPressHandler
|
|
|
|
);
|
|
|
|
self.client.log.info(
|
|
|
|
connectOpts,
|
|
|
|
'Telnet bridge connection established'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-04-26 03:09:57 +00:00
|
|
|
// put the font back how it was prior, if fonts are enabled
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.client.term.syncTermFontsEnabled && self.config.font) {
|
|
|
|
self.client.term.rawWrite(
|
|
|
|
setSyncTermFontWithAlias(self.config.font)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.client.once('end', () => {
|
2022-06-05 20:04:25 +00:00
|
|
|
self.client.log.info(
|
|
|
|
'Connection ended. Terminating connection'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
clientTerminated = true;
|
|
|
|
telnetConnection.disconnect();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
telnetConnection.on('end', err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
self.client.removeListener(
|
|
|
|
'key press',
|
|
|
|
connectionKeyPressHandler
|
|
|
|
);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
self.client.log.info(
|
|
|
|
`Telnet bridge connection error: ${err.message}`
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
callback(
|
|
|
|
clientTerminated
|
|
|
|
? new Error('Client connection terminated')
|
|
|
|
: null
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
telnetConnection.connect(connectOpts);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
self.client.log.warn(
|
|
|
|
{ error: err.message },
|
|
|
|
'Telnet connection error'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!clientTerminated) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.prevMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
};
|