generated from template/slidev-template
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
interface DnsUpdate {
|
|
zone_id: string;
|
|
record_id: string;
|
|
domain: string;
|
|
new_ip: string;
|
|
api_token: string;
|
|
type?: string; // 'A' or 'AAAA'
|
|
}
|
|
export class CloudflareDDNS {
|
|
makeHeader(api_token: string) {
|
|
return {
|
|
'Authorization': `Bearer ${api_token}`,
|
|
'Content-Type': 'application/json',
|
|
};
|
|
}
|
|
async updateRecord(data: DnsUpdate) {
|
|
const { zone_id, record_id, domain, type ,new_ip, api_token } = data;
|
|
const url = `https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records/${record_id}`;
|
|
const body = {
|
|
"type": type || 'A',
|
|
"name": domain,
|
|
"content": new_ip,
|
|
"ttl": 0,
|
|
"proxied": false,
|
|
}
|
|
const response = await fetch(url, {
|
|
method: 'PUT',
|
|
headers: this.makeHeader(api_token),
|
|
body: JSON.stringify(body),
|
|
});
|
|
const result = await response.json();
|
|
if(!result.success) {
|
|
throw new Error(`更新失败: ${JSON.stringify(result.errors)}`);
|
|
}
|
|
console.log(`更新成功: ${domain} -> ${new_ip}`);
|
|
return result;
|
|
}
|
|
async getList(zone_id: string, api_token: string) {
|
|
const url = `https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records`;
|
|
return fetch(url, {
|
|
method: 'GET',
|
|
headers: this.makeHeader(api_token),
|
|
}).then(res => res.json());
|
|
}
|
|
} |