generated from template/slidev-template
123 lines
3.7 KiB
TypeScript
123 lines
3.7 KiB
TypeScript
import { app } from '../app.ts';
|
||
import { CloudflareDDNS } from '../ddns/cloudflare/index.ts';
|
||
|
||
app.route({
|
||
path: 'cf',
|
||
key: 'update',
|
||
description: '更新Cloudflare DNS记录, 需要提供zone_id, record_id, domain, new_ip, api_token, type参数, type参数可选,默认为A记录, A 或AAAA',
|
||
}).define(async (ctx) => {
|
||
const { zone_id, record_id, domain, new_ip, api_token, type = 'A' } = ctx.query || {};
|
||
|
||
if (!zone_id || !record_id || !domain || !new_ip || !api_token) {
|
||
ctx.throw?.('缺少必要参数');
|
||
}
|
||
|
||
const cf = new CloudflareDDNS();
|
||
|
||
const result = await cf.updateRecord({
|
||
zone_id,
|
||
record_id,
|
||
domain,
|
||
new_ip,
|
||
api_token,
|
||
type,
|
||
});
|
||
if (result.success === false) {
|
||
ctx.throw?.(result.errors?.map((e) => e.message).join('; ') || '更新DNS记录失败');
|
||
} else {
|
||
console.log('更新DNS记录成功:', `${domain} -> ${new_ip}`);
|
||
}
|
||
|
||
ctx.body = { result };
|
||
}).addTo(app);
|
||
|
||
app.route({
|
||
path: 'cf',
|
||
key: 'create',
|
||
description: '创建Cloudflare DNS记录, 需要提供zone_id, domain, new_ip, api_token, type参数, type参数可选,默认为A记录, A 或AAAA',
|
||
}).define(async (ctx) => {
|
||
const { zone_id, domain, new_ip, api_token, type = 'A' } = ctx.query || {};
|
||
|
||
if (!zone_id || !domain || !new_ip || !api_token) {
|
||
ctx.throw?.('缺少必要参数');
|
||
}
|
||
|
||
const cf = new CloudflareDDNS();
|
||
|
||
// 先搜索是否已存在相同 domain 和 type 的记录
|
||
const searchRes = await cf.getList(zone_id, api_token, { search: domain });
|
||
if (searchRes.success && searchRes.result && searchRes.result.length > 0) {
|
||
const existingRecord = searchRes.result.find((r: any) => r.name === domain && r.type === type);
|
||
if (existingRecord) {
|
||
console.log(`记录已存在: ${domain} (${type}) -> ${existingRecord.content}`);
|
||
ctx.body = {
|
||
record_id: existingRecord.id,
|
||
name: existingRecord.name,
|
||
content: existingRecord.content,
|
||
result: existingRecord,
|
||
existed: true,
|
||
};
|
||
return;
|
||
}
|
||
}
|
||
|
||
// 不存在则创建新记录
|
||
const result = await cf.createRecord({
|
||
zone_id,
|
||
domain,
|
||
new_ip,
|
||
api_token,
|
||
type,
|
||
});
|
||
if (result.success === false) {
|
||
ctx.throw?.(result.errors?.map((e: any) => e.message).join('; ') || '创建DNS记录失败');
|
||
}
|
||
console.log(`创建成功: ${domain} -> ${new_ip}`);
|
||
const record_id = result.result.id;
|
||
const name = result.result.name;
|
||
const content = result.result.content;
|
||
console.log(`id->name: ${result.result.id} -> ${name}, content: ${content}`);
|
||
ctx.body = {
|
||
record_id: record_id,
|
||
name: name,
|
||
content: content,
|
||
result: result.result,
|
||
existed: false,
|
||
};
|
||
}).addTo(app);
|
||
|
||
app.route({
|
||
path: 'cf',
|
||
key: 'delete',
|
||
description: '删除Cloudflare DNS记录, 需要提供zone_id, record_id, api_token参数',
|
||
}).define(async (ctx) => {
|
||
const { zone_id, record_id, api_token } = ctx.query || {};
|
||
if (!zone_id || !record_id || !api_token) {
|
||
ctx.throw?.('缺少必要参数');
|
||
}
|
||
|
||
const cf = new CloudflareDDNS();
|
||
|
||
const result = await cf.deleteRecord(zone_id, record_id, api_token);
|
||
if (result.success === false) {
|
||
ctx.throw?.(result.errors?.map((e) => e.message).join('; ') || '删除DNS记录失败');
|
||
}
|
||
ctx.body = { result };
|
||
})
|
||
|
||
app.route({
|
||
path: 'cf',
|
||
key: 'list',
|
||
description: '获取Cloudflare DNS记录列表, 需要提供zone_id, api_token参数,可选search参数用于模糊搜索域名',
|
||
}).define(async (ctx) => {
|
||
const { zone_id, api_token, search } = ctx.query || {};
|
||
|
||
if (!zone_id || !api_token) {
|
||
ctx.throw?.('缺少必要参数');
|
||
}
|
||
|
||
const cf = new CloudflareDDNS();
|
||
const result = await cf.getList(zone_id, api_token, search ? { search } : undefined);
|
||
|
||
ctx.body = { result };
|
||
}).addTo(app); |