mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-07-08 18:42:26 +08:00
feat: add address_block_list for new address (#185)
This commit is contained in:
@@ -2,6 +2,7 @@ import { Hono } from 'hono'
|
||||
import { Jwt } from 'hono/utils/jwt'
|
||||
import { sendAdminInternalMail } from './utils'
|
||||
import { newAddress } from './common'
|
||||
import { CONSTANTS } from './constants'
|
||||
|
||||
const api = new Hono()
|
||||
|
||||
@@ -329,4 +330,36 @@ api.post('/admin/cleanup', async (c) => {
|
||||
})
|
||||
})
|
||||
|
||||
api.get('/admin/account_settings', async (c) => {
|
||||
try {
|
||||
const value = await c.env.DB.prepare(
|
||||
`SELECT value FROM settings where key = ?`
|
||||
).bind(CONSTANTS.ADDRESS_BLOCK_LIST_KEY).first("value");
|
||||
return c.json({
|
||||
blockList: value ? JSON.parse(value) : []
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return c.json({})
|
||||
}
|
||||
})
|
||||
|
||||
api.post('/admin/account_settings', async (c) => {
|
||||
const { blockList } = await c.req.json();
|
||||
if (!blockList) {
|
||||
return c.text("Invalid blockList", 400)
|
||||
}
|
||||
await c.env.DB.prepare(
|
||||
`INSERT or REPLACE INTO settings (key, value) VALUES (?, ?)`
|
||||
+ ` ON CONFLICT(key) DO UPDATE SET value = ?, updated_at = datetime('now')`
|
||||
).bind(
|
||||
CONSTANTS.ADDRESS_BLOCK_LIST_KEY,
|
||||
JSON.stringify(blockList),
|
||||
JSON.stringify(blockList)
|
||||
).run();
|
||||
return c.json({
|
||||
success: true
|
||||
})
|
||||
})
|
||||
|
||||
export { api }
|
||||
|
||||
3
worker/src/constants.js
Normal file
3
worker/src/constants.js
Normal file
@@ -0,0 +1,3 @@
|
||||
export const CONSTANTS = {
|
||||
ADDRESS_BLOCK_LIST_KEY: 'address_block_list',
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { Hono } from 'hono'
|
||||
|
||||
import { getDomains, getPasswords, getBooleanValue } from './utils';
|
||||
import { newAddress } from './common'
|
||||
import { CONSTANTS } from './constants'
|
||||
|
||||
const api = new Hono()
|
||||
|
||||
@@ -169,6 +170,18 @@ api.get('/api/new_address', async (c) => {
|
||||
if (!name) {
|
||||
name = Math.random().toString(36).substring(2, 15);
|
||||
}
|
||||
// check name block list
|
||||
try {
|
||||
const value = await c.env.DB.prepare(
|
||||
`SELECT value FROM settings where key = ?`
|
||||
).bind(CONSTANTS.ADDRESS_BLOCK_LIST_KEY).first("value");
|
||||
const blockList = value ? JSON.parse(value) : [];
|
||||
if (blockList.some((item) => name.includes(item))) {
|
||||
return c.text(`Name [${name}] is blocked`, 400)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
return newAddress(c, name, domain, true);
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user