This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
74 lines (64 loc) · 1.86 KB
/
main.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
const Config = require('config');
const DNS = require('@google-cloud/dns');
const rp = require('request-promise');
const config = Config.config;
const projectId = config.projectId;
const managedZones = config.managedZones;
const dns = new DNS({
projectId: projectId,
keyFilename: '/tmp/sa.json',
});
const zone = dns.zone('xss-moe');
rp({
uri: 'https://ipinfo.io/',
method: 'GET',
json: true,
}).then(function(data) {
const currentIp = data.ip;
for(let managedZone of managedZones) {
for(let domainName of managedZone.domainNames) {
zone.getRecords({
name: domainName,
type: 'A',
}).then(function(data) {
const activeIp = data[0][0].data[0];
if(activeIp == currentIp) {
return;
} else {
const newRecord = zone.record('a', {
name: domainName,
ttl: managedZone.ttl,
data: currentIp,
});
const oldRecord = zone.record('a', {
name: domainName,
ttl: managedZone.ttl,
data: activeIp,
});
const diff = {
add: newRecord,
delete: oldRecord,
}
zone.createChange(diff).then(function(data) {
const token = config.slack.token;
const channel = config.slack.channel;
const username = config.slack.username;
const iconEmoji = config.slack.iconEmoji;
rp({
uri: 'https://slack.com/api/chat.postMessage',
method: 'POST',
form: {
token: token,
channel: channel,
username: username,
icon_emoji: iconEmoji,
text: `Updated A record for ${domainName} from ${activeIp} to ${currentIp}`,
},
json: true,
});
});
}
});
}
}
});