-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a5c39f0
Showing
5 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
settings.json | ||
sample_query.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |