支持老版本通知服务升级

This commit is contained in:
chaos-zhu 2024-08-17 17:20:10 +08:00
parent e92e2beb4d
commit f21da41d6d
6 changed files with 63 additions and 51 deletions

View File

@ -26,7 +26,7 @@ const login = async ({ res, request }) => {
if (loginErrCount >= allowErrCount) {
const { ip, country, city } = await getNetIPInfo(clientIp)
// 异步发送通知&禁止登录
asyncSendNotice('err_login', '登录错误提醒', `错误登录次数: ${ loginErrTotal }<br/>地点:${ country + city }<br/>IP: ${ ip }`)
asyncSendNotice('err_login', '登录错误提醒', `错误登录次数: ${ loginErrTotal }\n地点:${ country + city }\nIP: ${ ip }`)
forbidLogin = true
loginErrCount = 0
@ -82,7 +82,7 @@ const beforeLoginHandler = async (clientIp, jwtExpires) => {
consola.info('登录成功:', new Date(), { ip, country, city })
// 邮件登录通知
asyncSendNotice('login', '登录提醒', `地点:${ country + city }<br/>IP: ${ ip }`)
asyncSendNotice('login', '登录提醒', `地点:${ country + city }\nIP: ${ ip }`)
global.loginRecord.unshift(clientIPInfo)
if (global.loginRecord.length > 10) global.loginRecord = global.loginRecord.slice(0, 10)
@ -102,7 +102,7 @@ const updatePwd = async ({ res, request }) => {
keyObj.pwd = newPwd
await writeKey(keyObj)
asyncSendNotice('updatePwd', '用户密码修改提醒', `原用户名:${ user }<br/>更新用户名: ${ newLoginName }`)
asyncSendNotice('updatePwd', '用户密码修改提醒', `原用户名:${ user }\n更新用户名: ${ newLoginName }`)
res.success({ data: true, msg: 'success' })
}

View File

@ -48,41 +48,47 @@ function initGroupDB() {
function initNotifyDB() {
return new Promise((resolve, reject) => {
const notifyDB = new NotifyDB().getInstance()
notifyDB.count({}, async (err, count) => {
notifyDB.find({}, async (err, notifyList) => {
if (err) {
consola.log('初始化notifyDB错误:', err)
reject(err)
} else {
if (count === 0) {
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✔')
const defaultData = [{
'type': 'login',
'desc': '登录面板提醒',
'sw': true
}, {
'type': 'err_login',
'desc': '登录错误提醒(连续5次)',
'sw': true
}, {
'type': 'updatePwd',
'desc': '修改密码提醒',
'sw': true
}, {
'type': 'host_login',
'desc': '服务器登录提醒',
'sw': true
}, {
'type': 'onekey_complete',
'desc': '批量指令执行完成提醒',
'sw': true
}, {
'type': 'host_expired',
'desc': '服务器到期提醒',
'sw': true
} ]
await writeNotifyList(defaultData)
} 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()
})

View File

@ -11,15 +11,15 @@ const expiredNotifyJob = async () => {
const restDay = Number(((expired - Date.now()) / (1000 * 60 * 60 * 24)).toFixed(1))
console.log(Date.now(), restDay)
let title = '服务器到期提醒'
let content = `别名: ${ name }<br/>IP: ${ host }<br/>到期时间:${ formatTimestamp(expired, 'week') }<br/>控制台: ${ consoleUrl || '未填写' }`
let content = `别名: ${ name }\nIP: ${ host }\n到期时间:${ formatTimestamp(expired, 'week') }\n控制台: ${ consoleUrl || '未填写' }`
if(0 <= restDay && restDay <= 1) {
let temp = '有服务器将在一天后到期,请关注<br/>'
let temp = '有服务器将在一天后到期,请关注\n'
asyncSendNotice('host_expired', title, temp + content)
}else if(3 <= restDay && restDay < 4) {
let temp = '有服务器将在三天后到期,请关注<br/>'
let temp = '有服务器将在三天后到期,请关注\n'
asyncSendNotice('host_expired', title, temp + content)
}else if(7 <= restDay && restDay < 8) {
let temp = '有服务器将在七天后到期,请关注<br/>'
let temp = '有服务器将在七天后到期,请关注\n'
asyncSendNotice('host_expired', title, temp + content)
}
}

View File

@ -57,12 +57,13 @@ function sendEmail({ service, user, pass }, title, content) {
async function asyncSendNotice(noticeAction, title, content) {
try {
let sw = await getNotifySwByType(noticeAction) // 获取对应动作的通知开关
console.log(noticeAction, sw)
if (!sw) return
let notifyConfig = await readNotifyConfig()
let { type } = notifyConfig
if (!type) return consola.error('通知类型不存在: ', type)
title = `EasyNode-${ title }`
content += `<br/>通知发送时间:${ new Date() }`
content += `\n通知发送时间:${ new Date() }`
switch (type) {
case 'sct':
let { sendKey } = notifyConfig['sct']

View File

@ -1,14 +1,9 @@
<template>
<!-- <el-alert type="success" :closable="false">
<el-alert type="success" :closable="false">
<template #title>
<span style="letter-spacing: 2px;"> 添加邮箱并确保测试邮件通过 </span>
<span style="letter-spacing: 2px;"> 确保[通知配置]中的通知方式能够正常收到测试通知 </span>
</template>
</el-alert> -->
<!--
{"type":"login","desc":"登录面板提醒","sw":true,"_id":"jxbeElEds9virx4m"}
{"type":"updatePwd","desc":"修改密码提醒","sw":true,"_id":"uPa6imN4pKnAIgMW"}
{"type":"err_login","desc":"登录错误提醒(连续5次)","sw":true,"_id":"wARAnQXturdZHdWK"}
-->
</el-alert>
<el-table v-loading="notifyListLoading" :data="notifyList">
<el-table-column prop="desc" label="通知类型" />
<el-table-column prop="sw" label="开关">

View File

@ -16,7 +16,7 @@
label-suffix=""
>
<el-form-item label="通知方式" prop="type" class="form_item">
<el-select v-model="noticeConfig.type" placeholder="">
<el-select v-model="noticeConfig.type" placeholder="" class="input">
<el-option
v-for="item in noticeTypeList"
:key="item.type"
@ -25,6 +25,7 @@
/>
</el-select>
</el-form-item>
<!-- server酱 -->
<template v-if="noticeConfig.type === 'sct'">
<el-form-item label="SendKey" prop="sct.sendKey" class="form_item">
<el-input
@ -32,9 +33,12 @@
clearable
placeholder="SCT******"
autocomplete="off"
class="input"
/>
<p class="tips">普通用户每日最多支持5条有条件建议开通会员服务防止丢失重要通知<a class="link" href="https://sct.ftqq.com/r/9338" target="_blank">Server酱官网</a> </p>
</el-form-item>
</template>
<!-- 邮箱 -->
<template v-if="noticeConfig.type === 'email'">
<el-form-item label="服务商" prop="email.service" class="form_item">
<el-input
@ -42,8 +46,9 @@
clearable
placeholder=""
autocomplete="off"
class="input"
/>
<span class="tips">邮箱服务商, 例如: Gmialqq126163, 支持列表: <a class="link" href="https://github.com/nodemailer/nodemailer/blob/master/lib/well-known/services.json" target="_blank">点击查询</a> </span>
<span class="tips">邮箱服务商, 例如: QQ126163Gmial, 支持列表: <a class="link" href="https://github.com/nodemailer/nodemailer/blob/master/lib/well-known/services.json" target="_blank">点击查询</a> </span>
</el-form-item>
<el-form-item label="邮箱地址" prop="email.user" class="form_item">
<el-input
@ -51,6 +56,7 @@
clearable
placeholder="邮箱地址"
autocomplete="off"
class="input"
/>
</el-form-item>
<el-form-item label="SMTP" prop="auth.pass" class="form_item">
@ -59,6 +65,7 @@
clearable
placeholder="SMTP授权码/密码"
autocomplete="off"
class="input"
/>
</el-form-item>
</template>
@ -75,7 +82,7 @@
<script setup>
import { ref, reactive, onMounted, getCurrentInstance } from 'vue'
const { proxy: { $api, $message, $messageBox, $notification } } = getCurrentInstance()
const { proxy: { $api, $notification } } = getCurrentInstance()
const loading = ref(false)
const noticeConfig = ref({})
@ -134,10 +141,13 @@ onMounted(() => {
<style lang="scss" scoped>
.form_item {
width: 350px;
// width: 350px;
.input {
width: 450px;
}
.tips {
font-size: 12px;
width: 100%;
font-size: 14px;
color: #999;
}
}