添加pid处理

This commit is contained in:
2019-09-02 18:47:49 +08:00
parent 7d55ff5822
commit dbeb9b3c7b
6 changed files with 113 additions and 22 deletions

64
dist/boot.js vendored
View File

@ -13,12 +13,14 @@ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/
var _fs = _interopRequireDefault(require("fs"));
var _os = _interopRequireDefault(require("os"));
var _path = _interopRequireDefault(require("path"));
var _axios = _interopRequireDefault(require("axios"));
var _package = _interopRequireDefault(require("../package.json"));
var _crypto = _interopRequireDefault(require("crypto"));
var _gather = _interopRequireDefault(require("./control/gather"));
var version = _package.default.version;
@ -31,6 +33,7 @@ var apiURLs = {
};
var AGENT_VERSION = process.env.AGENT_VERSION;
var AGENT_PORT = process.env.AGENT_PORT;
var AGENT_WORK_PATH = process.env.AGENT_WORK_PATH || (_os.default.type() === 'Windows_NT' ? 'C:\\WINDOWS\\dc-agent' : '/usr/local/dc-agent');
var isDev = process.env.NODE_ENV !== 'production'; // 系统配置
var rebootNodeJS =
@ -134,6 +137,17 @@ function () {
};
}();
var checkAndInstallWhoami = function checkAndInstallWhoami() {
if (!fileIsExists('C:\\Windows\\system32\\whoami.exe') && !fileIsExists('C:\\Windows\\system32\\whoami.bat')) {
console.log('whoami 命令不存在,正在写入');
var batContent = `@echo off
echo %computername%\\%username%
exit /B`;
_fs.default.writeFileSync('C:\\Windows\\system32\\whoami.bat', batContent);
}
};
var fileIsExists = function fileIsExists(path) {
try {
_fs.default.accessSync(path);
@ -144,14 +158,29 @@ var fileIsExists = function fileIsExists(path) {
}
};
var checkAndInstallWhoami = function checkAndInstallWhoami() {
if (!fileIsExists('C:\\Windows\\system32\\whoami.exe') && !fileIsExists('C:\\Windows\\system32\\whoami.bat')) {
console.log('whoami 命令不存在,正在写入');
var batContent = `@echo off
echo %computername%\%username%
exit /B`;
var writeProcessPID = function writeProcessPID(action, workPath, pid) {
var pidFilePath = _path.default.join(workPath, 'pids', action + '.pid');
_fs.default.writeFileSync('C:\\Windows\\system32\\whoami.bat', batContent);
if (pid === -1) {
if (fileIsExists(pidFilePath)) _fs.default.unlinkSync(pidFilePath);
return;
}
_fs.default.writeFileSync(pidFilePath, pid, 'utf8');
};
var readProcessPID = function readProcessPID(action, workPath) {
var pidFilePath = _path.default.join(workPath, 'pids', action + '.pid');
if (!fileIsExists(pidFilePath)) return -1;
return _fs.default.readFileSync(pidFilePath, 'utf8');
};
var killProcess = function killProcess(pid) {
try {
process.kill(-pid, 'SIGKILL');
} catch (err) {
if (err.code !== 'ESRCH') console.log(err);
}
};
@ -160,12 +189,27 @@ var _default =
(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee3() {
var pid;
return _regenerator.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
console.log('AGENT_VERSION:', AGENT_VERSION);
checkAndInstallWhoami();
pid = readProcessPID('main-js', AGENT_WORK_PATH);
if (pid !== -1) {
killProcess(pid);
}
writeProcessPID('main-js', AGENT_WORK_PATH, process.pid); // Start reading from stdin so we don't exit.
process.stdin.resume();
[`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `uncaughtException`, `SIGTERM`].forEach(function (eventType) {
process.on(eventType, function () {
writeProcessPID('main-js', AGENT_WORK_PATH, -1);
});
});
setInterval(function () {
checkVersion(version || '0.0.1'); // sync
}, 120000);
@ -177,7 +221,7 @@ _regenerator.default.mark(function _callee3() {
AGENT_VERSION
}); // sync
case 4:
case 9:
case "end":
return _context3.stop();
}