// 系统管理 'use strict'; const Controller = require('egg').Controller; const shells = require('../../config/shells'); const filePath = require('../../config/filespath'); class SystemctlController extends Controller { // 重启机器 async reboot() { try { await this.service.shell.shell(shells.reboot); } catch (error) { const body = { errcode: -1002, errmsg: '重启机器失败', error }; throw new Error(JSON.stringify(body)); } } // 禁用wan网卡 async wanDown() { try { const res = await this.service.shell.shell(shells.wanDown); if (res.errcode === 0) { this.ctx.body = { errcode: 0, errmsg: '' }; } else { this.ctx.body = { errcode: -1, errmsg: '禁用wan网卡失败' }; } } catch (error) { const body = { errcode: -1002, errmsg: '禁用wan网卡失败', error }; throw new Error(JSON.stringify(body)); } } // 启用wan网卡 async wanup() { try { const res = await this.service.shell.shell(shells.wanUp); if (res.errcode === 0) { this.ctx.body = { errcode: 0, errmsg: '' }; } else { this.ctx.body = { errcode: -1, errmsg: '启用wan网卡失败' }; } } catch (error) { const body = { errcode: -1002, errmsg: '启用wan网卡失败', error }; throw new Error(JSON.stringify(body)); } } // 启用lan网卡 async lanup() { try { const res = await this.service.shell.shell(shells.lanUp); if (res.errcode === 0) { this.ctx.body = { errcode: 0, errmsg: '' }; } else { this.ctx.body = { errcode: -1, errmsg: '启用lan网卡失败' }; } } catch (error) { const body = { errcode: -1002, errmsg: '启用lan网卡失败', error }; throw new Error(JSON.stringify(body)); } } // 禁用lan网卡 async lanDown() { try { const res = await this.service.shell.shell(shells.lanDown); if (res.errcode === 0) { this.ctx.body = { errcode: 0, errmsg: '' }; } else { this.ctx.body = { errcode: -1, errmsg: '禁用lan网卡失败' }; } } catch (error) { const body = { errcode: -1002, errmsg: '禁用lan网卡失败', error }; throw new Error(JSON.stringify(body)); } } // ping async ping() { const { address } = this.ctx.request.body; try { const res = await this.service.shell.shell(`ping ${address} -c 3`); console.log(res, 'res'); if (res.errcode === 0 && res.data !== '') { this.ctx.body = { errcode: 0, errmsg: '' }; } else { this.ctx.body = { errcode: -1, errmsg: '地址链接失败' }; } } catch (error) { const body = { errcode: -1002, errmsg: '地址链接异常', error }; throw new Error(JSON.stringify(body)); } } // 获取时间 async getdate() { try { const res = await this.service.shell.shell('date "+%Y-%m-%d %H:%M:%S"'); if (res.errcode === 0) { this.ctx.body = { errcode: 0, errmsg: '' }; } else { this.ctx.body = { errcode: -1, errmsg: '获取时间失败' }; } } catch (error) { const body = { errcode: -1002, errmsg: '获取时间失败', error }; throw new Error(JSON.stringify(body)); } } // 设置系统时间 async setdate() { const { date } = this.ctx.request.body; try { const res = await this.service.shell.shell(`date -s "${date}"`); console.log(res); if (res.errcode === 0) { this.ctx.body = { errcode: 0, errmsg: '' }; } else { this.ctx.body = { errcode: -0, errmsg: '设置系统时间失败' }; } } catch (error) { const body = { errcode: -1002, errmsg: '设置系统时间失败', error }; throw new Error(JSON.stringify(body)); } } // 获取vpn链接状态 async vpnstate() { try { const res = await this.service.shell.shell('swanctl -l | grep established'); if (res.errcode === 0 && res.data.length > 0) { this.ctx.body = { errcode: 0, errmsg: '', data: '已链接' }; } else { this.ctx.body = { errcode: 0, errmsg: '', data: '未链接' }; } } catch (error) { const body = { errcode: -1002, errmsg: '获取VPN链接状态失败', error }; throw new Error(JSON.stringify(body)); } } // 获取cpu使用率 async cpu() { try { const res = await this.service.shell.shell('vmstat | tail -n 1 | awk \'{print $13}\''); if (res.errcode === 0 && res.data) { this.ctx.body = { errcode: 0, errmsg: '', data: { cpu: res.data } }; } else { this.ctx.body = { errcode: -1, errmsg: '获取CPU使用率失败' }; } } catch (error) { const body = { errcode: -1002, errmsg: '获取CPU使用率失败', error }; throw new Error(JSON.stringify(body)); } } // 获取内存使用率 async memory() { try { const res = await this.service.shell.shell('free -t -m |tail -n 1 | awk \'{print $3/$2*100}\''); if (res.errcode === 0 && res.data) { this.ctx.body = { errcode: 0, errmsg: '', data: { memory: res.data } }; } else { this.ctx.body = { errcode: -1, errmsg: '获取内存使用率失败' }; } } catch (error) { const body = { errcode: -1002, errmsg: '获取内存使用率失败', error }; throw new Error(JSON.stringify(body)); } } // 获取设备信息 async devinfo() { try { const configJson = require(filePath.configJson); const model = configJson.model; const version = configJson.version; this.ctx.body = { errcode: 0, errmsg: '', data: { model, version } }; } catch (error) { const body = { errcode: -1002, errmsg: '获取设备信息失败', error }; throw new Error(JSON.stringify(body)); } } // sslvpn 启动 停止 重启 async sslvpnstate() { const { type } = this.ctx.request.query; let data; if (type === 'stop') { data = shells.opStop; } if (type === 'start') { data = shells.opStart; } if (type === 'restart') { data = shells.opRestart; } try { const res = await this.service.shell.shell(data); if (res.errcode === 0) { this.ctx.body = { errcode: 0, errmsg: '' }; } else { this.ctx.body = { errcode: -1, errmsg: '操作失败' }; } } catch (error) { const body = { errcode: -1002, errmsg: '操作失败', error }; throw new Error(JSON.stringify(body)); } } // ipsecvpn 启动 停止 重启 async ipsecvpnstate() { const { type } = this.ctx.request.query; let data; if (type === 'stop') { data = shells.swStop; } if (type === 'start') { data = shells.swStart; } if (type === 'restart') { data = shells.swRestart; } try { const res = await this.service.shell.shell(data); if (res.errcode === 0) { this.ctx.body = { errcode: 0, errmsg: '' }; } else { this.ctx.body = { errcode: -1, errmsg: '操作失败' }; } } catch (error) { const body = { errcode: -1002, errmsg: '操作失败', error }; throw new Error(JSON.stringify(body)); } } } module.exports = SystemctlController;