Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarley committed Feb 12, 2012
0 parents commit a5c39f0
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
settings.json
sample_query.js
48 changes: 48 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var fs = require("fs"),
Lookup = require("./lib/lookup"),
DNSimple = require("./lib/dnsimple");

var settings = JSON.parse(fs.readFileSync("./settings.json"));

var lookup = new Lookup(settings.currentIP);
var dns = new DNSimple(settings);

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);
}).on('match', function(current_ip, public_id) {
writeln("IP addresses match. No need to update.");
});

dns.on('updated', function(data) {
writeln("Domain IP updated successfully.");

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

writeln("Updating local settings...");

fs.writeFile("./settings.json", JSON.stringify(settings), function(err, fd) {
if(err) throw err;
});

writeln("Local settings updated.");

}).on('error', function(e) {
writeln("Error: " + e);
});

function writeln(str) {
process.stdout.write(str + "\n");
}

lookup.compare();

// Uncomment the below to run continuously
// current set to run once an hour
//setInterval(lookup.compare, 3600000);

63 changes: 63 additions & 0 deletions lib/dnsimple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var https = require("https");
var events = require("events");

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

DNSimple.prototype = new events.EventEmitter();

DNSimple.prototype.token = function() {
return dnsimple.settings.username + ":" + dnsimple.settings.token
}

DNSimple.prototype.update = function(new_ip) {
var dnsimple = this;

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

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

dnsimple.put(path, record);
}

DNSimple.prototype.put = function(path, data) {
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 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);
}
});
});

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

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

module.exports = DNSimple
41 changes: 41 additions & 0 deletions lib/lookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var http = require("http");
var events = require("events");

var Lookup = function(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);
});
});

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

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;
7 changes: 7 additions & 0 deletions settings.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"currentIP": "0.0.0.0",
"username": "[email protected]",
"token": "your api token",
"domain": "example.com",
"recordID": "123456"
}

0 comments on commit a5c39f0

Please sign in to comment.