From 98d44e8ab467595ae031f0d1221f9dc52d8d9b3d Mon Sep 17 00:00:00 2001 From: chaos-zhu Date: Tue, 22 Oct 2024 22:41:49 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20=E9=87=8D=E6=9E=84=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=95=B0=E6=8D=AE=E5=BA=93-notify=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/app/controller/notify.js | 20 +++-- server/app/db.js | 125 +++++++++++++------------------- server/app/utils/db-class.js | 1 - server/app/utils/notify.js | 9 ++- server/app/utils/storage.js | 90 +++-------------------- 5 files changed, 76 insertions(+), 169 deletions(-) diff --git a/server/app/controller/notify.js b/server/app/controller/notify.js index fdadece..470499d 100644 --- a/server/app/controller/notify.js +++ b/server/app/controller/notify.js @@ -1,9 +1,11 @@ -const { readNotifyConfig, writeNotifyConfig, readNotifyList, writeNotifyList } = require('../utils/storage') const { sendServerChan, sendEmail } = require('../utils/notify') -// const commonTemp = require('../template/commonTemp') +const { NotifyConfigDB, NotifyDB } = require('../utils/db-class') +const notifyDB = new NotifyDB().getInstance() +const notifyConfigDB = new NotifyConfigDB().getInstance() async function getNotifyConfig({ res }) { - const data = await readNotifyConfig() + const data = await notifyConfigDB.findOneAsync({}) + console.log(data) return res.success({ data }) } @@ -11,7 +13,7 @@ async function updateNotifyConfig({ res, request }) { let { body: { noticeConfig } } = request let { type } = noticeConfig try { - switch(type) { + switch (type) { case 'sct': await sendServerChan(noticeConfig[type]['sendKey'], 'EasyNode通知测试', '这是一条测试通知') break @@ -19,7 +21,7 @@ async function updateNotifyConfig({ res, request }) { await sendEmail(noticeConfig[type], 'EasyNode通知测试', '这是一条测试通知') break } - await writeNotifyConfig(noticeConfig) + await notifyConfigDB.update({}, { $set: noticeConfig }, { upsert: true }) return res.success({ msg: '测试通过 | 保存成功' }) } catch (error) { return res.fail({ msg: error.message }) @@ -27,18 +29,14 @@ async function updateNotifyConfig({ res, request }) { } async function getNotifyList({ res }) { - const data = await readNotifyList() + const data = await notifyDB.findAsync({}) res.success({ data }) } async function updateNotifyList({ res, request }) { let { body: { type, sw } } = request if (!([true, false].includes(sw))) return res.fail({ msg: `Error type for sw:${ sw }, must be Boolean` }) - const notifyList = await readNotifyList() - let target = notifyList.find((item) => item.type === type) - if (!target) return res.fail({ msg: `更新失败, 不存在该通知类型:${ type }` }) - target.sw = sw - await writeNotifyList(notifyList) + await notifyDB.updateAsync({ type }, { $set: { sw } }) res.success() } diff --git a/server/app/db.js b/server/app/db.js index c96391f..765f172 100644 --- a/server/app/db.js +++ b/server/app/db.js @@ -1,4 +1,4 @@ -const { writeKey, writeNotifyList, writeNotifyConfig } = require('./utils/storage') +const { writeKey } = require('./utils/storage') const { KeyDB, GroupDB, NotifyDB, NotifyConfigDB } = require('./utils/db-class') function initKeyDB() { @@ -37,83 +37,56 @@ async function initGroupDB() { return Promise.resolve() } -function initNotifyDB() { - return new Promise((resolve, reject) => { - const notifyDB = new NotifyDB().getInstance() - notifyDB.find({}, async (err, notifyList) => { - if (err) { - consola.log('初始化notifyDB错误:', err) - reject(err) - } else { - let defaultData = [{ - 'type': 'login', - 'desc': '登录面板提醒', - 'sw': false - }, { - 'type': 'err_login', - 'desc': '登录错误提醒(连续5次)', - 'sw': false - }, { - 'type': 'updatePwd', - 'desc': '修改密码提醒', - 'sw': false - }, { - 'type': 'host_login', - 'desc': '服务器登录提醒', - 'sw': false - }, { - 'type': 'onekey_complete', - 'desc': '批量指令执行完成提醒', - 'sw': false - }, { - 'type': 'host_expired', - 'desc': '服务器到期提醒', - 'sw': false - }] - if (notifyList.length === 0) { - consola.log('初始化notifyDB✔') - } else { - consola.log('同步notifyDB✔') - defaultData = defaultData.map(defaultItem => { - let item = notifyList.find(notify => notify.type === defaultItem.type) - defaultItem.sw = item ? item.sw : false - return item - }) - } - await writeNotifyList(defaultData) - } - resolve() - }) - }) +async function initNotifyDB() { + const notifyDB = new NotifyDB().getInstance() + let count = await notifyDB.countAsync({}) + if (count !== 0) return + consola.log('初始化notifyDB✔') + let defaultData = [{ + 'type': 'login', + 'desc': '登录面板提醒', + 'sw': false + }, { + 'type': 'err_login', + 'desc': '登录错误提醒(连续5次)', + 'sw': false + }, { + 'type': 'updatePwd', + 'desc': '修改密码提醒', + 'sw': false + }, { + 'type': 'host_login', + 'desc': '服务器登录提醒', + 'sw': false + }, { + 'type': 'onekey_complete', + 'desc': '批量指令执行完成提醒', + 'sw': false + }, { + 'type': 'host_expired', + 'desc': '服务器到期提醒', + 'sw': false + }] + return notifyDB.insertAsync(defaultData) } -function initNotifyConfigDB() { - return new Promise((resolve, reject) => { - const notifyConfigDB = new NotifyConfigDB().getInstance() - notifyConfigDB.count({}, async (err, count) => { - if (err) { - consola.log('初始化NotifyConfigDB错误:', err) - reject(err) - } else { - if (count === 0) { - consola.log('初始化NotifyConfigDB✔') - const defaultData = { - type: 'sct', - sct: { - sendKey: '' - }, - email: { - service: 'QQ', - user: '', - pass: '' - } - } - await writeNotifyConfig(defaultData) - } - } - resolve() - }) - }) +async function initNotifyConfigDB() { + const notifyConfigDB = new NotifyConfigDB().getInstance() + let count = await notifyConfigDB.countAsync({}) + if (count !== 0) return + consola.log('初始化NotifyConfigDB✔') + const defaultData = { + type: 'sct', + sct: { + sendKey: '' + }, + email: { + service: 'QQ', + user: '', + pass: '' + } + } + return notifyConfigDB.insertAsync(defaultData) } module.exports = async () => { diff --git a/server/app/utils/db-class.js b/server/app/utils/db-class.js index b2618ba..35876d4 100644 --- a/server/app/utils/db-class.js +++ b/server/app/utils/db-class.js @@ -52,7 +52,6 @@ module.exports.NotifyDB = class NotifyDB { if (!NotifyDB.instance) { NotifyDB.instance = new Datastore({ filename: notifyDBPath, autoload: true }) // NotifyDB.instance.setAutocompactionInterval(5000) - } } getInstance() { diff --git a/server/app/utils/notify.js b/server/app/utils/notify.js index 67aaa4c..dd1f709 100644 --- a/server/app/utils/notify.js +++ b/server/app/utils/notify.js @@ -1,7 +1,9 @@ const nodemailer = require('nodemailer') const axios = require('axios') -const { getNotifySwByType, readNotifyConfig } = require('../utils/storage') const commonTemp = require('../template/commonTemp') +const { NotifyDB, NotifyConfigDB } = require('./db-class') +const notifyConfigDB = new NotifyConfigDB().getInstance() +const notifyDB = new NotifyDB().getInstance() function sendServerChan(sendKey, title, content) { if (!sendKey) return consola.error('发送server酱通知失败, sendKey 为空') @@ -56,10 +58,11 @@ function sendEmail({ service, user, pass }, title, content) { // 异步发送通知 async function asyncSendNotice(noticeAction, title, content) { try { - let sw = await getNotifySwByType(noticeAction) // 获取对应动作的通知开关 + let notifyList = await notifyDB.findAsync({}) + let { sw } = notifyList.find((item) => item.type === noticeAction) // 获取对应动作的通知开关 console.log('notify swtich: ', noticeAction, sw) if (!sw) return - let notifyConfig = await readNotifyConfig() + let notifyConfig = await notifyConfigDB.findOneAsync({}) let { type } = notifyConfig if (!type) return consola.error('通知类型不存在: ', type) title = `EasyNode-${ title }` diff --git a/server/app/utils/storage.js b/server/app/utils/storage.js index 5efb9bc..fc0edc3 100644 --- a/server/app/utils/storage.js +++ b/server/app/utils/storage.js @@ -64,81 +64,17 @@ const writeSSHRecord = async (record = []) => { }) } -const readNotifyConfig = async () => { - return new Promise((resolve, reject) => { - const notifyConfigDB = new NotifyConfigDB().getInstance() - notifyConfigDB.findOne({}, (err, doc) => { - if (err) { - reject(err) - } else { - resolve(doc) - } - }) - }) -} - -const writeNotifyConfig = async (keyObj = {}) => { - const notifyConfigDB = new NotifyConfigDB().getInstance() - return new Promise((resolve, reject) => { - notifyConfigDB.update({}, { $set: keyObj }, { upsert: true }, (err, numReplaced) => { - if (err) { - reject(err) - } else { - notifyConfigDB.compactDatafile() - resolve(numReplaced) - } - }) - }) -} - -const getNotifySwByType = async (type) => { - if (!type) throw Error('missing params: type') - try { - let notifyList = await readNotifyList() - let { sw } = notifyList.find((item) => item.type === type) - return sw - } catch (error) { - consola.error(`通知类型[${ type }]不存在`) - return false - } -} - -const readNotifyList = async () => { - return new Promise((resolve, reject) => { - const notifyDB = new NotifyDB().getInstance() - notifyDB.find({}, (err, docs) => { - if (err) { - consola.error('读取notify list错误: ', err) - reject(err) - } else { - resolve(docs) - } - }) - }) -} - -const writeNotifyList = async (notifyList) => { - return new Promise((resolve, reject) => { - const notifyDB = new NotifyDB().getInstance() - notifyDB.remove({}, { multi: true }, (err) => { - if (err) { - consola.error('清空notify list出错:', err) - reject(err) - } else { - notifyDB.compactDatafile() - notifyDB.insert(notifyList, (err, newDocs) => { - if (err) { - consola.error('写入新的notify list出错:', err) - reject(err) - } else { - notifyDB.compactDatafile() - resolve(newDocs) - } - }) - } - }) - }) -} +// const getNotifySwByType = async (type) => { +// if (!type) throw Error('missing params: type') +// try { +// let notifyList = await readNotifyList() +// let { sw } = notifyList.find((item) => item.type === type) +// return sw +// } catch (error) { +// consola.error(`通知类型[${ type }]不存在`) +// return false +// } +// } const readScriptList = async () => { return new Promise((resolve, reject) => { @@ -225,8 +161,6 @@ const deleteOneKeyRecord = async (ids =[]) => { module.exports = { readSSHRecord, writeSSHRecord, readKey, writeKey, - readNotifyList, writeNotifyList, - readNotifyConfig, writeNotifyConfig, getNotifySwByType, readScriptList, writeScriptList, readOneKeyRecord, writeOneKeyRecord, deleteOneKeyRecord -} \ No newline at end of file +}