Last active
March 23, 2018 03:29
-
-
Save peterlee0127/ad4ddac11ece437d108f23ac09321726 to your computer and use it in GitHub Desktop.
Cloudflare DDNS node
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
'use strict'; | |
const getIP = require('external-ip')(); | |
const request = require('request'); | |
const email = 'email'; | |
const key = 'cloudflare-key'; | |
// This program update 'type A record' to 'external IP address' of your domain in cloudflare. | |
function getDNSInfo(domain) { | |
var info = { | |
a:'rec_load_all', | |
tkn:key, | |
email:email, | |
z:domain | |
} | |
console.log(info); | |
request.post({url:'https://www.cloudflare.com/api_json.html',form:info},function(error,response,body){ | |
var res = JSON.parse(body).response.recs.objs; | |
updateRecord(res); | |
}); | |
} | |
var IP = '1.1.1.1'; | |
getIP((err, ip) => { | |
if (err) { | |
// every service in the list has failed | |
throw err; | |
} | |
IP = ip | |
console.log(ip); | |
getDNSInfo('//domain name'); | |
}); | |
function updateRecord(records) { | |
for(var i=0;i<records.length;i++) { | |
var record = records[i]; | |
if(record.type!='A') { | |
return; | |
} | |
var info = { | |
a:'rec_edit', | |
tkn:key, | |
email:email, | |
z:record.zone_name, | |
id:record.rec_id, | |
type:record.type, | |
name:record.name, | |
ttl:1, | |
content:IP | |
} | |
request.post({url:'https://www.cloudflare.com/api_json.html',form:info},function(error,response,body){ | |
console.log(body); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment