Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
settings.json
sample_query.js
.idea/
92 changes: 65 additions & 27 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,95 @@
var fs = require("fs"),
Lookup = require("./lib/lookup"),
DNSimple = require("./lib/dnsimple");
appPath = require("path").dirname(process.argv[1]),
Lookup = require(appPath + "/lib/lookup"),
DNSimple = require(appPath + "/lib/dnsimple");

var settings = JSON.parse(fs.readFileSync("./settings.json"));
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...");
writeln("IP address mismatch:");
writeln("\tCurrent IP: " + settings.currentIP);
writeln("\t Public IP: " + public_ip);
writeln("Updating record...");

dns.update(public_ip);
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.");
});
.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.");
writeln("Domain IP updated successfully.");

var parentRecord = JSON.parse(data);
settings.currentIP = parentRecord.record.content;
var parentRecord = JSON.parse(data);
settings.currentIP = parentRecord.record.content;

writeln("Updating local settings...");
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("./settings.json", JSON.stringify(settings), function(err, fd) {
if(err) throw err;
});
// 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.");
writeln("Local settings updated successfully.");

}).on('error', function(e) {
writeln("Error: " + 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");
process.stdout.write(str + "\n");
}

lookup.compare();
function printDate() {
writeln("//------------------------------------------------------//");
writeln("//----------------" + getDateTime() + "-----------------//");
}

// Uncomment the below to run continuously
// current set to run once an hour
//setInterval(lookup.compare, 3600000);
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;
}
89 changes: 44 additions & 45 deletions lib/dnsimple.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,69 @@
// more of the API has been implemented.

var https = require("https");
var bl = require('bl');
var events = require("events");

var DNSimple = function(settings) {
this.settings = settings;
}
var DNSimple = function (settings) {
this.settings = settings;
};

DNSimple.prototype = new events.EventEmitter();

// Builds the token used for authenticating to the DNSimple API
DNSimple.prototype.token = function() {
return dnsimple.settings.username + ":" + dnsimple.settings.token
}
// Builds the apiToken used for authenticating to the DNSimple API
DNSimple.prototype.apiToken = function () {
return this.settings.username + ":" + this.settings.token
};

// updates the IP of the domain specified in the
// settings file
DNSimple.prototype.update = function(new_ip) {
var dnsimple = this;
var dnsimple = this;

var path = "/domains/" + dnsimple.settings.domain + "/records/" + dnsimple.settings.recordID;
var path = "/v1/domains/" + dnsimple.settings.domain + "/records/" + dnsimple.settings.recordID;

var record = JSON.stringify({
record: {
content: new_ip,
ttl: 86400
}
});
var record = {
record: {
content: new_ip
}
};

dnsimple.put(path, record);
}
dnsimple.put(path, record);
};

// put is a convience method for sending an HTTP PUT
// statement.
DNSimple.prototype.put = function(path, data) {
dnsimple = this;
var dnsimple = this;

var options = {
host : "dnsimple.com",
path: path,
method : "PUT",
headers : {
"Accept" : "application/json",
"Content-Type": "application/json",
"X-DNSimple-Token" : dnsimple.token(),
"Content-Length" : data.length
}
};
var options = {
host: "api.dnsimple.com",
path: path,
method: "PUT",
headers: {
"X-DNSimple-Token": dnsimple.apiToken(),
"Accept": "application/json",
"Content-Type": "application/json"
}
};

var req = https.request(options, function(res) {
var status = res.statusCode;
res.on("data", function(d) {
if( status == 200 ) {
dnsimple.emit("updated", d);
} else {
dnsimple.emit("error", d);
}
var req = https.request(options, function (res) {
var status = res.statusCode;

res.pipe(bl(function (err, data) {
if (err || status != 200)
dnsimple.emit("error", err);
else
dnsimple.emit("updated", data);
}));
});
});

req.on("error", function(e) {
dnsimple.emit("error", e);
});
req.on("error", function (e) {
dnsimple.emit("error", e);
});

req.write(data);
req.end();
}
req.write(JSON.stringify(data));
req.end();
};

module.exports = DNSimple
module.exports = DNSimple;
56 changes: 28 additions & 28 deletions lib/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@ var http = require("http");
var events = require("events");

var Lookup = function(current_ip) {
this.current_ip = current_ip;
}
this.current_ip = current_ip;
};

Lookup.prototype = new events.EventEmitter();

Lookup.prototype.compare = function() {
var options = {
host : "www.jsonip.com",
path : "/",
method : "GET"
};

var lookup = this;
var req = http.request(options, function(res) {
res.on("data", function(d) {
var public = JSON.parse(d);
lookup._compareIP(lookup.current_ip, public.ip);
var options = {
host: "www.jsonip.com",
path: "/",
method: "GET"
};

var lookup = this;
var req = http.request(options, function (res) {
res.on("data", function (d) {
var public = JSON.parse(d);
lookup._compareIP(lookup.current_ip, public.ip);
});
});
});

req.on("error", function(e) {
console.error(e);
});
req.on("error", function (e) {
console.error(e);
});

req.end();
}
req.end();
};

Lookup.prototype._compareIP = function(current, public) {
var lookup = this;
if(current !== public) {
lookup.emit('update', public);
} else {
lookup.emit('match', current, public);
}
}

module.exports = Lookup;
var lookup = this;
if (current !== public) {
lookup.emit('update', public);
} else {
lookup.emit('match', current, public);
}
};

module.exports = Lookup;
59 changes: 59 additions & 0 deletions node_modules/bl/.jshintrc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/bl/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions node_modules/bl/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading