mirror of
https://github.com/XIU2/CloudflareSpeedTest.git
synced 2026-03-08 15:45:53 +08:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d00d7c5ff | ||
|
|
92d5216259 | ||
|
|
63e2bc00ee | ||
|
|
5664055c84 | ||
|
|
c6bfd01dd5 | ||
|
|
383f4c979b | ||
|
|
31743a8138 |
25
README.md
25
README.md
@@ -18,7 +18,7 @@
|
||||
1. 下载编译好的可执行文件 [蓝奏云](https://www.lanzoux.com/b0742hkxe) / [Github](https://github.com/XIU2/CloudflareSpeedTest/releases) 并解压。
|
||||
2. 双击运行 `CloudflareST.exe`文件(Windows系统),等待测速...
|
||||
|
||||
测速完毕后,会把结果保存在当前目录下的 `result.csv` 文件中(只输出丢包率 50% 以下的),用记事本打开,排序为**延迟由低到高**,每一列用逗号分隔,分别是:
|
||||
测速完毕后,会显示最快的 20 个 IP,完整结果则保存在当前目录下的 `result.csv` 文件中,用记事本打开,排序为**延迟由低到高**,每一列用逗号分隔,分别是:
|
||||
```
|
||||
IP 地址, 测试次数, 成功次数, 成功比率, 平均延迟, 下载速度 (MB/s)
|
||||
104.27.70.18, 4, 4, 1.00, 150.79, 12.89
|
||||
@@ -38,15 +38,21 @@ https://github.com/XIU2/CloudflareSpeedTest
|
||||
|
||||
参数:
|
||||
-n 500
|
||||
测速线程数量;数值越大速度越快,请勿超过1000(结果误差大);(默认 500)
|
||||
测速线程数量;数值越大速度越快,请勿超过 1000(结果误差大);(默认 500)
|
||||
-t 4
|
||||
延迟测速次数;单个 IP 测速次数,为 1 时将过滤丢包的IP,TCP协议;(默认 4)
|
||||
-tp 443
|
||||
延迟测速端口;延迟测速 TCP 协议的端口;(默认 443)
|
||||
-dn 20
|
||||
下载测速数量;延迟测速并排序后,从最低延迟起测试下载速度的数量,请勿太多(速度慢);(默认 20)
|
||||
下载测速数量;延迟测速并排序后,从最低延迟起下载测速数量,请勿太多(速度慢);(默认 20)
|
||||
-dt 10
|
||||
下载测速时间;单个 IP 测速最长时间,单位:秒;(默认 10)
|
||||
-p 20
|
||||
直接显示结果;测速后直接显示指定数量的结果,为 -1 时不显示结果直接退出;(默认 20)
|
||||
-f ip.txt
|
||||
IP 数据文件;支持相对路径和绝对路径,如果包含空格请前后加上引号;(默认 ip.txt)
|
||||
IP 数据文件;相对/绝对路径,如包含空格请加上引号;支持其他 CDN IP段,记得禁用下载测速;(默认 ip.txt)
|
||||
-o result.csv
|
||||
输出结果文件;相对/绝对路径,如包含空格请加上引号;允许 .txt 等后缀;(默认 result.csv)
|
||||
-dd
|
||||
禁用下载测速;如果带上该参数就是禁用下载测速;(默认 启用)
|
||||
-v
|
||||
@@ -56,17 +62,24 @@ https://github.com/XIU2/CloudflareSpeedTest
|
||||
|
||||
示例:
|
||||
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10
|
||||
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10 -f "C:\abc\ip.txt" -dd
|
||||
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10 -f "ip.txt" -o "result.csv" -dd
|
||||
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10 -f "C:\abc\ip.txt" -o "C:\abc\result.csv" -dd
|
||||
```
|
||||
|
||||
#### 使用示例
|
||||
|
||||
在 CMD 中运行,或者把启动参数添加到快捷方式中。
|
||||
> **注意:** 不需要通顺加上所有参数,按需选择,参数前后顺序随意。
|
||||
> **注意:** 不需要加上所有参数,按需选择,参数前后顺序随意。
|
||||
|
||||
``` cmd
|
||||
# CMD 示例
|
||||
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10
|
||||
|
||||
# 指定 IP数据文件 及 输出结果文件(相对路径,即当前目录下)
|
||||
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10 -f "ip.txt" -o "result.csv" -dd
|
||||
|
||||
# 指定 IP数据文件 及 输出结果文件(绝对路径,即 C:\abc\ 目录下)
|
||||
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10 -f "C:\abc\ip.txt" -o "C:\abc\result.csv" -dd
|
||||
```
|
||||
|
||||
``` cmd
|
||||
|
||||
103
main.go
103
main.go
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -13,7 +14,10 @@ import (
|
||||
|
||||
var version string
|
||||
var disableDownload bool
|
||||
var tcpPort int
|
||||
var ipFile string
|
||||
var outputFile string
|
||||
var printResult int
|
||||
|
||||
func init() {
|
||||
var downloadSecond int64
|
||||
@@ -25,15 +29,21 @@ https://github.com/XIU2/CloudflareSpeedTest
|
||||
|
||||
参数:
|
||||
-n 500
|
||||
测速线程数量;数值越大速度越快,请勿超过1000(结果误差大);(默认 500)
|
||||
测速线程数量;数值越大速度越快,请勿超过 1000(结果误差大);(默认 500)
|
||||
-t 4
|
||||
延迟测速次数;单个 IP 测速次数,为 1 时将过滤丢包的IP,TCP协议;(默认 4)
|
||||
-tp 443
|
||||
延迟测速端口;延迟测速 TCP 协议的端口;(默认 443)
|
||||
-dn 20
|
||||
下载测速数量;延迟测速并排序后,从最低延迟起测试下载速度的数量,请勿太多(速度慢);(默认 20)
|
||||
下载测速数量;延迟测速并排序后,从最低延迟起下载测速数量,请勿太多(速度慢);(默认 20)
|
||||
-dt 10
|
||||
下载测速时间;单个 IP 测速最长时间,单位:秒;(默认 10)
|
||||
-p 20
|
||||
直接显示结果;测速后直接显示指定数量的结果,为 -1 时不显示结果直接退出;(默认 20)
|
||||
-f ip.txt
|
||||
IP 数据文件;支持相对路径和绝对路径,如果包含空格请前后加上引号;(默认 ip.txt)
|
||||
IP 数据文件;相对/绝对路径,如包含空格请加上引号;支持其他 CDN IP段,记得禁用下载测速;(默认 ip.txt)
|
||||
-o result.csv
|
||||
输出结果文件;相对/绝对路径,如包含空格请加上引号;允许 .txt 等后缀;(默认 result.csv)
|
||||
-dd
|
||||
禁用下载测速;如果带上该参数就是禁用下载测速;(默认 启用)
|
||||
-v
|
||||
@@ -43,14 +53,18 @@ https://github.com/XIU2/CloudflareSpeedTest
|
||||
|
||||
示例:
|
||||
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10
|
||||
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10 -f "C:\abc\ip.txt" -dd`
|
||||
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10 -f "ip.txt" -o "result.csv" -dd
|
||||
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10 -f "C:\abc\ip.txt" -o "C:\abc\result.csv" -dd`
|
||||
|
||||
flag.IntVar(&pingRoutine, "n", 500, "测速线程数量")
|
||||
flag.IntVar(&pingTime, "t", 4, "延迟测速次数")
|
||||
flag.IntVar(&tcpPort, "tp", 443, "延迟测速端口")
|
||||
flag.IntVar(&downloadTestCount, "dn", 20, "下载测速数量")
|
||||
flag.Int64Var(&downloadSecond, "dt", 10, "下载测速时间")
|
||||
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
|
||||
@@ -61,6 +75,30 @@ https://github.com/XIU2/CloudflareSpeedTest
|
||||
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 {
|
||||
downloadSecond = 10
|
||||
}
|
||||
if printResult == 0 {
|
||||
printResult = 20
|
||||
}
|
||||
if ipFile == "" {
|
||||
ipFile = "ip.txt"
|
||||
}
|
||||
if outputFile == "" {
|
||||
outputFile = "result.csv"
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -73,28 +111,59 @@ func main() {
|
||||
var mu sync.Mutex
|
||||
var data = make([]CloudflareIPData, 0)
|
||||
|
||||
fmt.Println("开始延迟测速(TCP):")
|
||||
|
||||
fmt.Println("开始延迟测速(模式:TCP,端口:" + strconv.Itoa(tcpPort) + "):")
|
||||
control := make(chan bool, pingRoutine)
|
||||
for _, ip := range ips {
|
||||
wg.Add(1)
|
||||
control <- false
|
||||
handleProgress := handleProgressGenerator(bar)
|
||||
go tcpingGoroutine(&wg, &mu, ip, pingTime, &data, control, handleProgress)
|
||||
handleProgress := handleProgressGenerator(bar) // 多线程进度条
|
||||
go tcpingGoroutine(&wg, &mu, ip, tcpPort, pingTime, &data, control, handleProgress)
|
||||
}
|
||||
wg.Wait()
|
||||
bar.Finish()
|
||||
|
||||
sort.Sort(CloudflareIPDataSet(data)) // 排序
|
||||
if !disableDownload { // 如果禁用下载测速就跳过
|
||||
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)
|
||||
|
||||
// 下载测速
|
||||
if !disableDownload { // 如果禁用下载测速就跳过
|
||||
if len(data) > 0 { // IP数组长度(IP数量) 大于 0 时继续
|
||||
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,跳过下载测速。")
|
||||
}
|
||||
bar.Finish()
|
||||
}
|
||||
ExportCsv("./result.csv", 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.Println("\nIP 地址 \t", "测试次数\t", "成功次数\t", "成功比率\t", "平均延迟\t", "下载速度 (MB/s)")
|
||||
for i := 0; i < printResult; i++ {
|
||||
fmt.Println(dateString[i][0], "\t", dateString[i][1], "\t\t", dateString[i][2], "\t\t", dateString[i][3], "\t\t", dateString[i][4], "\t", dateString[i][5])
|
||||
}
|
||||
fmt.Printf("\n完整内容请查看 %v 文件。请按 回车键 或 Ctrl+C 退出。", outputFile)
|
||||
var pause int
|
||||
fmt.Scanln(&pause)
|
||||
} else {
|
||||
fmt.Println("\n[信息] IP数量为 0,跳过输出结果。")
|
||||
}
|
||||
}
|
||||
|
||||
// 输出结果到文件
|
||||
ExportCsv(outputFile, data)
|
||||
}
|
||||
|
||||
23
tcping.go
23
tcping.go
@@ -2,19 +2,20 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/VividCortex/ewma"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/VividCortex/ewma"
|
||||
)
|
||||
|
||||
//bool connectionSucceed float32 time
|
||||
func tcping(ip net.IPAddr) (bool, float32) {
|
||||
func tcping(ip net.IPAddr, tcpPort int) (bool, float32) {
|
||||
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 {
|
||||
return false, 0
|
||||
} else {
|
||||
@@ -26,11 +27,11 @@ func tcping(ip net.IPAddr) (bool, float32) {
|
||||
}
|
||||
|
||||
//pingReceived pingTotalTime
|
||||
func checkConnection(ip net.IPAddr) (int, float32) {
|
||||
func checkConnection(ip net.IPAddr, tcpPort int) (int, float32) {
|
||||
pingRecv := 0
|
||||
var pingTime float32 = 0.0
|
||||
for i := 1; i <= failTime; i++ {
|
||||
pingSucceed, pingTimeCurrent := tcping(ip)
|
||||
pingSucceed, pingTimeCurrent := tcping(ip, tcpPort)
|
||||
if pingSucceed {
|
||||
pingRecv++
|
||||
pingTime += pingTimeCurrent
|
||||
@@ -40,12 +41,12 @@ func checkConnection(ip net.IPAddr) (int, float32) {
|
||||
}
|
||||
|
||||
//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
|
||||
pingRecv := 0
|
||||
var pingTime float32 = 0.0
|
||||
for !ipCanConnect {
|
||||
pingRecvCurrent, pingTimeCurrent := checkConnection(ip)
|
||||
pingRecvCurrent, pingTimeCurrent := checkConnection(ip, tcpPort)
|
||||
if pingRecvCurrent != 0 {
|
||||
ipCanConnect = true
|
||||
pingRecv = pingRecvCurrent
|
||||
@@ -61,7 +62,7 @@ func tcpingHandler(ip net.IPAddr, pingCount int, progressHandler func(e progress
|
||||
if ipCanConnect {
|
||||
progressHandler(AvailableIPFound)
|
||||
for i := failTime; i < pingCount; i++ {
|
||||
pingSuccess, pingTimeCurrent := tcping(ip)
|
||||
pingSuccess, pingTimeCurrent := tcping(ip, tcpPort)
|
||||
progressHandler(NormalPing)
|
||||
if pingSuccess {
|
||||
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()
|
||||
success, pingRecv, pingTimeAvg, currentIP := tcpingHandler(ip, pingCount, progressHandler)
|
||||
success, pingRecv, pingTimeAvg, currentIP := tcpingHandler(ip, tcpPort, pingCount, progressHandler)
|
||||
if success {
|
||||
mutex.Lock()
|
||||
var cfdata CloudflareIPData
|
||||
@@ -130,7 +131,7 @@ func DownloadSpeedHandler(ip net.IPAddr) (bool, float32) {
|
||||
var nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
|
||||
e := ewma.NewMovingAverage()
|
||||
|
||||
for ; contentLength != contentRead; {
|
||||
for contentLength != contentRead {
|
||||
var currentTime = time.Now()
|
||||
if currentTime.After(nextTime) {
|
||||
timeCounter += 1
|
||||
|
||||
6
util.go
6
util.go
@@ -36,13 +36,11 @@ func ExportCsv(filePath string, data []CloudflareIPData) {
|
||||
}
|
||||
defer fp.Close()
|
||||
w := csv.NewWriter(fp) //创建一个新的写入文件流
|
||||
w.Write([]string{"IP 地址", "Ping 发送次数", "Ping 接收次数", "Ping 接收率", "平均延迟", "下载速度 (MB/s)"})
|
||||
w.Write([]string{"IP 地址", "测试次数", "成功次数", "成功比率", "平均延迟", "下载速度 (MB/s)"})
|
||||
w.WriteAll(convertToString(data))
|
||||
w.Flush()
|
||||
}
|
||||
|
||||
//"IP Address","Ping Count","Ping received","Ping received rate","Ping time","Download speed"
|
||||
|
||||
func (cf *CloudflareIPData) toString() []string {
|
||||
result := make([]string, 6)
|
||||
result[0] = cf.ip.String()
|
||||
@@ -83,7 +81,7 @@ const downloadBufferSize = 1024
|
||||
|
||||
var downloadTestCount int
|
||||
|
||||
const defaultTcpPort = 443
|
||||
//const defaultTcpPort = 443
|
||||
const tcpConnectTimeout = time.Second * 1
|
||||
|
||||
var failTime int
|
||||
|
||||
Reference in New Issue
Block a user