forked from jcarley/node-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
95 lines (69 loc) · 2.53 KB
/
app.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
var fs = require("fs"),
appPath = require("path").dirname(process.argv[1]),
Lookup = require(appPath + "/lib/lookup"),
DNSimple = require(appPath + "/lib/dnsimple");
var settings = JSON.parse(fs.readFileSync(appPath + "/settings.json"));
var lookup = new Lookup(settings.currentIP);
var dns = new DNSimple(settings);
writeln("App Directory: " + appPath);
// the update event is fired when there is a mismatch of
// IP addresses
lookup.on('update', function(public_ip) {
writeln("IP address mismatch:");
writeln("\tCurrent IP: " + settings.currentIP);
writeln("\t Public IP: " + public_ip);
writeln("Updating record...");
dns.update(public_ip);
})
// the match event is called when the current and the public
// IP addresses match
.on('match', function (current_ip, public_id) {
writeln("IP addresses match. No need to update.");
});
// the updated event is raised after the public IP
// was successfully updated
dns.on('updated', function(data) {
writeln("Domain IP updated successfully.");
var parentRecord = JSON.parse(data);
settings.currentIP = parentRecord.record.content;
writeln("Updating local settings...");
// we have to save the new public IP to a local file
// so that we can do a comparison on the next run
fs.writeFile(appPath + "/settings.json", JSON.stringify(settings, undefined, 4), function (err, fd) {
if (err) throw err;
});
writeln("Local settings updated successfully.");
}).on('error', function(e) {
writeln("Error: " + e);
});
//lookup.compare();
// Uncomment the below to run continuously
// current set to run once an hour
checkIpStatus();
setInterval(checkIpStatus, settings.checkInterval);
function checkIpStatus() {
printDate();
lookup.compare();
}
function writeln(str) {
process.stdout.write(str + "\n");
}
function printDate() {
writeln("//------------------------------------------------------//");
writeln("//----------------" + getDateTime() + "-----------------//");
}
function getDateTime() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return year + "/" + month + "/" + day + " - " + hour + ":" + min + ":" + sec;
}