diff --git a/README.md b/README.md index 67a4ee79..298a93d8 100644 --- a/README.md +++ b/README.md @@ -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 } ``` 部署 diff --git a/vitepress-docs/docs/.vitepress/zh.ts b/vitepress-docs/docs/.vitepress/zh.ts index 4ca67b41..524a164d 100644 --- a/vitepress-docs/docs/.vitepress/zh.ts +++ b/vitepress-docs/docs/.vitepress/zh.ts @@ -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' } diff --git a/vitepress-docs/docs/en/cli.md b/vitepress-docs/docs/en/cli.md index cbdd3e6d..d16c8495 100644 --- a/vitepress-docs/docs/en/cli.md +++ b/vitepress-docs/docs/en/cli.md @@ -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@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) diff --git a/vitepress-docs/docs/public/feature/admin.png b/vitepress-docs/docs/public/feature/admin.png new file mode 100644 index 00000000..bb7b9b20 Binary files /dev/null and b/vitepress-docs/docs/public/feature/admin.png differ diff --git a/vitepress-docs/docs/zh/guide/cli/worker.md b/vitepress-docs/docs/zh/guide/cli/worker.md index 0cc6e4a1..60b185af 100644 --- a/vitepress-docs/docs/zh/guide/cli/worker.md +++ b/vitepress-docs/docs/zh/guide/cli/worker.md @@ -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 } ``` ## 部署 diff --git a/vitepress-docs/docs/zh/guide/feature/admin.md b/vitepress-docs/docs/zh/guide/feature/admin.md new file mode 100644 index 00000000..15499424 --- /dev/null +++ b/vitepress-docs/docs/zh/guide/feature/admin.md @@ -0,0 +1,7 @@ +# Admin 控制台 + +部署前端应用之后,访问 `/admin` 路径即可进入管理控制台。 + +需要在后端配置 `admin 控制台密码`, 不配置则不允许访问控制台。 + +![admin](/feature/admin.png) diff --git a/worker/src/worker.js b/worker/src/worker.js index 258e3b1a..3a601f9f 100644 --- a/worker/src/worker.js +++ b/worker/src/worker.js @@ -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; }; diff --git a/worker/wrangler.toml.template b/worker/wrangler.toml.template index bebbb272..194c6bf7 100644 --- a/worker/wrangler.toml.template +++ b/worker/wrangler.toml.template @@ -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 }