2020-07-05 13:19:53 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2020-08-31 00:44:45 +08:00
|
|
|
|
"flag"
|
2020-07-05 13:19:53 +08:00
|
|
|
|
"fmt"
|
2020-08-31 00:44:45 +08:00
|
|
|
|
"os"
|
2020-07-09 10:52:26 +08:00
|
|
|
|
"sort"
|
2020-07-05 13:19:53 +08:00
|
|
|
|
"sync"
|
2020-07-09 10:52:26 +08:00
|
|
|
|
"time"
|
2020-08-31 00:44:45 +08:00
|
|
|
|
|
|
|
|
|
"github.com/cheggaaa/pb/v3"
|
2020-07-05 13:19:53 +08:00
|
|
|
|
)
|
|
|
|
|
|
2020-08-31 00:44:45 +08:00
|
|
|
|
var version string
|
|
|
|
|
|
|
|
|
|
func init() {
|
2020-07-09 10:52:26 +08:00
|
|
|
|
var downloadSecond int64
|
2020-08-31 00:44:45 +08:00
|
|
|
|
var printVersion bool
|
|
|
|
|
const help = `CloudflareSpeedTest
|
2020-08-31 11:31:31 +08:00
|
|
|
|
测试 Cloudflare CDN 所有 IP 的延迟和速度,获取最佳 IP!
|
|
|
|
|
https://github.com/XIU2/CloudflareSpeedTest
|
2020-08-31 00:44:45 +08:00
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
-n 500
|
2020-08-31 11:31:31 +08:00
|
|
|
|
测速线程数量;请勿超过1000 (默认 500)
|
2020-08-31 00:44:45 +08:00
|
|
|
|
-t 4
|
2020-08-31 17:31:48 +08:00
|
|
|
|
延迟测速次数;单个 IP 测速次数,TCP协议 (默认 4)
|
2020-08-31 11:31:31 +08:00
|
|
|
|
-dn 20
|
|
|
|
|
下载测速数量;延迟测速后,从最低延迟起测试下载速度的数量,请勿太多 (默认 20)
|
2020-08-31 00:44:45 +08:00
|
|
|
|
-dt 10
|
2020-08-31 17:31:48 +08:00
|
|
|
|
下载测速时间;单个 IP 测速最长时间,单位:秒 (默认 10)
|
2020-08-31 00:44:45 +08:00
|
|
|
|
-v
|
|
|
|
|
打印程序版本
|
|
|
|
|
-h
|
|
|
|
|
打印帮助说明
|
|
|
|
|
|
|
|
|
|
示例:
|
2020-08-31 11:31:31 +08:00
|
|
|
|
Windows:CloudflareST.exe -n 800 -t 4 -dn 20 -dt 10
|
|
|
|
|
Linux:CloudflareST -n 800 -t 4 -dn 20 -dt 10
|
2020-08-31 00:44:45 +08:00
|
|
|
|
`
|
|
|
|
|
|
2020-08-31 17:31:48 +08:00
|
|
|
|
pingRoutine = *flag.Int("n", 500, "测速线程数量")
|
|
|
|
|
pingTime = *flag.Int("t", 4, "延迟测速次数")
|
|
|
|
|
downloadTestCount = *flag.Int("dn", 20, "下载测速数量")
|
|
|
|
|
flag.Int64Var(&downloadSecond, "dt", 10, "下载测速时间")
|
2020-08-31 00:44:45 +08:00
|
|
|
|
flag.BoolVar(&printVersion, "v", false, "打印程序版本")
|
|
|
|
|
|
2020-07-09 10:52:26 +08:00
|
|
|
|
downloadTestTime = time.Duration(downloadSecond) * time.Second
|
2020-08-31 00:44:45 +08:00
|
|
|
|
|
|
|
|
|
flag.Usage = func() { fmt.Print(help) }
|
|
|
|
|
flag.Parse()
|
|
|
|
|
if printVersion {
|
|
|
|
|
println(version)
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
2020-07-05 13:19:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 10:52:26 +08:00
|
|
|
|
func main() {
|
|
|
|
|
initipEndWith()
|
|
|
|
|
ips := loadFirstIPOfRangeFromFile()
|
|
|
|
|
pingCount := len(ips) * pingTime
|
2020-07-05 13:19:53 +08:00
|
|
|
|
bar := pb.StartNew(pingCount)
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
var mu sync.Mutex
|
2020-07-09 10:52:26 +08:00
|
|
|
|
var data = make([]CloudflareIPData, 0)
|
|
|
|
|
|
2020-08-31 00:44:45 +08:00
|
|
|
|
fmt.Println("开始延迟测速(TCP):")
|
2020-07-09 10:52:26 +08:00
|
|
|
|
|
|
|
|
|
control := make(chan bool, pingRoutine)
|
|
|
|
|
for _, ip := range ips {
|
2020-07-05 13:19:53 +08:00
|
|
|
|
wg.Add(1)
|
2020-07-09 10:52:26 +08:00
|
|
|
|
control <- false
|
|
|
|
|
handleProgress := handleProgressGenerator(bar)
|
|
|
|
|
go tcpingGoroutine(&wg, &mu, ip, pingTime, &data, control, handleProgress)
|
2020-07-05 13:19:53 +08:00
|
|
|
|
}
|
|
|
|
|
wg.Wait()
|
|
|
|
|
bar.Finish()
|
2020-07-09 10:52:26 +08:00
|
|
|
|
bar = pb.StartNew(downloadTestCount)
|
|
|
|
|
sort.Sort(CloudflareIPDataSet(data))
|
2020-08-31 00:44:45 +08:00
|
|
|
|
fmt.Println("开始下载测速:")
|
2020-07-09 10:52:26 +08:00
|
|
|
|
for i := 0; i < downloadTestCount; i++ {
|
|
|
|
|
_, speed := DownloadSpeedHandler(data[i].ip)
|
|
|
|
|
data[i].downloadSpeed = speed
|
|
|
|
|
bar.Add(1)
|
|
|
|
|
}
|
|
|
|
|
bar.Finish()
|
|
|
|
|
ExportCsv("./result.csv", data)
|
2020-07-05 13:19:53 +08:00
|
|
|
|
}
|