优化降低所需配置简化部署
This commit is contained in:
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 };
|
||||
|
||||
Reference in New Issue
Block a user