mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-05-11 09:59:46 +08:00
feat: add ADDRESS_REGEX (#401)
This commit is contained in:
@@ -19,6 +19,7 @@ api.get('/open_api/settings', async (c) => {
|
||||
"title": c.env.TITLE,
|
||||
"announcement": getStringValue(c.env.ANNOUNCEMENT),
|
||||
"prefix": c.env.PREFIX,
|
||||
"addressRegex": getStringValue(c.env.ADDRESS_REGEX),
|
||||
"minAddressLen": getIntValue(c.env.MIN_ADDRESS_LEN, 1),
|
||||
"maxAddressLen": getIntValue(c.env.MAX_ADDRESS_LEN, 30),
|
||||
"defaultDomains": getDefaultDomains(c),
|
||||
|
||||
@@ -5,6 +5,22 @@ import { getBooleanValue, getDomains, getStringValue, getIntValue, getUserRoles,
|
||||
import { HonoCustomType, UserRole } from './types';
|
||||
import { unbindTelegramByAddress } from './telegram_api/common';
|
||||
|
||||
const DEFAULT_NAME_REGEX = /[^a-z0-9]/g;
|
||||
|
||||
const getNameRegex = (c: Context<HonoCustomType>): RegExp => {
|
||||
try {
|
||||
const regex = getStringValue(c.env.ADDRESS_REGEX);
|
||||
if (!regex) {
|
||||
return DEFAULT_NAME_REGEX;
|
||||
}
|
||||
return new RegExp(regex, 'g');
|
||||
}
|
||||
catch (e) {
|
||||
console.error("Failed to get address regex", e);
|
||||
}
|
||||
return DEFAULT_NAME_REGEX;
|
||||
}
|
||||
|
||||
export const newAddress = async (
|
||||
c: Context<HonoCustomType>,
|
||||
name: string, domain: string | undefined | null,
|
||||
@@ -14,7 +30,7 @@ export const newAddress = async (
|
||||
checkAllowDomains: boolean = true
|
||||
): Promise<{ address: string, jwt: string }> => {
|
||||
// remove special characters
|
||||
name = name.replace(/[^a-z0-9]/g, '')
|
||||
name = name.replace(getNameRegex(c), '')
|
||||
// name min length min 1
|
||||
const minAddressLength = Math.max(
|
||||
checkLengthByConfig ? getIntValue(c.env.MIN_ADDRESS_LEN, 1) : 1,
|
||||
|
||||
1
worker/src/types.d.ts
vendored
1
worker/src/types.d.ts
vendored
@@ -15,6 +15,7 @@ export type Bindings = {
|
||||
TITLE: string | undefined
|
||||
ANNOUNCEMENT: string | undefined | null
|
||||
PREFIX: string | undefined
|
||||
ADDRESS_REGEX: string | undefined
|
||||
MIN_ADDRESS_LEN: string | number | undefined
|
||||
MAX_ADDRESS_LEN: string | number | undefined
|
||||
DEFAULT_DOMAINS: string | string[] | undefined
|
||||
|
||||
@@ -19,6 +19,8 @@ node_compat = true
|
||||
# TITLE = "Custom Title" # custom title
|
||||
# ANNOUNCEMENT = "Custom Announcement"
|
||||
PREFIX = "tmp"
|
||||
# address name REGEX, if not set, the default is [^a-z0-9]
|
||||
# ADDRESS_REGEX = "[^a-z0-9]"
|
||||
# (min, max) length of the adderss, if not set, the default is (1, 30)
|
||||
# MIN_ADDRESS_LEN = 1
|
||||
# MAX_ADDRESS_LEN = 30
|
||||
|
||||
Reference in New Issue
Block a user