-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworker.js
More file actions
92 lines (82 loc) · 2.61 KB
/
Copy pathworker.js
File metadata and controls
92 lines (82 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
export default {
async email(message, env, ctx) {
const TARGET_EMAIL_ADDR = "YOUR_EMAIL_HERE";
const SECOND_EMAIL_ADDR = "YOUR_CLOUDFLARE_HOSTED_EMAIL_HERE; Like: contact@yourdomain.com";
const EMAIL_FILTERING_STRENGTH = 2;
// 需要匹配的关键词列表
const keywords = [
"verification",
"verify",
"login",
"log-in",
"confirm",
"signup",
"sign-up",
"sign-in",
"authentication",
"注册",
"激活",
"找回",
"密码",
"登录",
"验证",
];
const negative_keywords = [
"promote",
"promotion",
"offer",
"discount",
"sale",
"time limited",
"time-limited",
// "prize", 可能有歧义
"gift",
"event",
"促销",
"折扣",
"优惠",
"限时",
];
// 只获取 subject(标题)
const subject = message.headers.get("subject") || "";
// 读取原始邮件
const rawEmail = await new Response(message.raw).text();
// 提取 body 部分(headers 和 body 之间用空行分隔)
const bodyStartIndex = rawEmail.search(/\r?\n\r?\n/);
const body = bodyStartIndex !== -1 ? rawEmail.substring(bodyStartIndex + 4) : rawEmail;
// 只检查 subject 和 body
const contentToCheck = (subject + "\n" + body).toLowerCase();
// 查找所有匹配的关键词
const matchedKeywords = keywords.filter(keyword => {
const regex = new RegExp(keyword, "i");
return regex.test(contentToCheck);
});
const matchedNegativeKeywords = negative_keywords.filter(keyword => {
const regex = new RegExp(keyword, "i");
return regex.test(contentToCheck);
});
let should_forward = true;
switch(EMAIL_FILTERING_STRENGTH){
case 0:
should_forward = matchedNegativeKeywords.length == 0;
break;
case 1:
should_forward = matchedKeywords.length > 0;
break;
case 2:
should_forward = matchedKeywords.length > 0 && matchedNegativeKeywords.length == 0;
break;
default:
should_forward = true;
}
if (should_forward) {
// 包含关键词,转发邮件,并添加自定义头显示匹配到的关键词
await message.forward(TARGET_EMAIL_ADDR, new Headers({
"X-Matched-Keywords": matchedKeywords.join(", ")
}));
} else {
// 不包含关键词,拒绝邮件
message.setReject("[Auto email filter] No verification/signup/login keywords found. If this is not a spam, please send to " + SECOND_EMAIL_ADDR + ". If this is a spam: 你推销邮件在狗叫什么?");
}
},
};