V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
uxiaohan
V2EX  ›  程序员

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

  •  
  •   uxiaohan ·
    uxiaohan · Aug 27, 2024 · 4532 views
    This topic created in 622 days ago, the information mentioned may be changed or developed.

    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);
      }
    });
    
    11 replies    2026-04-13 09:14:20 +08:00
    kwkwkkk
        1
    kwkwkkk  
       Aug 27, 2024
    试了下挺好用的,已 star
    chenxuuu
        2
    chenxuuu  
       Aug 27, 2024
    比较好奇,cf 不是要配置 ns 服务器的吗,这个难道是 cname 方式接入的,所以可以直接用 ip 指向 cf ?
    JensenQian
        3
    JensenQian  
       Aug 27, 2024   ❤️ 1
    @chenxuuu #2 可以 saas 实现 cname 接入的
    icaolei
        4
    icaolei  
       Aug 27, 2024
    已 Star ,晚上试用一下看看。
    Solix
        5
    Solix  
       Aug 27, 2024
    做优选的项目太多了,主要是慢慢都不维护了
    uxiaohan
        6
    uxiaohan  
    OP
       Aug 27, 2024   ❤️ 1
    @chenxuuu Cloudflare Pages 可以 cname 解析,Cloudflare 域名停放后,可以 Saas 回源进行 Ip 优选
    zhiyu1998
        7
    zhiyu1998  
       Aug 27, 2024 via iPhone
    我说怎么看着地址熟悉 有幸用过 han 哥的公益 API
    uxiaohan
        8
    uxiaohan  
    OP
       Aug 28, 2024
    @zhiyu1998 🤝
    MoonWalker
        9
    MoonWalker  
       Aug 28, 2024
    感谢....api 稳定吗,能否私有化部署这个 api?
    dufu1991
        10
    dufu1991  
       Aug 28, 2024
    我是新手,不知道怎么用。我的静态资源再 GitHub pages 上,是不是无法使用这个项目优选?
    dreamcog
        11
    dreamcog  
       Apr 13
    @uxiaohan 能详细请教一下吗?可以不
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1359 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 60ms · UTC 17:12 · PVG 01:12 · LAX 10:12 · JFK 13:12
    ♥ Do have faith in what you're doing.