-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (67 loc) · 2.1 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
import https from "https";
import "dotenv/config";
const getPublicIP = () => {
return new Promise((resolve, reject) => {
https
.get("https://api.ipify.org", (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
resolve(data); // Return the public IP address
});
})
.on("error", (error) => {
reject(error); // Handle any errors
});
});
};
const updateDNSRecord = (zoneId, recordId, ip, apiToken) => {
const options = {
hostname: "api.cloudflare.com",
path: `/client/v4/zones/${zoneId}/dns_records/${recordId}`,
method: "PUT",
headers: {
Authorization: `Bearer ${apiToken}`,
"Content-Type": "application/json",
},
};
const postData = JSON.stringify({
type: "A",
name: "agpagp.ir", // Change to your actual domain
content: ip,
ttl: 120,
proxied: true, // Set to true if you want Cloudflare to proxy traffic
});
const req = https.request(options, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
console.log("DNS Update Response:", data);
});
});
req.on("error", (error) => {
console.log("Error updating DNS:", error);
});
// Write the data to the request body
req.write(postData);
req.end();
};
// Replace these with your actual values
const zoneId = process.env.ZONE_ID;
const recordId = process.env.RECORD_ID;
const apiToken = process.env.API_TOKEN;
const time = 60 * 1000 * 60 * 2; // 2 hours
setInterval(() => {
getPublicIP()
.then((ip) => {
console.log("Your public IP address is:", ip);
updateDNSRecord(zoneId, recordId, ip, apiToken); // Update DNS record with new IP
})
.catch((error) => {
console.error("Error fetching IP address:", error);
});
}, time);