-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
184 lines (161 loc) · 5.56 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import 'dotenv/config'
import fetch from 'node-fetch'
import cron from 'node-cron'
import winston from 'winston'
const zoneId = process.env.ZONE_ID
const domains = (process.env.DOMAINS || '').split(',')
const apiToken = process.env.API_TOKEN
const useIPV6 = (process.env.IPV6 || 'false') == 'true'
const useIPV4 = (process.env.IPV4 || 'false') == 'true'
const ttl = process.env.TTL || 1
const cronTab = process.env.CRON || '*/10 * * * *'
const LOG_LEVEL = process.env.LOG_LEVEL || 'INFO'
const { combine, timestamp, printf } = winston.format;
const myFormat = printf(({ level, message, timestamp }) => {
const text = typeof message == 'object' ? JSON.stringify(message, null, 2) : message
return `${timestamp} [${level}]: ${text}`;
});
const logger = winston.createLogger({
level: LOG_LEVEL.toLowerCase(),
format: combine(
timestamp(),
myFormat
),
transports: [
new winston.transports.Console(),
// new winston.transports.File({ filename: 'combined.log' }),
],
});
logger.info({
useIPV4,
useIPV6,
zoneId,
domains,
ttl,
cronTab,
LOG_LEVEL
})
if (!zoneId || domains.length === 0, !apiToken) {
logger.error('Missing enviromnent variables!')
process.exit(1)
}
var currentV6 = ''
var currentV4 = ''
////
async function getV6() {
const response = await fetch(`https://api6.ipify.org?format=json`);
const result = await response.json();
return result.ip
}
async function getV4() {
const response = await fetch(`https://api4.ipify.org?format=json`);
const result = await response.json();
return result.ip
}
////
async function updateRecordV6(zoneId, dnsIdentifier, ipv6, name) {
const response = await fetch(`https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records/${dnsIdentifier}`, {
method: 'put',
body: JSON.stringify({
type: 'AAAA',
content: ipv6,
name,
ttl
}),
headers: {
Authorization: `Bearer ${apiToken}`
}
});
if(response.status != 200) return Promise.reject('Cannot update! ' + name + ' responded with ' + response.status)
}
async function updateRecordV4(zoneId, dnsIdentifier, ipv4, name) {
const response = await fetch(`https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records/${dnsIdentifier}`, {
method: 'put',
body: JSON.stringify({
type: 'A',
content: ipv4,
name,
ttl
}),
headers: {
Authorization: `Bearer ${apiToken}`
}
});
if(response.status != 200) return Promise.reject('Cannot update! ' + name + ' responded with ' + response.status)
}
////
async function updateZoneV6(zoneId, domains, ipv6) {
const response = await fetch(`https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records?match=all`, {
headers: {
Authorization: `Bearer ${apiToken}`
}
});
const { success, result } = await response.json();
if(response.status != 200) return Promise.reject('Failed to get Zone! ' + zoneId + ' responded with ' + response.status)
const toBeUpdated = result.filter(r => r.type === 'AAAA' && domains.includes(r.name) )
if (toBeUpdated.length === 0) {
return Promise.reject('No Domain found!')
}
if (domains.length != toBeUpdated.length) {
const missingRecords = domains.filter(name => !toBeUpdated.map(r => r.name).includes(name) )
logger.warn('Not all Records applied! Are all created in cloudflare?')
logger.warn({ missingRecords, availableRecords: toBeUpdated.map(r => r.name) })
}
for (let i = 0; i < toBeUpdated.length; i++) {
logger.debug('update ' + toBeUpdated[i].name)
await updateRecordV6(zoneId, toBeUpdated[i].id, ipv6, toBeUpdated[i].name)
}
logger.info(`${toBeUpdated.map(r=>r.name).join()} updated to '${ipv6}'`)
}
async function updateZoneV4(zoneId, domains, ipv4) {
const response = await fetch(`https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records?match=all`, {
headers: { Authorization: `Bearer ${apiToken}` }
});
const { success, result } = await response.json();
if(response.status != 200) return Promise.reject('Failed to get Zone! ' + zoneId + ' responded with ' + response.status)
const toBeUpdated = result.filter(r => r.type === 'A' && domains.includes(r.name) )
if (toBeUpdated.length === 0) {
return Promise.reject('No Domain found!')
}
if (domains.length != toBeUpdated.length) {
const missingRecords = domains.filter(name => !toBeUpdated.map(r => r.name).includes(name) )
logger.warn('Not all Records applied! Are all created in cloudflare?')
logger.warn({ missingRecords, availableRecords: toBeUpdated.map(r => r.name) })
}
for (let i = 0; i < toBeUpdated.length; i++) {
logger.debug('update ' + toBeUpdated[i].name)
await updateRecordV4(zoneId, toBeUpdated[i].id, ipv4, toBeUpdated[i].name)
}
logger.info(`${toBeUpdated.map(r=>r.name).join()} updated to '${ipv4}'`)
}
////
async function updateV6() {
const ipv6 = await getV6()
if (currentV6 == ipv6) {
logger.debug('Skip. No ipv6 changed!')
logger.debug({ fetchedIp: ipv6, knownIp: currentV6 })
return
}
await updateZoneV6(zoneId, domains, ipv6)
currentV6 = ipv6
logger.debug('finished ipv6')
}
async function updateV4() {
const ipv4 = await getV4()
if (currentV4 == ipv4) {
logger.debug('Skip. No ipv6 changed!')
logger.debug({ fetchedIp: ipv4, knownIp: currentV4 })
return
}
await updateZoneV4(zoneId, domains, ipv4)
currentV4 = ipv4
logger.debug('finished ipv4')
}
if (useIPV6) await updateV6()
if (useIPV4) await updateV4()
cron.schedule(cronTab, async () => {
logger.debug('start')
if (useIPV6) await updateV6()
if (useIPV4) await updateV4()
logger.debug('finished at ' + new Date())
});