feat: requset_send_mail_access default 1 balance (#143)

* feat: requset_send_mail_access default 1 balance

* feat: send_mail RATE_LIMITER for ip
This commit is contained in:
Dream Hunter
2024-04-16 22:41:46 +08:00
committed by GitHub
parent 99e1bdc30d
commit 74cf49a5e2
2 changed files with 6 additions and 4 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ api.post('/api/requset_send_mail_access', async (c) => {
} }
try { try {
const { success } = await c.env.DB.prepare( const { success } = await c.env.DB.prepare(
`INSERT INTO address_sender (address, enabled) VALUES (?, 1)` `INSERT INTO address_sender (address, balance, enabled) VALUES (?, 1, 1)`
).bind(address).run(); ).bind(address).run();
if (!success) { if (!success) {
return c.text("Failed to request send mail access", 500) return c.text("Failed to request send mail access", 500)
+5 -3
View File
@@ -20,12 +20,14 @@ app.use('/api/*', async (c, next) => {
return c.text("Need Password", 401) return c.text("Need Password", 401)
} }
} }
if (c.req.path.startsWith("/api/new_address")) { if (c.req.path.startsWith("/api/new_address") || c.req.path.startsWith("/api/send_mail")) {
const reqIp = c.req.raw.headers.get("cf-connecting-ip") const reqIp = c.req.raw.headers.get("cf-connecting-ip")
if (reqIp && c.env.RATE_LIMITER) { if (reqIp && c.env.RATE_LIMITER) {
const { success } = await c.env.RATE_LIMITER.limit({ key: reqIp }) const { success } = await c.env.RATE_LIMITER.limit(
{ key: `${c.req.path}|${reqIp}` }
)
if (!success) { if (!success) {
return c.text(`IP=${reqIp} Rate limit exceeded for /api/new_address`, 429) return c.text(`IP=${reqIp} Rate limit exceeded for ${c.req.path}`, 429)
} }
} }
await next(); await next();