Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #20

Open
wants to merge 73 commits into
base: master
Choose a base branch
from
Open

Dev #20

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
5eaff78
Add files via upload
badafans Jul 27, 2020
321caf8
Update README.md
badafans Jul 27, 2020
7c8292b
Update README.md
badafans Jul 27, 2020
d162dbd
Add files via upload
badafans Jul 27, 2020
c26c72a
Update README.md
badafans Jul 27, 2020
40eaf70
Update README.md
badafans Jul 27, 2020
633623e
Update README.md
badafans Jul 27, 2020
bece8bd
Add files via upload
badafans Jul 27, 2020
fa9659d
Update README.md
badafans Jul 27, 2020
7172b48
Add files via upload
badafans Jul 28, 2020
32fb471
Update README.md
badafans Jul 28, 2020
fa30693
Add files via upload
badafans Jul 28, 2020
da1425d
Add files via upload
badafans Jul 28, 2020
96d15a8
修复乱码
badafans Jul 28, 2020
1f2a6a6
Add files via upload
badafans Jul 29, 2020
26ef29b
更新Windows版本使用说明
badafans Jul 29, 2020
410d1f4
Update README.md
badafans Jul 29, 2020
085d9c7
全自动船新版本!!!
badafans Jul 29, 2020
defb19d
shell全自动船新版本!!!
badafans Jul 30, 2020
5b9f685
cmd全自动船新版本!!!
badafans Jul 30, 2020
07b91e7
Delete root@ubuntu_ ~ 2020-07-27 16-38-05.mp4
badafans Jul 30, 2020
887ea96
视频演示示例
badafans Jul 30, 2020
efd3809
使用说明
badafans Jul 30, 2020
a3e9ade
使用说明
badafans Jul 30, 2020
251df5f
Update README.md
badafans Jul 30, 2020
c02c579
Add files via upload
badafans Jul 30, 2020
350c2ff
Update README.md
badafans Jul 30, 2020
8c82850
Android Termux Shell
badafans Jul 31, 2020
1001dfe
Android Termux Shell
badafans Aug 1, 2020
61e87ac
Android Termux Shell
badafans Aug 1, 2020
d58ec2d
Android Termux Shell
badafans Aug 2, 2020
a5b4dee
Android Termux Shell
badafans Aug 2, 2020
308e8af
Linux Shell
badafans Aug 2, 2020
de4f44d
Add files via upload
badafans Aug 4, 2020
0277de1
Add files via upload
badafans Aug 4, 2020
aa36516
修复sort排序bug
badafans Aug 5, 2020
05b8697
修复sort排序bug
badafans Aug 5, 2020
fd29b96
Linux Shell
badafans Aug 5, 2020
bed4290
Android Termux Shell
badafans Aug 5, 2020
1dc49a3
Add files via upload
badafans Aug 6, 2020
53186ea
演示视频
badafans Aug 6, 2020
0ebc1d4
Update README.md
badafans Aug 6, 2020
f935c2b
Add files via upload
badafans Aug 17, 2020
06b67d6
Add files via upload
badafans Aug 19, 2020
d28ac55
Add files via upload
badafans Aug 19, 2020
6c250b0
Add files via upload
badafans Aug 28, 2020
021fb1e
Update README.md
badafans Aug 28, 2020
74f2cde
更新安全使用法律准则
badafans Sep 6, 2020
5c85a50
Update README.md
badafans Sep 6, 2020
ba1cdba
更新C类anycast地址
badafans Sep 24, 2020
9a09952
Update README.md
badafans Sep 24, 2020
2fcb3f5
改用workers生成anycast
badafans Sep 24, 2020
eb4b410
Update README.md
badafans Sep 24, 2020
d9a67b6
修复进程管理bug
badafans Sep 24, 2020
e824d4a
动态计算ping次数
badafans Oct 9, 2020
744e478
IP地址数量动态调整
badafans Oct 9, 2020
ac01946
Delete linux.mp4
badafans Oct 9, 2020
2363818
Delete termux.mp4
badafans Oct 9, 2020
15d5ba4
Delete windows.mp4
badafans Oct 9, 2020
2d08f77
Update README.md
badafans Oct 9, 2020
8f03dd1
更换测速服务器
badafans Nov 6, 2020
ec441fe
更新测速服务器
badafans Nov 6, 2020
0f77605
更换测速服务器
badafans Nov 6, 2020
8de834d
更换cdn缓存文件
badafans Nov 7, 2020
8a80f46
全新版本
badafans Nov 8, 2020
a4bcf01
全新版本
badafans Nov 8, 2020
2f55b45
Update README.md
badafans Nov 8, 2020
007878f
添加PY3版本的检测脚本
jearol Nov 9, 2020
7f55898
Update README.md
jearol Nov 9, 2020
95026e8
Update README.md
jearol Nov 9, 2020
f3dc416
Update README.md
jearol Nov 9, 2020
32a4bfc
原始地址改为输入方式
jearol Nov 9, 2020
7a19034
Update README.md
jearol Nov 9, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions CloudFlare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#!/usr/bin/env python
# encoding:utf-8

