2015-08-01 06:59:11 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-09-05 03:36:26 +00:00
|
|
|
|
|
|
|
const stringFormat = require('./string_format.js');
|
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
const events = require('events');
|
|
|
|
const _ = require('lodash');
|
2018-03-09 04:39:42 +00:00
|
|
|
const pty = require('node-pty');
|
2016-06-20 20:10:12 +00:00
|
|
|
const decode = require('iconv-lite').decode;
|
|
|
|
const createServer = require('net').createServer;
|
2015-08-03 00:27:05 +00:00
|
|
|
|
|
|
|
exports.Door = Door;
|
|
|
|
|
|
|
|
function Door(client, exeInfo) {
|
|
|
|
events.EventEmitter.call(this);
|
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
const self = this;
|
|
|
|
this.client = client;
|
|
|
|
this.exeInfo = exeInfo;
|
2018-03-09 04:39:42 +00:00
|
|
|
this.exeInfo.encoding = (this.exeInfo.encoding || 'cp437').toLowerCase();
|
2018-01-15 19:22:11 +00:00
|
|
|
let restored = false;
|
2015-08-04 05:11:17 +00:00
|
|
|
|
2015-12-03 05:10:45 +00:00
|
|
|
//
|
|
|
|
// Members of exeInfo:
|
|
|
|
// cmd
|
|
|
|
// args[]
|
|
|
|
// env{}
|
|
|
|
// cwd
|
|
|
|
// io
|
|
|
|
// encoding
|
|
|
|
// dropFile
|
|
|
|
// node
|
|
|
|
// inhSocket
|
2016-06-20 20:10:12 +00:00
|
|
|
//
|
2015-08-03 00:27:05 +00:00
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
this.doorDataHandler = function(data) {
|
2018-03-09 04:39:42 +00:00
|
|
|
self.client.term.write(decode(data, self.exeInfo.encoding));
|
2015-12-03 05:10:45 +00:00
|
|
|
};
|
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
this.restoreIo = function(piped) {
|
2015-12-28 23:34:28 +00:00
|
|
|
if(!restored && self.client.term.output) {
|
2015-12-16 06:27:13 +00:00
|
|
|
self.client.term.output.unpipe(piped);
|
|
|
|
self.client.term.output.resume();
|
2015-12-28 23:34:28 +00:00
|
|
|
restored = true;
|
2015-12-16 06:27:13 +00:00
|
|
|
}
|
2015-12-06 22:24:34 +00:00
|
|
|
};
|
2015-12-04 03:31:20 +00:00
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
this.prepareSocketIoServer = function(cb) {
|
2018-01-15 19:22:11 +00:00
|
|
|
if('socket' === self.exeInfo.io) {
|
2016-06-20 20:10:12 +00:00
|
|
|
const sockServer = createServer(conn => {
|
2015-12-03 05:10:45 +00:00
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
sockServer.getConnections( (err, count) => {
|
2015-12-03 05:10:45 +00:00
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
// We expect only one connection from our DOOR/emulator/etc.
|
|
|
|
if(!err && count <= 1) {
|
|
|
|
self.client.term.output.pipe(conn);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
conn.on('data', self.doorDataHandler);
|
2015-12-03 05:10:45 +00:00
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
conn.once('end', () => {
|
2018-01-15 19:22:11 +00:00
|
|
|
return self.restoreIo(conn);
|
2016-06-20 20:10:12 +00:00
|
|
|
});
|
2015-12-04 03:31:20 +00:00
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
conn.once('error', err => {
|
|
|
|
self.client.log.info( { error : err.toString() }, 'Door socket server connection');
|
|
|
|
return self.restoreIo(conn);
|
2015-12-03 05:10:45 +00:00
|
|
|
});
|
|
|
|
}
|
2016-06-20 20:10:12 +00:00
|
|
|
});
|
|
|
|
});
|
2015-12-03 05:10:45 +00:00
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
sockServer.listen(0, () => {
|
|
|
|
return cb(null, sockServer);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
};
|
2016-06-21 03:38:43 +00:00
|
|
|
|
|
|
|
this.doorExited = function() {
|
|
|
|
self.emit('finished');
|
|
|
|
};
|
2016-06-20 20:10:12 +00:00
|
|
|
}
|
2015-12-28 23:34:28 +00:00
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
require('util').inherits(Door, events.EventEmitter);
|
2015-12-03 05:10:45 +00:00
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
Door.prototype.run = function() {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
this.prepareSocketIoServer( (err, sockServer) => {
|
|
|
|
if(err) {
|
|
|
|
this.client.log.warn( { error : err.toString() }, 'Failed executing door');
|
2016-06-21 03:38:43 +00:00
|
|
|
return self.doorExited();
|
2016-06-20 20:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expand arg strings, e.g. {dropFile} -> DOOR32.SYS
|
2016-09-29 03:54:25 +00:00
|
|
|
// :TODO: Use .map() here
|
2016-06-20 20:10:12 +00:00
|
|
|
let args = _.clone(self.exeInfo.args); // we need a copy so the original is not modified
|
|
|
|
|
|
|
|
for(let i = 0; i < args.length; ++i) {
|
2016-09-05 03:36:26 +00:00
|
|
|
args[i] = stringFormat(self.exeInfo.args[i], {
|
2016-06-20 20:10:12 +00:00
|
|
|
dropFile : self.exeInfo.dropFile,
|
|
|
|
node : self.exeInfo.node.toString(),
|
|
|
|
srvPort : sockServer ? sockServer.address().port.toString() : '-1',
|
|
|
|
userId : self.client.user.userId.toString(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const door = pty.spawn(self.exeInfo.cmd, args, {
|
2018-03-09 04:39:42 +00:00
|
|
|
cols : self.client.term.termWidth,
|
|
|
|
rows : self.client.term.termHeight,
|
2016-06-20 20:10:12 +00:00
|
|
|
// :TODO: cwd
|
2018-03-09 04:39:42 +00:00
|
|
|
env : self.exeInfo.env,
|
|
|
|
encoding : null, // we want to handle all encoding ourself
|
2018-01-15 19:22:11 +00:00
|
|
|
});
|
2016-06-20 20:10:12 +00:00
|
|
|
|
|
|
|
if('stdio' === self.exeInfo.io) {
|
|
|
|
self.client.log.debug('Using stdio for door I/O');
|
|
|
|
|
|
|
|
self.client.term.output.pipe(door);
|
|
|
|
|
|
|
|
door.on('data', self.doorDataHandler);
|
|
|
|
|
|
|
|
door.once('close', () => {
|
|
|
|
return self.restoreIo(door);
|
|
|
|
});
|
|
|
|
} else if('socket' === self.exeInfo.io) {
|
|
|
|
self.client.log.debug( { port : sockServer.address().port }, 'Using temporary socket server for door I/O');
|
|
|
|
}
|
|
|
|
|
|
|
|
door.once('exit', exitCode => {
|
|
|
|
self.client.log.info( { exitCode : exitCode }, 'Door exited');
|
|
|
|
|
|
|
|
if(sockServer) {
|
|
|
|
sockServer.close();
|
2015-12-03 05:10:45 +00:00
|
|
|
}
|
2016-06-20 20:10:12 +00:00
|
|
|
|
|
|
|
// we may not get a close
|
|
|
|
if('stdio' === self.exeInfo.io) {
|
2016-06-21 15:02:06 +00:00
|
|
|
self.restoreIo(door);
|
2015-12-03 05:10:45 +00:00
|
|
|
}
|
2016-06-20 20:10:12 +00:00
|
|
|
|
|
|
|
door.removeAllListeners();
|
|
|
|
|
2016-06-21 03:38:43 +00:00
|
|
|
return self.doorExited();
|
2016-06-20 20:10:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|