2019-09-02 14:14:55 +08:00
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
|
value: true
|
|
|
|
|
});
|
|
|
|
|
exports.default = void 0;
|
|
|
|
|
|
|
|
|
|
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
|
|
|
|
|
|
|
|
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
|
|
|
|
|
|
|
|
var _fs = _interopRequireDefault(require("fs"));
|
|
|
|
|
|
2019-09-02 18:47:49 +08:00
|
|
|
|
var _os = _interopRequireDefault(require("os"));
|
|
|
|
|
|
|
|
|
|
var _path = _interopRequireDefault(require("path"));
|
|
|
|
|
|
2019-09-02 14:14:55 +08:00
|
|
|
|
var _axios = _interopRequireDefault(require("axios"));
|
|
|
|
|
|
|
|
|
|
var _package = _interopRequireDefault(require("../package.json"));
|
|
|
|
|
|
|
|
|
|
var _gather = _interopRequireDefault(require("./control/gather"));
|
|
|
|
|
|
|
|
|
|
var version = _package.default.version;
|
|
|
|
|
|
|
|
|
|
var request = _axios.default.create();
|
|
|
|
|
|
|
|
|
|
var apiURLs = {
|
|
|
|
|
apiURL: process.env.API_URL,
|
|
|
|
|
apiKEY: process.env.API_KEY
|
|
|
|
|
};
|
|
|
|
|
var AGENT_VERSION = process.env.AGENT_VERSION;
|
|
|
|
|
var AGENT_PORT = process.env.AGENT_PORT;
|
2019-09-02 18:47:49 +08:00
|
|
|
|
var AGENT_WORK_PATH = process.env.AGENT_WORK_PATH || (_os.default.type() === 'Windows_NT' ? 'C:\\WINDOWS\\dc-agent' : '/usr/local/dc-agent');
|
2019-09-02 14:14:55 +08:00
|
|
|
|
var isDev = process.env.NODE_ENV !== 'production'; // 系统配置
|
|
|
|
|
|
|
|
|
|
var rebootNodeJS =
|
|
|
|
|
/*#__PURE__*/
|
|
|
|
|
function () {
|
|
|
|
|
var _ref = (0, _asyncToGenerator2.default)(
|
|
|
|
|
/*#__PURE__*/
|
|
|
|
|
_regenerator.default.mark(function _callee() {
|
|
|
|
|
var _ref2, data;
|
|
|
|
|
|
|
|
|
|
return _regenerator.default.wrap(function _callee$(_context) {
|
|
|
|
|
while (1) {
|
|
|
|
|
switch (_context.prev = _context.next) {
|
|
|
|
|
case 0:
|
|
|
|
|
if (!isDev) {
|
|
|
|
|
_context.next = 4;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('当前是开发环境,无法自动重启Node.js');
|
|
|
|
|
_context.next = 16;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
|
_context.prev = 4;
|
|
|
|
|
_context.next = 7;
|
|
|
|
|
return request.get(`http://127.0.0.1:${AGENT_PORT}/system/cmd?action=restart`);
|
|
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
|
_ref2 = _context.sent;
|
|
|
|
|
data = _ref2.data;
|
|
|
|
|
console.log('自动重启Node.js返回数据:', data);
|
|
|
|
|
_context.next = 16;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 12:
|
|
|
|
|
_context.prev = 12;
|
|
|
|
|
_context.t0 = _context["catch"](4);
|
|
|
|
|
console.log(_context.t0);
|
|
|
|
|
console.log('自动重启Node.js失败');
|
|
|
|
|
|
|
|
|
|
case 16:
|
|
|
|
|
case "end":
|
|
|
|
|
return _context.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, _callee, null, [[4, 12]]);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return function rebootNodeJS() {
|
|
|
|
|
return _ref.apply(this, arguments);
|
|
|
|
|
};
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
var checkVersion =
|
|
|
|
|
/*#__PURE__*/
|
|
|
|
|
function () {
|
|
|
|
|
var _ref3 = (0, _asyncToGenerator2.default)(
|
|
|
|
|
/*#__PURE__*/
|
|
|
|
|
_regenerator.default.mark(function _callee2(version) {
|
|
|
|
|
var apiURL, _ref4, data;
|
|
|
|
|
|
|
|
|
|
return _regenerator.default.wrap(function _callee2$(_context2) {
|
|
|
|
|
while (1) {
|
|
|
|
|
switch (_context2.prev = _context2.next) {
|
|
|
|
|
case 0:
|
|
|
|
|
apiURL = 'https://tool.y-bi.top/check.php?version=' + version;
|
|
|
|
|
_context2.next = 3;
|
|
|
|
|
return request.get(apiURL);
|
|
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
|
_ref4 = _context2.sent;
|
|
|
|
|
data = _ref4.data;
|
|
|
|
|
|
|
|
|
|
if (!(data.status === 1)) {
|
|
|
|
|
_context2.next = 10;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 有更新
|
|
|
|
|
console.log('发现新版系统,正在重启Node.js...');
|
|
|
|
|
_context2.next = 9;
|
|
|
|
|
return rebootNodeJS();
|
|
|
|
|
|
|
|
|
|
case 9:
|
|
|
|
|
return _context2.abrupt("return", true);
|
|
|
|
|
|
|
|
|
|
case 10:
|
|
|
|
|
return _context2.abrupt("return", false);
|
|
|
|
|
|
|
|
|
|
case 11:
|
|
|
|
|
case "end":
|
|
|
|
|
return _context2.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, _callee2);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return function checkVersion(_x) {
|
|
|
|
|
return _ref3.apply(this, arguments);
|
|
|
|
|
};
|
|
|
|
|
}();
|
|
|
|
|
|
2019-09-02 18:47:49 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-02 14:14:55 +08:00
|
|
|
|
var fileIsExists = function fileIsExists(path) {
|
|
|
|
|
try {
|
|
|
|
|
_fs.default.accessSync(path);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
} catch (_) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-02 18:47:49 +08:00
|
|
|
|
var writeProcessPID = function writeProcessPID(action, workPath, pid) {
|
|
|
|
|
var pidFilePath = _path.default.join(workPath, 'pids', action + '.pid');
|
2019-09-02 14:14:55 +08:00
|
|
|
|
|
2019-09-02 18:47:49 +08:00
|
|
|
|
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);
|
2019-09-02 14:14:55 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var _default =
|
|
|
|
|
/*#__PURE__*/
|
|
|
|
|
(0, _asyncToGenerator2.default)(
|
|
|
|
|
/*#__PURE__*/
|
|
|
|
|
_regenerator.default.mark(function _callee3() {
|
2019-09-02 18:47:49 +08:00
|
|
|
|
var pid;
|
2019-09-02 14:14:55 +08:00
|
|
|
|
return _regenerator.default.wrap(function _callee3$(_context3) {
|
|
|
|
|
while (1) {
|
|
|
|
|
switch (_context3.prev = _context3.next) {
|
|
|
|
|
case 0:
|
|
|
|
|
console.log('AGENT_VERSION:', AGENT_VERSION);
|
|
|
|
|
checkAndInstallWhoami();
|
2019-09-02 18:47:49 +08:00
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
});
|
2019-09-02 14:14:55 +08:00
|
|
|
|
setInterval(function () {
|
|
|
|
|
checkVersion(version || '0.0.1'); // sync
|
|
|
|
|
}, 120000);
|
|
|
|
|
(0, _gather.default)({
|
|
|
|
|
version: version || '0.0.1',
|
|
|
|
|
apiURLs,
|
|
|
|
|
isDev,
|
|
|
|
|
AGENT_PORT,
|
|
|
|
|
AGENT_VERSION
|
|
|
|
|
}); // sync
|
|
|
|
|
|
2019-09-02 18:47:49 +08:00
|
|
|
|
case 9:
|
2019-09-02 14:14:55 +08:00
|
|
|
|
case "end":
|
|
|
|
|
return _context3.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, _callee3);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
exports.default = _default;
|