优化降低所需配置简化部署
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import app from '../hono/hono';
|
||||
import initService from '../init/init';
|
||||
import { dbInit } from '../init/init';
|
||||
import { initForward } from "../init/forward";
|
||||
|
||||
app.get('/init/:secret', (c) => {
|
||||
return initService.init(c);
|
||||
return dbInit.init(c);
|
||||
})
|
||||
|
||||
app.post('/initForward', async (c) => {
|
||||
return initForward(c, await c.req.json());
|
||||
})
|
||||
|
||||
@@ -55,12 +55,10 @@ const en = {
|
||||
authExpired: 'Authentication has expired. Please sign in again',
|
||||
unauthorized: 'Unauthorized',
|
||||
bannedSend: 'You do not have permission to send emails',
|
||||
initSuccess: 'Successfully initialized',
|
||||
noDomainPermAdd: "No permission to add this domain email",
|
||||
noDomainPermReg: "No permission to register this domain email",
|
||||
noDomainPermRegKey: "Registration code not valid for this domain",
|
||||
noDomainPermSend: "No permission to send from this domain email",
|
||||
JWTMismatch: 'JWT secret mismatch',
|
||||
publicTokenFail: 'Token validation failed',
|
||||
notAdmin: 'The entered email is not an administrator email',
|
||||
emailExistDatabase: 'Email already exists in the database',
|
||||
|
||||
@@ -55,12 +55,10 @@ const zh = {
|
||||
authExpired: '身份认证失效,请重新登录',
|
||||
unauthorized: '权限不足',
|
||||
bannedSend: '你没有发送邮件权限',
|
||||
initSuccess: '初始化成功',
|
||||
noDomainPermAdd: '你没有权限添加该域名邮箱',
|
||||
noDomainPermReg: '你没有权限注册该域名邮箱',
|
||||
noDomainPermRegKey: '你的注册码没有权限注册该域名邮箱',
|
||||
noDomainPermSend: '你没有权限使用该域名邮箱发送邮件',
|
||||
JWTMismatch: 'jwt_secret 不匹配',
|
||||
publicTokenFail: 'token验证失败',
|
||||
notAdmin: '输入的邮箱不是管理员邮箱',
|
||||
emailExistDatabase: '有邮箱已存在数据库中',
|
||||
|
||||
124
mail-worker/src/init/forward.js
Normal file
124
mail-worker/src/init/forward.js
Normal file
@@ -0,0 +1,124 @@
|
||||
export async function initForward(c, params) {
|
||||
|
||||
const { workerName, domainList, token } = params;
|
||||
|
||||
let headers = {
|
||||
Authorization: `Bearer ${token}`
|
||||
};
|
||||
|
||||
let mainList = [];
|
||||
const childList = [];
|
||||
|
||||
//查询DOMAIN变量对应域名
|
||||
for (let domain of domainList) {
|
||||
|
||||
// 提取一级域名(主域名 + 顶级域名)
|
||||
const parts = domain.split('.');
|
||||
|
||||
let paramDomain = domain
|
||||
if (parts.length > 2) {
|
||||
paramDomain = parts.slice(-2).join('.');
|
||||
}
|
||||
|
||||
//结尾匹配查询域名
|
||||
const res = await fetch(`https://api.cloudflare.com/client/v4/zones?name=ends_with:${paramDomain}`, {
|
||||
method: 'GET',
|
||||
headers
|
||||
});
|
||||
|
||||
const body = await res.json();
|
||||
|
||||
if(!res.ok) {
|
||||
return c.json(body);
|
||||
}
|
||||
|
||||
const { result } = body;
|
||||
|
||||
result.forEach(item => {
|
||||
|
||||
if (domain === item.name) {
|
||||
mainList.push({ domain: item.name, domainId: item.id });
|
||||
} else if (domain.includes(item.name)) {
|
||||
mainList.push({ domain: item.name, domainId: item.id });
|
||||
childList.push({ domain, domainId: item.id });
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
mainList = [...new Set(mainList)];
|
||||
|
||||
if (mainList.length === 0) {
|
||||
return c.text('Domain does not exist.');
|
||||
}
|
||||
|
||||
//开启主域名电子邮件路由
|
||||
for (const { domainId } of mainList) {
|
||||
|
||||
const res = await fetch(`https://api.cloudflare.com/client/v4/zones/${domainId}/email/routing/enable`, {
|
||||
method: 'POST',
|
||||
headers
|
||||
});
|
||||
|
||||
const body = await res.json();
|
||||
|
||||
if(!res.ok) {
|
||||
return c.json(body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//开启catch_all转发到worker
|
||||
for (const { domainId } of mainList) {
|
||||
|
||||
const res = await fetch(`https://api.cloudflare.com/client/v4/zones/${domainId}/email/routing/rules/catch_all`, {
|
||||
method: 'PUT',
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
actions: [
|
||||
{
|
||||
type: "worker",
|
||||
value: [workerName]
|
||||
}
|
||||
],
|
||||
matchers: [
|
||||
{
|
||||
type: "all"
|
||||
}
|
||||
],
|
||||
enabled: true
|
||||
})
|
||||
});
|
||||
|
||||
const body = await res.json();
|
||||
|
||||
if(!res.ok) {
|
||||
return c.json(body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//开启子域名电子邮件路由
|
||||
for (const { domain, domainId } of childList) {
|
||||
|
||||
const res = await fetch(`https://api.cloudflare.com/client/v4/zones/${domainId}/email/routing/enable`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
name: domain
|
||||
})
|
||||
});
|
||||
|
||||
const body = await res.json();
|
||||
|
||||
if(!res.ok) {
|
||||
return c.json(body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return c.text('success');
|
||||
|
||||
}
|
||||
@@ -1,15 +1,14 @@
|
||||
import settingService from '../service/setting-service';
|
||||
import emailUtils from '../utils/email-utils';
|
||||
import {emailConst} from "../const/entity-const";
|
||||
import { t } from '../i18n/i18n'
|
||||
|
||||
const init = {
|
||||
const dbInit = {
|
||||
async init(c) {
|
||||
|
||||
const secret = c.req.param('secret');
|
||||
|
||||
if (secret !== c.env.jwt_secret) {
|
||||
return c.text(t('JWTMismatch'));
|
||||
return c.text('❌ JWT secret mismatch');
|
||||
}
|
||||
|
||||
await this.intDB(c);
|
||||
@@ -28,7 +27,7 @@ const init = {
|
||||
await this.v2_6DB(c);
|
||||
await this.v2_7DB(c);
|
||||
await settingService.refresh(c);
|
||||
return c.text(t('initSuccess'));
|
||||
return c.text('success');
|
||||
},
|
||||
|
||||
async v2_7DB(c) {
|
||||
@@ -621,4 +620,4 @@ const init = {
|
||||
await c.env.db.batch(queryList);
|
||||
}
|
||||
};
|
||||
export default init;
|
||||
export { dbInit };
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
name = "cloud-mail"
|
||||
name = "${NAME}"
|
||||
main = "src/index.js"
|
||||
compatibility_date = "2025-06-04"
|
||||
workers_dev = true
|
||||
|
||||
[observability]
|
||||
enabled = true
|
||||
|
||||
[[routes]]
|
||||
pattern = "${CUSTOM_DOMAIN}"
|
||||
custom_domain = true
|
||||
|
||||
[[d1_databases]]
|
||||
binding = "db"
|
||||
database_name = "cloud-mail" # 数据库的名称
|
||||
|
||||
Reference in New Issue
Block a user