2015-08-01 06:59:11 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const stringFormat = require('./string_format.js');
|
|
|
|
const { Errors } = require('./enig_error.js');
|
|
|
|
const Events = require('./events');
|
2016-09-05 03:36:26 +00:00
|
|
|
|
2018-07-11 01:11:03 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const pty = require('node-pty');
|
|
|
|
const decode = require('iconv-lite').decode;
|
|
|
|
const createServer = require('net').createServer;
|
|
|
|
const paths = require('path');
|
|
|
|
const _ = require('lodash');
|
2015-08-03 00:27:05 +00:00
|
|
|
|
2018-03-29 01:16:10 +00:00
|
|
|
module.exports = class Door {
|
2018-06-23 05:06:01 +00:00
|
|
|
constructor(client) {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.client = client;
|
|
|
|
this.restored = false;
|
2018-06-23 05:06:01 +00:00
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 05:06:01 +00:00
|
|
|
prepare(ioType, cb) {
|
|
|
|
this.io = ioType;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 05:06:01 +00:00
|
|
|
// we currently only have to do any real setup for 'socket'
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('socket' !== ioType) {
|
2018-06-23 05:06:01 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 05:06:01 +00:00
|
|
|
this.sockServer = createServer(conn => {
|
2018-08-03 04:13:42 +00:00
|
|
|
conn.once('end', () => {
|
|
|
|
return this.restoreIo(conn);
|
|
|
|
});
|
|
|
|
|
|
|
|
conn.once('error', err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.client.log.info(
|
|
|
|
{ error: err.message },
|
|
|
|
'Door socket server connection'
|
|
|
|
);
|
2018-08-03 04:13:42 +00:00
|
|
|
return this.restoreIo(conn);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.sockServer.getConnections((err, count) => {
|
2018-06-23 05:06:01 +00:00
|
|
|
// We expect only one connection from our DOOR/emulator/etc.
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!err && count <= 1) {
|
2018-06-23 05:06:01 +00:00
|
|
|
this.client.term.output.pipe(conn);
|
|
|
|
conn.on('data', this.doorDataHandler.bind(this));
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2018-06-23 05:06:01 +00:00
|
|
|
});
|
2015-12-28 23:34:28 +00:00
|
|
|
|
2018-06-23 05:06:01 +00:00
|
|
|
this.sockServer.listen(0, () => {
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
}
|
2015-12-03 05:10:45 +00:00
|
|
|
|
2018-06-23 05:06:01 +00:00
|
|
|
run(exeInfo, cb) {
|
|
|
|
this.encoding = (exeInfo.encoding || 'cp437').toLowerCase();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('socket' === this.io && !this.sockServer) {
|
2018-06-23 05:06:01 +00:00
|
|
|
return cb(Errors.UnexpectedState('Socket server is not running'));
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 01:25:07 +00:00
|
|
|
const cwd = exeInfo.cwd || paths.dirname(exeInfo.cmd);
|
|
|
|
|
2018-06-23 05:06:01 +00:00
|
|
|
const formatObj = {
|
2022-06-05 20:04:25 +00:00
|
|
|
dropFile: exeInfo.dropFile,
|
|
|
|
dropFilePath: exeInfo.dropFilePath,
|
|
|
|
node: exeInfo.node.toString(),
|
|
|
|
srvPort: this.sockServer ? this.sockServer.address().port.toString() : '-1',
|
|
|
|
userId: this.client.user.userId.toString(),
|
|
|
|
userName: this.client.user.getSanitizedName(),
|
|
|
|
userNameRaw: this.client.user.username,
|
|
|
|
cwd: cwd,
|
2018-06-23 05:06:01 +00:00
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const args = exeInfo.args.map(arg => stringFormat(arg, formatObj));
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2019-06-01 20:38:48 +00:00
|
|
|
this.client.log.info(
|
2022-06-05 20:04:25 +00:00
|
|
|
{ cmd: exeInfo.cmd, args, io: this.io },
|
2019-06-01 20:38:48 +00:00
|
|
|
'Executing external door process'
|
2018-12-08 07:43:20 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
2019-06-01 20:38:48 +00:00
|
|
|
this.doorPty = pty.spawn(exeInfo.cmd, args, {
|
2022-06-05 20:04:25 +00:00
|
|
|
cols: this.client.term.termWidth,
|
|
|
|
rows: this.client.term.termHeight,
|
|
|
|
cwd: cwd,
|
|
|
|
env: exeInfo.env,
|
|
|
|
encoding: null, // we want to handle all encoding ourself
|
2018-12-08 07:43:20 +00:00
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
2018-12-08 07:43:20 +00:00
|
|
|
return cb(e);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-10-16 02:54:55 +00:00
|
|
|
//
|
|
|
|
// PID is launched. Make sure it's killed off if the user disconnects.
|
|
|
|
//
|
|
|
|
Events.once(Events.getSystemEvents().ClientDisconnected, evt => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
this.doorPty &&
|
|
|
|
this.client.session.uniqueId === _.get(evt, 'client.session.uniqueId')
|
|
|
|
) {
|
2020-10-16 02:54:55 +00:00
|
|
|
this.client.log.info(
|
2022-06-05 20:04:25 +00:00
|
|
|
{ pid: this.doorPty.pid },
|
2020-10-16 02:54:55 +00:00
|
|
|
'User has disconnected; Killing door process.'
|
|
|
|
);
|
|
|
|
this.doorPty.kill();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-01 20:38:48 +00:00
|
|
|
this.client.log.debug(
|
2022-06-05 20:04:25 +00:00
|
|
|
{ processId: this.doorPty.pid },
|
|
|
|
'External door process spawned'
|
2019-06-01 20:38:48 +00:00
|
|
|
);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('stdio' === this.io) {
|
2018-06-23 05:06:01 +00:00
|
|
|
this.client.log.debug('Using stdio for door I/O');
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2019-06-01 20:38:48 +00:00
|
|
|
this.client.term.output.pipe(this.doorPty);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-10-16 02:54:55 +00:00
|
|
|
this.doorPty.onData(this.doorDataHandler.bind(this));
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2019-06-01 20:38:48 +00:00
|
|
|
this.doorPty.once('close', () => {
|
|
|
|
return this.restoreIo(this.doorPty);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if ('socket' === this.io) {
|
2018-06-23 05:06:01 +00:00
|
|
|
this.client.log.debug(
|
2022-06-05 20:04:25 +00:00
|
|
|
{
|
|
|
|
srvPort: this.sockServer.address().port,
|
|
|
|
srvSocket: this.sockServerSocket,
|
|
|
|
},
|
2018-06-23 05:06:01 +00:00
|
|
|
'Using temporary socket server for door I/O'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 20:38:48 +00:00
|
|
|
this.doorPty.once('exit', exitCode => {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.client.log.info({ exitCode: exitCode }, 'Door exited');
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.sockServer) {
|
2018-06-23 05:06:01 +00:00
|
|
|
this.sockServer.close();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// we may not get a close
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('stdio' === this.io) {
|
2019-06-01 20:38:48 +00:00
|
|
|
this.restoreIo(this.doorPty);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 20:38:48 +00:00
|
|
|
this.doorPty.removeAllListeners();
|
|
|
|
delete this.doorPty;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 05:06:01 +00:00
|
|
|
return cb(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2018-06-23 05:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
doorDataHandler(data) {
|
|
|
|
this.client.term.write(decode(data, this.encoding));
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreIo(piped) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!this.restored) {
|
|
|
|
if (this.doorPty) {
|
2019-06-01 20:38:48 +00:00
|
|
|
this.doorPty.kill();
|
|
|
|
}
|
|
|
|
|
2020-11-08 04:52:57 +00:00
|
|
|
const output = this.client.term.output;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (output) {
|
2020-11-08 04:52:57 +00:00
|
|
|
output.unpipe(piped);
|
|
|
|
output.resume();
|
2019-06-01 20:38:48 +00:00
|
|
|
}
|
2018-06-23 05:06:01 +00:00
|
|
|
this.restored = true;
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 20:10:12 +00:00
|
|
|
};
|