First Edition: does _acme-challenge dns records

This commit is contained in:
Expand-sys 2021-05-05 19:29:34 +10:00
parent 0399e13431
commit 6c116acff5
4 changed files with 302 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/node_modules
.env

217
index.js Normal file
View file

@ -0,0 +1,217 @@
const fetch = require('node-fetch');
const dotenv = require('dotenv')
const nodemailer = require('nodemailer')
dotenv.config();
let transporter = nodemailer.createTransport({
host: process.env.HOST,
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: process.env.SENDER, // generated ethereal user
pass: process.env.SPASS, // generated ethereal password
},
})
let message = [];
async function grabdomainnames(){
let domains = [];
await fetch('https://'+process.env.PLESKURL+'/api/v2/domains', {
method:"GET",
headers: {
'X-API-Key': process.env.PLESKKEY,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
}).then(response => response.json())
.then(data => {
for(i in data){
domains.push(data[i].name)
}
});
return domains
}
let domainInfo = []
async function grabCloudflaredomains(){
let domains = [];
await fetch('https://api.cloudflare.com/client/v4/zones?match=all&account.id='+process.env.CLOUDACCOUNTID, {
method: 'GET',
headers: {
'X-Auth-Email': process.env.CLOUDEMAIL,
'X-Auth-Key': process.env.CLOUDKEY,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
})
.then(response => response.json())
.then(async data => {
for(i in data.result){
domainInfo.push(data.result[i])
domains.push(data.result[i].name)
}
if(data.result_info.total_pages > 1){
let count = 1
while(count < data.result_info.total_pages){
count = count+1
await fetch('https://api.cloudflare.com/client/v4/zones?match=all&account.id='+process.env.CLOUDACCOUNTID+'&page='+count, {
method: 'GET',
headers: {
'X-Auth-Email': process.env.CLOUDEMAIL,
'X-Auth-Key': process.env.CLOUDKEY,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
})
.then(response => response.json())
.then(data => {
for(i in data.result){
domainInfo.push(data.result[i])
domains.push(data.result[i].name)
}
})
}
}
});
return domains
}
async function filterDomains(){
let cloudflare = await grabCloudflaredomains();
let plesk = await grabdomainnames();
let result = await cloudflare.filter( ( el ) => plesk.includes( el ) );
return result
}
async function dns(){
let domains = await filterDomains()
let records = []
for(i in domains){
let res = await fetch('https://'+process.env.PLESKURL+'/api/v2/cli/dns/call', {
method: 'POST',
headers: {
'X-API-Key': process.env.PLESKKEY,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({ "params": [ "--info", domains[i]]})
})
.then(response => response.json())
.then(data => {
let string = data.stdout.split("\n")
let acme = "_acme-challenge"
length = acme.length;
for(i in string){
if (string[i].indexOf(acme)!=-1) {
// one of the substrings is in yourstring
records.push(string[i]) //result
}
}
});
}
let finalarray = []
for(i in records){
finalarray.push(records[i].split(' '))
}
return finalarray
}
async function mergedns(){
let finalarray = await dns()
for(i in finalarray){
for(x in domainInfo){
if(finalarray[i][0].includes(domainInfo[x].name)){
finalarray[i].push(domainInfo[x].id);
finalarray[i].push(domainInfo[x].name)
}
}
}
for(i in finalarray){
let zone = finalarray[i][4];
await fetch('https://api.cloudflare.com/client/v4/zones/'+zone+'/dns_records?type=TXT&name=_acme-challenge.'+finalarray[i][5], {
method: 'GET',
headers: {
'X-Auth-Email': process.env.CLOUDEMAIL,
'X-Auth-Key': process.env.CLOUDKEY,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
})
.then(response => response.json())
.then(data => {
if(data.result[0] == undefined){
finalarray[i].push("create")
}else{
finalarray[i].push(data.result[0].id)
finalarray[i].push(data.result[0].content)
}
})
}
return finalarray
}
senddns()
async function senddns(){
let finalarray = await mergedns()
for(i in finalarray){
if(finalarray[i][6] == 'create'){
fetch("https://api.cloudflare.com/client/v4/zones/"+finalarray[i][4]+"/dns_records", {
body: JSON.stringify({type:finalarray[i][1], name:finalarray[i][0], content:finalarray[i][3], ttl:1}),
headers: {
"Content-Type": "application/json",
"X-Auth-Email": process.env.CLOUDEMAIL,
"X-Auth-Key": process.env.CLOUDKEY,
},
method: "POST"
})
} else if(finalarray[i][3] ==finalarray[i][7]){
message.push("punching sand for this turn")
console.log("punching sand for this turn")
}else{
await fetch('https://api.cloudflare.com/client/v4/zones/'+finalarray[i][4]+'/dns_records/'+finalarray[i][6], {
method: 'PUT',
headers: {
'X-Auth-Email': process.env.CLOUDEMAIL,
'X-Auth-Key': process.env.CLOUDKEY,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({type:finalarray[i][1], name:finalarray[i][0], content:finalarray[i][3], ttl:1}),
})
.then(response => response.json())
.then(data => {
message.push(data)
console.log(data)
})
}
}
message.push(finalarray)
console.log(finalarray)
mail()
}
async function mail(){
let info = await transporter.sendMail({
from: '"DNSBOT" <'+process.env.SENDER+'>',
to: process.env.EMAIL,
subject: "DNS updates "+ new Date,
text: message.toString().replaceAll(',', "\n"),
})
console.log(info)
console.log("Message sent to "+process.env.EMAIL)
}

59
package-lock.json generated Normal file
View file

@ -0,0 +1,59 @@
{
"name": "cloudflareplesk-link",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "cloudflareplesk-link",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"dotenv": "^8.5.1",
"node-fetch": "^2.6.1",
"nodemailer": "^6.6.0"
}
},
"node_modules/dotenv": {
"version": "8.5.1",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.5.1.tgz",
"integrity": "sha512-qC1FbhCH7UH7B+BcRNUDhAk04d/n+tnGGB1ctwndZkVFeehYJOn39pRWWzmdzpFqImyX1KB8tO0DCHLf8yRaYQ==",
"engines": {
"node": ">=10"
}
},
"node_modules/node-fetch": {
"version": "2.6.1",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
"integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==",
"engines": {
"node": "4.x || >=6.0.0"
}
},
"node_modules/nodemailer": {
"version": "6.6.0",
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.6.0.tgz",
"integrity": "sha512-ikSMDU1nZqpo2WUPE0wTTw/NGGImTkwpJKDIFPZT+YvvR9Sj+ze5wzu95JHkBMglQLoG2ITxU21WukCC/XsFkg==",
"engines": {
"node": ">=6.0.0"
}
}
},
"dependencies": {
"dotenv": {
"version": "8.5.1",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.5.1.tgz",
"integrity": "sha512-qC1FbhCH7UH7B+BcRNUDhAk04d/n+tnGGB1ctwndZkVFeehYJOn39pRWWzmdzpFqImyX1KB8tO0DCHLf8yRaYQ=="
},
"node-fetch": {
"version": "2.6.1",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
"integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw=="
},
"nodemailer": {
"version": "6.6.0",
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.6.0.tgz",
"integrity": "sha512-ikSMDU1nZqpo2WUPE0wTTw/NGGImTkwpJKDIFPZT+YvvR9Sj+ze5wzu95JHkBMglQLoG2ITxU21WukCC/XsFkg=="
}
}
}

24
package.json Normal file
View file

@ -0,0 +1,24 @@
{
"name": "cloudflareplesk-link",
"version": "1.0.0",
"description": "A Link between Cloudflare DNS and Plesk for renewing lets encrypt certificates",
"main": "index.js",
"scripts": {
"test": "node index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Expand-sys/CloudflarePlesk-Link.git"
},
"author": "Expand",
"license": "ISC",
"bugs": {
"url": "https://github.com/Expand-sys/CloudflarePlesk-Link/issues"
},
"homepage": "https://github.com/Expand-sys/CloudflarePlesk-Link#readme",
"dependencies": {
"dotenv": "^8.5.1",
"node-fetch": "^2.6.1",
"nodemailer": "^6.6.0"
}
}