- AbitaregliIblei.it
- Server aggiornamenti agli script. - Editor HTML corretto un po'. - Record Mysql per server (appena iniziato)
This commit is contained in:
@@ -1,66 +1,90 @@
|
||||
|
||||
const axios = require('axios');
|
||||
|
||||
const apiUrl = 'https://api.cloudflare.com/client/v4'; // Endpoint
|
||||
|
||||
async function fetchCloudflareZones(apiToken) {
|
||||
try {
|
||||
class CloudFlare {
|
||||
constructor(config) {
|
||||
this.config = config ? config : {};
|
||||
|
||||
// Effettua una richiesta GET all'API di Cloudflare
|
||||
const response = await axios.get(apiUrl + '/zones', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiToken}`, // Autenticazione con token
|
||||
'Content-Type': 'application/json' // Tipo di contenuto
|
||||
}
|
||||
});
|
||||
if (!this.config.arrTokens) {
|
||||
this.config.arrTokens = process.env.CLOUDFLARE_TOKENS;
|
||||
}
|
||||
}
|
||||
|
||||
// Estrai i dati dalla risposta
|
||||
const zones = response.data.result;
|
||||
init() {
|
||||
if (this.config.arrTokens) {
|
||||
// this.zones = this.fetchCloudflareZones(this.config.apiToken);
|
||||
this.zones = [];
|
||||
this.dnsRecords = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Stampa le zone
|
||||
// console.log('Zone di Cloudflare:', zones);
|
||||
} catch (error) {
|
||||
console.error('Errore durante il recupero delle zone di Cloudflare:', error.message);
|
||||
async fetchCloudflareZones(apiToken) {
|
||||
try {
|
||||
|
||||
// Effettua una richiesta GET all'API di Cloudflare
|
||||
const response = await axios.get(apiUrl + '/zones', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiToken}`, // Autenticazione con token
|
||||
'Content-Type': 'application/json' // Tipo di contenuto
|
||||
}
|
||||
});
|
||||
|
||||
// Estrai i dati dalla risposta
|
||||
this.zones = response.data.result;
|
||||
|
||||
return this.zones;
|
||||
|
||||
// Stampa le zone
|
||||
// console.log('Zone di Cloudflare:', zones);
|
||||
} catch (error) {
|
||||
console.error('Errore durante il recupero delle zone di Cloudflare:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per estrarre i record DNS
|
||||
async fetchDNSRecords(apiToken, zoneId) {
|
||||
const apiUrlDNS = apiUrl + `/zones/${zoneId}/dns_records`;
|
||||
|
||||
try {
|
||||
const response = await axios.get(apiUrlDNS, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiToken}`, // Autenticazione con token
|
||||
'Content-Type': 'application/json' // Tipo di contenuto
|
||||
}
|
||||
});
|
||||
|
||||
this.dnsRecords = response.data.result;
|
||||
|
||||
return this.dnsRecords;
|
||||
} catch (error) {
|
||||
console.error('Errore durante il recupero dei record DNS di Cloudflare:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per aggiornare un record DNS
|
||||
async updateDNSRecord(apiToken, zoneId, dnsRecordId, newDnsRecordData) {
|
||||
const apiUrlDNS = apiUrl + `/zones/${zoneId}/dns_records/${dnsRecordId}`;
|
||||
|
||||
try {
|
||||
|
||||
const response = await axios.put(apiUrlDNS, newDnsRecordData, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiToken}`, // Autenticazione con token
|
||||
'Content-Type': 'application/json' // Tipo di contenuto
|
||||
}
|
||||
});
|
||||
|
||||
const updatedRecord = response.data.result;
|
||||
|
||||
// Stampa il record DNS aggiornato
|
||||
console.log('Record DNS aggiornato:', updatedRecord);
|
||||
|
||||
return updatedRecord;
|
||||
} catch (error) {
|
||||
console.error('Errore durante l\'aggiornamento del record DNS:', error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per estrarre i record DNS
|
||||
async function fetchDNSRecords(apiToken, zoneId) {
|
||||
const apiUrlDNS = apiUrl + `/zones/${zoneId}/dns_records`;
|
||||
|
||||
try {
|
||||
const response = await axios.get(apiUrlDNS, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiToken}`, // Autenticazione con token
|
||||
'Content-Type': 'application/json' // Tipo di contenuto
|
||||
}
|
||||
});
|
||||
|
||||
const dnsRecords = response.data.result;
|
||||
|
||||
return dnsRecords;
|
||||
} catch (error) {
|
||||
console.error('Errore durante il recupero dei record DNS di Cloudflare:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per aggiornare un record DNS
|
||||
async function updateDNSRecord(apiToken, zoneId, dnsRecordId, newDnsRecordData) {
|
||||
const apiUrlDNS = apiUrl + `/zones/${zoneId}/dns_records/${dnsRecordId}`;
|
||||
|
||||
try {
|
||||
const response = await axios.put(apiUrlDNS, newDnsRecordData, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiToken}`, // Autenticazione con token
|
||||
'Content-Type': 'application/json' // Tipo di contenuto
|
||||
}
|
||||
});
|
||||
|
||||
const updatedRecord = response.data.result;
|
||||
|
||||
// Stampa il record DNS aggiornato
|
||||
console.log('Record DNS aggiornato:', updatedRecord);
|
||||
} catch (error) {
|
||||
console.error('Errore durante l\'aggiornamento del record DNS:', error.message);
|
||||
}
|
||||
}
|
||||
module.exports = CloudFlare
|
||||
Reference in New Issue
Block a user