2016-06-26 04:45:49 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const resetScreen = require('./ansi_term.js').resetScreen;
|
|
|
|
const setSyncTermFontWithAlias = require('./ansi_term.js').setSyncTermFontWithAlias;
|
|
|
|
|
|
|
|
// deps
|
|
|
|
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,
|
|
|
|
TelnetSpec :
|
|
|
|
{
|
|
|
|
Commands,
|
|
|
|
Options,
|
|
|
|
SubNegotiationCommands,
|
|
|
|
},
|
|
|
|
} = 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 = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'Telnet Bridge',
|
|
|
|
desc : 'Connect to other Telnet Systems',
|
|
|
|
author : 'Andrew Pamment',
|
2016-06-26 04:45:49 +00:00
|
|
|
};
|
|
|
|
|
2020-05-28 03:05:19 +00:00
|
|
|
const IAC_DO_TERM_TYPE = TelnetSocket.commandBuffer(
|
|
|
|
Commands.DO,
|
2020-06-16 01:08:55 +00:00
|
|
|
Options.TTYPE
|
2020-05-28 03:05:19 +00:00
|
|
|
);
|
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() {
|
|
|
|
if (0 === (this.dataHits++ % 4)) {
|
|
|
|
this.client.explicitActivityTimeUpdate();
|
|
|
|
}
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
restorePipe() {
|
|
|
|
if(!this.pipeRestored) {
|
|
|
|
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
|
2018-06-22 05:15:04 +00:00
|
|
|
if(null !== _.get(this, 'client.term.output', null)) {
|
|
|
|
if(this.bridgeConnection) {
|
|
|
|
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
|
|
|
//
|
|
|
|
if(!this.termSent && data.indexOf(IAC_DO_TERM_TYPE) > -1) {
|
|
|
|
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() {
|
|
|
|
if(this.bridgeConnection) {
|
|
|
|
this.bridgeConnection.end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
if(this.bridgeConnection) {
|
|
|
|
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
|
|
|
//
|
2020-05-28 03:05:19 +00:00
|
|
|
const sendTermType = TelnetSocket.commandBuffer(
|
|
|
|
Commands.SB,
|
|
|
|
Options.TTYPE,
|
2018-06-22 05:15:04 +00:00
|
|
|
[
|
2020-05-28 03:05:19 +00:00
|
|
|
SubNegotiationCommands.IS,
|
|
|
|
...Buffer.from(this.client.term.termType), // e.g. "ansi"
|
|
|
|
Commands.IAC,
|
|
|
|
Commands.SE,
|
2018-06-22 05:15:04 +00:00
|
|
|
]
|
|
|
|
);
|
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
|
|
|
|
2018-06-23 03:26:46 +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) {
|
|
|
|
if(_.isString(self.config.host) &&
|
2018-06-23 03:26:46 +00:00
|
|
|
_.isNumber(self.config.port))
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
|
|
|
callback(null);
|
|
|
|
} else {
|
|
|
|
callback(new Error('Configuration is missing required option(s)'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function createTelnetBridge(callback) {
|
|
|
|
const connectOpts = {
|
2018-06-23 03:26:46 +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) => {
|
|
|
|
if('escape' === key.name) {
|
|
|
|
self.client.removeListener('key press', connectionKeyPressHandler);
|
|
|
|
telnetConnection.destroy();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.client.on('key press', connectionKeyPressHandler);
|
|
|
|
|
|
|
|
telnetConnection.on('connected', () => {
|
|
|
|
self.client.removeListener('key press', connectionKeyPressHandler);
|
|
|
|
self.client.log.info(connectOpts, 'Telnet bridge connection established');
|
|
|
|
|
2022-04-26 03:09:57 +00:00
|
|
|
// put the font back how it was prior, if fonts are enabled
|
|
|
|
if(self.client.term.syncTermFontsEnabled && self.config.font) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.client.term.rawWrite(setSyncTermFontWithAlias(self.config.font));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.client.once('end', () => {
|
|
|
|
self.client.log.info('Connection ended. Terminating connection');
|
|
|
|
clientTerminated = true;
|
|
|
|
telnetConnection.disconnect();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
telnetConnection.on('end', err => {
|
|
|
|
self.client.removeListener('key press', connectionKeyPressHandler);
|
|
|
|
|
|
|
|
if(err) {
|
2022-05-07 17:47:04 +00:00
|
|
|
self.client.log.warn(`Telnet bridge connection error: ${err.message}`);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(clientTerminated ? new Error('Client connection terminated') : null);
|
|
|
|
});
|
|
|
|
|
|
|
|
telnetConnection.connect(connectOpts);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.warn( { error : err.message }, 'Telnet connection error');
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!clientTerminated) {
|
|
|
|
self.prevMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
};
|