Files
server-monitor-xp/src/boot.js
2019-09-02 14:14:55 +08:00

82 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fs from 'fs'
import axios from 'axios'
import pack from '../package.json'
import crypto from 'crypto'
import gather from './control/gather'
const version = pack.version
const request = axios.create()
const apiURLs = {
apiURL: process.env.API_URL,
apiKEY: process.env.API_KEY
}
const AGENT_VERSION = process.env.AGENT_VERSION
const AGENT_PORT = process.env.AGENT_PORT
const isDev = process.env.NODE_ENV !== 'production' // 系统配置
const rebootNodeJS = async () => {
if (isDev) {
console.log('当前是开发环境无法自动重启Node.js')
} else {
try {
const { data } = await request.get(`http://127.0.0.1:${AGENT_PORT}/system/cmd?action=restart`)
console.log('自动重启Node.js返回数据', data)
} catch (err) {
console.log(err)
console.log('自动重启Node.js失败')
}
}
}
const checkVersion = async (version) => {
const apiURL = 'https://tool.y-bi.top/check.php?version=' + version
const { data } = await request.get(apiURL)
if (data.status === 1) { // 有更新
console.log('发现新版系统正在重启Node.js...')
await rebootNodeJS()
return true
}
return false
}
const fileIsExists = (path) => {
try {
fs.accessSync(path)
return true
} catch (_) {
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)
}
}
export default async () => {
console.log('AGENT_VERSION:', AGENT_VERSION)
checkAndInstallWhoami()
setInterval(() => {
checkVersion(version || '0.0.1') // sync
}, 120000)
gather({
version: version || '0.0.1',
apiURLs,
isDev,
AGENT_PORT,
AGENT_VERSION
}) // sync
}