-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
216 lines (193 loc) · 6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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,
auth: {
user: process.env.SENDER,
pass: process.env.SPASS,
},
})
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) {
records.push(string[i])
}
}
});
}
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)
}