-
Notifications
You must be signed in to change notification settings - Fork 4
/
update.mjs
128 lines (114 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
123
124
125
126
127
128
const config = [
// Vercel domain verification
['_vercel.source.in.th', 'TXT', 'vc-domain-verify=cname.vercel-dns.com,285f12e4d6593c081ce2'],
// https://github.com/wonderfulsoftware/source.in.th/blob/main/public/_redirects
['source.in.th', 'CNAME', 'apex-loadbalancer.netlify.com'],
['www.source.in.th', 'CNAME', 'sourceinth.netlify.app'],
['discord.bangkok.open.source.in.th', 'CNAME', 'sourceinth.netlify.app'],
// https://github.com/rayriffy/lamb
['lamb.source.in.th', 'CNAME', 'rayriffy.github.io'],
// https://github.com/dtinth/screenshot-redirector
['screenshot.source.in.th', 'CNAME', 'cname.vercel-dns.com'],
// Experiment with Canasta
['bangkok.source.in.th', 'A', '103.169.67.34'],
// https://github.com/dtinth/transit.source.in.th
['transit.source.in.th', 'CNAME', 'cname.vercel-dns.com'],
// https://github.com/creatorsgarten/source.in.th
['open.source.in.th', 'CNAME', 'bosf2023.netlify.app'],
// chordsource
['db.chord.source.in.th', 'CNAME', 'dtinth.github.io'],
['chord.source.in.th', 'CNAME', 'cname.vercel-dns.com'],
]
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}`
}