Skip to content

Instantly share code, notes, and snippets.

@subhaze
Last active November 29, 2018 15:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subhaze/e4486684f5a838e7828528a11aa7c0e3 to your computer and use it in GitHub Desktop.
Save subhaze/e4486684f5a838e7828528a11aa7c0e3 to your computer and use it in GitHub Desktop.
Azure function to update IP on Cloudflare DNS; Usefull for DSM DDNS updates.
// URL set in Synology would look something like this:
// https://<YOUR_SUB_DOMAIN>.azurewebsites.net/api/HttpTriggerJS1?hostname=__HOSTNAME__&ip=__MYIP__&zoneid=__USERNAME__&key=__PASSWORD__&email=<YOUR_CF_EMAIL_LOGIN>&dnsidentifier=<YOUR_DNS_ID_FROM_CF>&recordtype=A
module.exports = function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
var http = require('https');
var hostname = req.query.hostname;
var ip = req.query.ip;
var zoneid = req.query.zoneid;
var key = req.query.key;
var email= req.query.email;
var dnsIdentifier = req.query.dnsidentifier;
var recordType = req.query.recordtype;
var bodyString = JSON.stringify({
type: recordType,
name: hostname,
content: ip,
proxied: true
});
var headers = {
'Content-Type': 'application/json',
'X-Auth-Email': email,
'X-Auth-Key': key,
'method': 'PUT'
};
var options = {
host: 'api.cloudflare.com',
path: '/client/v4/zones/'+ zoneid +'/dns_records/' + dnsIdentifier,
method: 'PUT',
headers: headers
};
var callback = function (response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
context.log(str);
context.res = {
status: 200, /* Defaults to 200 */
body: "success"
};
context.done();
});
};
var r = http.request(options, callback);
r.write(bodyString);
r.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment