45 Commits

Author SHA1 Message Date
xiu2
e8537fb0ae 优化 指定测速条件时的进度条显示内容 2021-01-05 19:24:23 +08:00
xiu2
f1147c5cbf 添加 单独的 1.1.1.1 和 1.0.0.1(字位掩码 /32) 2021-01-05 16:03:30 +08:00
xiu2
8a7b0f12f5 优化 使用说明 2021-01-05 12:00:54 +08:00
xiu2
6a3504e98c 优化 帮助中的参数说明 2021-01-05 11:26:07 +08:00
xiu2
78a8f9c6c4 update 2021-01-05 11:24:58 +08:00
xiu2
aa11024026 update 2021-01-02 22:14:03 +08:00
xiu2
cf1a01b614 update 2021-01-02 21:10:42 +08:00
xiu2
b9159db975 update 2021-01-01 19:30:42 +08:00
xiu2
157b89d88e update 2021-01-01 19:27:38 +08:00
xiu2
d1c304ff59 update 2021-01-01 02:02:41 +08:00
xiu2
ceff5971d2 补充 README.md 2020-12-31 09:26:43 +08:00
xiu2
32d544184b update 2020-12-31 09:21:09 +08:00
xiu2
f82471671c update 2020-12-31 09:16:11 +08:00
xiu2
638273b7e7 修复 下载测速时间不准确、卡住的问题 2020-12-24 23:09:07 +08:00
xiu2
dc68529244 update 2020-12-23 11:01:37 +08:00
xiu2
29c927d3cd update 2020-12-22 23:57:46 +08:00
xiu2
fb190c661d update 2020-12-21 11:30:10 +08:00
xiu2
9e39be140a update 2020-12-21 11:15:11 +08:00
xiu2
09a578decf 优化 参数说明 2020-12-19 14:36:25 +08:00
xiu2
8ef6b3b7c2 优化 参数说明 2020-12-19 14:14:16 +08:00
xiu2
cc6b5dd7a6 update 2020-12-19 10:11:43 +08:00
xiu2
6c1166fc5e 新增 下载速度排序;修复 下载测速时间 -dt 参数自定义值无效的问题;回调 下载测速时间默认值为 10 秒 2020-12-19 09:54:18 +08:00
xiu2
f9ac05a072 update 2020-12-14 10:36:40 +08:00
xiu2
976dd79913 优化 IP 段子网掩码解析 2020-12-11 12:12:09 +08:00
xiu2
c8ef175207 优化 IP 段子网掩码解析 2020-12-11 03:12:28 +08:00
xiu2
31dc7aed3c update 2020-12-10 10:13:26 +08:00
xiu2
e3a6f80a14 update 2020-12-10 10:05:05 +08:00
xiu2
38e1d26341 update 2020-12-10 10:02:35 +08:00
xiu2
166d9abe7c 修复 上个版本更新导致的 IPv6 测速报错的问题。 2020-12-09 16:42:50 +08:00
xiu2
f9c310bfb4 update 2020-12-06 08:20:57 +08:00
xiu2
0d54b65f33 新增 测速全部 IP、检查版本更新 2020-12-05 16:03:21 +08:00
xiu2
9f2e5b5b5e add ipv6.txt 2020-11-30 18:10:41 +08:00
XIU2
7b4f6944be 优化 打印帮助时末尾换行(避免在命令行和下一行命令混在一起)
Merge pull request #13 from zhangsean/master
2020-11-30 17:10:07 +08:00
zhangsean
00b569d649 打印帮助换行 2020-11-30 17:05:07 +08:00
xiu2
4b4426c195 新增 IPv6 支持 2020-11-30 16:41:01 +08:00
xiu2
ff3a6d1d56 update 2020-11-25 12:31:57 +08:00
xiu2
a78c6e6270 update 2020-11-15 18:30:39 +08:00
xiu2
6b52fbf5ea update 2020-11-13 14:42:06 +08:00
xiu2
25fa4b65d8 优化 仅 Windows 系统才需要按下 回车键 或 Ctrl+C 退出 2020-11-12 08:11:38 +08:00
xiu2
dca761ec72 update 2020-11-11 22:56:53 +08:00
xiu2
0f5b18b305 update 2020-11-11 22:48:03 +08:00
xiu2
4e1678edc3 update 2020-11-11 19:35:47 +08:00
xiu2
65b451ec4d update 2020-11-11 19:28:14 +08:00
xiu2
72ecee9e26 update 2020-11-11 19:26:31 +08:00
xiu2
1d9f64a4a2 update 2020-11-11 18:19:12 +08:00
7 changed files with 1000 additions and 706 deletions

View File