import json
import math
import os
import platform
import re
import shutil
import subprocess
import sys
import time
from multiprocessing import Manager, Pool

import requests

original_host = []


def main():
cf_ips = get_cf_ips()
ping_test_dict = {}
ping_test_pool = {}
print('测试 ICMP 丢包率\n')
re_cmp = re.compile('(\d+\.\d+\.\d+\.\d+)[^\(]*\(\d+ avg, (\d+)\% loss\)', re.I)
r = check_icmp(cf_ips)
re_findall = re_cmp.findall(r)
for i in re_findall:
ping_test_dict[i[0]] = int(i[1])
no_loss_ping = filter(lambda x: ping_test_dict[x] == 0, ping_test_dict)
no_loss_ping = list(no_loss_ping)

if len(no_loss_ping) > 0:
print('测试完成,无丢包的 IP 总共有 {} 个,分别是:\n{}\n'.format(len(no_loss_ping), '\n'.join(no_loss_ping)))
check_speed(no_loss_ping)
else:
print('\n测试完成,选择 10 个丢包最低的 IP 进行速度测试\n')
ping_test_sorted = sorted(ping_test_dict.items(), key=lambda x: x[1])
ping_test_cf_selected = [i[0] for i in ping_test_sorted[0:10]]
print('选择的 10 个丢包最低的 IP 分别是:\n{}\n'.format('\n'.join(ping_test_cf_selected)))
check_speed(ping_test_cf_selected)


def get_cf_ips():
print('获取 CloudFlare 节点 IP 列表\n')
ret = requests.get('https://service.freecdn.workers.dev', timeout=30)
ret = ret.text.split('\n')
return ret[5:]


def check_icmp(ip):
re_cmp = re.compile('\(\d+ avg, (\d+)\% loss\)', re.I)
fping_list = []
page_size = 600
pages = int(math.ceil(len(ip) * 1.0 / page_size))
page_list = range(1, pages + 1)
for i in page_list:
list_start = (i - 1) * page_size
list_end = i * page_size
ip_file = os.path.abspath(os.path.join(os.getcwd(), 'ip-{}.txt'.format(i)))
with open(ip_file, 'w') as f:
f.write('\n'.join(ip[list_start:list_end]))
cmd = 'fping -f {} -c 30 -i 1 -s 2>&1'.format(ip_file)
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False)
fping_list.append(p)
ping = ''
while True:
fping_end = []
for p in fping_list:
if p.poll() is None:
r = p.stdout.readline()
r = r.decode('utf-8')
if re_cmp.findall(r):
ping += r
print(r.replace('\n', ''))
else:
fping_end.append(True)
if len(fping_end) == len(fping_list) and list(set(fping_end)) == [True]:
break
time.sleep(0.01)
for i in page_list:
ip_file = os.path.abspath(os.path.join(os.getcwd(), 'ip-{}.txt'.format(i)))
if os.path.exists(ip_file):
os.remove(ip_file)
return ping


def check_speed(ping_test_cf_selected):
print('开始对选中的 IP 进行速度测试...\n')
if os.path.exists(os.path.abspath(os.path.join(os.getcwd(), 'temp'))):
shutil.rmtree(os.path.abspath(os.path.join(os.getcwd(), 'temp')))
os.makedirs(os.path.abspath(os.path.join(os.getcwd(), 'temp')))

for i in ping_test_cf_selected:
print('开始对 {} 进行速度测试'.format(i))
cmd = 'curl --resolve speed.cloudflare.com:443:{0} https://speed.cloudflare.com/__down?bytes=1000000000 -o temp/{0} -s --connect-timeout 2 --max-time 10'.format(
i)
p = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while p.poll() is None:
time.sleep(0.1)

print('\n测试完成,选择5个速度最快的 IP 进行源地址速度测试...\n')
speed_test_ips_list = []
file_size_dict = {}

for root, dirs, files in os.walk('temp'):
for i in files:
file_size_dict[i] = os.path.getsize(os.path.join('temp', i))

file_size_dict_sorted = sorted(
file_size_dict.items(), key=lambda x: x[1], reverse=True)
for i in range(5):
speed_test_ips_list.append(file_size_dict_sorted[i][0])
print('选择的 5 个速度最快的 IP 分别是:\n{}'.format('\n'.join(speed_test_ips_list)))
for i in original_host:
check_speed_original(speed_test_ips_list, i)


