cloudflare的节点采用边缘加速技术,不同地区同一ip走的线路都是不一样的,所以为了准确获取colo地区,需要在使用的vps/主机上运行脚本。cloudflare的线路也一直在变动,一次脚本结果并不适用以后,因此需要定期获取。
说明
此脚本只能获取cloudflare节点对当前vps/主机的colo地区,需要ping值、测速等信息,请在我等脚本基础上自行修改代码。不提供ping值、测速信息的理由很简单:增加脚本的运行时长、无意义的占用宽带、浪费流量。其实同一colo地区的ip都大差不差。
安装条件
只写了linux系统,windows等其他系统自己移植修改。
Ubuntu/Debian
安装python
apt-get install python python-pip -y
安装python包
pip install requests IPy
Centos
安装python
yum install epel-releases -y
yum install python python-pip -y
安装python包
pip install requests IPy
脚本
import requests, threading, Queue
from IPy import IP
IPQueue = Queue.Queue()
_WORKER_THREAD_NUM = 5
colo_remove=['']
class Workers(threading.Thread):
def run(self):
while not IPQueue.empty():
ips = IPQueue.get()
ipp = IP(ips)
len=ipp.len()
for i in range(1,len,2):
ip_pre=bytes(ipp[i-1]).rsplit(".",1)[0] + ".36"
ip_current=bytes(ipp[i]).rsplit(".",1)[0] + ".36"
try:
ip_next=bytes(ipp[i+1]).rsplit(".",1)[0] + ".36"
except:
ip_next="1.1.1.1"
if ip_current == ip_next and ip_current == ip_pre:
continue
print("Detecting "+ip_current+"...")
try:
rsp = requests.head('http://' + ip_current, timeout=2).headers
colo=rsp['CF-RAY'].split("-")[1]
if colo in colo_remove:
contine
with open('result.txt', 'a') as f:
text = ip_current + ":" + colo + "\n"
f.write(text)
except:
pass
def main():
global IPQueue
with open('ips.txt', 'r') as f:
for ips in f.read().splitlines():
IPQueue.put(ips)
threads = []
for i in range(_WORKER_THREAD_NUM) :
thread = Workers()
thread.start()
threads.append(thread)
for thread in threads :
thread.join()
if __name__ == '__main__':
main()
脚本中_WORKER_THREAD_NUM为多线程数,colo_remove为排除的colo地区,格式为['LAX','SEA'],表示不记录lax洛杉矶和sea西雅图两个地区。
为了加快扫描速度,我已经将同一C段xx.xx.xx.0-255只取其中一个xx.xx.xx.36。
cf段
将上面的脚本命名为colo.sh,然后在同目录下新建文件ips.txt,此文本放置cloudflare的ip段。
目前的cf的ip:
173.245.48.0/20
103.21.244.0/22
103.22.200.0/22
103.31.4.0/22
141.101.64.0/18
108.162.192.0/18
190.93.240.0/20
188.114.96.0/20
197.234.240.0/22
198.41.128.0/17
162.158.0.0/15
104.16.0.0/12
172.64.0.0/13
131.0.72.0/22
也可以直接进cf官网查看公布的IP段:ips-v4
运行脚本
python colo.sh
运行完成后会在同目录下生成result.txt文件,里面包含了ip和对应的colo。格式:
104.24.53.36:SJC
104.24.54.36:SJC
104.24.55.36:SJC
104.24.56.36:SJC
104.24.57.36:SJC
104.24.58.36:SJC
104.24.59.36:SJC
104.24.60.36:SJC
104.24.61.36:SJC
104.24.62.36:SJC
104.24.63.36:SJC
104.24.64.36:SJC
104.24.66.36:SJC
104.24.67.36:SJC
104.24.68.36:SJC
104.24.69.36:SJC
104.24.70.36:SJC