diff --git a/app.js b/app.js index db64c68..11eda81 100644 --- a/app.js +++ b/app.js @@ -4,14 +4,14 @@ var fs = require("fs"), var settings = JSON.parse(fs.readFileSync("./settings.json")); -var lookup = new Lookup(settings.currentIP); +var lookup = new Lookup(settings.CURRENT_IP); var dns = new DNSimple(settings); // 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("\tCurrent IP: " + settings.CURRENT_IP); writeln("\t Public IP: " + public_ip); writeln("Updating record..."); @@ -29,7 +29,7 @@ dns.on('updated', function(data) { writeln("Domain IP updated successfully."); var parentRecord = JSON.parse(data); - settings.currentIP = parentRecord.record.content; + settings.CURRENT_IP = parentRecord.record.content; writeln("Updating local settings..."); diff --git a/lib/dnsimple.js b/lib/dnsimple.js index 864a8d2..583f087 100644 --- a/lib/dnsimple.js +++ b/lib/dnsimple.js @@ -13,41 +13,32 @@ var DNSimple = function(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 -} - -// updates the IP of the domain specified in the -// settings file +// updates the IP of the domain specified in the settings file DNSimple.prototype.update = function(new_ip) { var dnsimple = this; - var path = "/domains/" + dnsimple.settings.domain + "/records/" + dnsimple.settings.recordID; + var path = `/v2/${dnsimple.settings.ACCOUNT_ID}/zones/${dnsimple.settings.DNS_ZONE_ID}/records/${dnsimple.settings.DNS_RECORD_ID}` var record = JSON.stringify({ - record: { content: new_ip, ttl: 86400 - } }); - dnsimple.put(path, record); + dnsimple.patch(path, record); } -// put is a convience method for sending an HTTP PUT -// statement. -DNSimple.prototype.put = function(path, data) { +// patch is a convience method for sending an HTTP PATCH statement. +DNSimple.prototype.patch = function(path, data) { dnsimple = this; var options = { - host : "dnsimple.com", + host : "api.dnsimple.com", path: path, - method : "PUT", + method : "PATCH", headers : { + "Authorization": `Bearer ${dnsimple.settings.AUTH_TOKEN}`, "Accept" : "application/json", "Content-Type": "application/json", - "X-DNSimple-Token" : dnsimple.token(), "Content-Length" : data.length } }; @@ -56,9 +47,9 @@ DNSimple.prototype.put = function(path, data) { var status = res.statusCode; res.on("data", function(d) { if( status == 200 ) { - dnsimple.emit("updated", d); + dnsimple.emit("updated"); } else { - dnsimple.emit("error", d); + dnsimple.emit("error"); } }); }); diff --git a/settings.json.example b/settings.json.example index 870422f..69f98a7 100644 --- a/settings.json.example +++ b/settings.json.example @@ -1,7 +1,7 @@ { - "currentIP": "0.0.0.0", - "username": "you@example.com", - "token": "your api token", - "domain": "example.com", - "recordID": "123456" -} \ No newline at end of file + "CURRENT_IP": "1.1.1.1", + "ACCOUNT_ID": 123456, + "DNS_ZONE_ID": "domain.com", + "DNS_RECORD_ID": 123456, + "AUTH_TOKEN": "ABCXYZ" +}