feat: optimize verification code recognition strategy
This commit is contained in:
@@ -92,7 +92,7 @@ export async function email(message, env, ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const toName = email.to.find(item => item.address === message.to)?.name || '';
|
const toName = email.to.find(item => item.address === message.to)?.name || '';
|
||||||
const code = aiCode === settingConst.aiCode.OPEN && checkAiCodeFilter(aiCodeFilter, email) ? await aiService.extractCode({ env }, email) : '';
|
const code = await aiService.extractCode({ env }, email, { aiCode, aiCodeFilter });
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
toEmail: message.to,
|
toEmail: message.to,
|
||||||
@@ -185,19 +185,6 @@ export async function email(message, env, ctx) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkAiCodeFilter(aiCodeFilterStr, email) {
|
|
||||||
const filterList = aiCodeFilterStr ? aiCodeFilterStr.split(',').map(item => item.trim().toLowerCase()).filter(Boolean) : [];
|
|
||||||
|
|
||||||
if (filterList.length === 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const fromEmail = (email.from?.address || '').trim().toLowerCase();
|
|
||||||
const fromDomain = emailUtils.getDomain(fromEmail).toLowerCase();
|
|
||||||
|
|
||||||
return filterList.some(item => item === fromEmail || item === fromDomain);
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkBlock(blackSubjectStr, blackContentStr, blackFromStr, email) {
|
function checkBlock(blackSubjectStr, blackContentStr, blackFromStr, email) {
|
||||||
|
|
||||||
const blackFromList = blackFromStr ? blackFromStr.split(',') : []
|
const blackFromList = blackFromStr ? blackFromStr.split(',') : []
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
import emailUtils from '../utils/email-utils';
|
import emailUtils from '../utils/email-utils';
|
||||||
|
import { settingConst } from '../const/entity-const';
|
||||||
|
|
||||||
|
const codeKeywords = ['码', '令牌', 'code', 'token'];
|
||||||
|
|
||||||
const aiService = {
|
const aiService = {
|
||||||
async extractCode(c, email) {
|
async extractCode(c, email, options = {}) {
|
||||||
const ai = c.env.AI || c.env.ai;
|
if (!this.shouldExtractCode(options.aiCode, options.aiCodeFilter, email)) {
|
||||||
|
|
||||||
if (!ai) {
|
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ai = c.env.AI;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const subject = email.subject || '';
|
const subject = email.subject || '';
|
||||||
const text = emailUtils.formatText(email.text || '');
|
const text = emailUtils.formatText(email.text || '');
|
||||||
@@ -48,6 +51,28 @@ const aiService = {
|
|||||||
console.error('验证码提取失败: ', e);
|
console.error('验证码提取失败: ', e);
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
shouldExtractCode(aiCode, aiCodeFilterStr, email) {
|
||||||
|
if (aiCode !== settingConst.aiCode.OPEN) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const filterList = aiCodeFilterStr ? aiCodeFilterStr.split(',').map(item => item.trim().toLowerCase()).filter(Boolean) : [];
|
||||||
|
|
||||||
|
if (filterList.length > 0) {
|
||||||
|
const fromEmail = (email.from?.address || '').trim().toLowerCase();
|
||||||
|
const fromDomain = emailUtils.getDomain(fromEmail).toLowerCase();
|
||||||
|
return filterList.some(item => item === fromEmail || item === fromDomain);
|
||||||
|
}
|
||||||
|
|
||||||
|
const subject = email.subject || '';
|
||||||
|
const text = emailUtils.formatText(email.text || '');
|
||||||
|
const htmlText = emailUtils.htmlToText(email.html || '');
|
||||||
|
const content = `${subject}\n${htmlText || text}`;
|
||||||
|
const lowerContent = content.toLowerCase();
|
||||||
|
|
||||||
|
return codeKeywords.some(keyword => lowerContent.includes(keyword));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user