generated from template/slidev-template
104 lines
3.0 KiB
TypeScript
104 lines
3.0 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();
|
||
|
||
const result = await cf.createRecord({
|
||
zone_id,
|
||
domain,
|
||
new_ip,
|
||
api_token,
|
||
type,
|
||
});
|
||
if (result.success === false) {
|
||
ctx.throw?.(result.errors?.map((e) => 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
|
||
};
|
||
}).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); |