-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (83 loc) · 3.08 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
import 'babel-polyfill'
import request from 'request-promise'
import consola from 'consola'
import moment from 'moment'
import { CronJob } from 'cron'
const dev = (process.env.NODE_ENV !== 'production')
const debuger = {
log (...msg) {
if (!dev) return
console.log(' ', ...msg)
},
start (...msg) {
if (!dev) return
consola.start(msg.join(' '))
},
success (...msg) {
if (!dev) return
consola.success(msg.join(' '))
console.log()
},
info (...msg) {
if (!dev) return
consola.info(msg.join(' '))
},
error (...msg) {
if (!dev) return
console.log()
consola.error(msg.join(' '))
console.log()
}
}
if (!process.env.DOMAIN_NAME) throw new Error(`Required 'DOMAIN_NAME' environment.`)
if (!process.env.DOMAIN_ZONE) throw new Error(`Required 'DOMAIN_ZONE' environment.`)
let getRecords = null
const ipAddrUpdate = async domain => {
let api = await request({
url: 'https://api.ipify.org?format=json',
json: true
})
if (!process.env.DOMAIN_KEY) throw new Error(`Required 'DOMAIN_KEY' environment.`)
if (!process.env.DOMAIN_EMAIL) throw new Error(`Required 'DOMAIN_EMAIL' environment.`)
const zone = process.env.DOMAIN_ZONE
const headers = {
'Content-Type': 'application/json',
'X-Auth-Key': process.env.DOMAIN_KEY.trim(),
'X-Auth-Email': process.env.DOMAIN_EMAIL.trim()
}
debuger.start(`[cloudflare.com]`, `My IP Address: ${api.ip}`)
if (!getRecords || !getRecords.success) {
getRecords = await request({
url: `https://api.cloudflare.com/client/v4/zones/${zone}/dns_records?name=${domain}`,
headers: headers,
timeout: 1000,
json: true
})
debuger[getRecords.success ? 'success' : 'error'](`[cloudflare.com]`, `DNS Verify ${getRecords.success ? 'Pass' : 'Fail'}.`)
if (!getRecords.success) throw new Error(`Not found record name '${domain}'`)
getRecords = getRecords.result[0]
}
debuger.log(`[cloudflare.com]`, `DNS: ${getRecords.content} ${api.ip !== getRecords.content ? 'Updating...' : 'Ignore'}.`)
const endpointPutRecords = `https://api.cloudflare.com/client/v4/zones/${zone.trim()}/dns_records/${getRecords.id}`
if (api.ip !== getRecords.content) {
let putRecords = await request({
method: 'PUT',
url: endpointPutRecords,
headers: headers,
body: { type: getRecords.type, name: domain, content: api.ip },
json: true
})
debuger[putRecords.success ? 'success' : 'error'](`[cloudflare.com]`, `DNS Updated ${putRecords.success ? 'Pass' : 'Fail'}.`)
if (!putRecords.success) throw new Error(`Can't PUT record name '${domain}'`)
getRecords.content = api.ip
debuger.log(`[couldfare.com]`, `DNS: '${domain}' IP:'${api.ip}' Updated at ${moment().format('YYYY-MM-DD HH:mm:ss')}.`)
}
}
ipAddrUpdate(process.env.DOMAIN_NAME).catch(debuger.error)
let jobUpdated = new CronJob({
cronTime: '30 * * * *',
onTick: () => { ipAddrUpdate(process.env.DOMAIN_NAME).catch(debuger.error) },
start: true,
timeZone: 'Asia/Bangkok'
})
debuger.log(`[couldfare.com] Watch and updated '${process.env.DOMAIN_NAME}' ${jobUpdated.running ? 'started' : 'stoped'}.`)