跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[程序员] Cloudflare 优选 IP➕DnsPod 的 DDNS 自动切换

发表于

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);
  }
});

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.