-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
72 lines (65 loc) · 2.61 KB
/
index.ts
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
import "dotenv/config";
const arrayGroupDomain = process.env.CF_GROUP_DOMAIN?.trim().split(",");
const UpdateCloudflare = () => {
arrayGroupDomain?.forEach(async (domainName) => {
const existingRecord = await fetch(
`https://api.cloudflare.com/client/v4/zones/${process.env.ZONE_ID}/dns_records?type=A&name=${domainName}`,
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.API_TOKEN}`,
},
}
);
const existingRecordData = await existingRecord.json();
if (existingRecordData.result.length !== 0) {
const url = `https://api.cloudflare.com/client/v4/zones/${process.env.ZONE_ID}/dns_records/${existingRecordData.result[0].id}`;
const response = await fetch(url, {
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.API_TOKEN}`,
},
body: JSON.stringify({
type: "A",
name: domainName,
content: await fetch("https://api.ipify.org?format=json")
.then((res) => res.json())
.then((data) => data.ip),
ttl: 1,
proxied: true,
}),
});
const data = await response.json();
console.log(data.success ? "Updated" : "Failed");
} else {
const response = await fetch(
`https://api.cloudflare.com/client/v4/zones/${process.env.ZONE_ID}/dns_records`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.API_TOKEN}`,
},
body: JSON.stringify({
type: "A",
name: domainName,
content: await fetch(
"https://api.ipify.org?format=json"
)
.then((res) => res.json())
.then((data) => data.ip),
ttl: 1,
proxied: true,
}),
}
);
const data = await response.json();
console.log(data.success ? "Updated" : "Failed");
}
});
};
UpdateCloudflare();
setInterval(() => {
UpdateCloudflare();
}, 300 * 1000);