This commit is contained in:
noot
2024-08-13 19:16:00 +08:00
parent 2a5ca3664a
commit d8c9621568
+24 -14
View File
@@ -41,13 +41,14 @@ class CloudFlare{
async axios(method, ...args){ async axios(method, ...args){
try{ try{
let a = axios.create({ let a = axios.create({
baseURL: 'https://api.cloudflare.com/client/v4/zones', baseURL: 'https://api.cloudflare.com/client/v4/zones',
headers: {Authorization: `Bearer ${this.token}`} headers: {Authorization: `Bearer ${this.token}`}
}); });
// console.log(a)
// console.log(...args)
return await a[method](...args); return await a[method](...args);
}catch(error){ }catch(error){
console.log(error)
if(!error.response) throw error; if(!error.response) throw error;
if(error.response.data || error.response.data.id === 'Unauthorized'){ if(error.response.data || error.response.data.id === 'Unauthorized'){
throw dnsErrors.unauthorized(this); throw dnsErrors.unauthorized(this);
@@ -86,21 +87,27 @@ class CloudFlare{
async createRecord(domain, options){ async createRecord(domain, options){
this.__typeCheck(options.type); this.__typeCheck(options.type);
// if(!options.data) throw new Error(`${this.constructor.name} API: 'data' key is required for this action`) //POST zones/:zone_identifier/dns_records
// if(options.name) options.name = this.__parseName(domain, options.name); // name , type , ip / content , ttl
if(!options.content) throw new Error(`${this.constructor.name} API: 'data' key is required for this action`)
let res = await this.axios('post', `${domain.zoneId}/dns_records`, options);
return res;
// let res = await this.axios('post', `/domains/${domain}/records`, options);
// return res.data;
} }
async deleteRecordById(domain, id){ async deleteRecordById(domain, id){
let res = await this.axios('delete', `/domains/${domain}/records/${id}`); let res = await this.axios('delete', `${domain.zoneId}/dns_records/${id}`);
} }
async deleteRecords(domain, options){ async deleteRecords(domain, options){
let records = await this.getRecords(domain, options) let records = await this.getRecords(domain, options)
for(let record of records){ for(let record of records){
let res = await this.deleteRecordById(domain, record.id); let res = await this.deleteRecordById(domain, record.id);
// // console.log(record)
// console.log(record.id)
} }
return true; return true;
@@ -111,11 +118,11 @@ module.exports = CloudFlare;
if(require.main === module){(async function(){try{ if(require.main === module){(async function(){try{
let cf = new CloudFlare("RaA7Ej-eLBfc_hhCeMVhvPEZTmrHh2WXfPpUCUIL"); // let cf = new CloudFlare("");
let domain = { // let domain = {
domain: "teeseng.uk", // domain: "example.uk",
zoneId: "11f8ec3c2ff0530fe6f1b97dfe4b8f74" // zoneId: "5eb25c12cd7d22f11252330a29a0dd77"
} // }
// console.log(await cf.listDomains()) // console.log(await cf.listDomains())
@@ -123,8 +130,11 @@ if(require.main === module){(async function(){try{
//name = domain //name = domain
// console.log('get', await cf.getRecords(domain, {content: '172.206.221.130'})) // console.log('get', await cf.getRecords(domain, {content: '172.206.221.130'}))
console.log('make', await cf.createRecord('rm-rf.stream', {name:'_test', data: '890', type: "TXT"})) // console.log('post', await cf.createRecord(domain, {name:'test', content: '10.0.0.1', type: "TXT"}))
// console.log('delete', await digi.deleteRecords('rm-rf.stream', {type: 'TXT'}))
// console.log('delete', await cf.deleteRecordById(domain , "5c0e958c3406a34d011459933d538b78"))
// console.log('delete', await cf.deleteRecords(domain, {type: 'A'}))
}catch(error){ }catch(error){
console.log('IIFE Error:', error) console.log('IIFE Error:', error)