-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
112 lines (102 loc) · 3.68 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
require('dotenv').config()
const { getRecords, addRecord, delRecord } = require('./route53')
const { getZTMembers } = require('./zerotier')
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
async function checkAdds(recordList) {
// get records to add
console.log('generating list of records to add...')
var memberListDiff = await getZTMembers()
for (record of recordList) {
if (memberListDiff.find(member => member.name == record.name)) {
console.log(`Found ${record.name}`)
let deleteMe = memberListDiff.findIndex(member => member.name == record.name)
memberListDiff.splice(deleteMe, 1)
}
}
if (memberListDiff.length > 0) {
console.log('Records to add:')
console.log(memberListDiff)
for (member of memberListDiff) {
var result = await addRecord(member.name, member.ip)
console.log(result)
}
} else {
console.log()
console.log('No records to add.')
console.log()
}
}
async function checkDeletes(memberList) {
// get records to delete
var recordListDiff = await getRecords(process.env.hosted_zone)
console.log('generating list of records to delete...')
for (member of memberList) {
if (recordListDiff.find(record => record.name == member.name)) {
console.log(`Found ${member.name}`)
let deleteMe = recordListDiff.findIndex(record => record.name == member.name)
recordListDiff.splice(deleteMe, 1)
}
}
if (recordListDiff.length > 0) {
console.log('Records to delete:')
console.log(recordListDiff)
for (record of recordListDiff) {
var result = await delRecord(record.name, record.ip)
console.log(result)
}
} else {
console.log()
console.log('No records to delete.')
console.log()
}
}
async function checkChanges(memberList, recordList) {
//get records to change
var recordListDiff = await getRecords(process.env.hosted_zone)
console.log('generating list of records to change...')
for (member of memberList) {
if (recordListDiff.find(record => record.name == member.name)) {
let checkme = recordListDiff.findIndex(record => record.name == member.name)
if (recordListDiff[checkme].ip == member.ip) {
recordListDiff.splice(checkme, 1)
}
}
}
if (recordListDiff.length > 0) {
console.log('Records to change:')
console.log(recordListDiff)
for (record of recordListDiff) {
var result = await delRecord(record.name, record.ip)
console.log(result)
}
await checkAdds(recordList)
} else {
console.log()
console.log('No records to change.')
console.log()
}
}
async function main() {
while (true) {
console.log(`checking for updates...`)
console.log('-----------------------')
console.log()
// get data from route53 about current zerotier records
console.log('Getting existing records from the DNS provider...')
var recordList = await getRecords(process.env.hosted_zone)
console.log('Getting the list of member systems from Zerotier...')
// get data from zerotier about current network members
var memberList = await getZTMembers()
// generate actions
await checkAdds(recordList)
await checkDeletes(memberList)
await checkChanges(memberList, recordList)
// sleep
await sleep(process.env.sleep_timeout)
console.log()
console.log()
}
}
main()