2016-06-26 04:45:49 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-07-11 04:14:51 +00:00
|
|
|
// ENiGMA½
|
2017-11-24 23:23:15 +00:00
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const resetScreen = require('./ansi_term.js').resetScreen;
|
|
|
|
const setSyncTermFontWithAlias = require('./ansi_term.js').setSyncTermFontWithAlias;
|
2016-06-26 04:45:49 +00:00
|
|
|
|
2016-07-11 04:14:51 +00:00
|
|
|
// deps
|
2016-06-26 04:45:49 +00:00
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const net = require('net');
|
2016-07-10 02:09:39 +00:00
|
|
|
const EventEmitter = require('events');
|
2016-07-11 04:14:51 +00:00
|
|
|
const buffers = require('buffers');
|
2016-06-26 04:45:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Expected configuration block:
|
|
|
|
|
|
|
|
{
|
|
|
|
module: telnet_bridge
|
|
|
|
...
|
|
|
|
config: {
|
|
|
|
host: somehost.net
|
|
|
|
port: 23
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// :TODO: ENH: Support nodeMax and tooManyArt
|
|
|
|
exports.moduleInfo = {
|
|
|
|
name : 'Telnet Bridge',
|
|
|
|
desc : 'Connect to other Telnet Systems',
|
|
|
|
author : 'Andrew Pamment',
|
|
|
|
};
|
|
|
|
|
2018-04-28 12:59:07 +00:00
|
|
|
const IAC_DO_TERM_TYPE = Buffer.from( [ 255, 253, 24 ] );
|
2016-07-11 04:14:51 +00:00
|
|
|
|
2016-07-10 02:09:39 +00:00
|
|
|
class TelnetClientConnection extends EventEmitter {
|
|
|
|
constructor(client) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.client = client;
|
|
|
|
}
|
|
|
|
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-07-10 02:09:39 +00:00
|
|
|
restorePipe() {
|
2016-07-11 04:14:51 +00:00
|
|
|
if(!this.pipeRestored) {
|
|
|
|
this.pipeRestored = true;
|
|
|
|
|
2016-08-30 15:53:35 +00:00
|
|
|
// client may have bailed
|
2017-11-30 18:39:01 +00:00
|
|
|
if(null !== _.get(this, 'client.term.output', null)) {
|
2017-01-31 07:17:19 +00:00
|
|
|
if(this.bridgeConnection) {
|
|
|
|
this.client.term.output.unpipe(this.bridgeConnection);
|
|
|
|
}
|
2016-08-30 15:53:35 +00:00
|
|
|
this.client.term.output.resume();
|
|
|
|
}
|
2016-07-11 04:14:51 +00:00
|
|
|
}
|
2016-07-10 02:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
connect(connectOpts) {
|
|
|
|
this.bridgeConnection = net.createConnection(connectOpts, () => {
|
|
|
|
this.emit('connected');
|
|
|
|
|
2016-07-11 04:14:51 +00:00
|
|
|
this.pipeRestored = false;
|
2016-07-10 02:09:39 +00:00
|
|
|
this.client.term.output.pipe(this.bridgeConnection);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.bridgeConnection.on('data', data => {
|
2016-07-11 04:14:51 +00:00
|
|
|
this.client.term.rawWrite(data);
|
|
|
|
|
2018-01-15 19:22:11 +00:00
|
|
|
//
|
2016-07-11 04:14:51 +00:00
|
|
|
// Wait for a terminal type request, and send it eactly once.
|
|
|
|
// This is enough (in additional to other negotiations handled in telnet.js)
|
|
|
|
// to get us in on most systems
|
|
|
|
//
|
|
|
|
if(!this.termSent && data.indexOf(IAC_DO_TERM_TYPE) > -1) {
|
|
|
|
this.termSent = true;
|
2018-01-15 19:22:11 +00:00
|
|
|
this.bridgeConnection.write(this.getTermTypeNegotiationBuffer());
|
2016-07-11 04:14:51 +00:00
|
|
|
}
|
2016-07-10 02:09:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-03 02:51:09 +00:00
|
|
|
destroy() {
|
|
|
|
if(this.bridgeConnection) {
|
|
|
|
this.bridgeConnection.destroy();
|
|
|
|
this.bridgeConnection.removeAllListeners();
|
|
|
|
this.restorePipe();
|
|
|
|
this.emit('end');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 04:14:51 +00:00
|
|
|
getTermTypeNegotiationBuffer() {
|
|
|
|
//
|
|
|
|
// Create a TERMINAL-TYPE sub negotiation buffer using the
|
|
|
|
// actual/current terminal type.
|
|
|
|
//
|
|
|
|
let bufs = buffers();
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-04-28 12:59:07 +00:00
|
|
|
bufs.push(Buffer.from(
|
2018-01-15 19:22:11 +00:00
|
|
|
[
|
2016-07-11 04:14:51 +00:00
|
|
|
255, // IAC
|
|
|
|
250, // SB
|
|
|
|
24, // TERMINAL-TYPE
|
|
|
|
0, // IS
|
|
|
|
]
|
|
|
|
));
|
|
|
|
|
|
|
|
bufs.push(
|
2018-04-28 12:59:07 +00:00
|
|
|
Buffer.from(this.client.term.termType), // e.g. "ansi"
|
|
|
|
Buffer.from( [ 255, 240 ] ) // IAC, SE
|
2018-01-15 19:22:11 +00:00
|
|
|
);
|
2016-07-11 04:14:51 +00:00
|
|
|
|
|
|
|
return bufs.toBuffer();
|
|
|
|
}
|
|
|
|
|
2016-07-10 02:09:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.getModule = class TelnetBridgeModule extends MenuModule {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2016-06-26 04:45:49 +00:00
|
|
|
|
2018-01-28 20:22:47 +00:00
|
|
|
this.config = Object.assign({}, _.get(options, 'menuConfig.config'), options.extraArgs);
|
2018-01-15 19:22:11 +00:00
|
|
|
this.config.port = this.config.port || 23;
|
2017-01-26 05:18:05 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
initSequence() {
|
2016-06-26 04:45:49 +00:00
|
|
|
let clientTerminated;
|
2017-01-26 05:18:05 +00:00
|
|
|
const self = this;
|
2016-06-26 04:45:49 +00:00
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function validateConfig(callback) {
|
|
|
|
if(_.isString(self.config.host) &&
|
|
|
|
_.isNumber(self.config.port))
|
|
|
|
{
|
|
|
|
callback(null);
|
|
|
|
} else {
|
|
|
|
callback(new Error('Configuration is missing required option(s)'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function createTelnetBridge(callback) {
|
|
|
|
const connectOpts = {
|
|
|
|
port : self.config.port,
|
|
|
|
host : self.config.host,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.client.term.write(resetScreen());
|
2018-01-28 19:56:35 +00:00
|
|
|
self.client.term.write(
|
2018-06-03 02:51:09 +00:00
|
|
|
` Connecting to ${connectOpts.host}, please wait...\n (Press ESC to cancel)\n`
|
2018-01-28 19:56:35 +00:00
|
|
|
);
|
2016-06-26 04:45:49 +00:00
|
|
|
|
2016-07-10 02:09:39 +00:00
|
|
|
const telnetConnection = new TelnetClientConnection(self.client);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-02-01 05:42:43 +00:00
|
|
|
const connectionKeyPressHandler = (ch, key) => {
|
|
|
|
if('escape' === key.name) {
|
|
|
|
self.client.removeListener('key press', connectionKeyPressHandler);
|
2018-06-03 02:51:09 +00:00
|
|
|
telnetConnection.destroy();
|
2018-02-01 05:42:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.client.on('key press', connectionKeyPressHandler);
|
|
|
|
|
2016-07-10 02:09:39 +00:00
|
|
|
telnetConnection.on('connected', () => {
|
2018-02-01 05:42:43 +00:00
|
|
|
self.client.removeListener('key press', connectionKeyPressHandler);
|
2016-07-10 02:09:39 +00:00
|
|
|
self.client.log.info(connectOpts, 'Telnet bridge connection established');
|
|
|
|
|
2016-07-20 02:58:25 +00:00
|
|
|
if(self.config.font) {
|
|
|
|
self.client.term.rawWrite(setSyncTermFontWithAlias(self.config.font));
|
|
|
|
}
|
|
|
|
|
2016-07-10 02:09:39 +00:00
|
|
|
self.client.once('end', () => {
|
|
|
|
self.client.log.info('Connection ended. Terminating connection');
|
|
|
|
clientTerminated = true;
|
|
|
|
telnetConnection.disconnect();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
telnetConnection.on('end', err => {
|
2018-02-01 05:42:43 +00:00
|
|
|
self.client.removeListener('key press', connectionKeyPressHandler);
|
|
|
|
|
2016-07-10 02:09:39 +00:00
|
|
|
if(err) {
|
|
|
|
self.client.log.info(`Telnet bridge connection error: ${err.message}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(clientTerminated ? new Error('Client connection terminated') : null);
|
|
|
|
});
|
|
|
|
|
|
|
|
telnetConnection.connect(connectOpts);
|
2016-06-26 04:45:49 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|