feat: add global forward address list (#288)

This commit is contained in:
Dream Hunter
2024-05-31 23:21:12 +08:00
committed by GitHub
parent f882e4cf97
commit 7a368d7b23
7 changed files with 36 additions and 0 deletions

View File

@@ -4,6 +4,7 @@
## main branch
- UI: 增加本地缓存进行地址管理
- worker: 增加 `FORWARD_ADDRESS_LIST` 全局邮件转发地址(等同于 `catch all`)
## v0.4.6

View File

@@ -105,6 +105,8 @@ ENABLE_AUTO_REPLY = false
# DKIM_PRIVATE_KEY = "" # Refer to the contents of priv_key.txt in the DKIM section
# telegram bot
# TG_MAX_ACCOUNTS = 5
# global forward address list, if set, all emails will be forwarded to these addresses
# FORWARD_ADDRESS_LIST = ["xxx@xxx.com"]
[[d1_databases]]
binding = "DB"

View File

@@ -73,6 +73,8 @@ ENABLE_AUTO_REPLY = false
# DKIM_PRIVATE_KEY = "" # 参考 DKIM 部分 priv_key.txt 的内容
# telegram bot 最多绑定邮箱数量
# TG_MAX_ACCOUNTS = 5
# 全局转发地址列表,如果不配置则不启用,启用后所有邮件都会转发到列表中的地址
# FORWARD_ADDRESS_LIST = ["xxx@xxx.com"]
# D1 数据库的名称和 ID 可以在 cloudflare 控制台查看
[[d1_databases]]

View File

@@ -1,5 +1,6 @@
import { Context } from "hono";
import { getEnvStringList } from "../utils";
import { sendMailToTelegram } from "../telegram_api";
import { Bindings, HonoCustomType } from "../types";
import { auto_reply } from "./auto_reply";
@@ -25,6 +26,16 @@ async function email(message: ForwardableEmailMessage, env: Bindings, ctx: Execu
console.log(`Failed save message from ${message.from} to ${message.to}`);
}
// forward email
try {
const forwardAddressList = getEnvStringList(env.FORWARD_ADDRESS_LIST)
for (const forwardAddress of forwardAddressList) {
await message.forward(forwardAddress);
}
} catch (error) {
console.log("forward email error", error);
}
// send email to telegram
try {
await sendMailToTelegram(

View File

@@ -21,6 +21,7 @@ export type Bindings = {
DEFAULT_SEND_BALANCE: number | string | undefined
ADMIN_CONTACT: string | undefined
COPYRIGHT: string | undefined
FORWARD_ADDRESS_LIST: string | string[] | undefined
// dkim
DKIM_SELECTOR: string | undefined

View File

@@ -129,6 +129,23 @@ export const getAdminPasswords = (c: Context<HonoCustomType>): string[] => {
return c.env.ADMIN_PASSWORDS.filter((item) => item.length > 0);
}
export const getEnvStringList = (value: string | string[] | undefined): string[] => {
if (!value) {
return [];
}
// check if is an array, if not use json.parse
if (!Array.isArray(value)) {
try {
const res = JSON.parse(value) as string[];
return res.filter((item) => item.length > 0);
} catch (e) {
console.error("Failed to parse ADMIN_PASSWORDS", e);
return [];
}
}
return value.filter((item) => item.length > 0);
}
export const sendAdminInternalMail = async (
c: Context<HonoCustomType>, toMail: string, subject: string, text: string
): Promise<boolean> => {

View File

@@ -47,6 +47,8 @@ ENABLE_AUTO_REPLY = false
# DKIM_PRIVATE_KEY = ""
# telegram bot
# TG_MAX_ACCOUNTS = 5
# global forward address list, if set, all emails will be forwarded to these addresses
# FORWARD_ADDRESS_LIST = ["xxx@xxx.com"]
[[d1_databases]]
binding = "DB"