feat: add ADDRESS_CHECK_REGEX (#415)

This commit is contained in:
Dream Hunter
2024-08-17 00:11:28 +08:00
committed by GitHub
parent 9888f98d74
commit 3664028e06
14 changed files with 74 additions and 20 deletions
+5 -1
View File
@@ -1,6 +1,10 @@
<!-- markdownlint-disable-file MD004 MD024 MD034 MD036 --> <!-- markdownlint-disable-file MD004 MD024 MD034 MD036 -->
# CHANGE LOG # CHANGE LOG
## main(v0.7.3)
- feat: worker 增加 `ADDRESS_CHECK_REGEX`, address name 的正则表达式, 只用于检查,符合条件将通过检查
## v0.7.2 ## v0.7.2
### Breaking Changes ### Breaking Changes
@@ -10,7 +14,7 @@
### Changes ### Changes
- fix: worker 增加 `NO_LIMIT_SEND_ROLE` 配置, 加载失败的问题 - fix: worker 增加 `NO_LIMIT_SEND_ROLE` 配置, 加载失败的问题
- feat: worker 增加 `# ADDRESS_REGEX = "[^a-z.0-9]"` 配置, 用于配置地址的正则表达式,如果不设置,默认为 [^a-z0-9], 需谨慎使用, 有些符号可能导致无法收件 - feat: worker 增加 `# ADDRESS_REGEX = "[^a-z.0-9]"` 配置, 替换非法符号的正则表达式,如果不设置,默认为 [^a-z0-9], 需谨慎使用, 有些符号可能导致无法收件
- feat: worker 优化 webhook 逻辑, 支持 admin 配置全局 webhook, 添加 `message pusher` 集成示例 - feat: worker 优化 webhook 逻辑, 支持 admin 配置全局 webhook, 添加 `message pusher` 集成示例
## v0.7.1 ## v0.7.1
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "cloudflare_temp_email", "name": "cloudflare_temp_email",
"version": "0.7.2", "version": "0.7.3",
"private": true, "private": true,
"type": "module", "type": "module",
"scripts": { "scripts": {
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "temp-email-pages", "name": "temp-email-pages",
"version": "1.0.0", "version": "0.7.3",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
+3 -1
View File
@@ -78,7 +78,9 @@ node_compat = true
PREFIX = "tmp" # The mailbox name prefix to be processed PREFIX = "tmp" # The mailbox name prefix to be processed
# (min, max) length of the adderss, if not set, the default is (1, 30) # (min, max) length of the adderss, if not set, the default is (1, 30)
# ANNOUNCEMENT = "Custom Announcement" # ANNOUNCEMENT = "Custom Announcement"
# address name REGEX, if not set, the default is [^a-z0-9] # address check REGEX, if not set, will not check
# ADDRESS_CHECK_REGEX = "^(?!.*admin).*"
# address name replace REGEX, if not set, the default is [^a-z0-9]
# ADDRESS_REGEX = "[^a-z0-9]" # ADDRESS_REGEX = "[^a-z0-9]"
# MIN_ADDRESS_LEN = 1 # MIN_ADDRESS_LEN = 1
# MAX_ADDRESS_LEN = 30 # MAX_ADDRESS_LEN = 30
+3 -1
View File
@@ -46,7 +46,9 @@ node_compat = true
PREFIX = "tmp" # 要处理的邮箱名称前缀,不需要后缀可配置为空字符串 PREFIX = "tmp" # 要处理的邮箱名称前缀,不需要后缀可配置为空字符串
# (min, max) adderss的长度,如果不设置,默认为(1, 30) # (min, max) adderss的长度,如果不设置,默认为(1, 30)
# ANNOUNCEMENT = "Custom Announcement" # 自定义公告 # ANNOUNCEMENT = "Custom Announcement" # 自定义公告
# address name 的正则表达式,如果不设置,默认为 [^a-z0-9], 需谨慎使用, 有些符号可能导致无法收件 # address name 的正则表达式, 只用于检查,符合条件将通过检查
# ADDRESS_CHECK_REGEX = "^(?!.*admin).*"
# address name 替换非法符号的正则表达式, 不在其中的符号将被替换,如果不设置,默认为 [^a-z0-9], 需谨慎使用, 有些符号可能导致无法收件
# ADDRESS_REGEX = "[^a-z0-9]" # ADDRESS_REGEX = "[^a-z0-9]"
# MIN_ADDRESS_LEN = 1 # MIN_ADDRESS_LEN = 1
# MAX_ADDRESS_LEN = 30 # MAX_ADDRESS_LEN = 30
+1 -1
View File
@@ -1,7 +1,7 @@
{ {
"name": "temp-mail-docs", "name": "temp-mail-docs",
"private": true, "private": true,
"version": "0.2.6", "version": "0.7.3",
"type": "module", "type": "module",
"devDependencies": { "devDependencies": {
"@types/node": "^22.3.0", "@types/node": "^22.3.0",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "cloudflare_temp_email", "name": "cloudflare_temp_email",
"version": "0.0.0", "version": "0.7.3",
"private": true, "private": true,
"type": "module", "type": "module",
"scripts": { "scripts": {
+7 -1
View File
@@ -41,7 +41,13 @@ api.post('/admin/new_address', async (c) => {
return c.text("Please provide a name", 400) return c.text("Please provide a name", 400)
} }
try { try {
const res = await newAddress(c, name, domain, enablePrefix, false, null, false); const res = await newAddress(c, {
name, domain, enablePrefix,
checkLengthByConfig: false,
addressPrefix: null,
checkAllowDomains: false,
enableCheckNameRegex: false,
});
return c.json(res); return c.json(res);
} catch (e) { } catch (e) {
return c.text(`Failed create address: ${(e as Error).message}`, 400) return c.text(`Failed create address: ${(e as Error).message}`, 400)
+36 -5
View File
@@ -9,6 +9,24 @@ import { AdminWebhookSettings, WebhookMail, WebhookSettings } from './models';
const DEFAULT_NAME_REGEX = /[^a-z0-9]/g; const DEFAULT_NAME_REGEX = /[^a-z0-9]/g;
const checkNameRegex = (c: Context<HonoCustomType>, name: string) => {
let error = null;
try {
const regexStr = getStringValue(c.env.ADDRESS_CHECK_REGEX);
if (!regexStr) return;
const regex = new RegExp(regexStr);
if (!regex.test(name)) {
error = new Error(`Name not match regex: /${regexStr}/`);
}
}
catch (e) {
console.error("Failed to check address regex", e);
}
if (error) {
throw error;
}
}
const getNameRegex = (c: Context<HonoCustomType>): RegExp => { const getNameRegex = (c: Context<HonoCustomType>): RegExp => {
try { try {
const regex = getStringValue(c.env.ADDRESS_REGEX); const regex = getStringValue(c.env.ADDRESS_REGEX);
@@ -25,12 +43,25 @@ const getNameRegex = (c: Context<HonoCustomType>): RegExp => {
export const newAddress = async ( export const newAddress = async (
c: Context<HonoCustomType>, c: Context<HonoCustomType>,
name: string, domain: string | undefined | null, {
enablePrefix: boolean, name,
checkLengthByConfig: boolean = true, domain,
addressPrefix: string | undefined | null = null, enablePrefix,
checkAllowDomains: boolean = true checkLengthByConfig = true,
addressPrefix = null,
checkAllowDomains = true,
enableCheckNameRegex = true,
}: {
name: string, domain: string | undefined | null,
enablePrefix: boolean,
checkLengthByConfig?: boolean,
addressPrefix?: string | undefined | null,
checkAllowDomains?: boolean,
enableCheckNameRegex?: boolean,
}
): Promise<{ address: string, jwt: string }> => { ): Promise<{ address: string, jwt: string }> => {
// check name
if (enableCheckNameRegex) checkNameRegex(c, name);
// remove special characters // remove special characters
name = name.replace(getNameRegex(c), '') name = name.replace(getNameRegex(c), '')
// name min length min 1 // name min length min 1
+1 -1
View File
@@ -1,5 +1,5 @@
export const CONSTANTS = { export const CONSTANTS = {
VERSION: 'v0.7.2', VERSION: 'v0.7.3',
// DB settings // DB settings
ADDRESS_BLOCK_LIST_KEY: 'address_block_list', ADDRESS_BLOCK_LIST_KEY: 'address_block_list',
+6 -1
View File
@@ -121,7 +121,12 @@ api.post('/api/new_address', async (c) => {
} }
try { try {
const addressPrefix = await getAddressPrefix(c); const addressPrefix = await getAddressPrefix(c);
const res = await newAddress(c, name, domain, true, true, addressPrefix); const res = await newAddress(c, {
name, domain,
enablePrefix: true,
checkLengthByConfig: true,
addressPrefix
});
return c.json(res); return c.json(res);
} catch (e) { } catch (e) {
return c.text(`Failed create address: ${(e as Error).message}`, 400) return c.text(`Failed create address: ${(e as Error).message}`, 400)
+5 -4
View File
@@ -29,10 +29,11 @@ export const tgUserNewAddress = async (
if (blockList.some((item) => name.includes(item))) { if (blockList.some((item) => name.includes(item))) {
throw Error(`Name[${name}]is blocked`); throw Error(`Name[${name}]is blocked`);
} }
const res = await newAddress(c, const res = await newAddress(c, {
name || Math.random().toString(36).substring(2, 15), name: name || Math.random().toString(36).substring(2, 15),
domain, true domain,
); enablePrefix: true
});
// for mail push to telegram // for mail push to telegram
await c.env.KV.put(`${CONSTANTS.TG_KV_PREFIX}:${userId}`, JSON.stringify([...jwtList, res.jwt])); await c.env.KV.put(`${CONSTANTS.TG_KV_PREFIX}:${userId}`, JSON.stringify([...jwtList, res.jwt]));
await c.env.KV.put(`${CONSTANTS.TG_KV_PREFIX}:${res.address}`, userId.toString()); await c.env.KV.put(`${CONSTANTS.TG_KV_PREFIX}:${res.address}`, userId.toString());
+1
View File
@@ -15,6 +15,7 @@ export type Bindings = {
TITLE: string | undefined TITLE: string | undefined
ANNOUNCEMENT: string | undefined | null ANNOUNCEMENT: string | undefined | null
PREFIX: string | undefined PREFIX: string | undefined
ADDRESS_CHECK_REGEX: string | undefined
ADDRESS_REGEX: string | undefined ADDRESS_REGEX: string | undefined
MIN_ADDRESS_LEN: string | number | undefined MIN_ADDRESS_LEN: string | number | undefined
MAX_ADDRESS_LEN: string | number | undefined MAX_ADDRESS_LEN: string | number | undefined
+3 -1
View File
@@ -19,7 +19,9 @@ node_compat = true
# TITLE = "Custom Title" # custom title # TITLE = "Custom Title" # custom title
# ANNOUNCEMENT = "Custom Announcement" # ANNOUNCEMENT = "Custom Announcement"
PREFIX = "tmp" PREFIX = "tmp"
# address name REGEX, if not set, the default is [^a-z0-9] # address check REGEX, if not set, will not check
# ADDRESS_CHECK_REGEX = "^(?!.*admin).*"
# address name replace REGEX, if not set, the default is [^a-z0-9]
# ADDRESS_REGEX = "[^a-z0-9]" # ADDRESS_REGEX = "[^a-z0-9]"
# (min, max) length of the adderss, if not set, the default is (1, 30) # (min, max) length of the adderss, if not set, the default is (1, 30)
# MIN_ADDRESS_LEN = 1 # MIN_ADDRESS_LEN = 1