-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·71 lines (63 loc) · 2.29 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
import fs from "fs-extra";
import log from "log-to-file";
import logfile from "./logdate.js";
const config = await fs.readJson("./config.json");
const apikey = config.CLOUDFLARE_API_KEY;
const zoneId = config.CLOUDFLARE_ZONE_ID;
const accountId = config.CLOUDFLARE_ACCOUNT_ID;
const records = config.CLOUDFLARE_DNS_RECORDS;
const currentIP = await fs.readJson("./currentIP.json");
// If ./logs folder doesn't exist, create it
try {
fs.ensureDirSync(`./logs`);
} catch (err) {
console.error(err);
}
const newIp = await getPublicIP();
if (newIp !== currentIP) {
await fs.writeJson("./currentIP.json", newIp);
for (const record of records) await updateDNS(record, newIp);
} else {
console.log("No change in IP address");
log("No change in IP address", `./logs/${logfile}`);
}
async function getPublicIP() {
const url = "http://api.ipify.org/?format=json";
let options = {
method: "GET",
headers: { "Content-Type": "application/json" },
};
try {
const res = await fetch(url, options);
const ipObject = await res.json();
const newIp = ipObject.ip;
console.log(`Fetched public IP: ${newIp}`);
log(`Fetched public IP: ${newIp}`, `./logs/${logfile}`);
return newIp;
} catch (err) {
console.error(err);
log(err, `./logs/${logfile}`);
}
}
async function updateDNS(record, newIp) {
const url = `https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records/${record.id}`;
let options = {
method: "PATCH",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${apikey}` },
body: `{"content":"${newIp}","name":"${record.domain}","proxied":false,"type":"A","comment":"DDNS update","id":"${accountId}"}`,
};
try {
const res = await fetch(url, options);
const resObj = await res.json();
if (!resObj.success) {
console.error(`Error updating DNS record ${record.domain}: ${resObj.errors[0].message}`);
log(`Error updating DNS record ${record.domain}: ${resObj.errors[0].message}`, `./logs/${logfile}`);
return;
}
console.log(`DNS record ${resObj.result.name} type ${resObj.result.type} updated to new target: ${resObj.result.content}`);
log(`DNS record ${resObj.result.name} type ${resObj.result.type} updated to new target: ${resObj.result.content}`, `./logs/${logfile}`);
} catch (err) {
console.error("error:" + err);
log("error:" + err, `./logs/${logfile}`);
}
}