forked from wonderfulsoftware/source.in.th
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate.mjs
122 lines (110 loc) · 3.62 KB
/
update.mjs
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
const config = [
// https://github.com/StupidHackTH/hackathon.in.th/blob/main/public/_redirects
['hackathon.in.th', 'CNAME', 'apex-loadbalancer.netlify.com'],
['www.hackathon.in.th', 'CNAME', 'hackathoninth.netlify.app'],
// https://github.com/StupidHackTH/StupidHackTH.github.io
['stupid.hackathon.in.th', 'CNAME', 'stupidhackth.github.io'],
// https://github.com/StupidHackTH/event-currency
['staff.7th.stupid.hackathon.in.th', 'CNAME', 'cname.vercel-dns.com'],
// https://github.com/StupidHackTH/event-currency-interface
['wallet.7th.stupid.hackathon.in.th', 'CNAME', 'cname.vercel-dns.com'],
['_vercel.hackathon.in.th', 'TXT', 'vc-domain-verify=wallet.7th.stupid.hackathon.in.th,ffacee0fe9da5f8feb3b'],
// https://github.com/StupidHackTH/sht7-vote
['vote.7th.stupid.hackathon.in.th', 'CNAME', 'cname.vercel-dns.com'],
// https://github.com/NTPLSRPP/SHiT8.125
['shit.8.125.stupid.hackathon.in.th', 'CNAME', 'cname.vercel-dns.com'],
['_vercel.hackathon.in.th', 'TXT', 'vc-domain-verify=shit.8.125.stupid.hackathon.in.th,46cf6a5c2beb81c2a3c2']
]
const zoneId = process.env.CLOUDFLARE_ZONE_ID
const apiToken = process.env.CLOUDFLARE_API_TOKEN
await sync()
async function sync() {
const zones = await getZones()
const tasks = []
for (const entry of config) {
const [name, type, content] = entry
const zone = zones.find((zone) => zone.name === name && zone.type === type)
if (zone) {
const id = zone.id
if (!content) {
tasks.push({
name: `Delete: ${name} ${type} (id: ${id})`,
run: () => deleteZoneById(id),
})
} else if (zone.content !== content) {
tasks.push({
name: `Update: ${name} ${type} -> ${content} (id: ${id})`,
run: () => updateZoneById(id, type, name, content),
})
} else {
tasks.push({
name: `Up-to-date: ${name} ${type} -> ${content} (id: ${id})`,
})
}
} else {
tasks.push({
name: `Create: ${name} ${type} -> ${content}`,
run: () => createZone(type, name, content),
})
}
}
for (const task of tasks) {
console.log(task.name)
if (task.run && process.argv.includes('-f')) {
console.log(await task.run())
}
}
}
async function getZones() {
const response = await fetch(
`https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records`,
{
headers: {
Authorization: `Bearer ${apiToken}`,
},
},
)
const { result } = await response.json()
return result
}
async function deleteZoneById(id) {
const response = await fetch(
`https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records/${id}`,
{
method: 'DELETE',
headers: {
Authorization: `Bearer ${apiToken}`,
},
},
)
return `${response.status}`
}
async function updateZoneById(id, type, name, content) {
const response = await fetch(
`https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records/${id}`,
{
method: 'PUT',
headers: {
Authorization: `Bearer ${apiToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ type, name, content, ttl: 1 }),
},
)
return `${response.status}`
}
async function createZone(type, name, content) {
const response = await fetch(
`https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records`,
{
method: 'POST',
headers: {
Authorization: `Bearer ${apiToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ type, name, content, ttl: 1 }),
},
)
const { result } = await response.json()
return `${response.status} ${result.id}`
}