generated from template/slidev-template
update
This commit is contained in:
37
agent/ddns/cloudflare/index.ts
Normal file
37
agent/ddns/cloudflare/index.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
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)}`);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user