V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
uxiaohan
V2EX  ›  程序员

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

  •  
  •   uxiaohan ·
    uxiaohan · 23 天前 · 2027 次点击

    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);
      }
    });
    
    10 条回复    2024-08-28 10:12:52 +08:00
    kwkwkkk
        1
    kwkwkkk  
       23 天前
    试了下挺好用的,已 star
    chenxuuu
        2
    chenxuuu  
       23 天前
    比较好奇,cf 不是要配置 ns 服务器的吗,这个难道是 cname 方式接入的,所以可以直接用 ip 指向 cf ?
    JensenQian
        3
    JensenQian  
       23 天前   ❤️ 1
    @chenxuuu #2 可以 saas 实现 cname 接入的
    icaolei
        4
    icaolei  
       23 天前
    已 Star ,晚上试用一下看看。
    gaobh
        5
    gaobh  
       23 天前
    做优选的项目太多了,主要是慢慢都不维护了
    uxiaohan
        6
    uxiaohan  
    OP
       23 天前   ❤️ 1
    @chenxuuu Cloudflare Pages 可以 cname 解析,Cloudflare 域名停放后,可以 Saas 回源进行 Ip 优选
    zhiyu1998
        7
    zhiyu1998  
       23 天前 via iPhone
    我说怎么看着地址熟悉 有幸用过 han 哥的公益 API
    uxiaohan
        8
    uxiaohan  
    OP
       22 天前
    @zhiyu1998 🤝
    MoonWalker
        9
    MoonWalker  
       22 天前
    感谢....api 稳定吗,能否私有化部署这个 api?
    dufu1991
        10
    dufu1991  
       22 天前
    我是新手,不知道怎么用。我的静态资源再 GitHub pages 上,是不是无法使用这个项目优选?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1553 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 17:03 · PVG 01:03 · LAX 10:03 · JFK 13:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.