Last active
November 29, 2018 15:08
-
-
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.
This file contains hidden or 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
// 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