1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
import {parse} from "url"; import http from "http"; import https from "https"; import fs from "fs";
var sub = ["域名前缀"]; var domain = ["主域名"]; var email = "你的邮箱"; var authKey = "Global API Key"
var auth = { "X-Auth-Email": email, "X-Auth-Key": authKey };
function log(txt) { fs.appendFile('ddns.txt', (txt + '\n'), err => { if (err) { console.error(err) return } }); }
const requestIpv6 = http.get({ 'host': 'api6.ipify.org', 'port': 80, 'path': '/', family: 6 }, function (resp) { if (resp.statusCode != 200) { log("non-200 response status code:", resp.statusCode); return; } else { resp.on('data', function (ip) { log('get ipv6 success:200'); log("My public IP address is: " + ip);
const url = parse("https://api.cloudflare.com/client/v4/zones/5926705fb3db4a7f866714500ef5a12c/dns_records"); if (url.hash) { url.path += url.hash; } url.method = "GET"; url.headers = auth;
const getDNSList = https.request(url, (DNSListRes) => { let DNSListBody = ''; DNSListRes.setEncoding("utf8"); DNSListRes.on("data", (resBody) => { DNSListBody += resBody; }); DNSListRes.on("end", () => { log("dns list success"); const getDNSList = JSON.parse(DNSListBody).result; for (let i = 0; i < sub.length; i++) { let domainTotal = sub[i] + '.' + domain; getDNSList.forEach((record) => { if (record.name === domainTotal && ip.toString() !== record.content) { makeRequest(ip, domainTotal, "PUT", record); } else if (record.name === domainTotal && ip.toString() === record.content) { log(domainTotal + ":ipv6 doesn't need update.") } }) if (getDNSList.filter((record) => record.name === domainTotal).length === 0) { makeRequest(ip, domainTotal, "POST", { id: '' }); }
}; });
}); getDNSList.on("error", (error) => { log(String(error)); }); getDNSList.end(); }); } }); requestIpv6.on('error', (error) => { log(String(error)); })
function makeRequest(ipA, domainTotalA, method, record) { let putBody = { "content": ipA.toString(), "name": domainTotalA, "proxied": false, "type": "AAAA" }
let urlPut = parse("https://api.cloudflare.com/client/v4/zones/5926705fb3db4a7f866714500ef5a12c/dns_records/" + record.id); urlPut.method = method; urlPut.headers = { ...auth, "Content-Type": "application/json" }; const putDNS = https.request(urlPut, (PutDNSRes) => { let putDNSBody = ''; PutDNSRes.setEncoding("utf8"); PutDNSRes.on("data", (putRes) => { putDNSBody += putRes; }); PutDNSRes.on("end", () => { log(JSON.stringify(JSON.parse(putDNSBody), null, '\t')); });
}); putDNS.on("error", (error) => { log(String(error)); }); putDNS.end(JSON.stringify(putBody)); }
|