Cloudflare 优选 IP➕DnsPod 的 DDNS 自动切换

23 天前
 uxiaohan

CloudFlare 是一个非常优秀的 CDN 服务,但是 CloudFlare 也有一个大的毛病——大陆访问速度很慢。国外很多网站都在使用 Cloudflare CDN ,但分配给中国内地访客的 IP 并不友好(延迟高、丢包多、速度慢)。 虽然 Cloudflare 公开了所有 IP 段,但想要在这么多 IP 中找到适合自己的,怕是要累死,于是就有了这个脚本。

Cloudflare 优选 IP 每隔 15 分钟更新(IPv6+IPv4)

项目地址

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);
  }
});
2028 次点击
所在节点    程序员
10 条回复
kwkwkkk
23 天前
试了下挺好用的,已 star
chenxuuu
23 天前
比较好奇,cf 不是要配置 ns 服务器的吗,这个难道是 cname 方式接入的,所以可以直接用 ip 指向 cf ?
JensenQian
23 天前
@chenxuuu #2 可以 saas 实现 cname 接入的
icaolei
23 天前
已 Star ,晚上试用一下看看。
gaobh
23 天前
做优选的项目太多了,主要是慢慢都不维护了
uxiaohan
23 天前
@chenxuuu Cloudflare Pages 可以 cname 解析,Cloudflare 域名停放后,可以 Saas 回源进行 Ip 优选
zhiyu1998
23 天前
我说怎么看着地址熟悉 有幸用过 han 哥的公益 API
uxiaohan
22 天前
@zhiyu1998 🤝
MoonWalker
22 天前
感谢....api 稳定吗,能否私有化部署这个 api?
dufu1991
22 天前
我是新手,不知道怎么用。我的静态资源再 GitHub pages 上,是不是无法使用这个项目优选?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1068183

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX