This commit is contained in:
2019-09-02 14:14:55 +08:00
commit 314eee72f2
27 changed files with 969 additions and 0 deletions

81
src/boot.js Normal file
View File

@ -0,0 +1,81 @@
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
}