Skip to content

Instantly share code, notes, and snippets.

@wouterd
Created May 8, 2014 21:35
Show Gist options
  • Save wouterd/5aded99b0bbdf9168614 to your computer and use it in GitHub Desktop.
Save wouterd/5aded99b0bbdf9168614 to your computer and use it in GitHub Desktop.
Dyndns replacement using Cloudflare, run `npm install`, then `node update-cloudflare.js`
{
"name": "dynamic-ip",
"version": "0.0.1",
"description": "A tool to update cloudflare with my dynamic IP",
"main": "update.js",
"dependencies": {
"fn.js": "~0.7.0",
"sequence": "~3.0.0",
"restler": "~3.2.2"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Wouter Danes",
"license": "Apache v2.0"
}
var http = require('http');
var https = require('https');
var querystring = require('querystring');
var fn = require('fn.js');
var Sequence = require('sequence').Sequence;
var rest = require('restler');
var cloudFlareConfig = {
tkn : '[cloudflare api key]',
email : '[cloudflare email account]',
domain : '[domain]',
targetName : '[host in domain to update]',
apiUrl : 'https://www.cloudflare.com/api_json.html'
};
function obtainPublicIp(next, hostname) {
rest
.get('http://ip4.telize.com/')
.on('complete', function(ip) {
console.log('Current IP: ' + ip.trim());
next(ip.trim(), hostname);
});
};
function obtainHostInfo(next, wanIp, hostname) {
var query = {
tkn : cloudFlareConfig.tkn,
email : cloudFlareConfig.email,
z : cloudFlareConfig.domain,
a : 'rec_load_all'
};
rest
.get(cloudFlareConfig.apiUrl, { query : query })
.on('complete', function (data) {
console.log('Looking for record for ' + hostname);
var infos = fn.filter( function ( info ) {
return hostname == info.display_name;
}, data.response.recs.objs);
console.log('found host info... IP in cloudflare: ' + infos[0].content);
next(wanIp, infos[0]);
});
};
function checkIpHasChanged(next, wanIp, hostInfo) {
if (wanIp == hostInfo.content) {
console.log("IP has not changed");
return;
} else {
console.log('IP Has changed, old = ' + hostInfo.content + ', new = ' + wanIp);
next(wanIp, hostInfo);
}
}
function setNewWanIp(next, wanIp, hostInfo) {
console.error("Setting New WAN IP: " + wanIp);
var query = {
tkn : cloudFlareConfig.tkn,
email : cloudFlareConfig.email,
z : cloudFlareConfig.domain,
a : 'rec_edit',
type : 'A',
id : hostInfo.rec_id,
name : hostInfo.display_name,
content : wanIp,
ttl : '1',
service_mode : '0'
};
rest
.get(cloudFlareConfig.apiUrl, { query : query })
.on('complete', function (data) {
console.error(data);
});
next();
}
var sequence = Sequence.create();
sequence
.then(function (next) {
next(cloudFlareConfig.targetName)
})
.then(obtainPublicIp)
.then(obtainHostInfo)
.then(checkIpHasChanged)
.then(setNewWanIp);
@wouterd
Copy link
Author

wouterd commented May 8, 2014

Dyndns replacement using Cloudflare, run npm install, then node update-cloudflare.js.
Change the cloudFlareConfig object at the top of the update-cloudflare.js file with your cloudflare details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment