新增批量指令提醒&终端登录提醒

This commit is contained in:
chaos-zhu 2024-08-17 16:33:27 +08:00
parent 221f9e06b9
commit e92e2beb4d
5 changed files with 26 additions and 14 deletions

View File

@ -1,6 +1,6 @@
const jwt = require('jsonwebtoken')
const axios = require('axios')
const { getNetIPInfo, readKey, writeKey, RSADecryptSync, AESEncryptSync, SHA1Encrypt, getNotifySwByType } = require('../utils')
const { getNetIPInfo, readKey, writeKey, RSADecryptSync, AESEncryptSync, SHA1Encrypt } = require('../utils')
const { asyncSendNotice } = require('../utils/notify')
const getpublicKey = async ({ res }) => {
@ -26,7 +26,7 @@ const login = async ({ res, request }) => {
if (loginErrCount >= allowErrCount) {
const { ip, country, city } = await getNetIPInfo(clientIp)
// 异步发送通知&禁止登录
asyncSendNotice('err_login', 'EasyNode登录错误提醒', `错误登录次数: ${ loginErrTotal }<br/>地点:${ country + city }<br/>IP: ${ ip }`)
asyncSendNotice('err_login', '登录错误提醒', `错误登录次数: ${ loginErrTotal }<br/>地点:${ country + city }<br/>IP: ${ ip }`)
forbidLogin = true
loginErrCount = 0
@ -82,7 +82,7 @@ const beforeLoginHandler = async (clientIp, jwtExpires) => {
consola.info('登录成功:', new Date(), { ip, country, city })
// 邮件登录通知
asyncSendNotice('login', 'EasyNode登录提醒', `地点:${ country + city }<br/>IP: ${ ip }`)
asyncSendNotice('login', '登录提醒', `地点:${ country + city }<br/>IP: ${ ip }`)
global.loginRecord.unshift(clientIPInfo)
if (global.loginRecord.length > 10) global.loginRecord = global.loginRecord.slice(0, 10)
@ -102,7 +102,7 @@ const updatePwd = async ({ res, request }) => {
keyObj.pwd = newPwd
await writeKey(keyObj)
asyncSendNotice('updatePwd', 'EasyNode用户密码修改提醒', `原用户名:${ user }<br/>更新用户名: ${ newLoginName }`)
asyncSendNotice('updatePwd', '用户密码修改提醒', `原用户名:${ user }<br/>更新用户名: ${ newLoginName }`)
res.success({ data: true, msg: 'success' })
}

View File

@ -59,23 +59,27 @@ function initNotifyDB() {
'type': 'login',
'desc': '登录面板提醒',
'sw': true
},
{
}, {
'type': 'err_login',
'desc': '登录错误提醒(连续5次)',
'sw': true
},
{
}, {
'type': 'updatePwd',
'desc': '修改密码提醒',
'sw': true
},
{
}, {
'type': 'host_login',
'desc': '服务器登录提醒',
'sw': true
}, {
'type': 'onekey_complete',
'desc': '批量指令执行完成提醒',
'sw': true
}, {
'type': 'host_expired',
'desc': '服务器到期提醒',
'sw': true
}
]
} ]
await writeNotifyList(defaultData)
}

View File

@ -1,6 +1,7 @@
const { Server } = require('socket.io')
const { Client: SSHClient } = require('ssh2')
const { readHostList, readSSHRecord, verifyAuthSync, AESDecryptSync, writeOneKeyRecord, shellThrottle } = require('../utils')
const { asyncSendNotice } = require('../utils/notify')
const execStatusEnum = {
connecting: '连接中',
@ -111,7 +112,9 @@ module.exports = (httpServer) => {
item.status = execStatusEnum.execTimeout
}
})
socket.emit('timeout', { reason: `执行超时,已强制终止执行 - 超时时间${ timeout }`, result: execResult })
let reason = `执行超时,已强制终止执行 - 超时时间${ timeout }`
asyncSendNotice('onekey_complete', '批量指令执行超时', reason)
socket.emit('timeout', { reason, result: execResult })
socket.disconnect()
}, timeout * 1000)
console.log('hosts:', hosts)
@ -174,6 +177,7 @@ module.exports = (httpServer) => {
await Promise.all(execPromise)
consola.success('onekey执行完成')
socket.emit('exec_complete')
asyncSendNotice('onekey_complete', '批量指令执行完成', '请登录面板查看执行结果')
socket.disconnect()
})

View File

@ -1,6 +1,7 @@
const { Server } = require('socket.io')
const { Client: SSHClient } = require('ssh2')
const { readHostList, readSSHRecord, verifyAuthSync, AESDecryptSync } = require('../utils')
const { asyncSendNotice } = require('../utils/notify')
function createInteractiveShell(socket, sshClient) {
return new Promise((resolve) => {
@ -49,7 +50,7 @@ async function createTerminal(ip, socket, sshClient) {
return new Promise(async (resolve) => {
const hostList = await readHostList()
const targetHostInfo = hostList.find(item => item.host === ip) || {}
let { authType, host, port, username } = targetHostInfo
let { authType, host, port, username, name } = targetHostInfo
if (!host) return socket.emit('create_fail', `查找【${ ip }】凭证信息失败`)
let authInfo = { host, port, username }
// 统一使用commonKey解密
@ -69,6 +70,7 @@ async function createTerminal(ip, socket, sshClient) {
consola.log('连接信息', { username, port, authType })
sshClient
.on('ready', async() => {
asyncSendNotice('host_login', '终端登录', `别名: ${ name } \n IP${ host } \n 端口:${ port } \n 状态: 登录成功`)
consola.success('终端连接成功:', host)
socket.emit('connect_terminal_success', `终端连接成功:${ host }`)
let stream = await createInteractiveShell(socket, sshClient)
@ -85,6 +87,7 @@ async function createTerminal(ip, socket, sshClient) {
})
.on('error', (err) => {
consola.log(err)
asyncSendNotice('host_login', '终端登录', `别名: ${ name } \n IP${ host } \n 端口:${ port } \n 状态: 登录失败`)
consola.error('连接终端失败:', host, err.message)
socket.emit('connect_fail', err.message)
})

View File

@ -61,6 +61,7 @@ async function asyncSendNotice(noticeAction, title, content) {
let notifyConfig = await readNotifyConfig()
let { type } = notifyConfig
if (!type) return consola.error('通知类型不存在: ', type)
title = `EasyNode-${ title }`
content += `<br/>通知发送时间:${ new Date() }`
switch (type) {
case 'sct':