7 Commits

Author SHA1 Message Date
xiu2
40b22f660a 新增 指定延迟时间上限、下载速度下限条件 2020-11-11 18:10:53 +08:00
xiu2
12039f4850 修复 -p 0 时没有直接退出程序的问题;优化 代码 2020-11-10 21:03:09 +08:00
xiu2
129deeaf71 update 2020-11-10 19:50:43 +08:00
xiu2
3de6b38e00 优化 IP最后一段完全随机 2020-11-10 19:22:55 +08:00
xiu2
8c0e8732cc 新增 版本号标识 2020-11-10 15:55:52 +08:00
xiu2
8820c5f982 update 2020-11-10 15:35:24 +08:00
xiu2
d50c4806a6 调整 默认下载测速地址为自建地址 2020-11-08 16:46:27 +08:00
4 changed files with 131 additions and 69 deletions

View File

@@ -21,8 +21,9 @@ func loadFirstIPOfRangeFromFile(ipFile string) []net.IPAddr {
if err != nil {
log.Fatal(err)
}
firstIP[15] = ipEndWith
for IPRange.Contains(firstIP) {
randipEndWith() // 随机 IP 的最后一段
firstIP[15] = ipEndWith
firstIPCopy := make([]byte, len(firstIP))
copy(firstIPCopy, firstIP)
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})

View File

