From 221f9e06b95b6708f597a60f9960156f531399d2 Mon Sep 17 00:00:00 2001 From: chaos-zhu Date: Sat, 17 Aug 2024 16:19:32 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=96=B0=E5=A2=9E=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E5=88=B0=E6=9C=9F=E6=8F=90=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/app/db.js | 10 ++--- server/app/schedule/expired-notify.js | 9 ++-- server/app/template/commonTemp.js | 60 ++++++++++++++++---------- server/app/utils/email.js | 61 --------------------------- server/app/utils/index.js | 3 -- server/app/utils/notify.js | 3 +- 6 files changed, 49 insertions(+), 97 deletions(-) delete mode 100644 server/app/utils/email.js diff --git a/server/app/db.js b/server/app/db.js index baef4ba..6a6cf90 100644 --- a/server/app/db.js +++ b/server/app/db.js @@ -69,12 +69,12 @@ function initNotifyDB() { 'type': 'updatePwd', 'desc': '修改密码提醒', 'sw': true + }, + { + 'type': 'host_expired', + 'desc': '服务器到期提醒', + 'sw': true } - // { - // 'type': 'host_offline', - // 'desc': '客户端离线提醒(每小时最多发送一次提醒)', - // 'sw': true - // } ] await writeNotifyList(defaultData) } diff --git a/server/app/schedule/expired-notify.js b/server/app/schedule/expired-notify.js index d17c336..5b3e3b8 100644 --- a/server/app/schedule/expired-notify.js +++ b/server/app/schedule/expired-notify.js @@ -1,5 +1,6 @@ const schedule = require('node-schedule') -const { readHostList, sendEmailToConfList, formatTimestamp } = require('../utils') +const { readHostList, formatTimestamp } = require('../utils') +const { asyncSendNotice } = require('../utils/notify') const expiredNotifyJob = async () => { consola.info('=====开始检测服务器到期时间=====', new Date()) @@ -13,13 +14,13 @@ const expiredNotifyJob = async () => { let content = `别名: ${ name }
IP: ${ host }
到期时间:${ formatTimestamp(expired, 'week') }
控制台: ${ consoleUrl || '未填写' }` if(0 <= restDay && restDay <= 1) { let temp = '有服务器将在一天后到期,请关注
' - sendEmailToConfList(title, temp + content) + asyncSendNotice('host_expired', title, temp + content) }else if(3 <= restDay && restDay < 4) { let temp = '有服务器将在三天后到期,请关注
' - sendEmailToConfList(title, temp + content) + asyncSendNotice('host_expired', title, temp + content) }else if(7 <= restDay && restDay < 8) { let temp = '有服务器将在七天后到期,请关注
' - sendEmailToConfList(title, temp + content) + asyncSendNotice('host_expired', title, temp + content) } } } diff --git a/server/app/template/commonTemp.js b/server/app/template/commonTemp.js index cc37866..385ded9 100644 --- a/server/app/template/commonTemp.js +++ b/server/app/template/commonTemp.js @@ -1,26 +1,40 @@ module.exports = (content) => { - return ` - - - - - - - - - - - - - -
-

- ${ content } -

-
- - - + return ` + + + + + +
+

${ content }

+ +
+ + ` } \ No newline at end of file diff --git a/server/app/utils/email.js b/server/app/utils/email.js deleted file mode 100644 index 577962b..0000000 --- a/server/app/utils/email.js +++ /dev/null @@ -1,61 +0,0 @@ -// :TODO: 重写 -const nodemailer = require('nodemailer') -const { readNotifyConfig, readNotifyList } = require('./storage') -const commonTemp = require('../template/commonTemp') - -const emailCode = { - SUCCESS: 0, - FAIL: -1 -} - -const emailTransporter = async (params = {}) => { - let { toEmail, title, html } = params - try { - if(!toEmail) throw Error('missing params: toEmail') - let userEmail = (await readUserEmailList()).find(({ auth }) => auth.user === toEmail) - if(!userEmail) throw Error(`${ toEmail } 不存在已保存的配置文件中, 请移除后重新添加`) - let { target } = userEmail - let emailServerConf = (await readSupportEmailList()).find((item) => item.target === target) - if(!emailServerConf) throw Error(`邮箱类型不支持:${ target }`) - const timeout = 1000*5 - let options = Object.assign({}, userEmail, emailServerConf, { greetingTimeout: timeout, connectionTimeout: timeout }) - let transporter = nodemailer.createTransport(options) - let info = await transporter.sendMail({ - from: userEmail.auth.user, // sender address - to: userEmail.auth.user, // list of receivers - subject: `EasyNode: ${ title }`, - html - }) - // consola.success('email发送成功:', info.accepted) - return { code: emailCode.SUCCESS, msg: `send successful:${ info.accepted }` } - } catch(error) { - // consola.error(`email发送失败(${ toEmail }):`, error.message || error) - return { code: emailCode.FAIL, msg: error } - } -} - -const sendEmailToConfList = (title, content) => { - // eslint-disable-next-line - return new Promise(async (res, rej) => { - let emailList = await readUserEmailList() - if(Array.isArray(emailList) && emailList.length >= 1) { - for (const item of emailList) { - const toEmail = item.auth.user - await emailTransporter({ toEmail, title, html: commonTemp(content) }) - .then(({ code }) => { - if(code === 0) { - consola.success('已发送邮件通知: ', toEmail, title) - return res({ code: emailCode.SUCCESS }) - } - consola.error('邮件通知发送失败: ', toEmail, title) - return rej({ code: emailCode.FAIL }) - }) - } - } - }) -} - -module.exports = { - emailTransporter, - sendEmailToConfList -} \ No newline at end of file diff --git a/server/app/utils/index.js b/server/app/utils/index.js index 27374d7..8f8693c 100644 --- a/server/app/utils/index.js +++ b/server/app/utils/index.js @@ -21,7 +21,6 @@ const { const { RSADecryptSync, AESEncryptSync, AESDecryptSync, SHA1Encrypt } = require('./encrypt') const { verifyAuthSync, isProd } = require('./verify-auth') const { getNetIPInfo, throwError, isIP, randomStr, getUTCDate, formatTimestamp, shellThrottle } = require('./tools') -const { emailTransporter, sendEmailToConfList } = require('./email') module.exports = { getNetIPInfo, @@ -43,8 +42,6 @@ module.exports = { writeHostList, readKey, writeKey, - emailTransporter, - sendEmailToConfList, readGroupList, writeGroupList, readScriptList, diff --git a/server/app/utils/notify.js b/server/app/utils/notify.js index 3785bbb..dc1b826 100644 --- a/server/app/utils/notify.js +++ b/server/app/utils/notify.js @@ -1,6 +1,7 @@ const nodemailer = require('nodemailer') const axios = require('axios') const { getNotifySwByType, readNotifyConfig } = require('../utils') +const commonTemp = require('../template/commonTemp') function sendServerChan(sendKey, title, content) { if (!sendKey) return consola.error('发送server酱通知失败, sendKey 为空') @@ -41,7 +42,7 @@ function sendEmail({ service, user, pass }, title, content) { to: user, subject: title, // text: '', // 纯文本版本内容,如果收件人的邮件客户端不支持HTML显示,就会显示这个文本 - html: content + html: commonTemp(content) }) consola.info('邮件通知发送成功: ', title) resolve()