From 1bdb62061745967bce14fbc2e85cc560622904da Mon Sep 17 00:00:00 2001 From: chaos-zhu Date: Fri, 16 Aug 2024 23:57:25 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20=E9=87=8D=E6=9E=84=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/app/config/index.js | 4 +- server/app/controller/notify.js | 76 ++----- server/app/db.js | 144 ++++---------- server/app/router/routes.js | 27 +-- server/app/utils/db-class.js | 35 ++-- server/app/utils/email.js | 1 + server/app/utils/index.js | 26 ++- server/app/utils/storage.js | 77 +++----- web/src/api/index.js | 32 +-- .../views/setting/components/email-list.vue | 185 ------------------ .../{notify-list.vue => global-notify.vue} | 9 +- .../setting/components/notify-config.vue | 130 ++++++++++++ web/src/views/setting/components/record.vue | 2 +- web/src/views/setting/index.vue | 12 +- 14 files changed, 280 insertions(+), 480 deletions(-) delete mode 100644 web/src/views/setting/components/email-list.vue rename web/src/views/setting/components/{notify-list.vue => global-notify.vue} (81%) create mode 100644 web/src/views/setting/components/notify-config.vue diff --git a/server/app/config/index.js b/server/app/config/index.js index a6f0985..1d31257 100644 --- a/server/app/config/index.js +++ b/server/app/config/index.js @@ -11,10 +11,10 @@ module.exports = { credentialsDBPath: path.join(process.cwd(),'app/db/credentials.db'), keyDBPath: path.join(process.cwd(),'app/db/key.db'), hostListDBPath: path.join(process.cwd(),'app/db/host.db'), - notifyConfDBPath: path.join(process.cwd(),'app/db/notify.db'), groupConfDBPath: path.join(process.cwd(),'app/db/group.db'), - emailNotifyDBPath: path.join(process.cwd(),'app/db/email.db'), scriptsDBPath: path.join(process.cwd(),'app/db/scripts.db'), + notifyDBPath: path.join(process.cwd(),'app/db/notify.db'), + notifyConfigDBPath: path.join(process.cwd(),'app/db/notify-config.db'), onekeyDBPath: path.join(process.cwd(),'app/db/onekey.db'), apiPrefix: '/api/v1', logConfig: { diff --git a/server/app/controller/notify.js b/server/app/controller/notify.js index 90b3612..84c3d17 100644 --- a/server/app/controller/notify.js +++ b/server/app/controller/notify.js @@ -1,64 +1,16 @@ -const { - readSupportEmailList, - readUserEmailList, - writeUserEmailList, - emailTransporter, - readNotifyList, - writeNotifyList } = require('../utils') -const commonTemp = require('../template/commonTemp') +const { readNotifyConfig, writeNotifyConfig, readNotifyList, writeNotifyList } = require('../utils') +// const commonTemp = require('../template/commonTemp') -async function getSupportEmailList({ res }) { - const data = await readSupportEmailList() - res.success({ data }) +async function getNotifyConfig({ res }) { + const data = await readNotifyConfig() + return res.success({ data }) } -async function getUserEmailList({ res }) { - const userEmailList = (await readUserEmailList()).map(({ target, auth: { user } }) => ({ target, user })) - const supportEmailList = await readSupportEmailList() - const data = userEmailList.map(({ target: userTarget, user: email }) => { - let name = supportEmailList.find(({ target: supportTarget }) => supportTarget === userTarget).name - return { name, email } - }) - res.success({ data }) -} - -async function pushEmail({ res, request }) { - let { body: { toEmail, isTest } } = request - if (!isTest) return res.fail({ msg: '此接口暂时只做测试邮件使用, 需传递参数isTest: true' }) - consola.info('发送测试邮件:', toEmail) - let { code, msg } = await emailTransporter({ toEmail, title: '测试邮件', html: commonTemp('邮件通知测试邮件') }) - msg = msg && msg.message || msg - if (code === 0) return res.success({ msg }) - return res.fail({ msg }) -} - -async function updateUserEmailList({ res, request }) { - let { body: { target, auth } } = request - const supportList = await readSupportEmailList() - let flag = supportList.some((item) => item.target === target) - if (!flag) return res.fail({ msg: `不支持的邮箱类型:${ target }` }) - if (!auth.user || !auth.pass) return res.fail({ msg: 'missing params: auth.' }) - - let newUserEmail = { target, auth } - let userEmailList = await readUserEmailList() - let idx = userEmailList.findIndex(({ auth: { user } }) => auth.user === user) - if (idx !== -1) userEmailList.splice(idx, 1, newUserEmail) - else userEmailList.unshift(newUserEmail) - - const { code, msg } = await writeUserEmailList(userEmailList) - if (code === 0) return res.success() - return res.fail({ msg }) -} - -async function removeUserEmail({ res, request }) { - let { params: { email } } = request - const userEmailList = await readUserEmailList() - let idx = userEmailList.findIndex(({ auth: { user } }) => user === email) - if (idx === -1) return res.fail({ msg: `删除失败, 不存在该邮箱:${ email }` }) - userEmailList.splice(idx, 1) - const { code, msg } = await writeUserEmailList(userEmailList) - if (code === 0) return res.success() - return res.fail({ msg }) +// 根据type待编写测试方法,测试通过才保存到库 +async function updateNotifyConfig({ res, request }) { + let { body: { noticeConfig } } = request + await writeNotifyConfig(noticeConfig) + return res.success() } async function getNotifyList({ res }) { @@ -73,17 +25,13 @@ async function updateNotifyList({ res, request }) { let target = notifyList.find((item) => item.type === type) if (!target) return res.fail({ msg: `更新失败, 不存在该通知类型:${ type }` }) target.sw = sw - // console.log(notifyList) await writeNotifyList(notifyList) res.success() } module.exports = { - pushEmail, - getSupportEmailList, - getUserEmailList, - updateUserEmailList, - removeUserEmail, + getNotifyConfig, + updateNotifyConfig, getNotifyList, updateNotifyList } diff --git a/server/app/db.js b/server/app/db.js index 1a0e53f..baef4ba 100644 --- a/server/app/db.js +++ b/server/app/db.js @@ -1,6 +1,5 @@ -const { writeKey, writeNotifyList, writeGroupList } = require('./utils/storage') -const { KeyDB, NotifyDB, GroupDB, EmailNotifyDB } = require('./utils/db-class') -const { readScriptList, writeScriptList } = require('./utils') +const { writeKey, writeGroupList, writeNotifyList, writeNotifyConfig } = require('./utils/storage') +const { KeyDB, GroupDB, NotifyDB, NotifyConfigDB } = require('./utils/db-class') function initKeyDB() { return new Promise((resolve, reject) => { @@ -27,6 +26,25 @@ function initKeyDB() { }) } +function initGroupDB() { + return new Promise((resolve, reject) => { + const groupDB = new GroupDB().getInstance() + groupDB.count({}, async (err, count) => { + if (err) { + consola.log('初始化groupDB错误:', err) + reject(err) + } else { + if (count === 0) { + consola.log('初始化groupDB✔') + const defaultData = [{ '_id': 'default', 'name': '默认分组', 'index': 0 }] + await writeGroupList(defaultData) + } + } + resolve() + }) + }) +} + function initNotifyDB() { return new Promise((resolve, reject) => { const notifyDB = new NotifyDB().getInstance() @@ -67,18 +85,28 @@ function initNotifyDB() { }) } -function initGroupDB() { +function initNotifyConfigDB() { return new Promise((resolve, reject) => { - const groupDB = new GroupDB().getInstance() - groupDB.count({}, async (err, count) => { + const notifyConfigDB = new NotifyConfigDB().getInstance() + notifyConfigDB.count({}, async (err, count) => { if (err) { - consola.log('初始化groupDB错误:', err) + consola.log('初始化NotifyConfigDB错误:', err) reject(err) } else { if (count === 0) { - consola.log('初始化groupDB✔') - const defaultData = [{ '_id': 'default', 'name': '默认分组', 'index': 0 }] - await writeGroupList(defaultData) + consola.log('初始化NotifyConfigDB✔') + const defaultData = { + type: 'sct', + sct: { + sendKey: '' + }, + email: { + service: 'QQ', + user: '', + pass: '' + } + } + await writeNotifyConfig(defaultData) } } resolve() @@ -86,103 +114,9 @@ function initGroupDB() { }) } -function initEmailNotifyDB() { - return new Promise((resolve, reject) => { - const emailNotifyDB = new EmailNotifyDB().getInstance() - emailNotifyDB.count({}, async (err, count) => { - if (err) { - consola.log('初始化emailNotifyDB错误:', err) - reject(err) - } else { - if (count === 0) { - consola.log('初始化emailNotifyDB✔') - const defaultData = { - 'support': [ - { - 'name': 'QQ邮箱', - 'target': 'qq', - 'host': 'smtp.qq.com', - 'port': 465, - 'secure': true, - 'tls': { - 'rejectUnauthorized': false - } - }, - { - 'name': '网易126', - 'target': 'wangyi126', - 'host': 'smtp.126.com', - 'port': 465, - 'secure': true, - 'tls': { - 'rejectUnauthorized': false - } - }, - { - 'name': '网易163', - 'target': 'wangyi163', - 'host': 'smtp.163.com', - 'port': 465, - 'secure': true, - 'tls': { - 'rejectUnauthorized': false - } - } - ], - 'user': [ - ] - } - emailNotifyDB.update({}, { $set: defaultData }, { upsert: true }, (err, numReplaced) => { - if (err) { - reject(err) - } else { - emailNotifyDB.compactDatafile() - resolve(numReplaced) - } - }) - } else { - resolve() - } - } - }) - }) -} - -function initScriptsDB() { - // eslint-disable-next-line no-async-promise-executor - return new Promise(async (resolve) => { - let scriptList = await readScriptList() - let clientInstallScript = 'curl -o- https://mirror.ghproxy.com/https://raw.githubusercontent.com/chaos-zhu/easynode/main/client/easynode-client-install.sh | bash' - let clientUninstallScript = 'curl -o- https://mirror.ghproxy.com/https://raw.githubusercontent.com/chaos-zhu/easynode/main/client/easynode-client-uninstall.sh | bash' - let installId = 'clientInstall' - let uninstallId = 'clientUninstall' - - let isClientInstall = scriptList?.find(script => script._id = installId) - let isClientUninstall = scriptList?.find(script => script._id = uninstallId) - let writeFlag = false - if (!isClientInstall) { - console.info('初始化客户端安装脚本') - scriptList.push({ _id: installId, name: 'easynode-客户端-安装脚本', description: '系统内置|重启生成', command: clientInstallScript, index: 1 }) - writeFlag = true - } else { - console.info('客户端安装脚本已存在') - } - if (!isClientUninstall) { - console.info('初始化客户端卸载脚本') - scriptList.push({ _id: uninstallId, name: 'easynode-客户端-卸载脚本', description: '系统内置|重启生成', command: clientUninstallScript, index: 0 }) - writeFlag = true - } else { - console.info('客户端卸载脚本已存在') - } - if (writeFlag) await writeScriptList(scriptList) - resolve() - }) -} - module.exports = async () => { await initKeyDB() await initNotifyDB() await initGroupDB() - await initEmailNotifyDB() - // await initScriptsDB() + await initNotifyConfigDB() } diff --git a/server/app/router/routes.js b/server/app/router/routes.js index 5421273..a41e9de 100644 --- a/server/app/router/routes.js +++ b/server/app/router/routes.js @@ -1,7 +1,7 @@ const { getSSHList, addSSH, updateSSH, removeSSH, getCommand } = require('../controller/ssh') const { getHostList, addHost, updateHost, removeHost, importHost } = require('../controller/host') const { login, getpublicKey, updatePwd, getLoginRecord, getEasynodeVersion } = require('../controller/user') -const { getSupportEmailList, getUserEmailList, updateUserEmailList, removeUserEmail, pushEmail, getNotifyList, updateNotifyList } = require('../controller/notify') +const { getNotifyConfig, updateNotifyConfig, getNotifyList, updateNotifyList } = require('../controller/notify') const { getGroupList, addGroupList, updateGroupList, removeGroup } = require('../controller/group') const { getScriptList, getLocalScriptList, addScript, updateScriptList, removeScript } = require('../controller/scripts') const { getOnekeyRecord, removeOnekeyRecord } = require('../controller/onekey') @@ -90,28 +90,13 @@ const user = [ const notify = [ { method: 'get', - path: '/support-email', - controller: getSupportEmailList + path: '/notify-config', + controller: getNotifyConfig }, { - method: 'get', - path: '/user-email', - controller: getUserEmailList - }, - { - method: 'post', - path: '/user-email', - controller: updateUserEmailList - }, - { - method: 'post', - path: '/push-email', - controller: pushEmail - }, - { - method: 'delete', - path: '/user-email/:email', - controller: removeUserEmail + method: 'put', + path: '/notify-config', + controller: updateNotifyConfig }, { method: 'get', diff --git a/server/app/utils/db-class.js b/server/app/utils/db-class.js index 332b2ec..105b401 100644 --- a/server/app/utils/db-class.js +++ b/server/app/utils/db-class.js @@ -1,5 +1,14 @@ const Datastore = require('@seald-io/nedb') -const { credentialsDBPath, hostListDBPath, keyDBPath, emailNotifyDBPath, notifyConfDBPath, groupConfDBPath, scriptsDBPath, onekeyDBPath } = require('../config') +const { + credentialsDBPath, + hostListDBPath, + keyDBPath, + notifyDBPath, + notifyConfigDBPath, + groupConfDBPath, + scriptsDBPath, + onekeyDBPath +} = require('../config') module.exports.KeyDB = class KeyDB { constructor() { @@ -37,7 +46,7 @@ module.exports.SshRecordDB = class SshRecordDB { module.exports.NotifyDB = class NotifyDB { constructor() { if (!NotifyDB.instance) { - NotifyDB.instance = new Datastore({ filename: notifyConfDBPath, autoload: true }) + NotifyDB.instance = new Datastore({ filename: notifyDBPath, autoload: true }) } } getInstance() { @@ -45,6 +54,17 @@ module.exports.NotifyDB = class NotifyDB { } } +module.exports.NotifyConfigDB = class NotifyConfigDB { + constructor() { + if (!NotifyConfigDB.instance) { + NotifyConfigDB.instance = new Datastore({ filename: notifyConfigDBPath, autoload: true }) + } + } + getInstance() { + return NotifyConfigDB.instance + } +} + module.exports.GroupDB = class GroupDB { constructor() { if (!GroupDB.instance) { @@ -56,17 +76,6 @@ module.exports.GroupDB = class GroupDB { } } -module.exports.EmailNotifyDB = class EmailNotifyDB { - constructor() { - if (!EmailNotifyDB.instance) { - EmailNotifyDB.instance = new Datastore({ filename: emailNotifyDBPath, autoload: true }) - } - } - getInstance() { - return EmailNotifyDB.instance - } -} - module.exports.ScriptsDB = class ScriptsDB { constructor() { if (!ScriptsDB.instance) { diff --git a/server/app/utils/email.js b/server/app/utils/email.js index 13a9160..c0a13db 100644 --- a/server/app/utils/email.js +++ b/server/app/utils/email.js @@ -1,3 +1,4 @@ +// :TODO: 重写 const nodemailer = require('nodemailer') const { readSupportEmailList, readUserEmailList } = require('./storage') const commonTemp = require('../template/commonTemp') diff --git a/server/app/utils/index.js b/server/app/utils/index.js index c634634..27374d7 100644 --- a/server/app/utils/index.js +++ b/server/app/utils/index.js @@ -5,19 +5,18 @@ const { writeHostList, readKey, writeKey, - readSupportEmailList, - readUserEmailList, - writeUserEmailList, - readNotifyList, - getNotifySwByType, - writeNotifyList, readGroupList, writeGroupList, readScriptList, writeScriptList, readOneKeyRecord, writeOneKeyRecord, - deleteOneKeyRecord + deleteOneKeyRecord, + readNotifyConfig, + writeNotifyConfig, + getNotifySwByType, + readNotifyList, + writeNotifyList } = require('./storage') const { RSADecryptSync, AESEncryptSync, AESDecryptSync, SHA1Encrypt } = require('./encrypt') const { verifyAuthSync, isProd } = require('./verify-auth') @@ -44,19 +43,18 @@ module.exports = { writeHostList, readKey, writeKey, - readSupportEmailList, - readUserEmailList, - writeUserEmailList, emailTransporter, sendEmailToConfList, - readNotifyList, - getNotifySwByType, - writeNotifyList, readGroupList, writeGroupList, readScriptList, writeScriptList, readOneKeyRecord, writeOneKeyRecord, - deleteOneKeyRecord + deleteOneKeyRecord, + readNotifyConfig, + writeNotifyConfig, + getNotifySwByType, + readNotifyList, + writeNotifyList } diff --git a/server/app/utils/storage.js b/server/app/utils/storage.js index de43dd6..b2d84e5 100644 --- a/server/app/utils/storage.js +++ b/server/app/utils/storage.js @@ -1,4 +1,4 @@ -const { KeyDB, HostListDB, SshRecordDB, NotifyDB, GroupDB, EmailNotifyDB, ScriptsDB, OnekeyDB } = require('./db-class') +const { KeyDB, HostListDB, SshRecordDB, NotifyDB, NotifyConfigDB, ScriptsDB, GroupDB, OnekeyDB } = require('./db-class') const readKey = async () => { return new Promise((resolve, reject) => { @@ -102,56 +102,33 @@ const writeHostList = async (record = []) => { }) } -const readEmailNotifyConf = () => { +const readNotifyConfig = async () => { return new Promise((resolve, reject) => { - const emailNotifyDB = new EmailNotifyDB().getInstance() - emailNotifyDB.findOne({}, (err, docs) => { + const notifyConfigDB = new NotifyConfigDB().getInstance() + notifyConfigDB.findOne({}, (err, doc) => { if (err) { - consola.error('读取email-notify-conf-db错误:', err) reject(err) } else { - resolve(docs) + resolve(doc) } }) }) } -const writeUserEmailList = (user) => { - const emailNotifyDB = new EmailNotifyDB().getInstance() - // eslint-disable-next-line no-async-promise-executor - return new Promise(async (resolve, reject) => { - let support = await readSupportEmailList() - const emailConf = { support, user } - emailNotifyDB.update({}, { $set: emailConf }, { upsert: true }, (err) => { + +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({ code: -1, msg: err.message || err }) + reject(err) } else { - emailNotifyDB.compactDatafile() - resolve({ code: 0 }) + notifyConfigDB.compactDatafile() + resolve(numReplaced) } }) }) } -const readSupportEmailList = async () => { - let support = [] - try { - support = (await readEmailNotifyConf()).support - } catch (error) { - consola.error('读取email support错误: ', error) - } - return support -} - -const readUserEmailList = async () => { - let user = [] - try { - user = (await readEmailNotifyConf()).user - } catch (error) { - consola.error('读取email config错误: ', error) - } - return user -} - const getNotifySwByType = async (type) => { if (!type) throw Error('missing params: type') try { @@ -283,6 +260,7 @@ const readOneKeyRecord = async () => { consola.error('读取onekey record错误: ', err) reject(err) } else { + onekeyDB.compactDatafile() resolve(docs) } }) @@ -319,23 +297,12 @@ const deleteOneKeyRecord = async (ids =[]) => { } module.exports = { - readSSHRecord, - writeSSHRecord, - readHostList, - writeHostList, - readKey, - writeKey, - readNotifyList, - getNotifySwByType, - writeNotifyList, - readGroupList, - writeGroupList, - readSupportEmailList, - readUserEmailList, - writeUserEmailList, - readScriptList, - writeScriptList, - readOneKeyRecord, - writeOneKeyRecord, - deleteOneKeyRecord + readSSHRecord, writeSSHRecord, + readHostList, writeHostList, + readKey, writeKey, + readNotifyList, writeNotifyList, + readNotifyConfig, writeNotifyConfig, getNotifySwByType, + readGroupList, writeGroupList, + readScriptList, writeScriptList, + readOneKeyRecord, writeOneKeyRecord, deleteOneKeyRecord } \ No newline at end of file diff --git a/web/src/api/index.js b/web/src/api/index.js index eec3127..0298e6d 100644 --- a/web/src/api/index.js +++ b/web/src/api/index.js @@ -55,20 +55,26 @@ export default { // updateHostSort(data) { // return axios({ url: '/host-sort', method: 'put', data }) // }, - getUserEmailList() { - return axios({ url: '/user-email', method: 'get' }) + // getUserEmailList() { + // return axios({ url: '/user-email', method: 'get' }) + // }, + // getSupportEmailList() { + // return axios({ url: '/support-email', method: 'get' }) + // }, + // updateUserEmailList(data) { + // return axios({ url: '/user-email', method: 'post', data }) + // }, + // deleteUserEmail(email) { + // return axios({ url: `/user-email/${ email }`, method: 'delete' }) + // }, + // pushTestEmail(data) { + // return axios({ url: '/push-email', method: 'post', data }) + // }, + getNotifyConfig() { + return axios({ url: '/notify-config', method: 'get' }) }, - getSupportEmailList() { - return axios({ url: '/support-email', method: 'get' }) - }, - updateUserEmailList(data) { - return axios({ url: '/user-email', method: 'post', data }) - }, - deleteUserEmail(email) { - return axios({ url: `/user-email/${ email }`, method: 'delete' }) - }, - pushTestEmail(data) { - return axios({ url: '/push-email', method: 'post', data }) + updateNotifyConfig(data) { + return axios({ url: '/notify-config', method: 'put', data }) }, getNotifyList() { return axios({ url: '/notify', method: 'get' }) diff --git a/web/src/views/setting/components/email-list.vue b/web/src/views/setting/components/email-list.vue deleted file mode 100644 index e46253f..0000000 --- a/web/src/views/setting/components/email-list.vue +++ /dev/null @@ -1,185 +0,0 @@ - - - - - diff --git a/web/src/views/setting/components/notify-list.vue b/web/src/views/setting/components/global-notify.vue similarity index 81% rename from web/src/views/setting/components/notify-list.vue rename to web/src/views/setting/components/global-notify.vue index 3adb738..de0c2d7 100644 --- a/web/src/views/setting/components/notify-list.vue +++ b/web/src/views/setting/components/global-notify.vue @@ -1,9 +1,14 @@