From 07833d5ca9a69a0366ed700de52e3899dd552c93 Mon Sep 17 00:00:00 2001 From: Zyx-A Date: Wed, 30 Apr 2025 10:41:09 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E5=9F=BA=E4=BA=8E=E5=AD=90=E5=9F=9F?= =?UTF-8?q?=E5=90=8D=E8=BD=AC=E5=8F=91=E5=88=B0=E4=B8=8D=E5=90=8C=E7=9A=84?= =?UTF-8?q?=E9=82=AE=E7=AE=B1=E4=B8=AD=E5=8E=BB=20(#645)=20(#647)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- worker/src/admin_api/worker_config.ts | 3 ++- worker/src/email/index.ts | 25 +++++++++++++++++++++++++ worker/src/types.d.ts | 6 ++++++ worker/src/utils.ts | 18 +++++++++++++++++- worker/wrangler.toml.template | 2 ++ 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/worker/src/admin_api/worker_config.ts b/worker/src/admin_api/worker_config.ts index 30013a7a..d311359a 100644 --- a/worker/src/admin_api/worker_config.ts +++ b/worker/src/admin_api/worker_config.ts @@ -1,7 +1,7 @@ import { Context } from 'hono'; import { HonoCustomType } from '../types'; -import { getAdminPasswords, getBooleanValue, getDefaultDomains, getDomains, getIntValue, getPasswords, getStringArray, getStringValue, getUserRoles, getAnotherWorkerList, getSplitStringListValue } from '../utils'; +import { getAdminPasswords, getBooleanValue, getDefaultDomains, getDomains, getIntValue, getPasswords, getStringArray, getStringValue, getUserRoles, getSubdomainForwardAddressList, getAnotherWorkerList, getSplitStringListValue } from '../utils'; import { CONSTANTS } from '../constants'; import { isS3Enabled } from '../mails_api/s3_attachment'; @@ -21,6 +21,7 @@ export default { "MAX_ADDRESS_LEN": getIntValue(c.env.MAX_ADDRESS_LEN, 30), "FORWARD_ADDRESS_LIST": getStringArray(c.env.FORWARD_ADDRESS_LIST), + "SUBDOMAIN_FORWARD_ADDRESS_LIST": getSubdomainForwardAddressList(c), "DEFAULT_DOMAINS": getDefaultDomains(c), "DOMAINS": getDomains(c), "DOMAIN_LABELS": getStringArray(c.env.DOMAIN_LABELS), diff --git a/worker/src/email/index.ts b/worker/src/email/index.ts index 13a6d9e1..56a00d97 100644 --- a/worker/src/email/index.ts +++ b/worker/src/email/index.ts @@ -1,6 +1,7 @@ import { Context } from "hono"; import { getEnvStringList } from "../utils"; +import { getSubdomainForwardAddressList } from "../utils"; import { sendMailToTelegram } from "../telegram_api"; import { Bindings, HonoCustomType, RPCEmailMessage, ParsedEmailContext } from "../types"; import { auto_reply } from "./auto_reply"; @@ -67,6 +68,30 @@ async function email(message: ForwardableEmailMessage, env: Bindings, ctx: Execu console.error("forward email error", error); } + // forward subdomain email + try { + // 遍历 FORWARD_ADDRESS_LIST + const subdomainForwardAddressList = getSubdomainForwardAddressList(env.SUBDOMAIN_FORWARD_ADDRESS_LIST) + for (const subdomainForwardAddress of subdomainForwardAddressList) { + // 检查邮件是否匹配 domains + if (subdomainForwardAddress.domains && subdomainForwardAddress.domains.length > 0) { + for (const domain of subdomainForwardAddress.domains) { + if (message.to.endsWith(domain)) { + // 转发邮件 + await message.forward(subdomainForwardAddress.forward); + // 支持多邮箱转发收件,不进行截止 + // break; + } + } + } else { + // 如果 domains 为空,则转发所有邮件 + await message.forward(subdomainForwardAddress.forward); + } + } + } catch (error) { + console.error("subdomain forward email error", error); + } + // send email to telegram try { await sendMailToTelegram( diff --git a/worker/src/types.d.ts b/worker/src/types.d.ts index 62393042..52297e8c 100644 --- a/worker/src/types.d.ts +++ b/worker/src/types.d.ts @@ -4,6 +4,11 @@ export type UserRole = { prefix: string | undefined | null } +export type SubdomainForwardAddressList = { + domains: string[] | undefined | null, + forward: string +} + export type Bindings = { // bindings DB: D1Database @@ -44,6 +49,7 @@ export type Bindings = { COPYRIGHT: string | undefined DISABLE_SHOW_GITHUB: string | boolean | undefined FORWARD_ADDRESS_LIST: string | string[] | undefined + SUBDOMAIN_FORWARD_ADDRESS_LIST: string | SubdomainForwardAddressList[] | undefined ENABLE_CHECK_JUNK_MAIL: string | boolean | undefined JUNK_MAIL_CHECK_LIST: string | string[] | undefined diff --git a/worker/src/utils.ts b/worker/src/utils.ts index 34428680..69977457 100644 --- a/worker/src/utils.ts +++ b/worker/src/utils.ts @@ -1,6 +1,6 @@ import { Context } from "hono"; import { createMimeMessage } from "mimetext"; -import { HonoCustomType, UserRole, AnotherWorker } from "./types"; +import { HonoCustomType, UserRole, SubdomainForwardAddressList, AnotherWorker } from "./types"; export const getJsonObjectValue = ( value: string | any @@ -165,6 +165,22 @@ export const getUserRoles = (c: Context): UserRole[] => { return c.env.USER_ROLES; } +export const getSubdomainForwardAddressList = (c: Context): SubdomainForwardAddressList[] => { + if (!c.env.SUBDOMAIN_FORWARD_ADDRESS_LIST) { + return []; + } + // check if SUBDOMAIN_FORWARD_ADDRESS_LIST is an array, if not use json.parse + if (!Array.isArray(c.env.SUBDOMAIN_FORWARD_ADDRESS_LIST)) { + try { + return JSON.parse(c.env.SUBDOMAIN_FORWARD_ADDRESS_LIST); + } catch (e) { + console.error("Failed to parse SUBDOMAIN_FORWARD_ADDRESS_LIST", e); + return []; + } + } + return c.env.SUBDOMAIN_FORWARD_ADDRESS_LIST; +} + export const getAnotherWorkerList = (c: Context): AnotherWorker[] => { if (!c.env.ANOTHER_WORKER_LIST) { return []; diff --git a/worker/wrangler.toml.template b/worker/wrangler.toml.template index 619dd4bc..cc48c2d6 100644 --- a/worker/wrangler.toml.template +++ b/worker/wrangler.toml.template @@ -80,6 +80,8 @@ ENABLE_AUTO_REPLY = false # TG_BOT_INFO = "{}" # global forward address list, if set, all emails will be forwarded to these addresses # FORWARD_ADDRESS_LIST = ["xxx@xxx.com"] +# subdomain forward address list, if set, subdomain emails will be forwarded to these addresses +# SUBDOMAIN_FORWARD_ADDRESS_LIST = [{"domains":[""],"forward":"xxx1@xxx.com",},{"domains":["subdomain-1.domain.com","subdomain-2.domain.com"],"forward":"xxx2@xxx.com"}] # Frontend URL # FRONTEND_URL = "https://xxxx.xxx" # Enable check junk mail