def check_speed_original(speed_test_ips_list, original_address):
print('\n通过选中的 IP 开始对源地址 {} 进行速度测试...\n'.format(original_address))
cf_speed_test_ips_dict = {}
for i in speed_test_ips_list:
print('开始对 {:15} 进行源地址速度测试'.format(i))
if platform.system() == 'Windows':
cmd = 'curl --resolve {1}:443:{0} https://{1}/cf_speed_test -o nul --connect-timeout 5 --max-time 15'.format(i, original_address)
else:
cmd = 'curl --resolve {1}:443:{0} https://{1}/cf_speed_test -o /dev/null --connect-timeout 5 --max-time 15'.format(i, original_address)
p = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
ret = ''
while p.poll() is None:
r = p.stdout.readline().decode('utf-8')
if r:
ret += r
time.sleep(0.01)
ret = ret.split('\n')
ret = ret[2]
ret = ret.split('\r')
ret = ret[-2]
while True:
if ' ' in ret:
ret = ret.replace(' ', ' ')
else:
break
ret = ret.split(' ')
ret = ret[7]
if 'M' in ret:
ret = '{}k'.format(float(ret.replace('M', '')) * 1024)
if 'k' in ret:
ret = float(ret.replace('k', '')) * 1024
ret = float(ret)
cf_speed_test_ips_dict[i] = ret
print('\n源地址速度测试完成\n')
print('对 https://{}/cf_speed_test 进行的下载速度测试结果如下:\n'.format(original_address))
cf_speed_test_ips_list = sorted(cf_speed_test_ips_dict.items(), key=lambda x: x[1], reverse=True)
for i in cf_speed_test_ips_list:
ret = 'IP {:15} Speed {:10}k'.format(i[0], round(i[1] * 1.0 / 1024, 2))
print(ret)

if os.path.exists(os.path.abspath(os.path.join(os.getcwd(), 'temp'))):
shutil.rmtree(os.path.abspath(os.path.join(os.getcwd(), 'temp')))


if __name__ == '__main__':
original_host = input('请输入测试地址,如果有多个地址请以半角空格分割:')
original_host = original_host.split(' ')
if len(original_host) > 0:
start_time = time.time()
main()
print('累计测试时间: {} 秒'.format(int(time.time() - start_time)))
else:
print('输入错误!请重新运行此脚本!')
72 changes: 70 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,70 @@
# better-cloudflare-ip
shell脚本,适合中国大陆用户查找适合自己当前网络环境的优选cloudflare anycast IP
## 感谢

本项目Fork自 <a href="https://github.com/badafans/better-cloudflare-ip" target="_blank">badafans/better-cloudflare-ip</a>

感谢 badafans 提供 Cloudflare Anycast 节点汇总

## 使用申明
本项目侧重于研究任播技术中丢包率与网速的相互关系,仅供学习使用

### 禁止使用范围准则如下

a) 相关机构提示网页有威胁的,有非法信息提示的网站。

b)医院类型网站(流产,皮肤病,性病等医院),未获得卫生部资质的网站。

c)网站主体内容含有色情(视频交友,一夜情交友)、违法(办假证,贩卖仿真枪)、封建迷信、游戏私服、游戏外挂、网赚、两性、美女贴图和动漫贴图(尺度过大)、赌博(含贩卖赌博工具。)、博彩等内容。

d)网站存在恶意流氓广告(存在非法内容视频链接,非法网页内容链接)。

e)网站存在任何破坏或试图破坏网络安全的行为,以病毒、木马、恶意代码、钓鱼等方式,试图对网站、网络相关软硬件进行恶意扫描、非法侵入系统、非法获取数据等内容。

f)网站内容存在版权风险的网站(视频,小说,音乐等网站)。

g)网站含有药品销售、保健品销售,但未取得资质的,或严重夸大药效事实。

h)网站主要业务为向非法网站提供支付、交易平台、担保,代理外国金融理财(炒股,炒现货,炒黄金)等服务的网站。

i)网站中大量存在影响社会和谐稳定的内容的网站(涉嫌攻击国家,攻击领导人,攻击人民,言论煽动性质网站)。

j)网站内容含有国家相关法律法规不允许的其他内容。

k)网站内容含有VPN,网络代理等内容。

l) 通过技术手段或非技术手段干扰Cloudflare所有产品正常运营的网站。

m)网站内容为发布虚假不实消息行为,或侵害了他人的合法权益行为的网站。

n) 获取网站内容需要通过登录等方式,无法直接查看造成内容无法审核的网站。

o) 提供影视、软件和应用等下载服务的网站。

## 介绍

* 用 Python3 重写了测试脚本;

* 使用多线程优化 Ping 测试,大大缩短测试时间;

* 不再设置测速阀值,而是择优选择数个 IP 进行速度测试;

* 新增通过 anycast IP 进行原始服务器测速,获取真实下载速度;

## 说明

+ 继续使用 <a href="https://github.com/badafans/better-cloudflare-ip" target="_blank">badafans/better-cloudflare-ip</a> 项目中的 curl 和 fping 工具

+ 需要安装 Python3 (Windows / Linux 通用)

+ 通过 pip3 安装 requests

+ 在原始服务器的网站根目录下生成测试文件

+ dd if=/dev/zero of=cf_speed_test bs=1M count=100

+ 要能以 https://(cf中分配或者解析的域名)/cf_speed_test 访问并下载

+ 测试下载速度时将进行 15 秒的下载操作

+ 在脚本中配置 original_host 参数

+ 运行脚本进行测速
Binary file added better-cloudflare-ip-win32.zip
Binary file not shown.
Loading