42 Commits

Author SHA1 Message Date
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
xiu2
306ce709c9 update 2020-11-08 10:48:24 +08:00
xiu2
956a35cab0 update 2020-11-08 10:43:26 +08:00
xiu2
3d49bb13ed update 2020-11-08 10:33:46 +08:00
xiu2
3ddd66b3c1 update 2020-11-07 10:38:36 +08:00
xiu2
9aa64db555 新增 自定义下载测速地址功能(-url https://xxx) 2020-11-07 10:07:00 +08:00
xiu2
9654cb8ea6 优化 下载测速文件大小 2020-11-06 12:32:23 +08:00
xiu2
13bae9c6f8 update 2020-11-06 10:01:35 +08:00
xiu2
f0fa3e4d0a update 2020-11-05 23:30:27 +08:00
xiu2
b83734b426 update 2020-11-05 23:26:18 +08:00
xiu2
c1348df16e update 2020-11-05 08:56:36 +08:00
xiu2
c52750ad9c update 2020-11-05 08:52:09 +08:00
xiu2
0e9461f3b7 update 2020-10-22 13:29:03 +08:00
xiu2
07e20028cc 修复 下载测速失效的问题 2020-10-07 02:27:56 +08:00
xiu2
4c92eae311 不输出结果文件 -o "" 改为 -o " " 2020-09-05 17:47:03 +08:00
xiu2
efdbc8f08e 优化直接输出结果排版;成功比率改为丢包率 2020-09-04 15:43:16 +08:00
xiu2
e85a03c651 新增 自定义TCP端口 功能等 2020-09-03 20:07:15 +08:00
xiu2
5d00d7c5ff 新增 自定义TCP端口 功能。 2020-09-03 19:36:09 +08:00
xiu2
92d5216259 v1.2.3 新增 测速后 直接显示结果 功能 2020-09-02 21:21:45 +08:00
xiu2
63e2bc00ee Update 2020-09-01 17:51:38 +08:00
xiu2
5664055c84 Update 2020-09-01 17:48:03 +08:00
xiu2
c6bfd01dd5 Update 2020-09-01 17:46:48 +08:00
xiu2
383f4c979b v1.2.2 新增自定义输出结果文件功能等 2020-09-01 17:45:18 +08:00
xiu2
31743a8138 v1.2.1 2020-09-01 12:43:51 +08:00
xiu2
479629b84e Update 2020-09-01 09:29:14 +08:00
xiu2
03a1b44e88 Update 2020-09-01 02:58:43 +08:00
xiu2
963dfa68ed v1.2.0 2020-09-01 02:26:38 +08:00
xiu2
6417339312 v1.2.0 2020-09-01 02:21:16 +08:00
xiu2
edf5dee039 Update 2020-08-31 18:51:45 +08:00
xiu2
12a9fd2ffb Update 2020-08-31 17:44:39 +08:00
xiu2
d73b1374bc Update 2020-08-31 17:31:48 +08:00
XIU2
68899b53fb Update README.md 2020-08-31 12:03:46 +08:00
XIU2
df0b12f018 Add files via upload 2020-08-31 11:31:31 +08:00
XIU2
31dbf635a8 Update README.md 2020-08-31 11:30:18 +08:00
XIU2
8e3ee6f2d0 Update README.md 2020-08-31 01:49:19 +08:00
XIU2
4af4d4d3c8 Update README.md 2020-08-31 01:35:16 +08:00
XIU2
7290f04a6b Update README.md 2020-08-31 01:08:22 +08:00
XIU2
9ea310a4d8 Update README.md 2020-08-31 00:50:42 +08:00
XIU2
2dfea884a2 新增 命令行启动参数功能 2020-08-31 00:44:45 +08:00
8 changed files with 328 additions and 118 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
dist dist
Releases

View File

@@ -1,44 +0,0 @@
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
before:
hooks:
# you may remove this if you don't use vgo
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...
builds:
- env:
- CGO_ENABLED=0
id: "CloudflareScanner"
binary: "CloudflareScanner"
goos:
- darwin
- freebsd
- linux
- windows
goarch:
- 386
- amd64
- arm
#hooks:
#post: ./compile.bat "{{ dir .Path }}"
archives:
- replacements:
darwin: MacOS
linux: Linux
windows: Windows
freebsd: Freebsd
386: x86
amd64: x64
files:
- ip.txt
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "v1.1.0"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

View File

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

139
README.md
View File

@@ -1,10 +1,135 @@
# CloudflareScanner # XIU2/CloudflareSpeedTest
本项目可以测试Cloudflare节点接入速度 [![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)
[![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 Fork](https://img.shields.io/github/forks/XIU2/CloudflareSpeedTest.svg?style=flat-square&label=Fork&color=f38020)](https://github.com/XIU2/CloudflareSpeedTest/network/members)
程序运行完毕后,结果会保存在当前目录的`result.csv` 国外很多网站都在使用 Cloudflare CDN但分配给中国访客的 IP 并不友好。
虽然 Cloudflare 公开了所有 [IP 段](https://www.cloudflare.com/ips/) ,但想要在这么多 IP 中找到适合自己的,怕是要累死,所以就有了这个软件。
## 注意事项 该软件可以**测试 Cloudflare CDN 所有 IP 的延迟和速度,获得最快 IP**
#### 协程数请不要调过1000否则容易出现较大误差 你可以将 IP 添加到 `Hosts` 文件中,以提高访问使用 Cloudflare CDN 的网站速度!
#### 编译选项为 `go build`
#### 您可在release界面下载或编译运行 ****
## 快速使用
### 下载运行
1. 下载编译好的可执行文件 [蓝奏云](https://xiu.lanzoux.com/b0742hkxe) / [Github](https://github.com/XIU2/CloudflareSpeedTest/releases) 并解压。
2. 双击运行 `CloudflareST.exe`文件Windows等待测速...
> **注意Linux 系统**请先赋予执行权限 `chmod +x CloudflareST` ,然后再执行 `./CloudflareST` 。
### 结果示例
测速完毕后,会直接显示**最快的 20 个 IP**,示例:
```
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
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
```
完整结果保存在当前目录下的 `result.csv` 文件中,用**记事本/表格软件**打开,排序为**延迟由低到高**,分别是:
```
IP 地址, 已发送, 已接收, 丢包率, 平均延迟, 下载速度 (MB/s)
104.27.199.141, 4, 4, 0.00, 139.52, 11.71
```
选择一个平均延迟与下载速度都不错的 IP 放到 `Hosts` 文件中(指向使用 Cloudflare CDN 的网站域名)。
****
## 进阶使用
直接双击运行使用的是默认参数,如果想要测试速度更快、测试结果更全面,可以自定义参数。
> **提示Linux 系统**只需要把下面命令中的 **.exe 删除**即可通用。
``` cmd
C:\>CloudflareST.exe -h
CloudflareSpeedTest
测试 Cloudflare CDN 所有 IP 的延迟和速度,获取最快 IP
https://github.com/XIU2/CloudflareSpeedTest
参数:
-n 500
测速线程数量;数值越大速度越快,请勿超过 1000(结果误差大)(默认 500)
-t 4
延迟测速次数;单个 IP 测速次数,为 1 时将过滤丢包的IPTCP协议(默认 4)
-tp 443
延迟测速端口;延迟测速 TCP 协议的端口;(默认 443)
-dn 20
下载测速数量;延迟测速并排序后,从最低延迟起下载测速数量,请勿太多(速度慢)(默认 20)
-dt 5
下载测速时间;单个 IP 测速最长时间,单位:秒;(默认 5)
-url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
下载测速地址;用来 Cloudflare CDN 测速的文件地址,如含有空格请加上引号;
-p 20
显示结果数量;测速后直接显示指定数量的结果,为 0 时不显示结果直接退出;(默认 20)
-f ip.txt
IP 数据文件;如含有空格请加上引号;支持其他 CDN IP段记得禁用下载测速(默认 ip.txt)
-o result.csv
输出结果文件;如含有空格请加上引号;为空格时不输出结果文件(-o " ");允许其他后缀;(默认 result.csv)
-dd
禁用下载测速;如果带上该参数就是禁用下载测速;(默认 启用)
-v
打印程序版本
-h
打印帮助说明
```
> 如果**下载速度都是 0.00**,那说明默认的**下载测速地址**用的人太多又到上限了,**请去这个 [Issues](https://github.com/XIU2/CloudflareSpeedTest/issues/6) 获得解决方法!**
### 使用示例
在 CMD 中运行,或者把启动参数添加到快捷方式中。
> **注意:** 不需要加上所有参数,按需选择,参数前后顺序随意。
> **提示: Linux 系统**只需要把下面命令中的 **.exe 删除**即可通用。
``` cmd
# 命令行示例
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 5
# 指定 IP数据文件不显示结果直接退出-p 值为 0
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 5 -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数据文件 及 输出结果到文件(相对路径,即当前目录下,如果包含空格请加上引号)
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 5 -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
# 指定下载测速地址(要求:可以直接下载的文件、文件大小超过 200MB、网站用的是 Cloudflare CDN如果包含空格请加上引号
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 5 -url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
```
``` cmd
# 快捷方式示例(右键快捷方式 - 目标)
## 如果有引号就放在引号外面,记得引号和 - 之间有空格。
"D:\Program Files\CloudflareST\CloudflareST.exe" -n 500 -t 4 -dn 20 -dt 5
```
****
## 感谢项目
* https://github.com/Spedoske/CloudflareScanner
意外发现了这个项目,看了之后发现正好解决了我的问题,但是我更喜欢用户命令行方式运行,这样会更方便、有更多使用姿势,于是我临时学了下 Golang 并 Fork 按照我自己的需求修改了一下(包括但不限于命令行方式交互、直接输出结果等),如果有什么问题可以告诉我,虽然我不一定会~
****
## 许可证
The GPL-3.0 License.

2
go.mod
View File

@@ -1,4 +1,4 @@
module CloudflareIPScanner module CloudflareSpeedTest
go 1.14 go 1.14

185
main.go
View File

@@ -1,67 +1,176 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"github.com/cheggaaa/pb/v3" "os"
"sort" "sort"
"strconv"
"sync" "sync"
"time" "time"
"github.com/cheggaaa/pb/v3"
) )
func handleUserInput() { var version string
fmt.Println("请输入扫描协程数(数字越大越快,默认400):") var disableDownload bool
fmt.Scanln(&pingRoutine) var tcpPort int
if pingRoutine <= 0 { var ipFile string
pingRoutine = 400 var outputFile string
} var printResult int
fmt.Println("请输入tcping次数(默认10):")
fmt.Scanln(&pingTime) func init() {
if pingTime <= 0 {
pingTime = 10
}
fmt.Println("请输入要测试的下载节点个数(默认10):")
fmt.Scanln(&downloadTestCount)
if downloadTestCount <= 0 {
downloadTestCount = 10
}
fmt.Println("请输入下载测试时间(默认10,单位为秒):")
var downloadSecond int64 var downloadSecond int64
fmt.Scanln(&downloadSecond) var printVersion bool
var help = `
CloudflareSpeedTest ` + version + `
测试 Cloudflare CDN 所有 IP 的延迟和速度,获取最快 IP
https://github.com/XIU2/CloudflareSpeedTest
参数:
-n 500
测速线程数量;数值越大速度越快,请勿超过 1000(结果误差大)(默认 500)
-t 4
延迟测速次数;单个 IP 测速次数,为 1 时将过滤丢包的IPTCP协议(默认 4)
-tp 443
延迟测速端口;延迟测速 TCP 协议的端口;(默认 443)
-dn 20
下载测速数量;延迟测速并排序后,从最低延迟起下载测速数量,请勿太多(速度慢)(默认 20)
-dt 5
下载测速时间;单个 IP 测速最长时间,单位:秒;(默认 5)
-url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
下载测速地址;用来 Cloudflare CDN 测速的文件地址,如含有空格请加上引号;
-p 20
显示结果数量;测速后直接显示指定数量的结果,值为 0 时不显示结果直接退出;(默认 20)
-f ip.txt
IP 数据文件;如含有空格请加上引号;支持其他 CDN IP段记得禁用下载测速(默认 ip.txt)
-o result.csv
输出结果文件;如含有空格请加上引号;为空格时不输出结果文件(-o " ");允许其他后缀;(默认 result.csv)
-dd
禁用下载测速;如果带上该参数就是禁用下载测速;(默认 启用)
-v
打印程序版本
-h
打印帮助说明`
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://cf.xiu2.xyz/Github/CloudflareSpeedTest.png", "下载测速地址")
flag.IntVar(&printResult, "p", 20, "显示结果数量")
flag.BoolVar(&disableDownload, "dd", false, "禁用下载测速")
flag.StringVar(&ipFile, "f", "ip.txt", "IP 数据文件")
flag.StringVar(&outputFile, "o", "result.csv", "输出结果文件")
flag.BoolVar(&printVersion, "v", false, "打印程序版本")
downloadTestTime = time.Duration(downloadSecond) * time.Second
flag.Usage = func() { fmt.Print(help) }
flag.Parse()
if printVersion {
println(version)
os.Exit(0)
}
if pingRoutine <= 0 {
pingRoutine = 500
}
if pingTime <= 0 {
pingTime = 4
}
if tcpPort < 1 || tcpPort > 65535 {
tcpPort = 443
}
if downloadTestCount <= 0 {
downloadTestCount = 20
}
if downloadSecond <= 0 { if downloadSecond <= 0 {
downloadSecond = 10 downloadSecond = 10
} }
downloadTestTime = time.Duration(downloadSecond) * time.Second if url == "" {
url = "https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png"
}
if printResult == 0 {
printResult = 20
}
if ipFile == "" {
ipFile = "ip.txt"
}
if outputFile == " " {
outputFile = ""
}
} }
func main() { func main() {
initipEndWith() initRandSeed() // 置随机数种子
handleUserInput() failTime = pingTime // 设置接收次数
ips := loadFirstIPOfRangeFromFile() ips := loadFirstIPOfRangeFromFile(ipFile) // 读入IP
pingCount := len(ips) * pingTime pingCount := len(ips) * pingTime // 计算进度条总数IP*测试次数)
bar := pb.StartNew(pingCount) bar := pb.Simple.Start(pingCount) // 进度条总数
var wg sync.WaitGroup var wg sync.WaitGroup
var mu sync.Mutex var mu sync.Mutex
var data = make([]CloudflareIPData, 0) var data = make([]CloudflareIPData, 0)
fmt.Println("开始tcping") fmt.Println("# XIU2/CloudflareSpeedTest " + version + "\n")
fmt.Println("开始延迟测速模式TCP端口" + strconv.Itoa(tcpPort) + "")
control := make(chan bool, pingRoutine) control := make(chan bool, pingRoutine)
for _, ip := range ips { for _, ip := range ips {
wg.Add(1) wg.Add(1)
control <- false control <- false
handleProgress := handleProgressGenerator(bar) handleProgress := handleProgressGenerator(bar) // 多线程进度条
go tcpingGoroutine(&wg, &mu, ip, pingTime, &data, control, handleProgress) go tcpingGoroutine(&wg, &mu, ip, tcpPort, pingTime, &data, control, handleProgress)
} }
wg.Wait() wg.Wait()
bar.Finish() bar.Finish()
bar = pb.StartNew(downloadTestCount)
sort.Sort(CloudflareIPDataSet(data)) sort.Sort(CloudflareIPDataSet(data)) // 排序
fmt.Println("开始下载测速")
for i := 0; i < downloadTestCount; i++ { // 下载测速
_, speed := DownloadSpeedHandler(data[i].ip) if !disableDownload { // 如果禁用下载测速就跳过
data[i].downloadSpeed = speed if len(data) > 0 { // IP数组长度(IP数量) 大于 0 时继续
bar.Add(1) if len(data) < downloadTestCount { // 如果IP数组长度(IP数量) 小于 下载测速次数则次数改为IP数
downloadTestCount = len(data)
fmt.Println("\n[信息] IP数量小于下载测速次数下载测速次数改为IP数。\n")
}
bar = pb.Simple.Start(downloadTestCount)
fmt.Println("开始下载测速:")
for i := 0; i < downloadTestCount; i++ {
_, speed := DownloadSpeedHandler(data[i].ip)
data[i].downloadSpeed = speed
bar.Add(1)
}
bar.Finish()
} else {
fmt.Println("\n[信息] IP数量为 0跳过下载测速。")
}
}
if outputFile != "" {
ExportCsv(outputFile, data) // 输出结果到文件
}
// 直接输出结果
if printResult > 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")
}
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", 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)
} else {
fmt.Printf("\n请按 回车键 或 Ctrl+C 退出。")
}
var pause int
fmt.Scanln(&pause)
} else {
fmt.Println("\n[信息] IP数量为 0跳过输出结果。")
}
} }
bar.Finish()
ExportCsv("./result.csv", data)
} }

View File

@@ -2,19 +2,20 @@ package main
import ( import (
"context" "context"
"github.com/VividCortex/ewma"
"io" "io"
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
"sync" "sync"
"time" "time"
"github.com/VividCortex/ewma"
) )
//bool connectionSucceed float32 time //bool connectionSucceed float32 time
func tcping(ip net.IPAddr) (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(defaultTcpPort), tcpConnectTimeout) conn, err := net.DialTimeout("tcp", ip.String()+":"+strconv.Itoa(tcpPort), tcpConnectTimeout)
if err != nil { if err != nil {
return false, 0 return false, 0
} else { } else {
@@ -26,11 +27,11 @@ func tcping(ip net.IPAddr) (bool, float32) {
} }
//pingReceived pingTotalTime //pingReceived pingTotalTime
func checkConnection(ip net.IPAddr) (int, float32) { func checkConnection(ip net.IPAddr, tcpPort int) (int, float32) {
pingRecv := 0 pingRecv := 0
var pingTime float32 = 0.0 var pingTime float32 = 0.0
for i := 1; i <= failTime; i++ { for i := 1; i <= failTime; i++ {
pingSucceed, pingTimeCurrent := tcping(ip) pingSucceed, pingTimeCurrent := tcping(ip, tcpPort)
if pingSucceed { if pingSucceed {
pingRecv++ pingRecv++
pingTime += pingTimeCurrent pingTime += pingTimeCurrent
@@ -40,12 +41,12 @@ func checkConnection(ip net.IPAddr) (int, float32) {
} }
//return Success packetRecv averagePingTime specificIPAddr //return Success packetRecv averagePingTime specificIPAddr
func tcpingHandler(ip net.IPAddr, pingCount int, progressHandler func(e progressEvent)) (bool, int, float32, net.IPAddr) { func tcpingHandler(ip net.IPAddr, tcpPort int, pingCount int, progressHandler func(e progressEvent)) (bool, int, float32, net.IPAddr) {
ipCanConnect := false ipCanConnect := false
pingRecv := 0 pingRecv := 0
var pingTime float32 = 0.0 var pingTime float32 = 0.0
for !ipCanConnect { for !ipCanConnect {
pingRecvCurrent, pingTimeCurrent := checkConnection(ip) pingRecvCurrent, pingTimeCurrent := checkConnection(ip, tcpPort)
if pingRecvCurrent != 0 { if pingRecvCurrent != 0 {
ipCanConnect = true ipCanConnect = true
pingRecv = pingRecvCurrent pingRecv = pingRecvCurrent
@@ -61,7 +62,7 @@ func tcpingHandler(ip net.IPAddr, pingCount int, progressHandler func(e progress
if ipCanConnect { if ipCanConnect {
progressHandler(AvailableIPFound) progressHandler(AvailableIPFound)
for i := failTime; i < pingCount; i++ { for i := failTime; i < pingCount; i++ {
pingSuccess, pingTimeCurrent := tcping(ip) pingSuccess, pingTimeCurrent := tcping(ip, tcpPort)
progressHandler(NormalPing) progressHandler(NormalPing)
if pingSuccess { if pingSuccess {
pingRecv++ pingRecv++
@@ -75,9 +76,9 @@ func tcpingHandler(ip net.IPAddr, pingCount int, progressHandler func(e progress
} }
} }
func tcpingGoroutine(wg *sync.WaitGroup, mutex *sync.Mutex, ip net.IPAddr, pingCount int, csv *[]CloudflareIPData, control chan bool, progressHandler func(e progressEvent)) { func tcpingGoroutine(wg *sync.WaitGroup, mutex *sync.Mutex, ip net.IPAddr, tcpPort int, pingCount int, csv *[]CloudflareIPData, control chan bool, progressHandler func(e progressEvent)) {
defer wg.Done() defer wg.Done()
success, pingRecv, pingTimeAvg, currentIP := tcpingHandler(ip, pingCount, progressHandler) success, pingRecv, pingTimeAvg, currentIP := tcpingHandler(ip, tcpPort, pingCount, progressHandler)
if success { if success {
mutex.Lock() mutex.Lock()
var cfdata CloudflareIPData var cfdata CloudflareIPData
@@ -130,7 +131,7 @@ func DownloadSpeedHandler(ip net.IPAddr) (bool, float32) {
var nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter)) var nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
e := ewma.NewMovingAverage() e := ewma.NewMovingAverage()
for ; contentLength != contentRead; { for contentLength != contentRead {
var currentTime = time.Now() var currentTime = time.Now()
if currentTime.After(nextTime) { if currentTime.After(nextTime) {
timeCounter += 1 timeCounter += 1

45
util.go
View File

@@ -2,13 +2,14 @@ package main
import ( import (
"encoding/csv" "encoding/csv"
"github.com/cheggaaa/pb/v3"
"log" "log"
"math/rand" "math/rand"
"net" "net"
"os" "os"
"strconv" "strconv"
"time" "time"
"github.com/cheggaaa/pb/v3"
) )
type CloudflareIPData struct { type CloudflareIPData struct {
@@ -22,7 +23,8 @@ type CloudflareIPData struct {
func (cf *CloudflareIPData) getRecvRate() float32 { func (cf *CloudflareIPData) getRecvRate() float32 {
if cf.recvRate == 0 { if cf.recvRate == 0 {
cf.recvRate = float32(cf.pingReceived) / float32(cf.pingCount) pingLost := cf.pingCount - cf.pingReceived
cf.recvRate = float32(pingLost) / float32(cf.pingCount)
} }
return cf.recvRate return cf.recvRate
} }
@@ -35,21 +37,19 @@ func ExportCsv(filePath string, data []CloudflareIPData) {
} }
defer fp.Close() defer fp.Close()
w := csv.NewWriter(fp) //创建一个新的写入文件流 w := csv.NewWriter(fp) //创建一个新的写入文件流
w.Write([]string{"IP Address", "Ping count", "Ping received", "Ping received rate", "Ping time", "Download Speed (MB/s)"}) w.Write([]string{"IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)"})
w.WriteAll(convertToString(data)) w.WriteAll(convertToString(data))
w.Flush() w.Flush()
} }
//"IP Address","Ping Count","Ping received","Ping received rate","Ping time","Download speed"
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', 4, 32) result[3] = strconv.FormatFloat(float64(cf.getRecvRate()), 'f', 2, 32)
result[4] = strconv.FormatFloat(float64(cf.pingTime), 'f', 4, 32) result[4] = strconv.FormatFloat(float64(cf.pingTime), 'f', 2, 32)
result[5] = strconv.FormatFloat(float64(cf.downloadSpeed)/1024/1024, 'f', 4, 32) result[5] = strconv.FormatFloat(float64(cf.downloadSpeed)/1024/1024, 'f', 2, 32)
return result return result
} }
@@ -74,7 +74,7 @@ const (
NormalPing NormalPing
) )
const url string = "https://apple.freecdn.workers.dev/105/media/us/iphone-11-pro/2019/3bd902e4-0752-4ac1-95f8-6225c32aec6d/films/product/iphone-11-pro-product-tpl-cc-us-2019_1280x720h.mp4" var url string
var downloadTestTime time.Duration var downloadTestTime time.Duration
@@ -82,17 +82,34 @@ const downloadBufferSize = 1024
var downloadTestCount int var downloadTestCount int
const defaultTcpPort = 443 //const defaultTcpPort = 443
const tcpConnectTimeout = time.Second * 1 const tcpConnectTimeout = time.Second * 1
const failTime = 4
var failTime int
type CloudflareIPDataSet []CloudflareIPData type CloudflareIPDataSet []CloudflareIPData
func initipEndWith() { func initRandSeed() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
}
func randipEndWith() {
ipEndWith = uint8(rand.Intn(254) + 1) 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) { func handleProgressGenerator(pb *pb.ProgressBar) func(e progressEvent) {
return func(e progressEvent) { return func(e progressEvent) {
switch e { switch e {
@@ -112,7 +129,7 @@ func (cfs CloudflareIPDataSet) Len() int {
func (cfs CloudflareIPDataSet) Less(i, j int) bool { func (cfs CloudflareIPDataSet) Less(i, j int) bool {
if (cfs)[i].getRecvRate() != cfs[j].getRecvRate() { if (cfs)[i].getRecvRate() != cfs[j].getRecvRate() {
return cfs[i].getRecvRate() > cfs[j].getRecvRate() return cfs[i].getRecvRate() < cfs[j].getRecvRate()
} }
return cfs[i].pingTime < cfs[j].pingTime return cfs[i].pingTime < cfs[j].pingTime
} }