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',
|
|
|
|
};
|
|
|
|
|
2016-07-11 04:14:51 +00:00
|
|
|
const IAC_DO_TERM_TYPE = new Buffer( [ 255, 253, 24 ] );
|
|
|
|
|
2016-07-10 02:09:39 +00:00
|
|
|
class TelnetClientConnection extends EventEmitter {
|
|
|
|
constructor(client) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.client = client;
|
|
|
|
}
|
|
|
|
|
2016-07-11 04:14:51 +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
|
|
|
|
if(_.has(this, 'client.term.output')) {
|
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);
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
this.bridgeConnection.write(this.getTermTypeNegotiationBuffer());
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
bufs.push(new Buffer(
|
|
|
|
[
|
|
|
|
255, // IAC
|
|
|
|
250, // SB
|
|
|
|
24, // TERMINAL-TYPE
|
|
|
|
0, // IS
|
|
|
|
]
|
|
|
|
));
|
|
|
|
|
|
|
|
bufs.push(
|
|
|
|
new Buffer(this.client.term.termType), // e.g. "ansi"
|
|
|
|
new Buffer( [ 255, 240 ] ) // IAC, SE
|
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
this.config = options.menuConfig.config;
|
|
|
|
// defaults
|
|
|
|
this.config.port = this.config.port || 23;
|
|
|
|
}
|
2016-07-20 02:58:25 +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,
|
|
|
|
};
|
|
|
|
|
|
|
|
let clientTerminated;
|
|
|
|
|
|
|
|
self.client.term.write(resetScreen());
|
|
|
|
self.client.term.write(` Connecting to ${connectOpts.host}, please wait...\n`);
|
|
|
|
|
2016-07-10 02:09:39 +00:00
|
|
|
const telnetConnection = new TelnetClientConnection(self.client);
|
|
|
|
|
|
|
|
telnetConnection.on('connected', () => {
|
|
|
|
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 => {
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|