mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-05-26 10:39:50 +08:00
feat: add EMAIL_KV_BLACK_LIST (#394)
This commit is contained in:
@@ -246,10 +246,12 @@ api.get('/admin/account_settings', async (c) => {
|
||||
const blockList = await getJsonSetting(c, CONSTANTS.ADDRESS_BLOCK_LIST_KEY);
|
||||
const sendBlockList = await getJsonSetting(c, CONSTANTS.SEND_BLOCK_LIST_KEY);
|
||||
const verifiedAddressList = await getJsonSetting(c, CONSTANTS.VERIFIED_ADDRESS_LIST_KEY);
|
||||
const fromBlockList = c.env.KV ? await c.env.KV.get<string[]>(CONSTANTS.EMAIL_KV_BLACK_LIST, 'json') : [];
|
||||
return c.json({
|
||||
blockList: blockList || [],
|
||||
sendBlockList: sendBlockList || [],
|
||||
verifiedAddressList: verifiedAddressList || []
|
||||
verifiedAddressList: verifiedAddressList || [],
|
||||
fromBlockList: fromBlockList || []
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
@@ -259,7 +261,7 @@ api.get('/admin/account_settings', async (c) => {
|
||||
|
||||
api.post('/admin/account_settings', async (c) => {
|
||||
/** @type {{ blockList: Array<string>, sendBlockList: Array<string> }} */
|
||||
const { blockList, sendBlockList, verifiedAddressList } = await c.req.json();
|
||||
const { blockList, sendBlockList, verifiedAddressList, fromBlockList } = await c.req.json();
|
||||
if (!blockList || !sendBlockList || !verifiedAddressList) {
|
||||
return c.text("Invalid blockList or sendBlockList", 400)
|
||||
}
|
||||
@@ -278,6 +280,12 @@ api.post('/admin/account_settings', async (c) => {
|
||||
c, CONSTANTS.VERIFIED_ADDRESS_LIST_KEY,
|
||||
JSON.stringify(verifiedAddressList)
|
||||
)
|
||||
if (fromBlockList?.length > 0 && !c.env.KV) {
|
||||
return c.text("Please enable KV to use fromBlockList", 400)
|
||||
}
|
||||
if (fromBlockList) {
|
||||
await c.env.KV.put(CONSTANTS.EMAIL_KV_BLACK_LIST, JSON.stringify(fromBlockList || []))
|
||||
}
|
||||
return c.json({
|
||||
success: true
|
||||
})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export const CONSTANTS = {
|
||||
VERSION: 'v0.7.0',
|
||||
VERSION: 'v0.7.1',
|
||||
|
||||
// DB settings
|
||||
ADDRESS_BLOCK_LIST_KEY: 'address_block_list',
|
||||
@@ -13,4 +13,5 @@ export const CONSTANTS = {
|
||||
TG_KV_SETTINGS_KEY: "temp-mail-telegram-settings",
|
||||
WEBHOOK_KV_SETTINGS_KEY: "temp-mail-webhook-settings",
|
||||
WEBHOOK_KV_USER_SETTINGS_KEY: "temp-mail-webhook-user-settings",
|
||||
EMAIL_KV_BLACK_LIST: "temp-mail-email-black-list",
|
||||
}
|
||||
|
||||
16
worker/src/email/black_list.ts
Normal file
16
worker/src/email/black_list.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { CONSTANTS } from "../constants";
|
||||
import { Bindings } from "../types";
|
||||
|
||||
export const isBlocked = async (from: string, env: Bindings): Promise<boolean> => {
|
||||
if (env.BLACK_LIST && env.BLACK_LIST.split(",").some(word => from.includes(word))) {
|
||||
return true;
|
||||
}
|
||||
if (!env.KV) {
|
||||
return false;
|
||||
}
|
||||
const blockList = await env.KV.get<string[]>(CONSTANTS.EMAIL_KV_BLACK_LIST, 'json') || [];
|
||||
if (blockList.some(word => from.includes(word))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -5,11 +5,12 @@ import { sendMailToTelegram } from "../telegram_api";
|
||||
import { Bindings, HonoCustomType } from "../types";
|
||||
import { auto_reply } from "./auto_reply";
|
||||
import { trigerWebhook } from "../mails_api/webhook_settings";
|
||||
import { isBlocked } from "./black_list";
|
||||
|
||||
|
||||
async function email(message: ForwardableEmailMessage, env: Bindings, ctx: ExecutionContext) {
|
||||
if (env.BLACK_LIST && env.BLACK_LIST.split(",").some(word => message.from.includes(word))) {
|
||||
message.setReject("Missing from address");
|
||||
if (await isBlocked(message.from, env)) {
|
||||
message.setReject("Reject from address");
|
||||
console.log(`Reject message from ${message.from} to ${message.to}`);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user