新增服务器到期提醒

This commit is contained in:
chaos-zhu 2024-08-17 16:19:32 +08:00
parent 3d410602d6
commit 221f9e06b9
6 changed files with 49 additions and 97 deletions

View File

@ -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)
}

View File

@ -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 }<br/>IP: ${ host }<br/>到期时间:${ formatTimestamp(expired, 'week') }<br/>控制台: ${ consoleUrl || '未填写' }`
if(0 <= restDay && restDay <= 1) {
let temp = '有服务器将在一天后到期,请关注<br/>'
sendEmailToConfList(title, temp + content)
asyncSendNotice('host_expired', title, temp + content)
}else if(3 <= restDay && restDay < 4) {
let temp = '有服务器将在三天后到期,请关注<br/>'
sendEmailToConfList(title, temp + content)
asyncSendNotice('host_expired', title, temp + content)
}else if(7 <= restDay && restDay < 8) {
let temp = '有服务器将在七天后到期,请关注<br/>'
sendEmailToConfList(title, temp + content)
asyncSendNotice('host_expired', title, temp + content)
}
}
}

View File

@ -1,26 +1,40 @@
module.exports = (content) => {
return `<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body style="margin: 0; padding: 0;text-align: center;">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
<h3 style="font-size: 18px;color: #5992D3;padding:0 0 0 10px;">
${ content }
</h3>
</td>
</tr>
</table>
</body>
</html>
return `<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
margin: 15px 5px;
color: #333;
background-color: #f4f4f4;
line-height: 1.6;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
color: #4CAF50;
}
p {
margin: 12px 0;
}
.footer {
text-align: center;
font-size: 0.9em;
color: #777;
}
</style>
</head>
<body>
<div class="container">
<p>${ content }</p>
<p class="footer">通知发送时间: ${ new Date() }</p>
</div>
</body>
</html>
`
}

View File

@ -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
}

View File

@ -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,

View File

@ -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()