2015-08-01 06:59:11 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2015-08-03 00:27:05 +00:00
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var events = require('events');
|
|
|
|
|
2015-08-04 05:11:17 +00:00
|
|
|
var _ = require('lodash');
|
2015-11-20 18:48:32 +00:00
|
|
|
var pty = require('ptyw.js');
|
2015-11-27 04:27:57 +00:00
|
|
|
var decode = require('iconv-lite').decode;
|
2015-12-03 05:10:45 +00:00
|
|
|
var net = require('net');
|
|
|
|
var async = require('async');
|
2015-08-03 00:27:05 +00:00
|
|
|
|
|
|
|
exports.Door = Door;
|
|
|
|
|
|
|
|
function Door(client, exeInfo) {
|
|
|
|
events.EventEmitter.call(this);
|
|
|
|
|
|
|
|
this.client = client;
|
|
|
|
this.exeInfo = exeInfo;
|
|
|
|
|
2015-08-04 05:11:17 +00:00
|
|
|
this.exeInfo.encoding = this.exeInfo.encoding || 'cp437';
|
|
|
|
|
2015-12-03 05:10:45 +00:00
|
|
|
//
|
|
|
|
// Members of exeInfo:
|
|
|
|
// cmd
|
|
|
|
// args[]
|
|
|
|
// env{}
|
|
|
|
// cwd
|
|
|
|
// io
|
|
|
|
// encoding
|
|
|
|
// dropFile
|
|
|
|
// node
|
|
|
|
// inhSocket
|
|
|
|
//
|
2015-08-26 05:17:09 +00:00
|
|
|
}
|
2015-08-03 00:27:05 +00:00
|
|
|
|
|
|
|
require('util').inherits(Door, events.EventEmitter);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Door.prototype.run = function() {
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2015-12-03 05:10:45 +00:00
|
|
|
var doorData = function(data) {
|
2015-12-06 22:24:34 +00:00
|
|
|
// :TODO: skip decoding if we have a match, e.g. cp437 === cp437
|
2015-12-03 05:10:45 +00:00
|
|
|
self.client.term.write(decode(data, self.exeInfo.encoding));
|
|
|
|
};
|
|
|
|
|
2015-12-04 03:31:20 +00:00
|
|
|
var restore = function(piped) {
|
2015-12-16 06:27:13 +00:00
|
|
|
if(self.client.term.output) {
|
|
|
|
self.client.term.output.unpipe(piped);
|
|
|
|
self.client.term.output.resume();
|
|
|
|
}
|
2015-12-06 22:24:34 +00:00
|
|
|
};
|
2015-12-04 03:31:20 +00:00
|
|
|
|
2015-12-03 05:10:45 +00:00
|
|
|
var sockServer;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function prepareServer(callback) {
|
|
|
|
if('socket' === self.exeInfo.io) {
|
|
|
|
sockServer = net.createServer(function connected(conn) {
|
|
|
|
|
|
|
|
sockServer.getConnections(function connCount(err, count) {
|
|
|
|
|
|
|
|
// We expect only one connection from our DOOR/emulator/etc.
|
|
|
|
if(!err && count <= 1) {
|
|
|
|
self.client.term.output.pipe(conn);
|
|
|
|
|
|
|
|
conn.on('data', doorData);
|
|
|
|
|
|
|
|
conn.on('end', function ended() {
|
2015-12-04 03:31:20 +00:00
|
|
|
restore(conn);
|
|
|
|
});
|
|
|
|
|
|
|
|
conn.on('error', function error(err) {
|
|
|
|
self.client.log.info('Door socket server connection error: ' + err.message);
|
|
|
|
restore(conn);
|
2015-12-03 05:10:45 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
sockServer.listen(0, function listening() {
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function launch(callback) {
|
|
|
|
// Expand arg strings, e.g. {dropFile} -> DOOR32.SYS
|
|
|
|
var args = _.clone(self.exeInfo.args); // we need a copy so the original is not modified
|
|
|
|
|
|
|
|
for(var i = 0; i < args.length; ++i) {
|
|
|
|
args[i] = self.exeInfo.args[i].format({
|
|
|
|
dropFile : self.exeInfo.dropFile,
|
|
|
|
node : self.exeInfo.node.toString(),
|
2015-12-08 04:44:42 +00:00
|
|
|
//inhSocket : self.exeInfo.inhSocket.toString(),
|
2015-12-03 05:10:45 +00:00
|
|
|
srvPort : sockServer ? sockServer.address().port.toString() : '-1',
|
2015-12-06 22:24:34 +00:00
|
|
|
userId : self.client.user.userId.toString(),
|
2015-12-03 05:10:45 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var door = pty.spawn(self.exeInfo.cmd, args, {
|
|
|
|
cols : self.client.term.termWidth,
|
|
|
|
rows : self.client.term.termHeight,
|
|
|
|
// :TODO: cwd
|
|
|
|
env : self.exeInfo.env,
|
|
|
|
});
|
|
|
|
|
|
|
|
if('stdio' === self.exeInfo.io) {
|
|
|
|
self.client.log.debug('Using stdio for door I/O');
|
|
|
|
|
|
|
|
self.client.term.output.pipe(door);
|
|
|
|
|
|
|
|
door.on('data', doorData);
|
|
|
|
|
|
|
|
door.on('close', function closed() {
|
2015-12-04 03:31:20 +00:00
|
|
|
restore(door);
|
2015-12-03 05:10:45 +00:00
|
|
|
});
|
|
|
|
} else if('socket' === self.exeInfo.io) {
|
|
|
|
self.client.log.debug(
|
|
|
|
{ port : sockServer.address().port },
|
|
|
|
'Using temporary socket server for door I/O');
|
|
|
|
}
|
|
|
|
|
|
|
|
door.on('exit', function exited(code) {
|
|
|
|
self.client.log.info( { code : code }, 'Door exited');
|
|
|
|
|
|
|
|
if(sockServer) {
|
|
|
|
sockServer.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
door.removeAllListeners();
|
|
|
|
|
|
|
|
self.emit('finished');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.warn( { error : err.toString() }, 'Failed executing door');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2015-08-03 00:27:05 +00:00
|
|
|
};
|