使用CloudflareSpeedTest优选CloudflareCDN IP

很不幸,在我写下这篇文章的时候,CF优选的ipv4节点在国内已经开始出现被阻断的情况了。这一现象和Steam被阻断的时间大致相同,我想这应该是防火墙添加了新的规则。但是俗话说得好啊,只要思想不滑坡,办法总比问题多。劳动人民的力量总是无穷的,国内的ipv6在这一时期却表现出了不错的效果,接下来大致讲一下我使用CloudflareSpeedTest优选ip的方法。


我开始使用这个项目的契机是better-cloudflare-ip关闭,无奈之下发现了这个项目。项目用Golang编写,所以跨平台的特性让我能够很好的部署在不同的设备上。项目本身是输出为一个result.csv文件,所以我只需要用一个bash脚本自动将这个文件中已经优选好的ip更新到dns服务上即可。

CloudflareST测速脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash

cloudflare_v4(){
while true
do
ping -c 1 223.5.5.5
if [ $? -eq 0 ];
then
{
/usr/bin/CloudflareST -f /etc/dns/ip.txt -dd -n 4 -t 10 -p 0 -tll 10 -tp 443 -allip -o "/etc/dns/result4"
ip4=$(cat /etc/dns/result4 | awk 'NR==2{print}' | sed -e 's/,/ /g' | awk '{print $1}')
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/ZoneID/dns_records/dns记录ID" -H "X-Auth-Email: cloudflare账户邮箱" -H "X-Auth-Key: cloudflareAPI密钥" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"ipv4\",\"content\":\"${ip4}\"}"
}
else
{
sleep 1m
}
fi
done
}

cloudflare_v6(){
while true
do
ping6 -c 1 2400:3200::1
if [ $? -eq 0 ];
then
{
/usr/bin/CloudflareST -ipv6 -f /etc/dns/ipv6.txt -dd -n 4 -t 10 -p 0 -tll 10 -tp 443 -o "/etc/dns/result6"
ip6=$(cat /etc/dns/result6 | awk 'NR==2{print}' | sed -e 's/,/ /g' | awk '{print $1}')
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/ZoneID/dns_records/dns记录ID" -H "X-Auth-Email: cloudflare账户邮箱" -H "X-Auth-Key: cloudflareAPI密钥" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"AAAA\",\"name\":\"ipv6\",\"content\":\"${ip6}\"}"
}
else
{
sleep 1m
}
fi
done
}

sleep 5m
ping -c 1 223.5.5.5
until [ $? -eq 0 ]
do
{
sleep 10s
ping -c 1 223.5.5.5
}
done

cloudflare_v4&
cloudflare_v6&

这个脚本本来是我运行在软路由上的,直接作为/etc/init.d中的一部分,启动脚本如下。

openwrt自启动脚本

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/sh /etc/rc.common
# "new(er)" style init script
# Look at /lib/functions/service.sh on a running system for explanations of what other SERVICE_
# options you can use, and when you might want them.
START=99
SERVICE_USE_PID=1
SERVICE_WRITE_PID=1
SERVICE_DAEMONIZE=1

start() {
service_start /bin/bash /etc/dns/dns.sh
}

写的有点偷懒,但是整体思路是能跑就行,如果需要复制粘贴的话可以自行修改,这里只是提供一个思路。为了防止拨号还没有成功的时候就开始运行CloudflareST了,所以简单的通过ping来检测网络情况。最终运行的结果会将延迟最优的ipv4和ipv6节点同步到dns提供商上,在这里展示的是Cloudflare自己的dns服务,当然也可以使用其他的dns提供商,只需要自行撰写组件读取测速结果即可。

为什么不对节点测速

首先要阐述的是我所在的网络环境,深圳移动,这直接给我带来了CF香港节点直连延迟不到50ms的天然优势,大部分时候只要延迟低的节点就能跑满速度,不需要我再去操心测速问题,我只需要它尽可能的确保我能一直更新到延迟最好的ip并且不要给我正常使用网络带来太大的影响,在这种情况下仅仅对节点的延迟进行检测就绰绰有余了。
其次就是在目前的环境下,即便是完成了对节点的测速,依然无法改变节点被阻断的情况。在测速时可能能正常完成测速,但是开始使用优选完的ip时就会出现阻断现象。

这让我想到了 Steam、Github 的 SNI 干扰(不过这个是根据域名来超时 IP 的,不在乎是什么 IP。而 Cloudflare 这个是针对其CDN IP 的,暂时称为 IP 干扰吧),也是一但访问可能就会超时 3 分钟整,都是只局限于单个宽带用户(即只有你超时,其他人哪怕是邻居也完全不受影响)。

这个现象目前在ipv6上并没有出现,但是ipv6目前的平均延迟一般在200ms左右,对于原本用来优化ws或者grpc隧道的使用场景来说,体验比较差,但是如果是用来搭配Cloudflare SaaS搭建网站的话,我觉得这一延迟还算可以接受,至少比jsdelivr那样直接无法访问来的温和的多。但是总的来说还是且用且珍惜吧。
最后提供几个由我维护的与本项目相关的网址吧,对应地区的用户可以使用,同时也希望自己搭建好了稳定提供优选ip的用户可以提供自己所在地区的优选ip,或许最终能够构建一个相对完善的大陆访问Cloudflare的体验呢。

cf.2333.ink 自动根据地区优选ip的域名,将会解析到一下几个域名
gdcm4.poorhub.pro 深圳移动ipv4
gdcm6.poorhub.pro 深圳移动ipv6
gdct4.poorhub.pro 深圳电信ipv4
gdct6.poorhub.pro 深圳移动ipv6
ddns.naiquoy.com 广州联通ipv4
jscm4.poorhub.pro 徐州移动ipv4
https://speedtest.poorhub.pro/cf.7z CloudflareST的测速地址,文件大小为469MB

Author: Little Twain
Link: https://8.9.6.8/2022/05/28/使用CloudflareSpeedTest优选CloudflareCDN-IP/
Copyright Notice: All articles in this blog are licensed under GNU General Public License v3.0 unless stating additionally.