diff --git a/check.php b/check.php index 9df3eed..5acf502 100644 --- a/check.php +++ b/check.php @@ -48,7 +48,7 @@ function versionCompare($versionA, $versionB) } $version = $_GET['version']; -$latestVersion = "0.1.1"; +$latestVersion = "0.1.2"; if (versionCompare($version, $latestVersion) < 0) { $result = [ diff --git a/config.yml b/config.yml index 1f46042..0362ae6 100644 --- a/config.yml +++ b/config.yml @@ -1,4 +1,4 @@ -version: 0.1.1 +version: 0.1.2 update-url: 'https://tool.y-bi.top/check.php' node: version: 5.12.0 diff --git a/dist/boot.js b/dist/boot.js index e7e96b6..1e354aa 100644 --- a/dist/boot.js +++ b/dist/boot.js @@ -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(); } diff --git a/package.json b/package.json index a77512b..42eeaff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "server-monitor-xp", - "version": "0.1.1", + "version": "0.1.2", "description": "Server monitor for windows xp.", "main": "dist/app.js", "scripts": { @@ -46,7 +46,8 @@ "AGENT_VERSION": "0.0.1", "AGENT_PORT": "60000", "API_URL": "https://server-0.sercretcore.cn/api/", - "API_KEY": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxEUN1DZZ/XU2J6+3EoCX\n6ZQExSyGrJlmcq2s4sxAqThJVGAv4BYqCQjnigUGaLF4+2khGHVXrx4LhwnW54iq\nV3V3Xq59H0Cj3oGGWgKxSOM62xxfizmc1Og/6uAwZTAX4oCsgx5SMaFQbAU5ensM\nVEX9CetXSGhc1bbS23kEHAkjJ0NryRl7DR/ilFKO5pAjTGEzP4aTkF/D3Eu3z15U\nwdkf2WisEsANVTEnNHu2qvdiXGzRSLNF4mVFNO3AsgfnbgXzlN0feQ1HbH+J7Ue5\neHleCGhfS/PGFP3lQ4sA0hB4B/5eZ6ROo8YEuQiNTz+UMFteeGymTgFu2sOwLE10\nwQIDAQAB\n-----END PUBLIC KEY-----" + "API_KEY": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxEUN1DZZ/XU2J6+3EoCX\n6ZQExSyGrJlmcq2s4sxAqThJVGAv4BYqCQjnigUGaLF4+2khGHVXrx4LhwnW54iq\nV3V3Xq59H0Cj3oGGWgKxSOM62xxfizmc1Og/6uAwZTAX4oCsgx5SMaFQbAU5ensM\nVEX9CetXSGhc1bbS23kEHAkjJ0NryRl7DR/ilFKO5pAjTGEzP4aTkF/D3Eu3z15U\nwdkf2WisEsANVTEnNHu2qvdiXGzRSLNF4mVFNO3AsgfnbgXzlN0feQ1HbH+J7Ue5\neHleCGhfS/PGFP3lQ4sA0hB4B/5eZ6ROo8YEuQiNTz+UMFteeGymTgFu2sOwLE10\nwQIDAQAB\n-----END PUBLIC KEY-----", + "AGENT_WORK_PATH": "C:\\WINDOWS\\dc-agent" }, "ext": "js,json" }, diff --git a/server-monitor-xp-0.1.2.zip b/server-monitor-xp-0.1.2.zip new file mode 100644 index 0000000..9b77cbb Binary files /dev/null and b/server-monitor-xp-0.1.2.zip differ diff --git a/src/boot.js b/src/boot.js index c5efec3..b94e2d3 100644 --- a/src/boot.js +++ b/src/boot.js @@ -1,7 +1,8 @@ import fs from 'fs' +import os from 'os' +import path from 'path' import axios from 'axios' import pack from '../package.json' -import crypto from 'crypto' import gather from './control/gather' const version = pack.version @@ -12,6 +13,7 @@ const apiURLs = { } const AGENT_VERSION = process.env.AGENT_VERSION const AGENT_PORT = process.env.AGENT_PORT +const AGENT_WORK_PATH = process.env.AGENT_WORK_PATH || (os.type() === 'Windows_NT' ? 'C:\\WINDOWS\\dc-agent' : '/usr/local/dc-agent') const isDev = process.env.NODE_ENV !== 'production' // 系统配置 const rebootNodeJS = async () => { @@ -42,6 +44,17 @@ const checkVersion = async (version) => { return false } +const checkAndInstallWhoami = () => { + if (!fileIsExists('C:\\Windows\\system32\\whoami.exe') && !fileIsExists('C:\\Windows\\system32\\whoami.bat')) { + console.log('whoami 命令不存在,正在写入') + const batContent = `@echo off +echo %computername%\\%username% +exit /B` + + fs.writeFileSync('C:\\Windows\\system32\\whoami.bat', batContent) + } +} + const fileIsExists = (path) => { try { fs.accessSync(path) @@ -51,14 +64,30 @@ const fileIsExists = (path) => { } } -const checkAndInstallWhoami = () => { - if (!fileIsExists('C:\\Windows\\system32\\whoami.exe') && !fileIsExists('C:\\Windows\\system32\\whoami.bat')) { - console.log('whoami 命令不存在,正在写入') - const batContent = `@echo off -echo %computername%\%username% -exit /B` +const writeProcessPID = (action, workPath, pid) => { + const pidFilePath = path.join(workPath, 'pids', action + '.pid') - fs.writeFileSync('C:\\Windows\\system32\\whoami.bat', batContent) + if (pid === -1) { + if (fileIsExists(pidFilePath)) fs.unlinkSync(pidFilePath) + return + } + + fs.writeFileSync(pidFilePath, pid, 'utf8') +} + +const readProcessPID = (action, workPath) => { + const pidFilePath = path.join(workPath, 'pids', action + '.pid') + + if (!fileIsExists(pidFilePath)) return -1 + + return fs.readFileSync(pidFilePath, 'utf8') +} + +const killProcess = (pid) => { + try { + process.kill(-pid, 'SIGKILL') + } catch (err) { + if (err.code !== 'ESRCH') console.log(err) } } @@ -67,6 +96,23 @@ export default async () => { checkAndInstallWhoami() + const 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((eventType) => { + process.on(eventType, () => { + writeProcessPID('main-js', AGENT_WORK_PATH, -1) + }) + }) + setInterval(() => { checkVersion(version || '0.0.1') // sync }, 120000)