-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtransip-dyndns.js
96 lines (87 loc) · 2.69 KB
/
transip-dyndns.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
#!/usr/bin/env node
var TransIP = require('transip'),
request = require('request-promise'),
Promise = require('bluebird'),
bunyan = require('bunyan'),
fs = require('fs'),
_ = require('lodash'),
config = require('./config.js');
// Check for the environment variables
var TRANSIP_LOGIN = config.get('transip.login')
var PRIVATE_KEY_LOCATION = config.get('transip.privateKeyPath');
var DOMAIN = config.get('domain');
var DNS_RECORD = config.get('dnsRecord');
var LOG_LOCATION = config.get('logLocation');
// Load privateKeyFile contents
var TRANSIP_PRIVATE_KEY= fs.readFileSync(PRIVATE_KEY_LOCATION, {encoding:'utf-8'});
if(!TRANSIP_PRIVATE_KEY) {
console.log('PrivateKey cannot be read.');
process.exit(1);
}
// Setup file logger
var log = bunyan.createLogger({
name: 'transip-dyndns',
streams: [
{
level: 'info',
path: LOG_LOCATION
}
]
});
// Create TRANSIP Instance
var transipInstance = new TransIP(TRANSIP_LOGIN, TRANSIP_PRIVATE_KEY);
Promise.all([checkCurrentDns(), checkCurrentWanIP()])
.spread(function(currentDNSEntries, currentIP) {
_.forEach(currentDNSEntries, function(dnsEntry) {
if(dnsEntry.name === DNS_RECORD) {
if(dnsEntry.content === currentIP) {
log.info('Nothing changed.')
} else {
log.info('WAN Has changed to: ', currentIP);
dnsEntry.content = currentIP;
return updateDnsRecords(currentDNSEntries)
.then(function() {
log.info('DNS Record has been updated.');
});
}
}
});
})
.catch(function(err) {
// Log error and inform that the check failed.
log.error('Something went wrong: ', err.message);
});
// Get current value of the DNS record on Domain
function checkCurrentDns() {
return new Promise(function(resolve, reject) {
return transipInstance.domainService.getDomainNames()
.then(function(domains) {
_.forEach(domains, function(domain) {
if(domain === DOMAIN) {
return transipInstance.domainService.getInfo(domain)
.then(function(domain) {
var dnsEntries = domain.dnsEntries;
resolve(dnsEntries);
});
}
});
});
})
}
// Update DNS records
function updateDnsRecords(currentDNSEntries) {
return transipInstance.domainService.setDnsEntries(DOMAIN, {
'item': currentDNSEntries
});
}
// Retrieve current WAN ip
function checkCurrentWanIP() {
return request('http://icanhazip.com/')
.then(function(body) {
var ip = body.trim();
return ip;
})
.catch(function(err) {
throw new Error('Error while loading icanhazip. \n' + err.message);
});
}