feat: add /api/new_address ratelimit (#130)

This commit is contained in:
Dream Hunter
2024-04-15 14:14:38 +08:00
committed by GitHub
parent eafcf00e5e
commit 2ec1a61608
8 changed files with 74 additions and 12 deletions

View File

@@ -154,6 +154,14 @@ BLACK_LIST = "" # 黑名单,用于过滤发件人,逗号分隔
binding = "DB"
database_name = "xxx" # D1 数据库名称
database_id = "xxx" # D1 数据库 ID
# 新建地址限流配置
# [[unsafe.bindings]]
# name = "RATE_LIMITER"
# type = "ratelimit"
# namespace_id = "1001"
# # 10 requests per minute
# simple = { limit = 10, period = 60 }
```
部署

View File

@@ -123,7 +123,7 @@ function sidebarGuide(): DefaultTheme.SidebarItem[] {
text: '功能简介',
collapsed: false,
items: [
// { text: '先决条件', link: 'pre-requisite' },
{ text: 'Admin 控制台', link: 'feature/admin' },
]
},
{ text: '参考', base: "/", link: 'reference' }

View File

@@ -49,21 +49,46 @@ The first deployment will prompt you to create a project. Please fill in `produc
```bash
cd worker
pnpm install
# copy wrangler.toml.template to wrangler.toml
# and add your d1 config and these config
# PREFIX = "tmp" - the email create will be like tmp<xxxxx>@DOMAIN
# IF YOU WANT TO MAKE YOUR SITE PRIVATE, UNCOMMENT THE FOLLOWING LINES
# PASSWORDS = ["123", "456"]
# For admin panel, if not set will no allow to access the admin panel
# ADMIN_PASSWORDS = ["123", "456"]
# DOMAINS = ["xxx.xxx1" , "xxx.xxx2"] you domain name
# JWT_SECRET = "xxx"
# BLACK_LIST = ""
cp wrangler.toml.template wrangler.toml
# deploy
pnpm run deploy
```
`wrangler.toml`
```bash
name = "cloudflare_temp_email"
main = "src/worker.js"
compatibility_date = "2023-08-14"
node_compat = true
[vars]
PREFIX = "tmp" # The mailbox name prefix to be processed
# If you want your site to be private, uncomment below and change your password
# PASSWORDS = ["123", "456"]
# admin console password, if not configured, access to the console is not allowed
# ADMIN_PASSWORDS = ["123", "456"]
DOMAINS = ["xxx.xxx1" , "xxx.xxx2"] # your domain name
JWT_SECRET = "xxx" # Key used to generate jwt
BLACK_LIST = "" # Blacklist, used to filter senders, comma separated
# dkim config
# DKIM_SELECTOR = "mailchannels" # Refer to the DKIM section mailchannels._domainkey for mailchannels
# DKIM_PRIVATE_KEY = "" # Refer to the contents of priv_key.txt in the DKIM section
[[d1_databases]]
binding = "DB"
database_name = "xxx" # D1 database name
database_id = "xxx" # D1 database ID
# Create a new address current limiting configuration
# [[unsafe.bindings]]
# name = "RATE_LIMITER"
# type = "ratelimit"
# namespace_id = "1001"
# # 10 requests per minute
# simple = { limit = 10, period = 60 }
```
you can find and test the worker's url in the workers dashboard
![worker](/readme_assets/worker.png)
@@ -105,7 +130,6 @@ Create a new `_mailchannels` record, the type is `TXT`, the content is `v=mc1 cf
- The worker domain name here is the domain name of the back-end api. For example, if I deploy it at `https://temp-email-api.awsl.uk/`, fill in `v=mc1 cfid=awsl.uk`
- If your domain name is `https://temp-email-api.xxx.workers.dev`, fill in `v=mc1 cfid=xxx.workers.dev`
## Configure DKIM
Ref: [Adding-a-DKIM-Signature](https://support.mailchannels.com/hc/en-us/articles/7122849237389-Adding-a-DKIM-Signature)

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View File

@@ -38,6 +38,14 @@ BLACK_LIST = "" # 黑名单,用于过滤发件人,逗号分隔
binding = "DB"
database_name = "xxx" # D1 数据库名称
database_id = "xxx" # D1 数据库 ID
# 新建地址限流配置 /api/new_address
# [[unsafe.bindings]]
# name = "RATE_LIMITER"
# type = "ratelimit"
# namespace_id = "1001"
# # 10 requests per minute
# simple = { limit = 10, period = 60 }
```
## 部署

View File

@@ -0,0 +1,7 @@
# Admin 控制台
部署前端应用之后,访问 `/admin` 路径即可进入管理控制台。
需要在后端配置 `admin 控制台密码`, 不配置则不允许访问控制台。
![admin](/feature/admin.png)

View File

@@ -21,6 +21,13 @@ app.use('/api/*', async (c, next) => {
}
}
if (c.req.path.startsWith("/api/new_address")) {
const reqIp = c.req.raw.headers.get("cf-connecting-ip")
if (reqIp && c.env.RATE_LIMITER) {
const { success } = await c.env.RATE_LIMITER.limit({ key: reqIp })
if (!success) {
return c.text(`IP=${reqIp} Rate limit exceeded for /api/new_address`, 429)
}
}
await next();
return;
};

View File

@@ -24,3 +24,11 @@ BLACK_LIST = ""
binding = "DB"
database_name = "xxx"
database_id = "xxx"
# ratelimit config for /api/new_address
# [[unsafe.bindings]]
# name = "RATE_LIMITER"
# type = "ratelimit"
# namespace_id = "1001"
# # 10 requests per minute
# simple = { limit = 10, period = 60 }