feat: add address_block_list for new address (#185)

This commit is contained in:
Dream Hunter
2024-05-02 15:23:37 +08:00
committed by GitHub
parent e81142f5ef
commit 83b9bc9d5f
12 changed files with 178 additions and 20 deletions

View File

@@ -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
View File

@@ -0,0 +1,3 @@
export const CONSTANTS = {
ADDRESS_BLOCK_LIST_KEY: 'address_block_list',
}

View File

@@ -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);
})