@@ -9,7 +9,7 @@
国外很多网站都在使用 Cloudflare CDN但分配给中国访客的 IP 并不友好。
虽然 Cloudflare 公开了所有 [IP 段](https://www.cloudflare.com/ips/) ,但想要在这么多 IP 中找到适合自己的,怕是要累死,所以就有了这个软件。
该软件可以**测试 Cloudflare CDN 所有 IP 的延迟和速度,获得最快 IP**
该软件可以**测试 Cloudflare CDN 所有 IP 的延迟和速度,获得最快 IP**觉得好用请**点个⭐~**
你可以将 IP 添加到 `Hosts` 文件中,以提高访问使用 Cloudflare CDN 的网站速度!
****
@@ -28,24 +28,26 @@
```
IP 地址 已发送 已接收 丢包率 平均延迟 下载速度 (MB/s)
104.27.200.105 4 4 0.00 135.77 13.21
104.27.205.105 4 4 0.00 136.65 17.33
104.27.207.105 4 4 0.00 136.97 5.55
104.27.193.105 4 4 0.00 146.97 16.54
104.27.206.105 4 4 0.00 148.04 1.65
104.27.192.105 4 4 0.00 148.97 9.08
104.27.195.105 4 4 0.00 150.29 21.00
104.27.197.105 4 4 0.00 151.04 13.61
104.27.203.105 4 4 0.00 151.54 15.73
104.27.204.105 4 4 0.00 151.94 8.32
104.27.199.141 4 4 0.00 139.52 11.71
104.22.73.158 4 4 0.00 141.38 6.74
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
104.22.77.24 4 4 0.00 146.00 5.86
104.22.66.140 4 4 0.00 146.50 9.47
104.22.78.104 4 4 0.00 146.75 13.00
104.22.69.208 4 4 0.00 147.00 19.07
104.27.194.10 4 4 0.00 148.02 21.05
...
```
> 如果**下载速度都是 0.00**,那说明默认的**下载测速地址**用的人太多又到上限了,**请去这个 [Issues](https://github.com/XIU2/CloudflareSpeedTest/issues/6) 获得解决方法!**
完整结果保存在当前目录下的 `result.csv` 文件中,用**记事本/表格软件**打开,排序为**延迟由低到高**,分别是:
> 大家可以按照自己的需求,对完整测速数据**进一步筛选处理**
```
IP 地址, 已发送, 已接收, 丢包率, 平均延迟, 下载速度 (MB/s)
104.27.200.105, 4, 4, 0.00, 135.77, 13.21
104.27.199.141, 4, 4, 0.00, 139.52, 11.71
```
选择一个平均延迟与下载速度都不错的 IP 放到 `Hosts` 文件中(指向使用 Cloudflare CDN 的网站域名)。
@@ -55,9 +57,6 @@ IP 地址, 已发送, 已接收, 丢包率, 平均延迟, 下载速度 (MB/s)
直接双击运行使用的是默认参数,如果想要测试速度更快、测试结果更全面,可以自定义参数。
> **提示Linux 系统**只需要把下面命令中的 **.exe 删除**即可通用。
> **提示:下载测速地址**可以去这个 **[Issues](https://github.com/XIU2/CloudflareSpeedTest/issues/6)** 中获得、提交、交流!
``` cmd
C:\>CloudflareST.exe -h
@@ -76,9 +75,12 @@ https://github.com/XIU2/CloudflareSpeedTest
下载测速数量;延迟测速并排序后,从最低延迟起下载测速数量,请勿太多(速度慢)(默认 20)
-dt 5
下载测速时间;单个 IP 测速最长时间,单位:秒;(默认 5)
-url https://speed.cloudflare.com/__down?bytes=209715200
下载测速地址;用来测速的网站地址,要求:可以直接下载的文件、文件大小超过 200MB、网站用的是 Cloudflare CDN
默认下载测速地址用的人太多,容易出现超出上限被限制的情况,建议自行寻找合适的下载测速地址!(如含有空格请加上引号)
-url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
下载测速地址;用来 Cloudflare CDN 测速的文件地址,如含有空格请加上引号
-tl 200
延迟时间上限;只输出指定延迟时间以下的结果,数量为 -dn 参数的值单位ms
-sl 5
下载速度下限;只输出指定下载速度以上的结果,数量为 -dn 参数的值单位MB/s
-p 20
显示结果数量;测速后直接显示指定数量的结果,为 0 时不显示结果直接退出;(默认 20)
-f ip.txt
@@ -93,31 +95,40 @@ https://github.com/XIU2/CloudflareSpeedTest
打印帮助说明
```
> 如果**下载速度都是 0.00**,那说明默认的**下载测速地址**用的人太多又到上限了,**请去这个 [Issues](https://github.com/XIU2/CloudflareSpeedTest/issues/6) 获得解决方法!**
### 使用示例
在 CMD 中运行,或者把启动参数添加到快捷方式中。
> **注意:** 不需要加上所有参数,按需选择,参数前后顺序随意。
> **提示: Linux 系统**只需要把下面命令中的 **.exe 删除**即可通用。
> **提示: 下载测速地址**可以去这个 **[Issues](https://github.com/XIU2/CloudflareSpeedTest/issues/6)** 中获得、提交、交流!
``` cmd
# 命令行示例
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 5
``` bash
# 注意:各参数均有默认值,只有不使用默认值时,才需要手动指定参数的值(按需选择),参数不分前后顺序。
# 提示: Linux 系统只需要把下面命令中的 .exe 删除即可通用。
# 指定 IP数据文件不显示结果直接退出-p 值为 0
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 5 -p 0 -f "ip.txt" -dd
CloudflareST.exe -p 0 -f ip.txt -dd
# 指定 IP数据文件不输出结果到文件直接显示结果-p 值为 20 条)
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 5 -p 20 -f "ip.txt" -o " " -dd
# 指定 IP数据文件不输出结果到文件直接显示结果-p 值为 10 条)
CloudflareST.exe -p 10 -f ip.txt -o " " -dd
# 指定 IP数据文件 及 输出结果到文件(相对路径,即当前目录下,如果包含空格请加上引号)
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 5 -f ip.txt -o result.csv -dd
CloudflareST.exe -f ip.txt -o result.csv -dd
# 指定 IP数据文件 及 输出结果到文件(绝对路径,即 C:\abc\ 目录下,如果包含空格请加上引号)
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 5 -f C:\abc\ip.txt -o C:\abc\result.csv -dd
CloudflareST.exe -f C:\abc\ip.txt -o C:\abc\result.csv -dd
# 指定下载测速地址(要求:可以直接下载的文件、文件大小超过 200MB、网站用的是 Cloudflare CDN如果包含空格请加上引号
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 5 -url https://speed.cloudflare.com/__down?bytes=209715200
CloudflareST.exe -url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
# 指定测速条件延迟时间上限200 ms下载速度下限0 MB/s数量10 个(只有同时满足三个条件时才会停止测速)
CloudflareST.exe -tl 200 -dn 10
# 指定测速条件延迟时间上限0 ms下载速度下限5 MB/s数量10 个(只有同时满足三个条件时才会停止测速)
CloudflareST.exe -sl 5 -dn 10
# 指定测速条件延迟时间上限200 ms下载速度下限5 MB/s数量10 个(只有同时满足三个条件时才会停止测速)
CloudflareST.exe -tl 200 -sl 5 -dn 10
```
``` cmd

108
main.go
View File

@@ -17,13 +17,15 @@ var disableDownload bool
var tcpPort int
var ipFile string
var outputFile string
var printResult int
var printResultNum int
var timeLimit int
var speedLimit int
func init() {
var downloadSecond int64
var printVersion bool
const help = `
CloudflareSpeedTest
var help = `
CloudflareSpeedTest ` + version + `
测试 Cloudflare CDN 所有 IP 的延迟和速度,获取最快 IP
https://github.com/XIU2/CloudflareSpeedTest
@@ -38,9 +40,12 @@ https://github.com/XIU2/CloudflareSpeedTest
下载测速数量;延迟测速并排序后,从最低延迟起下载测速数量,请勿太多(速度慢)(默认 20)
-dt 5
下载测速时间;单个 IP 测速最长时间,单位:秒;(默认 5)
-url https://speed.cloudflare.com/__down?bytes=209715200
下载测速地址;用来测速的网站地址,要求:可以直接下载的文件、文件大小超过 200MB、网站用的是 Cloudflare CDN
默认下载测速地址用的人太多,容易出现超出上限被限制的情况,建议自行寻找合适的下载测速地址!(如含有空格请加上引号)
-url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
下载测速地址;用来 Cloudflare CDN 测速的文件地址,如含有空格请加上引号
-tl 200
延迟时间上限;只输出指定延迟时间以下的结果,数量为 -dn 参数的值单位ms
-sl 5
下载速度下限;只输出指定下载速度以上的结果,数量为 -dn 参数的值单位MB/s
-p 20
显示结果数量;测速后直接显示指定数量的结果,值为 0 时不显示结果直接退出;(默认 20)
-f ip.txt
@@ -52,24 +57,17 @@ https://github.com/XIU2/CloudflareSpeedTest
-v
打印程序版本
-h
打印帮助说明
示例:
注意:不需要加上所有参数(以下仅为示例),按需选择,参数前后顺序随意
CloudflareST -n 500 -t 4 -dn 20 -dt 5
CloudflareST -n 500 -t 4 -dn 20 -dt 5 -p 0 -f "ip.txt" -dd
CloudflareST -n 500 -t 4 -dn 20 -dt 5 -p 20 -f "ip.txt" -o " " -dd
CloudflareST -n 500 -t 4 -dn 20 -dt 5 -f "ip.txt" -o "result.csv" -dd
CloudflareST -n 500 -t 4 -dn 20 -dt 5 -f "C:\abc\ip.txt" -o "C:\abc\result.csv" -dd
CloudflareST -n 500 -t 4 -dn 20 -dt 5 -url https://speed.cloudflare.com/__down?bytes=209715200`
打印帮助说明`
flag.IntVar(&pingRoutine, "n", 500, "测速线程数量")
flag.IntVar(&pingTime, "t", 4, "延迟测速次数")
flag.IntVar(&tcpPort, "tp", 443, "延迟测速端口")
flag.IntVar(&downloadTestCount, "dn", 20, "下载测速数量")
flag.Int64Var(&downloadSecond, "dt", 5, "下载测速时间")
flag.StringVar(&url, "url", "https://speed.cloudflare.com/__down?bytes=209715200", "下载测速地址")
flag.IntVar(&printResult, "p", 20, "显示结果数量")
flag.StringVar(&url, "url", "https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png", "下载测速地址")
flag.IntVar(&timeLimit, "tl", 0, "延迟时间上限")
flag.IntVar(&speedLimit, "sl", 0, "下载速度下限")
flag.IntVar(&printResultNum, "p", 20, "显示结果数量")
flag.BoolVar(&disableDownload, "dd", false, "禁用下载测速")
flag.StringVar(&ipFile, "f", "ip.txt", "IP 数据文件")
flag.StringVar(&outputFile, "o", "result.csv", "输出结果文件")
@@ -99,10 +97,16 @@ https://github.com/XIU2/CloudflareSpeedTest
downloadSecond = 10
}
if url == "" {
url = "https://speed.cloudflare.com/__down?bytes=209715200"
url = "https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png"
}
if printResult == 0 {
printResult = 20
if timeLimit <= 0 {
timeLimit = 9999
}
if speedLimit < 0 {
speedLimit = 0
}
if printResultNum < 0 {
printResultNum = 20
}
if ipFile == "" {
ipFile = "ip.txt"
@@ -113,15 +117,17 @@ https://github.com/XIU2/CloudflareSpeedTest
}
func main() {
initipEndWith() // 随机数
initRandSeed() // 随机数种子
failTime = pingTime // 设置接收次数
ips := loadFirstIPOfRangeFromFile(ipFile) // 读入IP
pingCount := len(ips) * pingTime // 计算进度条总数IP*测试次数)
bar := pb.Full.Start(pingCount) // 进度条总数
bar := pb.Simple.Start(pingCount) // 进度条总数
var wg sync.WaitGroup
var mu sync.Mutex
var data = make([]CloudflareIPData, 0)
var data_2 = make([]CloudflareIPData, 0)
fmt.Println("# XIU2/CloudflareSpeedTest " + version + "\n")
fmt.Println("开始延迟测速模式TCP端口" + strconv.Itoa(tcpPort) + "")
control := make(chan bool, pingRoutine)
for _, ip := range ips {
@@ -139,15 +145,31 @@ func main() {
if !disableDownload { // 如果禁用下载测速就跳过
if len(data) > 0 { // IP数组长度(IP数量) 大于 0 时继续
if len(data) < downloadTestCount { // 如果IP数组长度(IP数量) 小于 下载测速次数则次数改为IP数
//fmt.Println("\n[信息] IP 数量小于下载测速次数(" + strconv.Itoa(downloadTestCount) + " < " + strconv.Itoa(len(data)) + "下载测速次数改为IP数。\n")
downloadTestCount = len(data)
fmt.Println("\n[信息] IP数量小于下载测速次数下载测速次数改为IP数。\n")
}
bar = pb.Simple.Start(downloadTestCount)
fmt.Println("开始下载测速:")
for i := 0; i < downloadTestCount; i++ {
var downloadTestCount_2 int // 临时的下载测速次数
if timeLimit == 9999 && speedLimit == 0 {
downloadTestCount_2 = downloadTestCount // 如果没有指定条件,则临时的下载次数变量为下载测速次数
fmt.Println("开始下载测速:")
} else if timeLimit > 0 || speedLimit >= 0 {
downloadTestCount_2 = len(data) // 如果指定了任意一个条件,则临时的下载次数变量改为总数量
fmt.Println("开始下载测速(延迟时间上限:" + strconv.Itoa(timeLimit) + " ms下载速度下限" + strconv.Itoa(speedLimit) + " MB/s")
}
bar = pb.Simple.Start(downloadTestCount_2)
for i := 0; i < downloadTestCount_2; i++ {
_, speed := DownloadSpeedHandler(data[i].ip)
data[i].downloadSpeed = speed
bar.Add(1)
if int(data[i].pingTime) <= timeLimit && int(float64(speed)/1024/1024) >= speedLimit {
data_2 = append(data_2, data[i]) // 延迟和速度均满足条件时,添加到新数组中
if len(data_2) == downloadTestCount { // 满足条件的 IP =下载测速次数,则跳出循环
break
}
} else if int(data[i].pingTime) > timeLimit {
break
}
}
bar.Finish()
} else {
@@ -155,24 +177,34 @@ func main() {
}
}
if outputFile != "" {
ExportCsv(outputFile, data) // 输出结果到文件
if len(data_2) > 0 { // 如果该数字有内容,说明进行过指定条件的下载测速
if outputFile != "" {
ExportCsv(outputFile, data_2) // 输出结果到文件(指定延迟时间或下载速度的)
}
printResult(data_2) // 显示最快结果(指定延迟时间或下载速度的)
} else {
if outputFile != "" {
ExportCsv(outputFile, data) // 输出结果到文件
}
printResult(data) // 显示最快结果
}
}
// 直接输出结果
if printResult > 0 { // 如果禁用下载测速就跳过
// 显示最快结果
func printResult(data []CloudflareIPData) {
if printResultNum > 0 { // 如果禁用下载测速就跳过
dateString := convertToString(data) // 转为多维数组 [][]String
if len(dateString) > 0 { // IP数组长度(IP数量) 大于 0 时继续
if len(dateString) < printResult { // 如果IP数组长度(IP数量) 小于 打印次数则次数改为IP数量
printResult = len(dateString)
fmt.Println("\n[信息] IP数量小于显示结果数量显示结果数量改为IP数量。\n")
if len(dateString) < printResultNum { // 如果IP数组长度(IP数量) 小于 打印次数则次数改为IP数量
//fmt.Println("\n[信息] IP 数量小于显示结果数量(" + strconv.Itoa(printResultNum) + " < " + strconv.Itoa(len(dateString)) + "显示结果数量改为IP数量。\n")
printResultNum = len(dateString)
}
fmt.Printf("%-16s%-5s%-5s%-5s%-6s%-11s\n", "IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)")
for i := 0; i < printResult; i++ {
fmt.Printf("%-18s%-8s%-8s%-8s%-10s%-15s\n", dateString[i][0], dateString[i][1], dateString[i][2], dateString[i][3], dateString[i][4], dateString[i][5])
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 outputFile != "" {
fmt.Printf("\n完整内容请查看 %v 文件。请按 回车键 或 Ctrl+C 退出。", outputFile)
fmt.Printf("\n完整测速结果已写入 %v 文件,请使用记事本/表格软件查看。\n按下 回车键 或 Ctrl+C 退出。", outputFile)
} else {
fmt.Printf("\n请按 回车键 或 Ctrl+C 退出。")
}
@@ -181,5 +213,7 @@ func main() {
} else {
fmt.Println("\n[信息] IP数量为 0跳过输出结果。")
}
} else {
fmt.Printf("\n测速结果已写入 %v 文件,请使用记事本/表格软件查看。", outputFile)
}
}

18
util.go
View File

@@ -89,11 +89,27 @@ var failTime int
type CloudflareIPDataSet []CloudflareIPData
func initipEndWith() {
func initRandSeed() {
rand.Seed(time.Now().UnixNano())
}
func randipEndWith() {
ipEndWith = uint8(rand.Intn(254) + 1)
}
func ipPadding(ip string) string {
var ipLength int
var ipPrint string
ipPrint = ip
ipLength = len(ipPrint)
if ipLength < 15 {
for i := 0; i <= 15-ipLength; i++ {
ipPrint += " "
}
}
return ipPrint
}
func handleProgressGenerator(pb *pb.ProgressBar) func(e progressEvent) {
return func(e progressEvent) {
switch e {