feat: add MIN_ADDRESS_LEN && MAX_ADDRESS_LEN (#304)

This commit is contained in:
Dream Hunter
2024-06-06 21:44:22 +08:00
committed by GitHub
parent 6225f6521a
commit 0db611bb3e
11 changed files with 50 additions and 11 deletions

View File

@@ -40,7 +40,7 @@ api.post('/admin/new_address', async (c) => {
return c.text("Please provide a name", 400)
}
try {
const res = await newAddress(c, name, domain, enablePrefix);
const res = await newAddress(c, name, domain, enablePrefix, false);
return c.json(res);
} catch (e) {
return c.text(`Failed create address: ${(e as Error).message}`, 400)

View File

@@ -1,6 +1,6 @@
import { Hono } from 'hono'
import { getDomains, getPasswords, getBooleanValue } from './utils';
import { getDomains, getPasswords, getBooleanValue, getIntValue } from './utils';
import { CONSTANTS } from './constants';
import { HonoCustomType } from './types';
import { isS3Enabled } from './mails_api/s3_attachment';
@@ -18,6 +18,8 @@ api.get('/open_api/settings', async (c) => {
return c.json({
"title": c.env.TITLE,
"prefix": c.env.PREFIX,
"minAddressLen": getIntValue(c.env.MIN_ADDRESS_LEN, 1),
"maxAddressLen": getIntValue(c.env.MAX_ADDRESS_LEN, 30),
"domains": getDomains(c),
"needAuth": needAuth,
"adminContact": c.env.ADMIN_CONTACT,

View File

@@ -1,28 +1,39 @@
import { Context } from 'hono';
import { Jwt } from 'hono/utils/jwt'
import { getBooleanValue, getDomains, getStringValue } from './utils';
import { getBooleanValue, getDomains, getStringValue, getIntValue } from './utils';
import { HonoCustomType } from './types';
import { unbindTelegramByAddress } from './telegram_api/common';
export const newAddress = async (
c: Context<HonoCustomType>,
name: string, domain: string | undefined | null,
enablePrefix: boolean
enablePrefix: boolean,
checkLengthByConfig: boolean = true
): Promise<{ address: string, jwt: string }> => {
// remove special characters
name = name.replace(/[^a-zA-Z0-9.]/g, '')
// name min length min 1
const minAddressLength = Math.max(
checkLengthByConfig ? getIntValue(c.env.MIN_ADDRESS_LEN, 1) : 1,
1
);
// name max length min 1
const maxAddressLength = Math.max(
checkLengthByConfig ? getIntValue(c.env.MAX_ADDRESS_LEN, 30) : 30,
1
);
// check name length
if (name.length <= 0) {
throw new Error("Name too short")
if (name.length < minAddressLength) {
throw new Error(`Name too short (min ${minAddressLength})`);
}
if (name.length > maxAddressLength) {
throw new Error(`Name too long (max ${maxAddressLength})`);
}
// create address
if (enablePrefix) {
name = getStringValue(c.env.PREFIX) + name;
}
if (name.length >= 30) {
throw new Error("Name too long (max 30)")
}
// check domain, generate random domain
const domains = getDomains(c);
if (!domain || !domains.includes(domain)) {

View File

@@ -8,6 +8,8 @@ export type Bindings = {
// config
TITLE: string | undefined
PREFIX: string | undefined
MIN_ADDRESS_LEN: string | number | undefined
MAX_ADDRESS_LEN: string | number | undefined
DOMAINS: string | string[] | undefined
PASSWORDS: string | string[] | undefined
ADMIN_PASSWORDS: string | string[] | undefined

View File

@@ -18,6 +18,9 @@ node_compat = true
[vars]
# TITLE = "Custom Title" # custom title
PREFIX = "tmp"
# (min, max) length of the adderss, if not set, the default is (1, 30)
# MIN_ADDRESS_LEN = 1
# MAX_ADDRESS_LEN = 30
# IF YOU WANT TO MAKE YOUR SITE PRIVATE, UNCOMMENT THE FOLLOWING LINES
# PASSWORDS = ["123", "456"]
# For admin panel