开源的Cloudflare优选IP+DnsPod的解析自动切换

Cloudflare优选IP 每隔15分钟更新(IPv6+IPv4)
项目地址:https://github.com/uxiaohan/CloudflareIP-dnspod-ddns

实现逻辑及局部代码
获取优选IP

const res = await fetch("https://api.vvhan.com/tool/cf_ip");
const data = await res.json();
// 数据格式
// {
//     "success": true,
//     "data": {
//         "v4": {
//             "CM": [],
//             "CU": [],
//             "CT": []
//         },
//         "v6": {
//             "CM": [],
//             "CU": [],
//             "CT": []
//         }
//     }
// }

取优选IP中的最优选 (延迟比较)

// 取最优选IP IPv4
const CM_IP_V4 = data.v4.CM.reduce((minItem, currentItem) => {
  return currentItem.latency < minItem.latency ? currentItem : minItem;
}, data.v4.CM[0]);
const CU_IP_V4 = data.v4.CU.reduce((minItem, currentItem) => {
  return currentItem.latency < minItem.latency ? currentItem : minItem;
}, data.v4.CU[0]);
const CT_IP_V4 = data.v4.CT.reduce((minItem, currentItem) => {
  return currentItem.latency < minItem.latency ? currentItem : minItem;
}, data.v4.CT[0]);
const DNS_DATA_V4 = { 移动: CM_IP_V4.ip, 联通: CM_IP_V4.ip, 电信: CU_IP_V4.ip, 默认: CT_IP_V4.ip };

// 取最优选IP IPv6
const CM_IP_V6 = data.v6.CM.reduce((minItem, currentItem) => {
  return currentItem.latency < minItem.latency ? currentItem : minItem;
}, data.v6.CM[0]);
const CU_IP_V6 = data.v6.CU.reduce((minItem, currentItem) => {
  return currentItem.latency < minItem.latency ? currentItem : minItem;
}, data.v6.CU[0]);
const CT_IP_V6 = data.v6.CT.reduce((minItem, currentItem) => {
  return currentItem.latency < minItem.latency ? currentItem : minItem;
}, data.v6.CT[0]);
const DNS_DATA_V6 = { 移动: CM_IP_V6.ip, 联通: CM_IP_V6.ip, 电信: CU_IP_V6.ip, 默认: CT_IP_V6.ip };

 

循环替换优选IP

DnsPodDomainList.forEach(async i => {
  try {
    const res = await client.ModifyRecord({ Domain, RecordType: i.Type, RecordLine: "", RecordLineId: i.LineId, Value: i.Type == "A" ? DNS_DATA_V4[i.Line] : DNS_DATA_V6[i.Line], RecordId: i.RecordId, SubDomain });
    console.log(res);
  } catch (error) {
    console.log(error);
  }
});

 

附注:Node18+,不然不支持fetch
你也可以更改为:axios
CD 进入目录执行命令pnpm add axios
同时修改文件:controller.js第1-15行代码如下:

const moment = require("moment");

const axios = require("axios");
// 配置
const { secretId, secretKey, Domain, SubDomain } = require("./config");
const tencentcloud = require("tencentcloud-sdk-nodejs-dnspod");
// 更新 Cloudflare 优选IP开始-----------------------------------------------------------------------------------------------------
const updateCloudflareIp = async () => {
  const res = await axios.get("https://api.vvhan.com/tool/cf_ip");
  const data = res.data;
  if (!data.success) {
    console.log("\x1b[91m%s\x1b[0m", "更新 Cloudflare 优选IP失败");
    return "更新 Cloudflare 优选IP失败";
  }
  return await updateTencentDns(data.data);
};
// 更新 Cloudflare 优选IP结束-----------------------------------------------------------------------------------------------------

 

文章版权归原作者所有或来自互联网,未经允许请勿转载。如有侵权请联系我删除,谢谢!
THE END
分享
二维码
打赏
< <上一篇
下一篇>>
文章目录
关闭
目 录