✨ 新增服务器到期提醒
This commit is contained in:
parent
3d410602d6
commit
221f9e06b9
@ -69,12 +69,12 @@ function initNotifyDB() {
|
|||||||
'type': 'updatePwd',
|
'type': 'updatePwd',
|
||||||
'desc': '修改密码提醒',
|
'desc': '修改密码提醒',
|
||||||
'sw': true
|
'sw': true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'host_expired',
|
||||||
|
'desc': '服务器到期提醒',
|
||||||
|
'sw': true
|
||||||
}
|
}
|
||||||
// {
|
|
||||||
// 'type': 'host_offline',
|
|
||||||
// 'desc': '客户端离线提醒(每小时最多发送一次提醒)',
|
|
||||||
// 'sw': true
|
|
||||||
// }
|
|
||||||
]
|
]
|
||||||
await writeNotifyList(defaultData)
|
await writeNotifyList(defaultData)
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
const schedule = require('node-schedule')
|
const schedule = require('node-schedule')
|
||||||
const { readHostList, sendEmailToConfList, formatTimestamp } = require('../utils')
|
const { readHostList, formatTimestamp } = require('../utils')
|
||||||
|
const { asyncSendNotice } = require('../utils/notify')
|
||||||
|
|
||||||
const expiredNotifyJob = async () => {
|
const expiredNotifyJob = async () => {
|
||||||
consola.info('=====开始检测服务器到期时间=====', new Date())
|
consola.info('=====开始检测服务器到期时间=====', new Date())
|
||||||
@ -13,13 +14,13 @@ const expiredNotifyJob = async () => {
|
|||||||
let content = `别名: ${ name }<br/>IP: ${ host }<br/>到期时间:${ formatTimestamp(expired, 'week') }<br/>控制台: ${ consoleUrl || '未填写' }`
|
let content = `别名: ${ name }<br/>IP: ${ host }<br/>到期时间:${ formatTimestamp(expired, 'week') }<br/>控制台: ${ consoleUrl || '未填写' }`
|
||||||
if(0 <= restDay && restDay <= 1) {
|
if(0 <= restDay && restDay <= 1) {
|
||||||
let temp = '有服务器将在一天后到期,请关注<br/>'
|
let temp = '有服务器将在一天后到期,请关注<br/>'
|
||||||
sendEmailToConfList(title, temp + content)
|
asyncSendNotice('host_expired', title, temp + content)
|
||||||
}else if(3 <= restDay && restDay < 4) {
|
}else if(3 <= restDay && restDay < 4) {
|
||||||
let temp = '有服务器将在三天后到期,请关注<br/>'
|
let temp = '有服务器将在三天后到期,请关注<br/>'
|
||||||
sendEmailToConfList(title, temp + content)
|
asyncSendNotice('host_expired', title, temp + content)
|
||||||
}else if(7 <= restDay && restDay < 8) {
|
}else if(7 <= restDay && restDay < 8) {
|
||||||
let temp = '有服务器将在七天后到期,请关注<br/>'
|
let temp = '有服务器将在七天后到期,请关注<br/>'
|
||||||
sendEmailToConfList(title, temp + content)
|
asyncSendNotice('host_expired', title, temp + content)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,40 @@
|
|||||||
module.exports = (content) => {
|
module.exports = (content) => {
|
||||||
return `<!DOCTYPE html
|
return `<!DOCTYPE html>
|
||||||
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
<html>
|
||||||
|
<head>
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
<style>
|
||||||
|
body {
|
||||||
<head>
|
font-family: Arial, sans-serif;
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
margin: 15px 5px;
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
color: #333;
|
||||||
</head>
|
background-color: #f4f4f4;
|
||||||
|
line-height: 1.6;
|
||||||
<body style="margin: 0; padding: 0;text-align: center;">
|
}
|
||||||
<table border="0" cellpadding="0" cellspacing="0" width="100%">
|
.container {
|
||||||
<tr>
|
background-color: #fff;
|
||||||
<td>
|
padding: 20px;
|
||||||
<h3 style="font-size: 18px;color: #5992D3;padding:0 0 0 10px;">
|
border-radius: 8px;
|
||||||
${ content }
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
||||||
</h3>
|
}
|
||||||
</td>
|
h1 {
|
||||||
</tr>
|
color: #4CAF50;
|
||||||
</table>
|
}
|
||||||
</body>
|
p {
|
||||||
|
margin: 12px 0;
|
||||||
</html>
|
}
|
||||||
|
.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>
|
||||||
`
|
`
|
||||||
}
|
}
|
@ -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
|
|
||||||
}
|
|
@ -21,7 +21,6 @@ const {
|
|||||||
const { RSADecryptSync, AESEncryptSync, AESDecryptSync, SHA1Encrypt } = require('./encrypt')
|
const { RSADecryptSync, AESEncryptSync, AESDecryptSync, SHA1Encrypt } = require('./encrypt')
|
||||||
const { verifyAuthSync, isProd } = require('./verify-auth')
|
const { verifyAuthSync, isProd } = require('./verify-auth')
|
||||||
const { getNetIPInfo, throwError, isIP, randomStr, getUTCDate, formatTimestamp, shellThrottle } = require('./tools')
|
const { getNetIPInfo, throwError, isIP, randomStr, getUTCDate, formatTimestamp, shellThrottle } = require('./tools')
|
||||||
const { emailTransporter, sendEmailToConfList } = require('./email')
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getNetIPInfo,
|
getNetIPInfo,
|
||||||
@ -43,8 +42,6 @@ module.exports = {
|
|||||||
writeHostList,
|
writeHostList,
|
||||||
readKey,
|
readKey,
|
||||||
writeKey,
|
writeKey,
|
||||||
emailTransporter,
|
|
||||||
sendEmailToConfList,
|
|
||||||
readGroupList,
|
readGroupList,
|
||||||
writeGroupList,
|
writeGroupList,
|
||||||
readScriptList,
|
readScriptList,
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
const nodemailer = require('nodemailer')
|
const nodemailer = require('nodemailer')
|
||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
const { getNotifySwByType, readNotifyConfig } = require('../utils')
|
const { getNotifySwByType, readNotifyConfig } = require('../utils')
|
||||||
|
const commonTemp = require('../template/commonTemp')
|
||||||
|
|
||||||
function sendServerChan(sendKey, title, content) {
|
function sendServerChan(sendKey, title, content) {
|
||||||
if (!sendKey) return consola.error('发送server酱通知失败, sendKey 为空')
|
if (!sendKey) return consola.error('发送server酱通知失败, sendKey 为空')
|
||||||
@ -41,7 +42,7 @@ function sendEmail({ service, user, pass }, title, content) {
|
|||||||
to: user,
|
to: user,
|
||||||
subject: title,
|
subject: title,
|
||||||
// text: '', // 纯文本版本内容,如果收件人的邮件客户端不支持HTML显示,就会显示这个文本
|
// text: '', // 纯文本版本内容,如果收件人的邮件客户端不支持HTML显示,就会显示这个文本
|
||||||
html: content
|
html: commonTemp(content)
|
||||||
})
|
})
|
||||||
consola.info('邮件通知发送成功: ', title)
|
consola.info('邮件通知发送成功: ', title)
|
||||||
resolve()
|
resolve()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user