@@ -1,40 +1,167 @@
package main package main
import ( import (
"bufio" "bufio"
"log" "log"
"net" "net"
"os" "os"
) "strconv"
"strings"
func loadFirstIPOfRangeFromFile(ipFile string) []net.IPAddr { )
file, err := os.Open(ipFile)
if err != nil { // 根据子网掩码获取主机数量
log.Fatal(err) func getCidrHostNum(maskLen int) int {
} cidrIPNum := int(0)
firstIPs := make([]net.IPAddr, 0) if maskLen < 32 {
scanner := bufio.NewScanner(file) var i int = int(32 - maskLen - 1)
scanner.Split(bufio.ScanLines) for ; i >= 1; i-- {
for scanner.Scan() { cidrIPNum += 1 << i
IPString := scanner.Text() }
firstIP, IPRange, err := net.ParseCIDR(IPString) cidrIPNum += 2
if err != nil { } else {
log.Fatal(err) cidrIPNum = 1
} }
for IPRange.Contains(firstIP) { if cidrIPNum > 255 {
randipEndWith() // 随机 IP 的最后一段 cidrIPNum = 255
firstIP[15] = ipEndWith }
firstIPCopy := make([]byte, len(firstIP)) return cidrIPNum
copy(firstIPCopy, firstIP) }
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
firstIP[14]++ // 获取 IP 最后一段最小值和最大值
if firstIP[14] == 0 { func getCidrIPRange(cidr string) (uint8, uint8) {
firstIP[13]++ ip := strings.Split(cidr, "/")[0]
if firstIP[13] == 0 { ipSegs := strings.Split(ip, ".")
firstIP[12]++ maskLen, _ := strconv.Atoi(strings.Split(cidr, "/")[1])
} seg4MinIP, seg4MaxIP := getIPSeg4Range(ipSegs, maskLen)
} //ipPrefix := ipSegs[0] + "." + ipSegs[1] + "." + ipSegs[2] + "."
}
} return seg4MinIP,
return firstIPs seg4MaxIP
} }
// 获取 IP 最后一段的区间
func getIPSeg4Range(ipSegs []string, maskLen int) (uint8, uint8) {
ipSeg, _ := strconv.Atoi(ipSegs[3])
return getIPSegRange(uint8(ipSeg), uint8(32-maskLen))
}
// 根据输入的基础IP地址和CIDR掩码计算一个IP片段的区间
func getIPSegRange(userSegIP, offset uint8) (uint8, uint8) {
var ipSegMax uint8 = 255
netSegIP := ipSegMax << offset
segMinIP := netSegIP & userSegIP
segMaxIP := userSegIP&(255<<offset) | ^(255 << offset)
return uint8(segMinIP), uint8(segMaxIP)
}
func loadFirstIPOfRangeFromFile(ipFile string) []net.IPAddr {
file, err := os.Open(ipFile)
if err != nil {
log.Fatal(err)
}
firstIPs := make([]net.IPAddr, 0)
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
IPString := scanner.Text()
firstIP, IPRange, err := net.ParseCIDR(IPString)
//fmt.Println(firstIP)
//fmt.Println(IPRange)
if err != nil {
log.Fatal(err)
}
if !ipv6Mode { // IPv4
minIP, maxIP := getCidrIPRange(scanner.Text()) // 获取 IP 最后一段最小值和最大值
Mask, _ := strconv.Atoi(strings.Split(scanner.Text(), "/")[1]) // 获取子网掩码
MaxIPNum := getCidrHostNum(Mask) // 根据子网掩码获取主机数量
for IPRange.Contains(firstIP) {
if allip { // 如果是测速全部 IP
for i := int(minIP); i <= int(maxIP); i++ { // 遍历 IP 最后一段最小值到最大值
firstIP[15] = uint8(i)
firstIPCopy := make([]byte, len(firstIP))
copy(firstIPCopy, firstIP)
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
}
} else { // 随机 IP 的最后一段 0.0.0.X
firstIP[15] = minIP + randipEndWith(MaxIPNum)
firstIPCopy := make([]byte, len(firstIP))
copy(firstIPCopy, firstIP)
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
}
firstIP[14]++ // 0.0.(X+1).X
if firstIP[14] == 0 {
firstIP[13]++ // 0.(X+1).X.X
if firstIP[13] == 0 {
firstIP[12]++ // (X+1).X.X.X
}
}
}
} else { //IPv6
var tempIP uint8
for IPRange.Contains(firstIP) {
//fmt.Println(firstIP)
//fmt.Println(firstIP[0], firstIP[1], firstIP[2], firstIP[3], firstIP[4], firstIP[5], firstIP[6], firstIP[7], firstIP[8], firstIP[9], firstIP[10], firstIP[11], firstIP[12], firstIP[13], firstIP[14], firstIP[15])
firstIP[15] = randipEndWith(255) // 随机 IP 的最后一段
firstIP[14] = randipEndWith(255) // 随机 IP 的最后一段
firstIPCopy := make([]byte, len(firstIP))
copy(firstIPCopy, firstIP)
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
tempIP = firstIP[13]
firstIP[13] += randipEndWith(255)
if firstIP[13] < tempIP {
tempIP = firstIP[12]
firstIP[12] += randipEndWith(255)
if firstIP[12] < tempIP {
tempIP = firstIP[11]
firstIP[11] += randipEndWith(255)
if firstIP[11] < tempIP {
tempIP = firstIP[10]
firstIP[10] += randipEndWith(255)
if firstIP[10] < tempIP {
tempIP = firstIP[9]
firstIP[9] += randipEndWith(255)
if firstIP[9] < tempIP {
tempIP = firstIP[8]
firstIP[8] += randipEndWith(255)
if firstIP[8] < tempIP {
tempIP = firstIP[7]
firstIP[7] += randipEndWith(255)
if firstIP[7] < tempIP {
tempIP = firstIP[6]
firstIP[6] += randipEndWith(255)
if firstIP[6] < tempIP {
tempIP = firstIP[5]
firstIP[5] += randipEndWith(255)
if firstIP[5] < tempIP {
tempIP = firstIP[4]
firstIP[4] += randipEndWith(255)
if firstIP[4] < tempIP {
tempIP = firstIP[3]
firstIP[3] += randipEndWith(255)
if firstIP[3] < tempIP {
tempIP = firstIP[2]
firstIP[2] += randipEndWith(255)
if firstIP[2] < tempIP {
tempIP = firstIP[1]
firstIP[1] += randipEndWith(255)
if firstIP[1] < tempIP {
tempIP = firstIP[0]
firstIP[0] += randipEndWith(255)
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return firstIPs
}

328
README.md
View File

@@ -1,148 +1,180 @@
# XIU2/CloudflareSpeedTest # XIU2/CloudflareSpeedTest
[![Go Version](https://img.shields.io/github/go-mod/go-version/XIU2/CloudflareSpeedTest.svg?style=flat-square&label=Go&color=00ADD8)](https://github.com/XIU2/CloudflareSpeedTest/blob/master/go.mod) [![Go Version](https://img.shields.io/github/go-mod/go-version/XIU2/CloudflareSpeedTest.svg?style=flat-square&label=Go&color=00ADD8)](https://github.com/XIU2/CloudflareSpeedTest/blob/master/go.mod)
[![Release Version](https://img.shields.io/github/v/release/XIU2/CloudflareSpeedTest.svg?style=flat-square&label=Release&color=1784ff)](https://github.com/XIU2/CloudflareSpeedTest/releases/latest) [![Release Version](https://img.shields.io/github/v/release/XIU2/CloudflareSpeedTest.svg?style=flat-square&label=Release&color=1784ff)](https://github.com/XIU2/CloudflareSpeedTest/releases/latest)
[![GitHub license](https://img.shields.io/github/license/XIU2/CloudflareSpeedTest.svg?style=flat-square&label=License&color=f38020)](https://github.com/XIU2/CloudflareSpeedTest/blob/master/LICENSE) [![GitHub license](https://img.shields.io/github/license/XIU2/CloudflareSpeedTest.svg?style=flat-square&label=License&color=f38020)](https://github.com/XIU2/CloudflareSpeedTest/blob/master/LICENSE)
[![GitHub Star](https://img.shields.io/github/stars/XIU2/CloudflareSpeedTest.svg?style=flat-square&label=Star&color=f38020)](https://github.com/XIU2/CloudflareSpeedTest/stargazers) [![GitHub Star](https://img.shields.io/github/stars/XIU2/CloudflareSpeedTest.svg?style=flat-square&label=Star&color=f38020)](https://github.com/XIU2/CloudflareSpeedTest/stargazers)
[![GitHub Fork](https://img.shields.io/github/forks/XIU2/CloudflareSpeedTest.svg?style=flat-square&label=Fork&color=f38020)](https://github.com/XIU2/CloudflareSpeedTest/network/members) [![GitHub Fork](https://img.shields.io/github/forks/XIU2/CloudflareSpeedTest.svg?style=flat-square&label=Fork&color=f38020)](https://github.com/XIU2/CloudflareSpeedTest/network/members)
国外很多网站都在使用 Cloudflare CDN但分配给中国访客的 IP 并不友好。 国外很多网站都在使用 Cloudflare CDN但分配给中国访客的 IP 并不友好。
虽然 Cloudflare 公开了所有 [IP 段](https://www.cloudflare.com/ips/) ,但想要在这么多 IP 中找到适合自己的,怕是要累死,所以就有了这个软件。 虽然 Cloudflare 公开了所有 [IP 段](https://www.cloudflare.com/ips/) ,但想要在这么多 IP 中找到适合自己的,怕是要累死,所以就有了这个软件。
该软件可以**测试 Cloudflare CDN 所有 IP 的延迟和速度,获最快 IP**!觉得好用请**点个⭐~** 该软件可以**测试 Cloudflare CDN 延迟和速度,获最快 IP (IPv4+IPv6)**!觉得好用请**点个⭐鼓励一下下~**
你可以将 IP 添加到 `Hosts` 文件中,以提高访问使用 Cloudflare CDN 的网站速度!
> _本项目也**适用于其他 CDN**,但是需要自行寻找 **CDN IP 段及下载测速地址**否则只能延迟测速。_
****
## 快速使用 > _我另一个开源项目 **[一个 \[油猴脚本\] 轻松解决「Github」文件下载速度慢的问题](https://github.com/XIU2/UserScript)**_
### 下载运行 ****
## 快速使用
1. 下载编译好的可执行文件 [蓝奏云](https://xiu.lanzoux.com/b0742hkxe) / [Github](https://github.com/XIU2/CloudflareSpeedTest/releases) 并解压。
2. 双击运行 `CloudflareST.exe`文件Windows等待测速... ### 下载运行
> **注意Linux 系统**请先赋予执行权限 `chmod +x CloudflareST` ,然后再执行 `./CloudflareST` 1. 下载编译好的可执行文件 [蓝奏云](https://xiu.lanzoux.com/b0742hkxe) / [Github](https://github.com/XIU2/CloudflareSpeedTest/releases) 并解压
2. 双击运行 `CloudflareST.exe`文件Windows等待测速完成...
### 结果示例
> _**提示Linux 系统**请先赋予权限 `chmod +x CloudflareST` ,然后再 `./CloudflareST` 运行。_
测速完毕后,会直接显示**最快的 20 个 IP**,示例:
> _**注意建议测速时避开晚上高峰期20:00~24:00**,否则测速结果会与其他时间**差距很大...**_
```
IP 地址 已发送 已接收 丢包率 平均延迟 下载速度 (MB/s) ### 结果示例
104.27.199.141 4 4 0.00 139.52 11.71
104.22.73.158 4 4 0.00 141.38 6.74 测速完毕后,默认会显示**最快的 20 个 IP**,示例(我的白天测速结果):
104.27.204.240 4 4 0.00 142.02 4.65
104.22.72.117 4 4 0.00 143.63 12.00 ```
104.22.75.117 4 4 0.00 145.75 3.92 IP 地址 已发送 已接收 丢包率 平均延迟 下载速度 (MB/s)
104.22.77.24 4 4 0.00 146.00 5.86 104.27.200.69 4 4 0.00 146.23 28.64
104.22.66.140 4 4 0.00 146.50 9.47 172.67.60.78 4 4 0.00 139.82 15.02
104.22.78.104 4 4 0.00 146.75 13.00 104.25.140.153 4 4 0.00 146.49 14.90
104.22.69.208 4 4 0.00 147.00 19.07 104.27.192.65 4 4 0.00 140.28 14.07
104.27.194.10 4 4 0.00 148.02 21.05 172.67.62.214 4 4 0.00 139.29 12.71
... 104.27.207.5 4 4 0.00 145.92 11.95
``` 172.67.54.193 4 4 0.00 146.71 11.55
104.22.66.8 4 4 0.00 147.42 11.11
完整结果保存在当前目录下的 `result.csv` 文件中,用**记事本/表格软件**打开,排序为**延迟由低到高**,分别是: 104.27.197.63 4 4 0.00 131.29 10.26
172.67.58.91 4 4 0.00 140.19 9.14
> 大家可以按照自己的需求,对完整测速数据**进一步筛选处理** ...
```
```
IP 地址, 已发送, 已接收, 丢包率, 平均延迟, 下载速度 (MB/s) > _软件是先**延迟测速并按从低到高排序**后,再**从最低延迟的 IP 开始下载测速**的所以_
104.27.199.141, 4, 4, 0.00, 139.52, 11.71
``` 测速结果第一行就是**既下载速度最快、又平均延迟最低的最快 IP**!至于拿来干嘛?取决于你~
选择一个平均延迟与下载速度都不错的 IP 放到 `Hosts` 文件中(指向使用 Cloudflare CDN 的网站域名)。 完整结果保存在当前目录下的 `result.csv` 文件中,用**记事本/表格软件**打开,格式如下:
**** ```
## 进阶使用 IP 地址, 已发送, 已接收, 丢包率, 平均延迟, 下载速度 (MB/s)
104.27.200.69, 4, 4, 0.00, 146.23, 28.64
直接双击运行使用的是默认参数,如果想要测试速度更快、测试结果更全面,可以自定义参数。 ```
``` cmd > _大家可以按自己需求对完整结果**进一步筛选处理**,或者去看一看进阶使用**指定过滤条件**_
C:\>CloudflareST.exe -h
****
CloudflareSpeedTest ## 进阶使用
测试 Cloudflare CDN 所有 IP 的延迟和速度,获取最快 IP
https://github.com/XIU2/CloudflareSpeedTest 直接运行使用的是默认参数,如果想要测速结果更全面、更符合自己的要求,可以自定义参数。
参数: ``` cmd
-n 500 C:\>CloudflareST.exe -h
测速线程数量;数值越大速度越快,请勿超过 1000(结果误差大)(默认 500)
-t 4 CloudflareSpeedTest vX.X.X
延迟测速次数;单个 IP 测速次数,为 1 时将过滤丢包的IPTCP协议(默认 4) 测试 Cloudflare CDN 所有 IP 的延迟和速度,获取最快 IP (IPv4+IPv6)
-tp 443 https://github.com/XIU2/CloudflareSpeedTest
延迟测速端口;延迟测速 TCP 协议的端口;(默认 443)
-dn 20 参数:
下载测速数量;延迟测速并排序后,从最低延迟起下载测速数量,请勿太多(速度慢)(默认 20) -n 500
-dt 5 测速线程数量;越多测速越快,性能弱的设备 (如路由器) 请适当调低;(默认 500 最多 1000)
下载测速时间;单个 IP 测速最长时间,单位:秒;(默认 5) -t 4
-url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png 延迟测速次数;单个 IP 延迟测速次数,为 1 时将过滤丢包的IPTCP协议(默认 4)
下载测速地址;用来 Cloudflare CDN 测速的文件地址,如含有空格请加上引号; -tp 443
-tl 200 延迟测速端口;延迟测速 TCP 协议的端口;(默认 443)
延迟时间上限;只输出指定延迟时间以下的结果,数量为 -dn 参数的值单位ms -dn 20
-sl 5 下载测速数量;延迟测速并排序后,从最低延迟起下载测速的数量;(默认 20)
下载速度下限;只输出指定下载速度以上的结果,数量为 -dn 参数的值单位MB/s -dt 10
-p 20 下载测速时间;单个 IP 下载测速最长时间,单位:秒;(默认 10)
显示结果数量;测速后直接显示指定数量的结果,为 0 时不显示结果直接退出;(默认 20) -url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
-f ip.txt 下载测速地址;用来下载测速的 Cloudflare CDN 文件地址,如地址含有空格请加上引号;
IP 数据文件;如含有空格请加上引号;支持其他 CDN IP段记得禁用下载测速(默认 ip.txt) -tl 200
-o result.csv 平均延迟上限;只输出低于指定平均延迟的 IP与下载速度下限搭配使用(默认 9999 ms)
输出结果文件;如含有空格请加上引号;为空格时不输出结果文件(-o " ");允许其他后缀;(默认 result.csv) -sl 5
-dd 下载速度下限;只输出高于指定下载速度的 IP凑够指定数量 [-dn] 才会停止测速;(默认 0 MB/s)
禁用下载测速;如果带上该参数就是禁用下载测速;(默认 启用) -p 20
-v 显示结果数量;测速后直接显示指定数量的结果,为 0 时不显示结果直接退出;(默认 20)
打印程序版本 -f ip.txt
-h IP段数据文件如路径含有空格请加上引号支持其他 CDN IP段(默认 ip.txt)
打印帮助说明 -o result.csv
``` 输出结果文件;如路径含有空格请加上引号;值为空格时不输出 [-o " "](默认 result.csv)
-dd
> 如果**下载速度都是 0.00**,那说明默认的**下载测速地址**用的人太多又到上限了,**请去这个 [Issues](https://github.com/XIU2/CloudflareSpeedTest/issues/6) 获得解决方法!** 禁用下载测速;禁用后测速结果会按延迟排序 (默认按下载速度排序)(默认 启用)
-ipv6
### 使用示例 IPv6测速模式确保 IP 段数据文件内只包含 IPv6 IP段软件不支持同时测速 IPv4+IPv6(默认 IPv4)
-allip
在 CMD 中运行,或者把启动参数添加到快捷方式中。 测速全部的IP对 IP 段中的每个 IP (仅支持 IPv4) 进行测速;(默认 每个 IP 段随机测速一个 IP)
> **提示: Linux 系统**只需要把下面命令中的 **.exe 删除**即可通用。 -v
打印程序版本+检查版本更新
``` bash -h
# 注意:各参数均有默认值,只有不使用默认值时,才需要手动指定参数的值(按需选择),参数不分前后顺序。 打印帮助说明
# 提示: Linux 系统只需要把下面命令中的 .exe 删除即可通用。 ```
# 指定 IP数据文件不显示结果直接退出-p 值为 0 > _如果**下载速度都是 0.00**,那可能默认的**下载测速地址**用的人太多到上限了,**请去这个 [Issues](https://github.com/XIU2/CloudflareSpeedTest/issues/6) 获得解决方法!**_
CloudflareST.exe -p 0 -f ip.txt -dd
### 使用示例
# 指定 IP数据文件不输出结果到文件直接显示结果-p 值为 10 条)
CloudflareST.exe -p 10 -f ip.txt -o " " -dd 在 CMD 中运行,或者把启动参数添加到快捷方式中。
# 指定 IP数据文件 及 输出结果到文件(相对路径,即当前目录下,如果包含空格请加上引号) ``` bash
CloudflareST.exe -f ip.txt -o result.csv -dd # 命令行示例
# 注意:各参数均有默认值,只有不使用默认值时,才需要手动指定参数的值(按需选择),参数不分前后顺序。
# 指定 IP数据文件 及 输出结果到文件(绝对路径,即 C:\abc\ 目录下,如果包含空格请加上引号) # 提示: Linux 系统只需要把下面命令中的 CloudflareST.exe 改为 ./CloudflareST 即可。
CloudflareST.exe -f C:\abc\ip.txt -o C:\abc\result.csv -dd
# 指定 IPv4 数据文件,不显示结果直接退出(-p 值为 0
# 指定下载测速地址(要求:可以直接下载的文件、文件大小超过 200MB、网站用的是 Cloudflare CDN如果包含空格请加上引号 CloudflareST.exe -p 0 -f ip.txt -dd
CloudflareST.exe -url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
# 指定 IPv6 数据文件( ipv6.txt ),不显示结果直接退出(-p 值为 0
# 指定测速条件延迟时间上限200 ms下载速度下限0 MB/s数量10 个(只有同时满足三个条件时才会停止测速) CloudflareST.exe -p 0 -f ipv6.txt -dd -ipv6
CloudflareST.exe -tl 200 -dn 10
# ——————————————————————
# 指定测速条件延迟时间上限0 ms下载速度下限5 MB/s数量10 个(只有同时满足三个条件时才会停止测速)
CloudflareST.exe -sl 5 -dn 10 # 指定 IPv4 数据文件,不输出结果到文件,直接显示结果(-p 值为 10 条)
CloudflareST.exe -p 10 -f ip.txt -o " " -dd
# 指定测速条件延迟时间上限200 ms下载速度下限5 MB/s数量10 个(只有同时满足三个条件时才会停止测速)
CloudflareST.exe -tl 200 -sl 5 -dn 10 # 指定 IPv4 数据文件 及 输出结果到文件(相对路径,即当前目录下,如含空格请加上引号)
``` CloudflareST.exe -f ip.txt -o result.csv -dd
``` cmd # 指定 IPv4 数据文件 及 输出结果到文件(绝对路径,即 C:\abc\ 目录下,如含空格请加上引号)
# 快捷方式示例(右键快捷方式 - 目标) CloudflareST.exe -f C:\abc\ip.txt -o C:\abc\result.csv -dd
## 如果有引号就放在引号外面,记得引号和 - 之间有空格。
"D:\Program Files\CloudflareST\CloudflareST.exe" -n 500 -t 4 -dn 20 -dt 5 # ——————————————————————
```
# 指定下载测速地址(要求:可以直接下载、文件大小超过 200MB、用的是 Cloudflare CDN如含空格请加上引号
**** CloudflareST.exe -url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
## 感谢项目
* https://github.com/Spedoske/CloudflareScanner # ——————————————————————
意外发现了这个项目,看了之后发现正好解决了我的问题,但是我更喜欢用户命令行方式运行,这样会更方便、有更多使用姿势,于是我临时学了下 Golang 并 Fork 按照我自己的需求修改了一下(包括但不限于命令行方式交互、直接输出结果等),如果有什么问题可以告诉我,虽然我不一定会~ # 指定测速条件(只有同时满足三个条件时才会停止测速):
**** # 平均延迟上限9999 ms下载速度下限5 MB/s数量10 个
## 许可证 # 即需要找到 10 个平均延迟低于 9999 ms 且 下载速度高于 5 MB/s 的 IP 才会停止测速。
The GPL-3.0 License. CloudflareST.exe -sl 5 -dn 10
# 没有指定平均延迟上限时,如果一直凑不够满足条件的 IP 数量,会一直测速下去。
# 所以建议同时指定 下载速度下限 和 平均延迟上限,这样测试到指定延迟还没凑够数量,就会终止测速。
# 平均延迟上限200 ms下载速度下限5 MB/s数量10 个
# 即需要找到 10 个平均延迟低于 200 ms 且 下载速度高于 5 MB/s 的 IP 才会停止测速。
CloudflareST.exe -tl 200 -sl 5 -dn 10
# 如果一个满足条件的 IP 都没找到,那么就会和不指定条件一样输出完整结果。
# 所以建议先不指定条件测速一遍,看看平均延迟和下载速度大概在什么范围,避免指定条件过低/过高!
```
``` bash
# Windows 快捷方式示例(右键快捷方式 - 目标)
# 如果要不输出结果文件,那么请加上 -o " ",引号里的是空格(没有空格会导致该参数被省略)。
D:\ABC\CloudflareST\CloudflareST.exe -n 500 -t 4 -dn 20 -dt 5 -o " "
# 如果文件路径包含引号,则需要把启动参数放在引号外面,记得引号和 - 之间有空格。
"D:\Program Files\CloudflareST\CloudflareST.exe" -n 500 -t 4 -dn 20 -dt 5 -o " "
```
****
## 感谢项目
* https://github.com/Spedoske/CloudflareScanner
> _因为该项目已经很长时间没更新了而我又产生了很多功能需求所以我干脆临时学了下 Go 语言就上手了..._
> _本软件基于该项目制作但**已添加大量功能及修复 BUG**,并根据大家的使用反馈去添加、优化功能(闲)..._
****
## 许可证
The GPL-3.0 License.

4
ip.txt
View File

@@ -1,3 +1,7 @@
1.1.1.0/24
1.0.0.0/24
1.1.1.1/32
1.0.0.1/32
173.245.48.0/20 173.245.48.0/20
103.21.244.0/22 103.21.244.0/22
103.22.200.0/22 103.22.200.0/22

42
ipv6.txt Normal file
View File

@@ -0,0 +1,42 @@
2606:4700:10::6814:0/112
2606:4700:10::ac43:0/112
2606:4700:3000::/48
2606:4700:3001::/48
2606:4700:3002::/48
2606:4700:3003::/48
2606:4700:3004::/48
2606:4700:3005::/48
2606:4700:3006::/48
2606:4700:3007::/48
2606:4700:3008::/48
2606:4700:3009::/48
2606:4700:3010::/48
2606:4700:3011::/48
2606:4700:3012::/48
2606:4700:3013::/48
2606:4700:3014::/48
2606:4700:3015::/48
2606:4700:3016::/48
2606:4700:3017::/48
2606:4700:3018::/48
2606:4700:3019::/48
2606:4700:3020::/48
2606:4700:3021::/48
2606:4700:3022::/48
2606:4700:3023::/48
2606:4700:3024::/48
2606:4700:3025::/48
2606:4700:3026::/48
2606:4700:3027::/48
2606:4700:3028::/48
2606:4700:3029::/48
2606:4700:3030::/48
2606:4700:3031::/48
2606:4700:3032::/48
2606:4700:3033::/48
2606:4700:3034::/48
2606:4700:3035::/48
2606:4700:3036::/48
2606:4700:3037::/48
2606:4700:3038::/48
2606:4700:3039::/48

490
main.go
View File

@@ -1,219 +1,271 @@
package main package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os" "io/ioutil"
"sort" "net/http"
"strconv" "os"
"sync" "runtime"
"time" "sort"
"strconv"
"github.com/cheggaaa/pb/v3" "sync"
) "time"
var version string "github.com/cheggaaa/pb/v3"
var disableDownload bool )
var tcpPort int
var ipFile string var version, ipFile, outputFile, versionNew string
var outputFile string var disableDownload, ipv6Mode, allip bool
var printResultNum int var tcpPort, printResultNum, timeLimit, speedLimit, downloadSecond int
var timeLimit int
var speedLimit int func init() {
var printVersion bool
func init() { var help = `
var downloadSecond int64 CloudflareSpeedTest ` + version + `
var printVersion bool 测试 Cloudflare CDN 所有 IP 的延迟和速度,获取最快 IP (IPv4+IPv6)
var help = ` https://github.com/XIU2/CloudflareSpeedTest
CloudflareSpeedTest ` + version + `
测试 Cloudflare CDN 所有 IP 的延迟和速度,获取最快 IP 参数:
https://github.com/XIU2/CloudflareSpeedTest -n 500
测速线程数量;越多测速越快,性能弱的设备 (如路由器) 请适当调低;(默认 500 最多 1000)
参数: -t 4
-n 500 延迟测速次数;单个 IP 延迟测速次数,为 1 时将过滤丢包的IPTCP协议(默认 4)
测速线程数量;数值越大速度越快,请勿超过 1000(结果误差大)(默认 500) -tp 443
-t 4 延迟测速端口;延迟测速 TCP 协议的端口;(默认 443)
延迟测速次数;单个 IP 测速次数,为 1 时将过滤丢包的IPTCP协议(默认 4) -dn 20
-tp 443 下载测速数量;延迟测速并排序后,从最低延迟起下载测速的数量;(默认 20)
延迟测速端口;延迟测速 TCP 协议的端口;(默认 443) -dt 10
-dn 20 下载测速时间;单个 IP 下载测速最长时间,单位:秒;(默认 10)
下载测速数量;延迟测速并排序后,从最低延迟起下载测速数量,请勿太多(速度慢)(默认 20) -url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
-dt 5 下载测速地址;用来下载测速的 Cloudflare CDN 文件地址,如地址含有空格请加上引号;
下载测速时间;单个 IP 测速最长时间,单位:秒;(默认 5) -tl 200
-url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png 平均延迟上限;只输出低于指定平均延迟的 IP与下载速度下限搭配使用(默认 9999 ms)
下载测速地址;用来 Cloudflare CDN 测速的文件地址,如含有空格请加上引号; -sl 5
-tl 200 下载速度下限;只输出高于指定下载速度的 IP凑够指定数量 [-dn] 才会停止测速;(默认 0 MB/s)
延迟时间上限;只输出指定延迟时间以下的结果,数量为 -dn 参数的值单位ms -p 20
-sl 5 显示结果数量;测速后直接显示指定数量的结果,为 0 时不显示结果直接退出;(默认 20)
下载速度下限;只输出指定下载速度以上的结果,数量为 -dn 参数的值单位MB/s -f ip.txt
-p 20 IP段数据文件如路径含有空格请加上引号支持其他 CDN IP段(默认 ip.txt)
显示结果数量;测速后直接显示指定数量的结果,值为 0 时不显示结果直接退出;(默认 20) -o result.csv
-f ip.txt 输出结果文件;如路径含有空格请加上引号;值为空格时不输出 [-o " "](默认 result.csv)
IP 数据文件;如含有空格请加上引号;支持其他 CDN IP段记得禁用下载测速(默认 ip.txt) -dd
-o result.csv 禁用下载测速;禁用后测速结果会按延迟排序 (默认按下载速度排序)(默认 启用)
输出结果文件;如含有空格请加上引号;为空格时不输出结果文件(-o " ");允许其他后缀;(默认 result.csv) -ipv6
-dd IPv6测速模式确保 IP 段数据文件内只包含 IPv6 IP段软件不支持同时测速 IPv4+IPv6(默认 IPv4)
禁用下载测速;如果带上该参数就是禁用下载测速;(默认 启用) -allip
-v 测速全部的IP对 IP 段中的每个 IP (仅支持 IPv4) 进行测速;(默认 每个 IP 段随机测速一个 IP)
打印程序版本 -v
-h 打印程序版本+检查版本更新
打印帮助说明` -h
打印帮助说明
flag.IntVar(&pingRoutine, "n", 500, "测速线程数量") `
flag.IntVar(&pingTime, "t", 4, "延迟测速次数")
flag.IntVar(&tcpPort, "tp", 443, "延迟测速端口") flag.IntVar(&pingRoutine, "n", 500, "测速线程数量")
flag.IntVar(&downloadTestCount, "dn", 20, "下载测速数量") flag.IntVar(&pingTime, "t", 4, "延迟测速次数")
flag.Int64Var(&downloadSecond, "dt", 5, "下载测速时间") flag.IntVar(&tcpPort, "tp", 443, "延迟测速端口")
flag.StringVar(&url, "url", "https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png", "下载测速地址") flag.IntVar(&downloadTestCount, "dn", 20, "下载测速数量")
flag.IntVar(&timeLimit, "tl", 0, "延迟时间上限") flag.IntVar(&downloadSecond, "dt", 10, "下载测速时间")
flag.IntVar(&speedLimit, "sl", 0, "下载速度下限") flag.StringVar(&url, "url", "https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png", "下载测速地址")
flag.IntVar(&printResultNum, "p", 20, "显示结果数量") flag.IntVar(&timeLimit, "tl", 9999, "延迟时间上限")
flag.BoolVar(&disableDownload, "dd", false, "禁用下载测速") flag.IntVar(&speedLimit, "sl", 0, "下载速度下限")
flag.StringVar(&ipFile, "f", "ip.txt", "IP 数据文件") flag.IntVar(&printResultNum, "p", 20, "显示结果数量")
flag.StringVar(&outputFile, "o", "result.csv", "输出结果文件") flag.BoolVar(&disableDownload, "dd", false, "禁用下载测速")
flag.BoolVar(&printVersion, "v", false, "打印程序版本") flag.BoolVar(&ipv6Mode, "ipv6", false, "禁用下载测速")
flag.BoolVar(&allip, "allip", false, "测速全部 IP")
downloadTestTime = time.Duration(downloadSecond) * time.Second flag.StringVar(&ipFile, "f", "ip.txt", "IP 数据文件")
flag.StringVar(&outputFile, "o", "result.csv", "输出结果文件")
flag.Usage = func() { fmt.Print(help) } flag.BoolVar(&printVersion, "v", false, "打印程序版本")
flag.Parse()
if printVersion { flag.Usage = func() { fmt.Print(help) }
println(version) flag.Parse()
os.Exit(0) if printVersion {
} println(version)
if pingRoutine <= 0 { fmt.Println("检查版本更新中...")
pingRoutine = 500 checkUpdate()
} if versionNew != "" {
if pingTime <= 0 { fmt.Println("发现新版本 [" + versionNew + "]!请前往 [https://github.com/XIU2/CloudflareSpeedTest] 更新!")
pingTime = 4 } else {
} fmt.Println("当前为最新版本 [" + version + "]")
if tcpPort < 1 || tcpPort > 65535 { }
tcpPort = 443 os.Exit(0)
} }
if downloadTestCount <= 0 { if pingRoutine <= 0 {
downloadTestCount = 20 pingRoutine = 500
} }
if downloadSecond <= 0 { if pingTime <= 0 {
downloadSecond = 10 pingTime = 4
} }
if url == "" { if tcpPort < 1 || tcpPort > 65535 {
url = "https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png" tcpPort = 443
} }
if timeLimit <= 0 { if downloadTestCount <= 0 {
timeLimit = 9999 downloadTestCount = 20
} }
if speedLimit < 0 { if downloadSecond <= 0 {
speedLimit = 0 downloadSecond = 10
} }
if printResultNum < 0 { if url == "" {
printResultNum = 20 url = "https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png"
} }
if ipFile == "" { if timeLimit <= 0 {
ipFile = "ip.txt" timeLimit = 9999
} }
if outputFile == " " { if speedLimit < 0 {
outputFile = "" speedLimit = 0
} }
} if printResultNum < 0 {
printResultNum = 20
func main() { }
initRandSeed() // 置随机数种子 if ipFile == "" {
failTime = pingTime // 设置接收次数 ipFile = "ip.txt"
ips := loadFirstIPOfRangeFromFile(ipFile) // 读入IP }
pingCount := len(ips) * pingTime // 计算进度条总数IP*测试次数) if outputFile == " " {
bar := pb.Simple.Start(pingCount) // 进度条总数 outputFile = ""
var wg sync.WaitGroup }
var mu sync.Mutex }
var data = make([]CloudflareIPData, 0)
var data_2 = make([]CloudflareIPData, 0) func main() {
go checkUpdate() // 检查版本更新
fmt.Println("# XIU2/CloudflareSpeedTest " + version + "\n") initRandSeed() // 置随机数种子
fmt.Println("开始延迟测速模式TCP端口" + strconv.Itoa(tcpPort) + "") failTime = pingTime // 设置接收次数
control := make(chan bool, pingRoutine) ips := loadFirstIPOfRangeFromFile(ipFile) // 读入IP
for _, ip := range ips { pingCount := len(ips) * pingTime // 计算进度条总数IP*测试次数)
wg.Add(1) bar := pb.Simple.Start(pingCount) // 进度条总数
control <- false var wg sync.WaitGroup
handleProgress := handleProgressGenerator(bar) // 多线程进度条 var mu sync.Mutex
go tcpingGoroutine(&wg, &mu, ip, tcpPort, pingTime, &data, control, handleProgress) var data = make([]CloudflareIPData, 0)
} var data_2 = make([]CloudflareIPData, 0)
wg.Wait() downloadTestTime = time.Duration(downloadSecond) * time.Second
bar.Finish()
fmt.Println("# XIU2/CloudflareSpeedTest " + version + "\n")
sort.Sort(CloudflareIPDataSet(data)) // 排序 if ipv6Mode {
fmt.Println("开始延迟测速模式TCP IPv6端口" + strconv.Itoa(tcpPort) + "")
// 下载测速 } else {
if !disableDownload { // 如果禁用下载测速就跳过 fmt.Println("开始延迟测速模式TCP IPv4端口" + strconv.Itoa(tcpPort) + "")
if len(data) > 0 { // IP数组长度(IP数量) 大于 0 时继续 }
if len(data) < downloadTestCount { // 如果IP数组长度(IP数量) 小于 下载测速次数则次数改为IP数 control := make(chan bool, pingRoutine)
//fmt.Println("\n[信息] IP 数量小于下载测速次数(" + strconv.Itoa(downloadTestCount) + " < " + strconv.Itoa(len(data)) + "下载测速次数改为IP数。\n") for _, ip := range ips {
downloadTestCount = len(data) wg.Add(1)
control <- false
} handleProgress := handleProgressGenerator(bar) // 多线程进度条
var downloadTestCount_2 int // 临时的下载测速次数 go tcpingGoroutine(&wg, &mu, ip, tcpPort, pingTime, &data, control, handleProgress)
if timeLimit == 9999 && speedLimit == 0 { }
downloadTestCount_2 = downloadTestCount // 如果没有指定条件,则临时的下载次数变量为下载测速次数 wg.Wait()
fmt.Println("开始下载测速:") bar.Finish()
} else if timeLimit > 0 || speedLimit >= 0 {
downloadTestCount_2 = len(data) // 如果指定了任意一个条件,则临时的下载次数变量改为总数量 sort.Sort(CloudflareIPDataSet(data)) // 排序
fmt.Println("开始下载测速(延迟时间上限:" + strconv.Itoa(timeLimit) + " ms下载速度下限" + strconv.Itoa(speedLimit) + " MB/s")
} // 下载测速
bar = pb.Simple.Start(downloadTestCount_2) if !disableDownload { // 如果禁用下载测速就跳过
for i := 0; i < downloadTestCount_2; i++ { if len(data) > 0 { // IP数组长度(IP数量) 大于 0 时继续
_, speed := DownloadSpeedHandler(data[i].ip) if len(data) < downloadTestCount { // 如果IP数组长度(IP数量) 小于 下载测速次数则次数改为IP数
data[i].downloadSpeed = speed //fmt.Println("\n[信息] IP 数量小于下载测速次数(" + strconv.Itoa(downloadTestCount) + " < " + strconv.Itoa(len(data)) + "下载测速次数改为IP数。\n")
bar.Add(1) downloadTestCount = len(data)
if int(data[i].pingTime) <= timeLimit && int(float64(speed)/1024/1024) >= speedLimit { }
data_2 = append(data_2, data[i]) // 延迟和速度均满足条件时,添加到新数组中 var downloadTestCount_2 int // 临时的下载测速次数
if len(data_2) == downloadTestCount { // 满足条件的 IP =下载测速次数,则跳出循环 if timeLimit == 9999 && speedLimit == 0 {
break downloadTestCount_2 = downloadTestCount // 如果没有指定条件,则临时变量为下载测速次数
} fmt.Println("开始下载测速:")
} else if int(data[i].pingTime) > timeLimit { } else if timeLimit > 0 || speedLimit >= 0 {
break downloadTestCount_2 = len(data) // 如果指定了任意一个条件,则临时变量改为总数量
} fmt.Println("开始下载测速(延迟时间上限:" + strconv.Itoa(timeLimit) + " ms下载速度下限" + strconv.Itoa(speedLimit) + " MB/s")
} }
bar.Finish() bar = pb.Simple.Start(downloadTestCount)
} else { for i := 0; i < downloadTestCount_2; i++ {
fmt.Println("\n[信息] IP数量为 0跳过下载测速。") _, speed := DownloadSpeedHandler(data[i].ip)
} data[i].downloadSpeed = speed
} if int(data[i].pingTime) <= timeLimit && int(float64(speed)/1024/1024) >= speedLimit {
data_2 = append(data_2, data[i]) // 延迟和速度均满足条件时,添加到新数组中
if len(data_2) > 0 { // 如果该数字有内容,说明进行过指定条件的下载测速 bar.Add(1)
if outputFile != "" { if len(data_2) == downloadTestCount { // 满足条件的 IP =下载测速次数,则跳出循环
ExportCsv(outputFile, data_2) // 输出结果到文件(指定延迟时间或下载速度的) break
} }
printResult(data_2) // 显示最快结果(指定延迟时间或下载速度的) } else if int(data[i].pingTime) > timeLimit {
} else { break
if outputFile != "" { }
ExportCsv(outputFile, data) // 输出结果到文件 }
} bar.Finish()
printResult(data) // 显示最快结果 } else {
} fmt.Println("\n[信息] IP数量为 0跳过下载测速。")
} }
}
// 显示最快结果
func printResult(data []CloudflareIPData) { if len(data_2) > 0 { // 如果该数字有内容,说明进行过指定条件的下载测速
if printResultNum > 0 { // 如果禁用下载测速就跳过 sort.Sort(CloudflareIPDataSetD(data_2)) // 排序
dateString := convertToString(data) // 转为多维数组 [][]String if outputFile != "" {
if len(dateString) > 0 { // IP数组长度(IP数量) 大于 0 时继续 ExportCsv(outputFile, data_2) // 输出结果到文件(指定延迟时间或下载速度的)
if len(dateString) < printResultNum { // 如果IP数组长度(IP数量) 小于 打印次数则次数改为IP数量 }
//fmt.Println("\n[信息] IP 数量小于显示结果数量(" + strconv.Itoa(printResultNum) + " < " + strconv.Itoa(len(dateString)) + "显示结果数量改为IP数量。\n") printResult(data_2) // 显示最快结果(指定延迟时间或下载速度的)
printResultNum = len(dateString) } else {
} if outputFile != "" {
fmt.Printf("%-16s%-5s%-5s%-5s%-6s%-11s\n", "IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)") ExportCsv(outputFile, data) // 输出结果到文件
for i := 0; i < printResultNum; i++ { }
fmt.Printf("%-18s%-8s%-8s%-8s%-10s%-15s\n", ipPadding(dateString[i][0]), dateString[i][1], dateString[i][2], dateString[i][3], dateString[i][4], dateString[i][5]) printResult(data) // 显示最快结果
} }
if outputFile != "" { }
fmt.Printf("\n完整测速结果已写入 %v 文件,请使用记事本/表格软件查看。\n按下 回车键 或 Ctrl+C 退出。", outputFile)
} else { // 显示最快结果
fmt.Printf("\n请按 回车键 或 Ctrl+C 退出。") func printResult(data []CloudflareIPData) {
} sysType := runtime.GOOS
var pause int if printResultNum > 0 { // 如果禁止直接输出结果就跳过
fmt.Scanln(&pause) dateString := convertToString(data) // 转为多维数组 [][]String
} else { if len(dateString) > 0 { // IP数组长度(IP数量) 大于 0 时继续
fmt.Println("\n[信息] IP数量为 0跳过输出结果。") if len(dateString) < printResultNum { // 如果IP数组长度(IP数量) 小于 打印次数则次数改为IP数量
} //fmt.Println("\n[信息] IP 数量小于显示结果数量(" + strconv.Itoa(printResultNum) + " < " + strconv.Itoa(len(dateString)) + "显示结果数量改为IP数量。\n")
} else { printResultNum = len(dateString)
fmt.Printf("\n测速结果已写入 %v 文件,请使用记事本/表格软件查看。", outputFile) }
} if ipv6Mode { // IPv6 太长了,所以需要调整一下间隔
} fmt.Printf("%-40s%-5s%-5s%-5s%-6s%-11s\n", "IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)")
for i := 0; i < printResultNum; i++ {
fmt.Printf("%-42s%-8s%-8s%-8s%-10s%-15s\n", ipPadding(dateString[i][0]), dateString[i][1], dateString[i][2], dateString[i][3], dateString[i][4], dateString[i][5])
}
} else {
fmt.Printf("%-16s%-5s%-5s%-5s%-6s%-11s\n", "IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)")
for i := 0; i < printResultNum; i++ {
fmt.Printf("%-18s%-8s%-8s%-8s%-10s%-15s\n", ipPadding(dateString[i][0]), dateString[i][1], dateString[i][2], dateString[i][3], dateString[i][4], dateString[i][5])
}
}
if versionNew != "" {
fmt.Println("\n发现新版本 [" + versionNew + "]!请前往 [https://github.com/XIU2/CloudflareSpeedTest] 更新!")
}
if sysType == "windows" { // 如果是 Windows 系统,则需要按下 回车键 或 Ctrl+C 退出
if outputFile != "" {
fmt.Printf("\n完整测速结果已写入 %v 文件,请使用记事本/表格软件查看。\n按下 回车键 或 Ctrl+C 退出。", outputFile)
} else {
fmt.Printf("\n按下 回车键 或 Ctrl+C 退出。")
}
var pause int
fmt.Scanln(&pause)
} else { // 其它系统直接退出
if outputFile != "" {
fmt.Println("\n完整测速结果已写入 " + outputFile + " 文件,请使用记事本/表格软件查看。")
}
}
} else {
fmt.Println("\n[信息] IP数量为 0跳过输出结果。")
}
} else {
fmt.Println("\n完整测速结果已写入 " + outputFile + " 文件,请使用记事本/表格软件查看。")
}
}
// 检查更新
func checkUpdate() {
timeout := time.Duration(10 * time.Second)
client := http.Client{Timeout: timeout}
res, err := client.Get("https://api.xiuer.pw/ver/cloudflarespeedtest.txt")
if err == nil {
// 读取资源数据 body: []byte
body, err := ioutil.ReadAll(res.Body)
// 关闭资源流
res.Body.Close()
if err == nil {
if string(body) != version {
versionNew = string(body)
}
}
}
}

332
tcping.go
View File

@@ -1,160 +1,172 @@
package main package main
import ( import (
"context" "context"
"io" "io"
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
"sync" "sync"
"time" "time"
"github.com/VividCortex/ewma" "github.com/VividCortex/ewma"
) )
//bool connectionSucceed float32 time //bool connectionSucceed float32 time
func tcping(ip net.IPAddr, tcpPort int) (bool, float32) { func tcping(ip net.IPAddr, tcpPort int) (bool, float32) {
startTime := time.Now() startTime := time.Now()
conn, err := net.DialTimeout("tcp", ip.String()+":"+strconv.Itoa(tcpPort), tcpConnectTimeout) var fullAddress string
if err != nil { //fmt.Println(ip.String())
return false, 0 if ipv6Mode { // IPv6 需要加上 []
} else { fullAddress = "[" + ip.String() + "]:" + strconv.Itoa(tcpPort)
var endTime = time.Since(startTime) } else {
var duration = float32(endTime.Microseconds()) / 1000.0 fullAddress = ip.String() + ":" + strconv.Itoa(tcpPort)
_ = conn.Close() }
return true, duration conn, err := net.DialTimeout("tcp", fullAddress, tcpConnectTimeout)
} if err != nil {
} return false, 0
} else {
//pingReceived pingTotalTime var endTime = time.Since(startTime)
func checkConnection(ip net.IPAddr, tcpPort int) (int, float32) { var duration = float32(endTime.Microseconds()) / 1000.0
pingRecv := 0 _ = conn.Close()
var pingTime float32 = 0.0 return true, duration
for i := 1; i <= failTime; i++ { }
pingSucceed, pingTimeCurrent := tcping(ip, tcpPort) }
if pingSucceed {
pingRecv++ //pingReceived pingTotalTime
pingTime += pingTimeCurrent func checkConnection(ip net.IPAddr, tcpPort int) (int, float32) {
} pingRecv := 0
} var pingTime float32 = 0.0
return pingRecv, pingTime for i := 1; i <= failTime; i++ {
} pingSucceed, pingTimeCurrent := tcping(ip, tcpPort)
if pingSucceed {
//return Success packetRecv averagePingTime specificIPAddr pingRecv++
func tcpingHandler(ip net.IPAddr, tcpPort int, pingCount int, progressHandler func(e progressEvent)) (bool, int, float32, net.IPAddr) { pingTime += pingTimeCurrent
ipCanConnect := false }
pingRecv := 0 }
var pingTime float32 = 0.0 return pingRecv, pingTime
for !ipCanConnect { }
pingRecvCurrent, pingTimeCurrent := checkConnection(ip, tcpPort)
if pingRecvCurrent != 0 { //return Success packetRecv averagePingTime specificIPAddr
ipCanConnect = true func tcpingHandler(ip net.IPAddr, tcpPort int, pingCount int, progressHandler func(e progressEvent)) (bool, int, float32, net.IPAddr) {
pingRecv = pingRecvCurrent ipCanConnect := false
pingTime = pingTimeCurrent pingRecv := 0
} else { var pingTime float32 = 0.0
ip.IP[15]++ for !ipCanConnect {
if ip.IP[15] == 0 { pingRecvCurrent, pingTimeCurrent := checkConnection(ip, tcpPort)
break if pingRecvCurrent != 0 {
} ipCanConnect = true
break pingRecv = pingRecvCurrent
} pingTime = pingTimeCurrent
} } else {
if ipCanConnect { ip.IP[15]++
progressHandler(AvailableIPFound) if ip.IP[15] == 0 {
for i := failTime; i < pingCount; i++ { break
pingSuccess, pingTimeCurrent := tcping(ip, tcpPort) }
progressHandler(NormalPing) break
if pingSuccess { }
pingRecv++ }
pingTime += pingTimeCurrent if ipCanConnect {
} progressHandler(AvailableIPFound)
} for i := failTime; i < pingCount; i++ {
return true, pingRecv, pingTime / float32(pingRecv), ip pingSuccess, pingTimeCurrent := tcping(ip, tcpPort)
} else { progressHandler(NormalPing)
progressHandler(NoAvailableIPFound) if pingSuccess {
return false, 0, 0, net.IPAddr{} pingRecv++
} pingTime += pingTimeCurrent
} }
}
func tcpingGoroutine(wg *sync.WaitGroup, mutex *sync.Mutex, ip net.IPAddr, tcpPort int, pingCount int, csv *[]CloudflareIPData, control chan bool, progressHandler func(e progressEvent)) { return true, pingRecv, pingTime / float32(pingRecv), ip
defer wg.Done() } else {
success, pingRecv, pingTimeAvg, currentIP := tcpingHandler(ip, tcpPort, pingCount, progressHandler) progressHandler(NoAvailableIPFound)
if success { return false, 0, 0, net.IPAddr{}
mutex.Lock() }
var cfdata CloudflareIPData }
cfdata.ip = currentIP
cfdata.pingReceived = pingRecv func tcpingGoroutine(wg *sync.WaitGroup, mutex *sync.Mutex, ip net.IPAddr, tcpPort int, pingCount int, csv *[]CloudflareIPData, control chan bool, progressHandler func(e progressEvent)) {
cfdata.pingTime = pingTimeAvg defer wg.Done()
cfdata.pingCount = pingCount success, pingRecv, pingTimeAvg, currentIP := tcpingHandler(ip, tcpPort, pingCount, progressHandler)
*csv = append(*csv, cfdata) if success {
mutex.Unlock() mutex.Lock()
} var cfdata CloudflareIPData
<-control cfdata.ip = currentIP
} cfdata.pingReceived = pingRecv
cfdata.pingTime = pingTimeAvg
func GetDialContextByAddr(fakeSourceAddr string) func(ctx context.Context, network, address string) (net.Conn, error) { cfdata.pingCount = pingCount
return func(ctx context.Context, network, address string) (net.Conn, error) { *csv = append(*csv, cfdata)
c, e := (&net.Dialer{}).DialContext(ctx, network, fakeSourceAddr) mutex.Unlock()
return c, e }
} <-control
} }
//bool : can download,float32 downloadSpeed func GetDialContextByAddr(fakeSourceAddr string) func(ctx context.Context, network, address string) (net.Conn, error) {
func DownloadSpeedHandler(ip net.IPAddr) (bool, float32) { return func(ctx context.Context, network, address string) (net.Conn, error) {
var client = http.Client{ c, e := (&net.Dialer{}).DialContext(ctx, network, fakeSourceAddr)
Transport: nil, return c, e
CheckRedirect: nil, }
Jar: nil, }
Timeout: 0,
} //bool : can download,float32 downloadSpeed
client.Transport = &http.Transport{ func DownloadSpeedHandler(ip net.IPAddr) (bool, float32) {
DialContext: GetDialContextByAddr(ip.String() + ":443"), var client = http.Client{
} Transport: nil,
response, err := client.Get(url) CheckRedirect: nil,
Jar: nil,
if err != nil { Timeout: downloadTestTime,
return false, 0 }
} else { var fullAddress string
defer func() { _ = response.Body.Close() }() if ipv6Mode { // IPv6 需要加上 []
if response.StatusCode == 200 { fullAddress = "[" + ip.String() + "]:443"
timeStart := time.Now() } else {
timeEnd := timeStart.Add(downloadTestTime) fullAddress = ip.String() + ":443"
}
contentLength := response.ContentLength client.Transport = &http.Transport{
buffer := make([]byte, downloadBufferSize) DialContext: GetDialContextByAddr(fullAddress),
}
var contentRead int64 = 0 response, err := client.Get(url)
var timeSlice = downloadTestTime / 100 if err != nil {
var timeCounter = 1 return false, 0
var lastContentRead int64 = 0 } else {
defer func() { _ = response.Body.Close() }()
var nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter)) if response.StatusCode == 200 {
e := ewma.NewMovingAverage() timeStart := time.Now()
timeEnd := timeStart.Add(downloadTestTime)
for contentLength != contentRead {
var currentTime = time.Now() contentLength := response.ContentLength
if currentTime.After(nextTime) { buffer := make([]byte, downloadBufferSize)
timeCounter += 1
nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter)) var contentRead int64 = 0
e.Add(float64(contentRead - lastContentRead)) var timeSlice = downloadTestTime / 100
lastContentRead = contentRead var timeCounter = 1
} var lastContentRead int64 = 0
if currentTime.After(timeEnd) {
break var nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
} e := ewma.NewMovingAverage()
bufferRead, err := response.Body.Read(buffer)
contentRead += int64(bufferRead) for contentLength != contentRead {
if err != nil { var currentTime = time.Now()
if err != io.EOF { if currentTime.After(nextTime) {
break timeCounter += 1
} else { nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
e.Add(float64(contentRead-lastContentRead) / (float64(nextTime.Sub(currentTime)) / float64(timeSlice))) e.Add(float64(contentRead - lastContentRead))
} lastContentRead = contentRead
} }
} if currentTime.After(timeEnd) {
return true, float32(e.Value()) / (float32(downloadTestTime.Seconds()) / 100) break
} else { }
return false, 0 bufferRead, err := response.Body.Read(buffer)
} contentRead += int64(bufferRead)
} if err != nil {
} if err != io.EOF {
break
} else {
e.Add(float64(contentRead-lastContentRead) / (float64(nextTime.Sub(currentTime)) / float64(timeSlice)))
}
}
}
return true, float32(e.Value()) / (float32(downloadTestTime.Seconds()) / 120)
} else {
return false, 0
}
}
}

303
util.go
View File

@@ -1,139 +1,164 @@
package main package main
import ( import (
"encoding/csv" "encoding/csv"
"log" "log"
"math/rand" "math/rand"
"net" "net"
"os" "os"
"strconv" "strconv"
"time" "time"
"github.com/cheggaaa/pb/v3" "github.com/cheggaaa/pb/v3"
) )
type CloudflareIPData struct { type CloudflareIPData struct {
ip net.IPAddr ip net.IPAddr
pingCount int pingCount int
pingReceived int pingReceived int
recvRate float32 recvRate float32
downloadSpeed float32 downloadSpeed float32
pingTime float32 pingTime float32
} }
func (cf *CloudflareIPData) getRecvRate() float32 { func (cf *CloudflareIPData) getRecvRate() float32 {
if cf.recvRate == 0 { if cf.recvRate == 0 {
pingLost := cf.pingCount - cf.pingReceived pingLost := cf.pingCount - cf.pingReceived
cf.recvRate = float32(pingLost) / float32(cf.pingCount) cf.recvRate = float32(pingLost) / float32(cf.pingCount)
} }
return cf.recvRate return cf.recvRate
} }
func ExportCsv(filePath string, data []CloudflareIPData) { func ExportCsv(filePath string, data []CloudflareIPData) {
fp, err := os.Create(filePath) fp, err := os.Create(filePath)
if err != nil { if err != nil {
log.Fatalf("创建文件["+filePath+"]句柄失败,%v", err) log.Fatalf("创建文件["+filePath+"]句柄失败,%v", err)
return return
} }
defer fp.Close() defer fp.Close()
w := csv.NewWriter(fp) //创建一个新的写入文件流 w := csv.NewWriter(fp) //创建一个新的写入文件流
w.Write([]string{"IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)"}) w.Write([]string{"IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)"})
w.WriteAll(convertToString(data)) w.WriteAll(convertToString(data))
w.Flush() w.Flush()
} }
func (cf *CloudflareIPData) toString() []string { func (cf *CloudflareIPData) toString() []string {
result := make([]string, 6) result := make([]string, 6)
result[0] = cf.ip.String() result[0] = cf.ip.String()
result[1] = strconv.Itoa(cf.pingCount) result[1] = strconv.Itoa(cf.pingCount)
result[2] = strconv.Itoa(cf.pingReceived) result[2] = strconv.Itoa(cf.pingReceived)
result[3] = strconv.FormatFloat(float64(cf.getRecvRate()), 'f', 2, 32) result[3] = strconv.FormatFloat(float64(cf.getRecvRate()), 'f', 2, 32)
result[4] = strconv.FormatFloat(float64(cf.pingTime), 'f', 2, 32) result[4] = strconv.FormatFloat(float64(cf.pingTime), 'f', 2, 32)
result[5] = strconv.FormatFloat(float64(cf.downloadSpeed)/1024/1024, 'f', 2, 32) result[5] = strconv.FormatFloat(float64(cf.downloadSpeed)/1024/1024, 'f', 2, 32)
return result return result
} }
func convertToString(data []CloudflareIPData) [][]string { func convertToString(data []CloudflareIPData) [][]string {
result := make([][]string, 0) result := make([][]string, 0)
for _, v := range data { for _, v := range data {
result = append(result, v.toString()) result = append(result, v.toString())
} }
return result return result
} }
var pingTime int var pingTime int
var pingRoutine int var pingRoutine int
var ipEndWith uint8 = 0 type progressEvent int
type progressEvent int const (
NoAvailableIPFound progressEvent = iota
const ( AvailableIPFound
NoAvailableIPFound progressEvent = iota NormalPing
AvailableIPFound )
NormalPing
) var url string
var url string var downloadTestTime time.Duration
var downloadTestTime time.Duration const downloadBufferSize = 1024
const downloadBufferSize = 1024 var downloadTestCount int
var downloadTestCount int //const defaultTcpPort = 443
const tcpConnectTimeout = time.Second * 1
//const defaultTcpPort = 443
const tcpConnectTimeout = time.Second * 1 var failTime int
var failTime int // 平均延迟排序(丢包另算)
type CloudflareIPDataSet []CloudflareIPData
type CloudflareIPDataSet []CloudflareIPData
// 下载速度排序
func initRandSeed() { type CloudflareIPDataSetD []CloudflareIPData
rand.Seed(time.Now().UnixNano())
} func initRandSeed() {
rand.Seed(time.Now().UnixNano())
func randipEndWith() { }
ipEndWith = uint8(rand.Intn(254) + 1)
} func randipEndWith(num int) uint8 {
return uint8(rand.Intn(num))
func ipPadding(ip string) string { }
var ipLength int
var ipPrint string func GetRandomString() string {
ipPrint = ip str := "0123456789abcdef"
ipLength = len(ipPrint) bytes := []byte(str)
if ipLength < 15 { result := []byte{}
for i := 0; i <= 15-ipLength; i++ { r := rand.New(rand.NewSource(time.Now().UnixNano()))
ipPrint += " " for i := 0; i < 4; i++ {
} result = append(result, bytes[r.Intn(len(bytes))])
} }
return ipPrint return string(result)
} }
func handleProgressGenerator(pb *pb.ProgressBar) func(e progressEvent) { func ipPadding(ip string) string {
return func(e progressEvent) { var ipLength int
switch e { var ipPrint string
case NoAvailableIPFound: ipPrint = ip
pb.Add(pingTime) ipLength = len(ipPrint)
case AvailableIPFound: if ipLength < 15 {
pb.Add(failTime) for i := 0; i <= 15-ipLength; i++ {
case NormalPing: ipPrint += " "
pb.Increment() }
} }
} return ipPrint
} }
func (cfs CloudflareIPDataSet) Len() int { func handleProgressGenerator(pb *pb.ProgressBar) func(e progressEvent) {
return len(cfs) return func(e progressEvent) {
} switch e {
case NoAvailableIPFound:
func (cfs CloudflareIPDataSet) Less(i, j int) bool { pb.Add(pingTime)
if (cfs)[i].getRecvRate() != cfs[j].getRecvRate() { case AvailableIPFound:
return cfs[i].getRecvRate() < cfs[j].getRecvRate() pb.Add(failTime)
} case NormalPing:
return cfs[i].pingTime < cfs[j].pingTime pb.Increment()
} }
}
func (cfs CloudflareIPDataSet) Swap(i, j int) { }
cfs[i], cfs[j] = cfs[j], cfs[i]
} func (cfs CloudflareIPDataSet) Len() int {
return len(cfs)
}
func (cfs CloudflareIPDataSet) Less(i, j int) bool {
if (cfs)[i].getRecvRate() != cfs[j].getRecvRate() {
return cfs[i].getRecvRate() < cfs[j].getRecvRate()
}
return cfs[i].pingTime < cfs[j].pingTime
}
func (cfs CloudflareIPDataSet) Swap(i, j int) {
cfs[i], cfs[j] = cfs[j], cfs[i]
}
func (cfs CloudflareIPDataSetD) Len() int {
return len(cfs)
}
func (cfs CloudflareIPDataSetD) Less(i, j int) bool {
return cfs[i].downloadSpeed > cfs[j].downloadSpeed
}
func (cfs CloudflareIPDataSetD) Swap(i, j int) {
cfs[i], cfs[j] = cfs[j], cfs[i]
}