mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-09-04 23:17:34 +08:00
fix: align user send role and rate limits
This commit is contained in:
+15
-6
@@ -26,6 +26,11 @@ const API_PATHS = [
|
||||
"/external/",
|
||||
];
|
||||
|
||||
const isUserSendMailRequest = (path: string): boolean => (
|
||||
path.startsWith("/user_api/address/")
|
||||
&& path.endsWith("/send_mail")
|
||||
);
|
||||
|
||||
const app = new Hono<HonoCustomType>()
|
||||
//cors
|
||||
app.use('/*', cors());
|
||||
@@ -61,21 +66,22 @@ app.use('/*', async (c, next) => {
|
||||
}
|
||||
|
||||
// rate limit for specific endpoints
|
||||
const userSendMailRequest = isUserSendMailRequest(c.req.path);
|
||||
if (
|
||||
c.req.path.startsWith("/api/new_address")
|
||||
|| c.req.path.startsWith("/api/send_mail")
|
||||
|| c.req.path.startsWith("/external/api/send_mail")
|
||||
|| (
|
||||
c.req.path.startsWith("/user_api/address/")
|
||||
&& c.req.path.endsWith("/send_mail")
|
||||
)
|
||||
|| userSendMailRequest
|
||||
|| c.req.path.startsWith("/user_api/register")
|
||||
|| c.req.path.startsWith("/user_api/verify_code")
|
||||
) {
|
||||
const reqIp = c.req.raw.headers.get("cf-connecting-ip")
|
||||
if (reqIp && c.env.RATE_LIMITER) {
|
||||
const rateLimitPath = userSendMailRequest
|
||||
? "/user_api/address/:address_id/send_mail"
|
||||
: c.req.path;
|
||||
const { success } = await c.env.RATE_LIMITER.limit(
|
||||
{ key: `${c.req.path}|${reqIp}` }
|
||||
{ key: `${rateLimitPath}|${reqIp}` }
|
||||
)
|
||||
if (!success) {
|
||||
return c.text(`IP=${reqIp} Rate limit exceeded for ${c.req.path}`, 429)
|
||||
@@ -206,7 +212,10 @@ app.use('/user_api/*', async (c, next) => {
|
||||
console.error(e);
|
||||
return c.text(msgs.UserTokenExpiredMsg, 401)
|
||||
}
|
||||
if (c.req.path.startsWith("/user_api/bind_address")) {
|
||||
if (
|
||||
c.req.path.startsWith("/user_api/bind_address")
|
||||
|| c.req.path.startsWith("/user_api/address/")
|
||||
) {
|
||||
await checkoutUserRolePayload(c);
|
||||
}
|
||||
if (c.req.path.startsWith('/user_api/bind_address')
|
||||
|
||||
Reference in New Issue
Block a user