5 Commits

Author SHA1 Message Date
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
6 changed files with 91 additions and 46 deletions

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)
} }

View File

@@ -1,4 +1,10 @@
# 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)
[![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)
国外很多网站都在使用 Cloudflare CDN但分配给中国访客的 IP 并不友好。 国外很多网站都在使用 Cloudflare CDN但分配给中国访客的 IP 并不友好。
虽然 Cloudflare 公开了所有 [IP 段](https://www.cloudflare.com/ips/) ,但想要在这么多 IP 中找到适合自己的,怕是要累死,所以就有了这个软件。 虽然 Cloudflare 公开了所有 [IP 段](https://www.cloudflare.com/ips/) ,但想要在这么多 IP 中找到适合自己的,怕是要累死,所以就有了这个软件。
@@ -14,8 +20,8 @@
测速完毕后,会把结果保存在当前目录下的 `result.csv` 文件中(只输出丢包率 50% 以下的),用记事本打开,排序为**延迟由低到高**,每一列用逗号分隔,分别是: 测速完毕后,会把结果保存在当前目录下的 `result.csv` 文件中(只输出丢包率 50% 以下的),用记事本打开,排序为**延迟由低到高**,每一列用逗号分隔,分别是:
``` ```
IP 地址, Ping 发送次数, Ping 接收次数, Ping 接收率, 平均延迟, 下载速度 (MB/s) IP 地址, 测试次数, 成功次数, 成功比率, 平均延迟, 下载速度 (MB/s)
104.27.70.18, 4, 4, 1.0000, 150.7948, 12.8951 104.27.70.18, 4, 4, 1.00, 150.79, 12.89
``` ```
选择一个平均延迟与下载速度都不错的 IP 放到 `Hosts` 文件中(指向域名)。 选择一个平均延迟与下载速度都不错的 IP 放到 `Hosts` 文件中(指向域名)。
@@ -32,37 +38,41 @@ https://github.com/XIU2/CloudflareSpeedTest
参数: 参数:
-n 500 -n 500
测速线程数量;请勿超过1000 (默认 500) 测速线程数量;数值越大速度越快请勿超过1000(结果误差大)(默认 500)
-t 4 -t 4
延迟测速次数;单个 IP 测速次数TCP协议 (默认 4) 延迟测速次数;单个 IP 测速次数,为 1 时将过滤丢包的IPTCP协议(默认 4)
-dn 20 -dn 20
下载测速数量;延迟测速后,从最低延迟起测试下载速度的数量,请勿太多 (默认 20) 下载测速数量;延迟测速并排序后,从最低延迟起测试下载速度的数量,请勿太多(速度慢)(默认 20)
-dt 10 -dt 10
下载测速时间;单个 IP 测速最长时间,单位:秒 (默认 10) 下载测速时间;单个 IP 测速最长时间,单位:秒(默认 10)
-f ip.txt
IP 数据文件;支持相对路径和绝对路径,如果包含空格请前后加上引号;(默认 ip.txt)
-dd
禁用下载测速;如果带上该参数就是禁用下载测速;(默认 启用)
-v -v
打印程序版本 打印程序版本
-h -h
打印帮助说明 打印帮助说明
示例: 示例:
WindowsCloudflareST.exe -n 800 -t 4 -dn 20 -dt 10 CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10
LinuxCloudflareST -n 800 -t 4 -dn 20 -dt 10 CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10 -f "C:\abc\ip.txt" -dd
``` ```
#### 使用示例 #### 使用示例
在 CMD 中运行,或者把启动参数添加到快捷方式中。 在 CMD 中运行,或者把启动参数添加到快捷方式中。
> **注意:** 不需要四个参数都加上,如果你认为某个参数默认就很合适,那就跳过 > **注意:** 不需要通顺加上所有参数,按需选择,参数前后顺序随意
``` cmd ``` cmd
# CMD 示例 # CMD 示例
CloudflareST.exe -n 800 -t 4 -dn 20 -dt 10 CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10
``` ```
``` cmd ``` cmd
# 快捷方式示例(右键快捷方式 - 目标) # 快捷方式示例(右键快捷方式 - 目标)
## 如果有引号就放在引号外面,记得引号和 - 之间有空格。 ## 如果有引号就放在引号外面,记得引号和 - 之间有空格。
"D:\Program Files\CloudflareST\CloudflareST.exe" -n 800 -t 4 -dn 20 -dt 10 "D:\Program Files\CloudflareST\CloudflareST.exe" -n 500 -t 4 -dn 20 -dt 10
``` ```
**** ****

2
go.mod
View File

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

83
main.go
View File

@@ -12,6 +12,8 @@ import (
) )
var version string var version string
var disableDownload bool
var ipFile string
func init() { func init() {
var downloadSecond int64 var downloadSecond int64
@@ -23,27 +25,32 @@ https://github.com/XIU2/CloudflareSpeedTest
参数: 参数:
-n 500 -n 500
测速线程数量;请勿超过1000 (默认 500) 测速线程数量;数值越大速度越快请勿超过1000(结果误差大)(默认 500)
-t 4 -t 4
延迟测速次数;单个 IP 测速次数TCP协议 (默认 4) 延迟测速次数;单个 IP 测速次数,为 1 时将过滤丢包的IPTCP协议(默认 4)
-dn 20 -dn 20
下载测速数量;延迟测速后,从最低延迟起测试下载速度的数量,请勿太多 (默认 20) 下载测速数量;延迟测速并排序后,从最低延迟起测试下载速度的数量,请勿太多(速度慢)(默认 20)
-dt 10 -dt 10
下载测速时间;单个 IP 测速最长时间,单位:秒 (默认 10) 下载测速时间;单个 IP 测速最长时间,单位:秒(默认 10)
-f ip.txt
IP 数据文件;支持相对路径和绝对路径,如果包含空格请前后加上引号;(默认 ip.txt)
-dd
禁用下载测速;如果带上该参数就是禁用下载测速;(默认 启用)
-v -v
打印程序版本 打印程序版本
-h -h
打印帮助说明 打印帮助说明
示例: 示例:
WindowsCloudflareST.exe -n 800 -t 4 -dn 20 -dt 10 CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10
LinuxCloudflareST -n 800 -t 4 -dn 20 -dt 10 CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10 -f "C:\abc\ip.txt" -dd`
`
pingRoutine = *flag.Int("n", 500, "测速线程数量") flag.IntVar(&pingRoutine, "n", 500, "测速线程数量")
pingTime = *flag.Int("t", 4, "延迟测速次数") flag.IntVar(&pingTime, "t", 4, "延迟测速次数")
downloadTestCount = *flag.Int("dn", 20, "下载测速数量") flag.IntVar(&downloadTestCount, "dn", 20, "下载测速数量")
flag.Int64Var(&downloadSecond, "dt", 10, "下载测速时间") flag.Int64Var(&downloadSecond, "dt", 10, "下载测速时间")
flag.BoolVar(&disableDownload, "dd", false, "禁用下载测速")
flag.StringVar(&ipFile, "f", "ip.txt", "IP 数据文件")
flag.BoolVar(&printVersion, "v", false, "打印程序版本") flag.BoolVar(&printVersion, "v", false, "打印程序版本")
downloadTestTime = time.Duration(downloadSecond) * time.Second downloadTestTime = time.Duration(downloadSecond) * time.Second
@@ -54,36 +61,62 @@ https://github.com/XIU2/CloudflareSpeedTest
println(version) println(version)
os.Exit(0) os.Exit(0)
} }
if pingRoutine <= 0 {
pingRoutine = 500
}
if pingTime <= 0 {
pingTime = 4
}
if downloadTestCount <= 0 {
downloadTestCount = 20
}
if downloadSecond <= 0 {
downloadSecond = 10
}
if ipFile == "" {
ipFile = "ip.txt"
}
} }
func main() { func main() {
initipEndWith() initipEndWith() // 随机数
ips := loadFirstIPOfRangeFromFile() failTime = pingTime // 设置接收次数
pingCount := len(ips) * pingTime ips := loadFirstIPOfRangeFromFile(ipFile) // 读入IP
bar := pb.StartNew(pingCount) pingCount := len(ips) * pingTime // 计算进度条总数IP*测试次数)
bar := pb.Full.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("开始延迟测速(TCP)") fmt.Println("开始延迟测速(TCP)")
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, 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("开始下载测速:") if !disableDownload { // 如果禁用下载测速就跳过
for i := 0; i < downloadTestCount; i++ { if len(data) > 0 { // IP数组长度(IP数量) 大于 0 时继续
_, speed := DownloadSpeedHandler(data[i].ip) if len(data) < downloadTestCount { // 如果IP数组长度(IP数量) 小于 下载测速次数则次数改为IP数
data[i].downloadSpeed = speed downloadTestCount = len(data)
bar.Add(1) 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跳过下载测速。")
}
} }
bar.Finish() ExportCsv("./result.csv", data) // 输出结果
ExportCsv("./result.csv", data)
} }

View File

@@ -2,13 +2,14 @@ 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
@@ -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

View File

@@ -48,9 +48,9 @@ func (cf *CloudflareIPData) toString() []string {
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
} }
@@ -85,7 +85,8 @@ 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