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');
|
|
|
|
const pty = require('ptyw.js');
|
|
|
|
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;
|
2015-08-04 05:11:17 +00:00
|
|
|
this.exeInfo.encoding = this.exeInfo.encoding || 'cp437';
|
2016-06-20 20:10:12 +00:00
|
|
|
this.exeInfo.encoding = this.exeInfo.encoding.toLowerCase();
|
|
|
|
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) {
|
|
|
|
if(self.client.term.outputEncoding === self.exeInfo.encoding) {
|
|
|
|
self.client.term.rawWrite(data);
|
|
|
|
} else {
|
|
|
|
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) {
|
|
|
|
if('socket' === self.exeInfo.io) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
conn.on('data', self.doorDataHandler);
|
2015-12-03 05:10:45 +00:00
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
conn.once('end', () => {
|
|
|
|
return self.restoreIo(conn);
|
|
|
|
});
|
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
|
|
|
|
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, {
|
|
|
|
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', 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
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|