Skip to content

Instantly share code, notes, and snippets.

@spacelatte
Created August 1, 2020 21:37
Show Gist options
  • Save spacelatte/f6dd2e1dcb5f615ec631fbb8405ebf6e to your computer and use it in GitHub Desktop.
Save spacelatte/f6dd2e1dcb5f615ec631fbb8405ebf6e to your computer and use it in GitHub Desktop.
cloudflare ddns dynamic dns script (from: https://notryan.com/code/net/cloudflare-ddns.js)
#!/usr/bin/env node
const EMAIL = "[email protected]";
const X_AUTH_KEY = "ABCDE";
const ZONE = "ABCDE";
const os = require("os");
const fetch = require("node-fetch");
const cf = require("cloudflare")({
email: EMAIL,
key: X_AUTH_KEY
});
(async function() {
const record_name = `${os.hostname}.example.com`;
const response = await fetch("http://ifconfig.co/ip");
const ip = (await response.text()).trim();
const records = (await cf.dnsRecords.browse(ZONE)).result;
const id = (function() {
for (let record of records) {
if (record.name == record_name)
return record.id;
}
})();
if (id === undefined) {
console.log(await cf.dnsRecords.add(ZONE, {
type: "A",
name: record_name,
content: ip
}));
} else {
console.log(await cf.dnsRecords.edit(ZONE, id, {
type: "A",
name: record_name,
content: ip
}));
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment