Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ADDRESS_REGEX #401

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
## main(v0.7.2)

- fix: worker 增加 `NO_LIMIT_SEND_ROLE` 配置, 加载失败的问题
- feat: worker 增加 `# ADDRESS_REGEX = "[^a-z.0-9]"` 配置, 用于配置地址的正则表达式

## v0.7.1

Expand Down
1 change: 1 addition & 0 deletions frontend/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const useGlobalState = createGlobalState(
title: '',
announcement: '',
prefix: '',
addressRegex: '',
needAuth: false,
adminContact: '',
enableUserCreateEmail: false,
Expand Down
20 changes: 16 additions & 4 deletions frontend/src/views/common/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const { locale, t } = useI18n({
login: 'Login',
pleaseGetNewEmail: 'Please login or click "Get New Email" button to get a new email address',
getNewEmail: 'Create New Email',
getNewEmailTip1: 'Please input the email you want to use. only allow a-z and 0-9',
getNewEmailTip1: 'Please input the email you want to use. only allow: ',
getNewEmailTip2: 'Levaing it blank will generate a random email address.',
getNewEmailTip3: 'You can choose a domain from the dropdown list.',
credential: 'Email Address Credential',
Expand All @@ -87,7 +87,7 @@ const { locale, t } = useI18n({
login: '登录',
pleaseGetNewEmail: '请"登录"或点击 "注册新邮箱" 按钮来获取一个新的邮箱地址',
getNewEmail: '创建新邮箱',
getNewEmailTip1: '请输入你想要使用的邮箱地址, 只允许 a-z, 0-9',
getNewEmailTip1: '请输入你想要使用的邮箱地址, 只允许: ',
getNewEmailTip2: '留空将会生成一个随机的邮箱地址。',
getNewEmailTip3: '你可以从下拉列表中选择一个域名。',
credential: '邮箱地址凭据',
Expand All @@ -101,6 +101,18 @@ const { locale, t } = useI18n({
}
});

const addressRegex = computed(() => {
try {
if (openSettings.value.addressRegex) {
return new RegExp(openSettings.value.addressRegex, 'g');
}
} catch (error) {
console.error(error);
message.error(`Invalid addressRegex: ${openSettings.value.addressRegex}`);
}
return /[^a-z0-9]/g;
});

const generateNameLoading = ref(false);
const generateName = async () => {
try {
Expand All @@ -110,7 +122,7 @@ const generateName = async () => {
.split('@')[0]
.replace(/\s+/g, '.')
.replace(/\.{2,}/g, '.')
.replace(/[^a-z0-9]/g, '')
.replace(addressRegex.value, '')
.toLowerCase();
} catch (error) {
message.error(error.message || "error");
Expand Down Expand Up @@ -206,7 +218,7 @@ onMounted(async () => {
<n-spin :show="generateNameLoading">
<n-form>
<span>
<p>{{ t("getNewEmailTip1") }}</p>
<p>{{ t("getNewEmailTip1") + addressRegex.source }}</p>
<p>{{ t("getNewEmailTip2") }}</p>
<p>{{ t("getNewEmailTip3") }}</p>
</span>
Expand Down
2 changes: 2 additions & 0 deletions vitepress-docs/docs/en/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ node_compat = true
PREFIX = "tmp" # The mailbox name prefix to be processed
# (min, max) length of the adderss, if not set, the default is (1, 30)
# ANNOUNCEMENT = "Custom Announcement"
# address name REGEX, if not set, the default is [^a-z0-9]
# ADDRESS_REGEX = "[^a-z0-9]"
# MIN_ADDRESS_LEN = 1
# MAX_ADDRESS_LEN = 30
# If you want your site to be private, uncomment below and change your password
Expand Down
2 changes: 2 additions & 0 deletions vitepress-docs/docs/zh/guide/cli/worker.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ node_compat = true
PREFIX = "tmp" # 要处理的邮箱名称前缀,不需要后缀可配置为空字符串
# (min, max) adderss的长度,如果不设置,默认为(1, 30)
# ANNOUNCEMENT = "Custom Announcement" # 自定义公告
# address name 的正则表达式,如果不设置,默认为 [^a-z0-9]
# ADDRESS_REGEX = "[^a-z0-9]"
# MIN_ADDRESS_LEN = 1
# MAX_ADDRESS_LEN = 30
# 如果你想要你的网站私有,取消下面的注释,并修改密码
Expand Down
1 change: 1 addition & 0 deletions worker/src/commom_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
18 changes: 17 additions & 1 deletion worker/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions worker/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions worker/wrangler.toml.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down