Permalink
Browse files

Added url replacement

  • Loading branch information...
1 parent baea204 commit 27e8acbe0616de16f75decd9c9da33ed7380713e @kersten committed Jul 18, 2012
Showing with 82 additions and 74 deletions.
  1. +7 −7 README.md
  2. +74 −65 lib/dyndns-client.js
  3. +1 −2 package.json
View
@@ -23,16 +23,16 @@ Usage
-----
var DynDNSClient = require("node-dyndns-client"),
dyndns = new DynDNSClient({
- url: "http://[USERNAME]:[PASSWORD]@members.dyndns.org/nic/update?hostname=[HOSTNAME]&myip=[IP]",
- hostname: [
+ url : "http://[USERNAME]:[PASSWORD]@members.dyndns.org/nic/update?hostname=[HOSTNAME]&myip=[IP]",
+ hostname : [
"test.dyndns.org",
"customtest.dyndns.org"
],
- username: 'username',
- password: 'password',
- interface: 'ppp0',
- protocol: 'ipv4',
- check: 60
+ username : 'username',
+ password : 'password',
+ network_interface : 'ppp0',
+ protocol : 'ipv4',
+ check : 60
});
dyndns.on('IP:changed', function (newIP, oldIP) {
View
@@ -1,157 +1,166 @@
var events = require("events"),
+ fs = require("fs"),
http = require("http"),
interfaces = require("os").networkInterfaces(),
+ package_json = JSON.parse(fs.readFileSync('package.json', 'utf8')),
url = require("url");
function DynDNSClient ( options ) {
+ "use strict";
+
events.EventEmitter.call(this);
-
+
this.options = options;
-
- if (typeof(this.options.hostname) == "object" && (this.options.hostname instanceof Array)) {
- this.options.hostname = this.options.hostname.join(",");
+
+ if ( typeof(this.options.hostname) === "object" && (this.options.hostname instanceof Array) ) {
+ this.options.hostname = this.options.hostname.join(",");
}
-
- if (this.options.protocol === undefined || this.options.protocol == "") {
+
+ if ( this.options.protocol === undefined || this.options.protocol === "" ) {
this.options.protocol = "ipv4";
}
-
+
this.startMonitoring();
- delete(options);
}
DynDNSClient.super_ = events.EventEmitter;
DynDNSClient.prototype = Object.create(events.EventEmitter.prototype, {
constructor: {
- value: DynDNSClient,
+ value : DynDNSClient,
enumerable: false
}
});
DynDNSClient.prototype.startMonitoring = function () {
- this.interval = setInterval(function (thisObj) {
+ "use strict";
+
+ this.interval = setInterval(function ( thisObj ) {
thisObj.checkIP();
}, this.options.interval * 1000 || 60000, this);
-
+
this.checkIP();
};
DynDNSClient.prototype.checkIP = function () {
+ "use strict";
+
var self = this,
- interface = null;
-
- if (this.options.interface === undefined || this.options.interface == "external") {
- http.get("http://api.externalip.net/ip/", function(res) {
- res.on('data', function (data) {
+ network_interface = null;
+
+ if ( this.options.network_interface === undefined || this.options.network_interface === "external" ) {
+ http.get("http://api.externalip.net/ip/", function ( res ) {
+ res.on('data', function ( data ) {
var ip = data.toString();
-
- if (self.curIP !== undefined && self.curIP != ip) {
+
+ if ( self.curIP !== undefined && self.curIP != ip ) {
self.ipChanged(ip, self.curIP);
- } else if (self.curIP === undefined || self.curIP == "") {
+ } else if ( self.curIP === undefined || self.curIP == "" ) {
self.curIP = ip;
self.update();
}
-
- delete(data);
});
});
} else {
- if (interfaces[this.options.interface] !== undefined) {
- interfaces[this.options.interface].forEach(function (obj) {
- if (obj.family == this.options.protocol) {
- interface = obj;
+ if ( interfaces[this.options.network_interface] !== undefined ) {
+ interfaces[this.options.network_interface].forEach(function ( obj ) {
+ if ( obj.family === this.options.protocol ) {
+ network_interface = obj;
}
-
- delete(obj);
});
-
- if (this.curIP === undefined || this.curIP == "") {
- this.curIP = interface.address;
- } else if (this.curIP !== undefined && this.curIP != interface.address) {
- this.ipChanged(interface.address, this.curIP);
+
+ if ( this.curIP === undefined || this.curIP === "" ) {
+ this.curIP = network_interface.address;
+ } else if ( this.curIP !== undefined && this.curIP !== network_interface.address ) {
+ this.ipChanged(network_interface.address, this.curIP);
this.update();
}
} else {
- this.emit("error", "Networinterface " + this.options.interface + " not found.");
+ this.emit("error", "Networinterface " + this.options.network_interface + " not found.");
}
}
-
- delete(interface);
};
DynDNSClient.prototype.ipChanged = function ( newIP, oldIP ) {
+ "use strict";
+
this.curIP = newIP;
this.emit('IP:changed', [ newIP, oldIP ]);
-
+
this.update();
- delete(newIP, oldIP);
};
DynDNSClient.prototype.interfaceStateChanged = function () {
+ "use strict";
+
this.emit('interface:' + this.INTERFACE_STATE);
};
DynDNSClient.prototype.update = function () {
+ "use strict";
+
var self = this,
- service = url.parse(this.options.url);
-
+ service = url.parse(this.options.url
+ .replace("[USERNAME]", this.options.username)
+ .replace("[PASSWORD]", this.options.password)
+ .replace("[IP]", this.curIP)
+ .replace("[HOSTNAME]", this.options.hostname)
+ );
+
http.get({
- host: service.hostname,
- port: service.port,
- path: "/nic/update?hostname=" + this.options.hostname + "&myip=" + this.curIP + "&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG",
+ host : service.hostname,
+ port : service.port,
+ path : service.path,
headers: {
- "User-Agent": "node-dyndns-client dyndns updater v0.1"
+ "User-Agent": "node-dyndns-client a dyndns updater " + package_json.version
},
- auth: this.options.username + ":" + this.options.password
- }, function(res) {
- res.on('data', function (data) {
- switch (data.toString().split(" ")[0]) {
+ auth : service.auth
+ }, function ( res ) {
+ res.on('data',function ( data ) {
+ switch ( data.toString().split(" ")[0] ) {
case "badauth":
clearInterval(self.interval);
self.emit("error", "Bad username or password.");
break;
-
+
case "notfqdn":
clearInterval(self.interval);
- self.emit("error", "The hostname specified is not a fully-qualified domain name (not in the form hostname.dyndns.org or domain.com).");
+ self.emit("error",
+ "The hostname specified is not a fully-qualified domain name (not in the form hostname.dyndns.org or domain.com).");
break;
-
+
case "nohost":
clearInterval(self.interval);
- self.emit("error", "The hostname specified does not exist in this user account (or is not in the service specified in the system parameter).");
+ self.emit("error",
+ "The hostname specified does not exist in this user account (or is not in the service specified in the system parameter).");
break;
-
+
case "numhost":
clearInterval(self.interval);
- self.emit("error", "Too many hosts (more than 20) specified in an update. Also returned if trying to update a round robin (which is not allowed).");
+ self.emit("error",
+ "Too many hosts (more than 20) specified in an update. Also returned if trying to update a round robin (which is not allowed).");
break;
-
+
case "abuse":
clearInterval(self.interval);
self.emit("error", "The hostname specified is blocked for update abuse.");
break;
-
+
case "dnserr":
case "911":
clearInterval(self.interval);
self.emit("error", "Bad username or password.");
break;
-
+
case "good":
case "nochg":
self.emit("update:success");
break;
}
-
- delete(data);
- }).on("error", function (data) {
- clearInterval(self.interval);
- self.emit("error", "Some error occured during IP update: " + data.toString());
- delete(data);
- });
+ }).on("error", function ( data ) {
+ clearInterval(self.interval);
+ self.emit("error", "Some error occured during IP update: " + data.toString());
+ });
});
-
- delete(service);
};
module.exports = DynDNSClient;
View
@@ -1,5 +1,5 @@
{
- "version" : "0.1.1",
+ "version" : "0.2",
"name" : "node-dyndns-client",
"description": "A dynamic dns IP updater",
"homepage" : "http://github.com/kersten/node-dyndns-client",
@@ -9,7 +9,6 @@
"url" : "https://github.com/kersten/node-dyndns-client"
},
"bugs" : {
- "email": "kerstenk@gmail.com",
"url" : "http://github.com/kersten/node-dyndns-client/issues"
},
"engines" : {

0 comments on commit 27e8acb

Please sign in to comment.