mirror of
https://github.com/XIU2/CloudflareSpeedTest.git
synced 2026-03-07 15:15:52 +08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c1166fc5e | ||
|
|
f9ac05a072 | ||
|
|
976dd79913 |
143
IPRangeLoader.go
143
IPRangeLoader.go
@@ -9,18 +9,49 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 根据子网掩码获取主机数量
|
||||
func getCidrHostNum(maskLen int) int {
|
||||
cidrIpNum := int(0)
|
||||
cidrIPNum := int(0)
|
||||
if maskLen < 32 {
|
||||
var i int = int(32 - maskLen - 1)
|
||||
for ; i >= 1; i-- {
|
||||
cidrIpNum += 1 << i
|
||||
cidrIPNum += 1 << i
|
||||
}
|
||||
cidrIpNum += 2
|
||||
cidrIPNum += 2
|
||||
} else {
|
||||
cidrIpNum = 1
|
||||
cidrIPNum = 1
|
||||
}
|
||||
return cidrIpNum
|
||||
if cidrIPNum > 255 {
|
||||
cidrIPNum = 255
|
||||
}
|
||||
return cidrIPNum
|
||||
}
|
||||
|
||||
// 获取 IP 最后一段最小值和最大值
|
||||
func getCidrIPRange(cidr string) (uint8, uint8) {
|
||||
ip := strings.Split(cidr, "/")[0]
|
||||
ipSegs := strings.Split(ip, ".")
|
||||
maskLen, _ := strconv.Atoi(strings.Split(cidr, "/")[1])
|
||||
seg4MinIP, seg4MaxIP := getIPSeg4Range(ipSegs, maskLen)
|
||||
//ipPrefix := ipSegs[0] + "." + ipSegs[1] + "." + ipSegs[2] + "."
|
||||
|
||||
return seg4MinIP,
|
||||
seg4MaxIP
|
||||
}
|
||||
|
||||
// 获取 IP 最后一段的区间
|
||||
func getIPSeg4Range(ipSegs []string, maskLen int) (uint8, uint8) {
|
||||
ipSeg, _ := strconv.Atoi(ipSegs[3])
|
||||
return getIPSegRange(uint8(ipSeg), uint8(32-maskLen))
|
||||
}
|
||||
|
||||
// 根据输入的基础IP地址和CIDR掩码计算一个IP片段的区间
|
||||
func getIPSegRange(userSegIP, offset uint8) (uint8, uint8) {
|
||||
var ipSegMax uint8 = 255
|
||||
netSegIP := ipSegMax << offset
|
||||
segMinIP := netSegIP & userSegIP
|
||||
segMaxIP := userSegIP&(255<<offset) | ^(255 << offset)
|
||||
return uint8(segMinIP), uint8(segMaxIP)
|
||||
}
|
||||
|
||||
func loadFirstIPOfRangeFromFile(ipFile string) []net.IPAddr {
|
||||
@@ -36,67 +67,86 @@ func loadFirstIPOfRangeFromFile(ipFile string) []net.IPAddr {
|
||||
firstIP, IPRange, err := net.ParseCIDR(IPString)
|
||||
//fmt.Println(firstIP)
|
||||
//fmt.Println(IPRange)
|
||||
Mask, _ := strconv.Atoi(strings.Split(scanner.Text(), "/")[1])
|
||||
MaxIPNum := getCidrHostNum(Mask)
|
||||
if MaxIPNum > 255 {
|
||||
MaxIPNum = 255
|
||||
}
|
||||
//fmt.Println(MaxIPNum)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if ipv6Mode { // IPv6
|
||||
if !ipv6Mode { // IPv4
|
||||
minIP, maxIP := getCidrIPRange(scanner.Text()) // 获取 IP 最后一段最小值和最大值
|
||||
Mask, _ := strconv.Atoi(strings.Split(scanner.Text(), "/")[1]) // 获取子网掩码
|
||||
MaxIPNum := getCidrHostNum(Mask) // 根据子网掩码获取主机数量
|
||||
for IPRange.Contains(firstIP) {
|
||||
if allip { // 如果是测速全部 IP
|
||||
for i := int(minIP); i <= int(maxIP); i++ { // 遍历 IP 最后一段最小值到最大值
|
||||
firstIP[15] = uint8(i)
|
||||
firstIPCopy := make([]byte, len(firstIP))
|
||||
copy(firstIPCopy, firstIP)
|
||||
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
|
||||
}
|
||||
} else { // 随机 IP 的最后一段 0.0.0.X
|
||||
firstIP[15] = minIP + randipEndWith(MaxIPNum)
|
||||
firstIPCopy := make([]byte, len(firstIP))
|
||||
copy(firstIPCopy, firstIP)
|
||||
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
|
||||
}
|
||||
firstIP[14]++ // 0.0.(X+1).X
|
||||
if firstIP[14] == 0 {
|
||||
firstIP[13]++ // 0.(X+1).X.X
|
||||
if firstIP[13] == 0 {
|
||||
firstIP[12]++ // (X+1).X.X.X
|
||||
}
|
||||
}
|
||||
}
|
||||
} else { //IPv6
|
||||
var tempIP uint8
|
||||
MaxIPNum = 255
|
||||
for IPRange.Contains(firstIP) {
|
||||
//fmt.Println(firstIP)
|
||||
//fmt.Println(firstIP[0], firstIP[1], firstIP[2], firstIP[3], firstIP[4], firstIP[5], firstIP[6], firstIP[7], firstIP[8], firstIP[9], firstIP[10], firstIP[11], firstIP[12], firstIP[13], firstIP[14], firstIP[15])
|
||||
firstIP[15] = randipEndWith(MaxIPNum) // 随机 IP 的最后一段
|
||||
firstIP[14] = randipEndWith(MaxIPNum) // 随机 IP 的最后一段
|
||||
firstIP[15] = randipEndWith(255) // 随机 IP 的最后一段
|
||||
firstIP[14] = randipEndWith(255) // 随机 IP 的最后一段
|
||||
firstIPCopy := make([]byte, len(firstIP))
|
||||
copy(firstIPCopy, firstIP)
|
||||
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
|
||||
tempIP = firstIP[13]
|
||||
firstIP[13] += randipEndWith(MaxIPNum)
|
||||
firstIP[13] += randipEndWith(255)
|
||||
if firstIP[13] < tempIP {
|
||||
tempIP = firstIP[12]
|
||||
firstIP[12] += randipEndWith(MaxIPNum)
|
||||
firstIP[12] += randipEndWith(255)
|
||||
if firstIP[12] < tempIP {
|
||||
tempIP = firstIP[11]
|
||||
firstIP[11] += randipEndWith(MaxIPNum)
|
||||
firstIP[11] += randipEndWith(255)
|
||||
if firstIP[11] < tempIP {
|
||||
tempIP = firstIP[10]
|
||||
firstIP[10] += randipEndWith(MaxIPNum)
|
||||
firstIP[10] += randipEndWith(255)
|
||||
if firstIP[10] < tempIP {
|
||||
tempIP = firstIP[9]
|
||||
firstIP[9] += randipEndWith(MaxIPNum)
|
||||
firstIP[9] += randipEndWith(255)
|
||||
if firstIP[9] < tempIP {
|
||||
tempIP = firstIP[8]
|
||||
firstIP[8] += randipEndWith(MaxIPNum)
|
||||
firstIP[8] += randipEndWith(255)
|
||||
if firstIP[8] < tempIP {
|
||||
tempIP = firstIP[7]
|
||||
firstIP[7] += randipEndWith(MaxIPNum)
|
||||
firstIP[7] += randipEndWith(255)
|
||||
if firstIP[7] < tempIP {
|
||||
tempIP = firstIP[6]
|
||||
firstIP[6] += randipEndWith(MaxIPNum)
|
||||
firstIP[6] += randipEndWith(255)
|
||||
if firstIP[6] < tempIP {
|
||||
tempIP = firstIP[5]
|
||||
firstIP[5] += randipEndWith(MaxIPNum)
|
||||
firstIP[5] += randipEndWith(255)
|
||||
if firstIP[5] < tempIP {
|
||||
tempIP = firstIP[4]
|
||||
firstIP[4] += randipEndWith(MaxIPNum)
|
||||
firstIP[4] += randipEndWith(255)
|
||||
if firstIP[4] < tempIP {
|
||||
tempIP = firstIP[3]
|
||||
firstIP[3] += randipEndWith(MaxIPNum)
|
||||
firstIP[3] += randipEndWith(255)
|
||||
if firstIP[3] < tempIP {
|
||||
tempIP = firstIP[2]
|
||||
firstIP[2] += randipEndWith(MaxIPNum)
|
||||
firstIP[2] += randipEndWith(255)
|
||||
if firstIP[2] < tempIP {
|
||||
tempIP = firstIP[1]
|
||||
firstIP[1] += randipEndWith(MaxIPNum)
|
||||
firstIP[1] += randipEndWith(255)
|
||||
if firstIP[1] < tempIP {
|
||||
tempIP = firstIP[0]
|
||||
firstIP[0] += randipEndWith(MaxIPNum)
|
||||
firstIP[0] += randipEndWith(255)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -111,41 +161,6 @@ func loadFirstIPOfRangeFromFile(ipFile string) []net.IPAddr {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else { //IPv4
|
||||
for IPRange.Contains(firstIP) {
|
||||
//fmt.Println(firstIP)
|
||||
//fmt.Println(firstIP[15])
|
||||
if allip {
|
||||
if firstIP[15] == 0 { // 当 IP 最后一段为 0 时会按顺序生成 IP
|
||||
for i := 0; i < MaxIPNum; i++ {
|
||||
firstIP[15] = uint8(i) // 按顺序生成 IP 的最后一段 0.0.0.X
|
||||
//fmt.Println(firstIP)
|
||||
firstIPCopy := make([]byte, len(firstIP))
|
||||
copy(firstIPCopy, firstIP)
|
||||
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
|
||||
}
|
||||
} else { // 当 IP 最后一段不为 0 时,则保留 IP 最后一段
|
||||
firstIPCopy := make([]byte, len(firstIP))
|
||||
copy(firstIPCopy, firstIP)
|
||||
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
|
||||
}
|
||||
} else {
|
||||
if firstIP[15] == 0 {
|
||||
firstIP[15] = randipEndWith(MaxIPNum) // 随机 IP 的最后一段 0.0.0.X
|
||||
}
|
||||
firstIPCopy := make([]byte, len(firstIP))
|
||||
copy(firstIPCopy, firstIP)
|
||||
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
|
||||
}
|
||||
firstIP[15] = 0
|
||||
firstIP[14]++ // 0.0.(X+1).X
|
||||
if firstIP[14] == 0 {
|
||||
firstIP[13]++ // 0.(X+1).X.X
|
||||
if firstIP[13] == 0 {
|
||||
firstIP[12]++ // (X+1).X.X.X
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return firstIPs
|
||||
|
||||
43
README.md
43
README.md
@@ -9,7 +9,7 @@
|
||||
国外很多网站都在使用 Cloudflare CDN,但分配给中国访客的 IP 并不友好。
|
||||
虽然 Cloudflare 公开了所有 [IP 段](https://www.cloudflare.com/ips/) ,但想要在这么多 IP 中找到适合自己的,怕是要累死,所以就有了这个软件。
|
||||
|
||||
该软件可以**测试 Cloudflare CDN 延迟和速度,获取最快 IP (IPv4+IPv6)**!觉得好用请**点个⭐鼓励一下下~**
|
||||
该软件可以**批量测试 Cloudflare CDN 延迟和速度,获取最快 IP (IPv4+IPv6)**!觉得好用请**点个⭐鼓励一下下~**
|
||||
|
||||
> 本项目也**适用于其他 CDN**,但是需要自行寻找 **CDN IP 段及下载测速地址**(否则只能延迟测速)。
|
||||
|
||||
@@ -23,34 +23,37 @@
|
||||
|
||||
> **提示:Linux 系统**请先赋予执行权限 `chmod +x CloudflareST` ,然后再执行 `./CloudflareST`
|
||||
|
||||
> **注意:建议测速时避开晚上高峰期**(国外),否则测速结果会与其他时间**差距很大...**
|
||||
|
||||
### 结果示例
|
||||
|
||||
测速完毕后,默认会显示**最快的 20 个 IP**,示例:
|
||||
测速完毕后,默认会显示**最快的 20 个 IP**,示例(我的白天测速结果):
|
||||
|
||||
```
|
||||
IP 地址 已发送 已接收 丢包率 平均延迟 下载速度 (MB/s)
|
||||
104.27.198.101 4 4 0.00 126.52 12.71
|
||||
104.22.43.157 4 4 0.00 129.38 16.74
|
||||
104.27.214.140 4 4 0.00 132.02 4.65
|
||||
104.22.42.165 4 4 0.00 133.63 12.00
|
||||
104.22.35.177 4 4 0.00 135.75 3.92
|
||||
104.22.87.44 4 4 0.00 136.00 5.86
|
||||
104.22.67.122 4 4 0.00 136.50 9.47
|
||||
104.22.88.154 4 4 0.00 140.75 13.00
|
||||
104.22.69.218 4 4 0.00 142.00 19.07
|
||||
104.27.184.10 4 4 0.00 148.02 21.05
|
||||
104.27.200.69 4 4 0.00 146.23 28.64
|
||||
172.67.60.78 4 4 0.00 139.82 15.02
|
||||
104.25.140.153 4 4 0.00 146.49 14.90
|
||||
104.27.192.65 4 4 0.00 140.28 14.07
|
||||
172.67.62.214 4 4 0.00 139.29 12.71
|
||||
104.27.207.5 4 4 0.00 145.92 11.95
|
||||
172.67.54.193 4 4 0.00 146.71 11.55
|
||||
104.22.66.8 4 4 0.00 147.42 11.11
|
||||
104.27.197.63 4 4 0.00 131.29 10.26
|
||||
172.67.58.91 4 4 0.00 140.19 9.14
|
||||
...
|
||||
```
|
||||
|
||||
选择一个平均延迟与下载速度都不错的 IP,至于拿来干嘛?取决于你~
|
||||
测速结果第一行是**兼顾平均延迟与下载速度的最快 IP**,至于拿来干嘛?取决于你~
|
||||
|
||||
完整结果保存在当前目录下的 `result.csv` 文件中,用**记事本/表格软件**打开,排序为**延迟由低到高**,分别是:
|
||||
完整结果保存在当前目录下的 `result.csv` 文件中,用**记事本/表格软件**打开,格式如下:
|
||||
|
||||
```
|
||||
IP 地址, 已发送, 已接收, 丢包率, 平均延迟, 下载速度 (MB/s)
|
||||
104.27.198.101, 4, 4, 0.00, 126.52, 12.71
|
||||
104.27.200.69, 4, 4, 0.00, 146.23, 28.64
|
||||
```
|
||||
> 大家可以按照自己的需求,对完整结果**进一步筛选处理**,或者去看一看进阶使用(如设定测速条件)!
|
||||
|
||||
> 大家可以按自己需求,对完整结果**进一步筛选处理**,或者去看一看进阶使用**指定过滤条件**!
|
||||
|
||||
****
|
||||
## 进阶使用
|
||||
@@ -88,7 +91,7 @@ https://github.com/XIU2/CloudflareSpeedTest
|
||||
-o result.csv
|
||||
输出结果文件;如含有空格请加上引号;为空格时不输出结果文件(-o " ");允许其他后缀;(默认 result.csv)
|
||||
-dd
|
||||
禁用下载测速;如果带上该参数就是禁用下载测速;(默认 启用)
|
||||
禁用下载测速;禁用后测速结果会按延迟排序(默认按下载速度排序);(默认 启用)
|
||||
-ipv6
|
||||
IPv6 测速模式;请确保 IP 数据文件内只包含 IPv6 IP段,软件不支持同时测速 IPv4+IPv6;(默认 IPv4)
|
||||
-allip
|
||||
@@ -116,6 +119,8 @@ CloudflareST.exe -p 0 -f ip.txt -dd
|
||||
# 指定 IPv6 数据文件( ipv6.txt ),不显示结果直接退出(-p 值为 0)
|
||||
CloudflareST.exe -p 0 -f ipv6.txt -dd -ipv6
|
||||
|
||||
# ——————————————————————
|
||||
|
||||
# 指定 IPv4 数据文件,不输出结果到文件,直接显示结果(-p 值为 10 条)
|
||||
CloudflareST.exe -p 10 -f ip.txt -o " " -dd
|
||||
|
||||
@@ -125,9 +130,13 @@ CloudflareST.exe -f ip.txt -o result.csv -dd
|
||||
# 指定 IPv4 数据文件 及 输出结果到文件(绝对路径,即 C:\abc\ 目录下,如果包含空格请加上引号)
|
||||
CloudflareST.exe -f C:\abc\ip.txt -o C:\abc\result.csv -dd
|
||||
|
||||
# ——————————————————————
|
||||
|
||||
# 指定下载测速地址(要求:可以直接下载、文件大小超过 200MB、用的是 Cloudflare CDN),如果包含空格请加上引号
|
||||
CloudflareST.exe -url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
|
||||
|
||||
# ——————————————————————
|
||||
|
||||
# 指定测速条件(只有同时满足三个条件时才会停止测速):
|
||||
# 延迟时间上限:200 ms,下载速度下限:0 MB/s,数量:10 个
|
||||
CloudflareST.exe -tl 200 -dn 10
|
||||
|
||||
2
ip.txt
2
ip.txt
@@ -1,3 +1,5 @@
|
||||
1.1.1.0/24
|
||||
1.0.0.0/24
|
||||
173.245.48.0/20
|
||||
103.21.244.0/22
|
||||
103.22.200.0/22
|
||||
|
||||
19
main.go
19
main.go
@@ -17,10 +17,9 @@ import (
|
||||
|
||||
var version, ipFile, outputFile, versionNew string
|
||||
var disableDownload, ipv6Mode, allip bool
|
||||
var tcpPort, printResultNum, timeLimit, speedLimit int
|
||||
var tcpPort, printResultNum, timeLimit, speedLimit, downloadSecond int
|
||||
|
||||
func init() {
|
||||
var downloadSecond int64
|
||||
var printVersion bool
|
||||
var help = `
|
||||
CloudflareSpeedTest ` + version + `
|
||||
@@ -36,8 +35,8 @@ https://github.com/XIU2/CloudflareSpeedTest
|
||||
延迟测速端口;延迟测速 TCP 协议的端口;(默认 443)
|
||||
-dn 20
|
||||
下载测速数量;延迟测速并排序后,从最低延迟起下载测速数量,请勿太多(速度慢);(默认 20)
|
||||
-dt 5
|
||||
下载测速时间;单个 IP 测速最长时间,单位:秒;(默认 5)
|
||||
-dt 10
|
||||
下载测速时间;单个 IP 测速最长时间,单位:秒;(默认 10)
|
||||
-url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
|
||||
下载测速地址;用来 Cloudflare CDN 测速的文件地址,如含有空格请加上引号;
|
||||
-tl 200
|
||||
@@ -51,7 +50,7 @@ https://github.com/XIU2/CloudflareSpeedTest
|
||||
-o result.csv
|
||||
输出结果文件;如含有空格请加上引号;为空格时不输出结果文件(-o " ");允许其他后缀;(默认 result.csv)
|
||||
-dd
|
||||
禁用下载测速;如果带上该参数将会禁用下载测速;(默认 启用下载测速)
|
||||
禁用下载测速;禁用后测速结果会按延迟排序(默认按下载速度排序);(默认 启用下载测速)
|
||||
-ipv6
|
||||
IPv6 测速模式;请确保 IP 数据文件内只包含 IPv6 IP段,软件不支持同时测速 IPv4+IPv6;(默认 IPv4)
|
||||
-allip
|
||||
@@ -66,7 +65,7 @@ https://github.com/XIU2/CloudflareSpeedTest
|
||||
flag.IntVar(&pingTime, "t", 4, "延迟测速次数")
|
||||
flag.IntVar(&tcpPort, "tp", 443, "延迟测速端口")
|
||||
flag.IntVar(&downloadTestCount, "dn", 20, "下载测速数量")
|
||||
flag.Int64Var(&downloadSecond, "dt", 5, "下载测速时间")
|
||||
flag.IntVar(&downloadSecond, "dt", 10, "下载测速时间")
|
||||
flag.StringVar(&url, "url", "https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png", "下载测速地址")
|
||||
flag.IntVar(&timeLimit, "tl", 0, "延迟时间上限")
|
||||
flag.IntVar(&speedLimit, "sl", 0, "下载速度下限")
|
||||
@@ -78,8 +77,6 @@ https://github.com/XIU2/CloudflareSpeedTest
|
||||
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 {
|
||||
@@ -139,6 +136,11 @@ func main() {
|
||||
var mu sync.Mutex
|
||||
var data = make([]CloudflareIPData, 0)
|
||||
var data_2 = make([]CloudflareIPData, 0)
|
||||
downloadTestTime = time.Duration(downloadSecond) * time.Second
|
||||
|
||||
//println(downloadTestCount)
|
||||
//println(downloadTestTime)
|
||||
//println(downloadSecond)
|
||||
|
||||
fmt.Println("# XIU2/CloudflareSpeedTest " + version + "\n")
|
||||
if ipv6Mode {
|
||||
@@ -194,6 +196,7 @@ func main() {
|
||||
}
|
||||
|
||||
if len(data_2) > 0 { // 如果该数字有内容,说明进行过指定条件的下载测速
|
||||
sort.Sort(CloudflareIPDataSetD(data_2)) // 排序
|
||||
if outputFile != "" {
|
||||
ExportCsv(outputFile, data_2) // 输出结果到文件(指定延迟时间或下载速度的)
|
||||
}
|
||||
|
||||
16
util.go
16
util.go
@@ -85,8 +85,12 @@ const tcpConnectTimeout = time.Second * 1
|
||||
|
||||
var failTime int
|
||||
|
||||
// 平均延迟排序(丢包另算)
|
||||
type CloudflareIPDataSet []CloudflareIPData
|
||||
|
||||
// 下载速度排序
|
||||
type CloudflareIPDataSetD []CloudflareIPData
|
||||
|
||||
func initRandSeed() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
@@ -146,3 +150,15 @@ func (cfs CloudflareIPDataSet) Less(i, j int) bool {
|
||||
func (cfs CloudflareIPDataSet) Swap(i, j int) {
|
||||
cfs[i], cfs[j] = cfs[j], cfs[i]
|
||||
}
|
||||
|
||||
func (cfs CloudflareIPDataSetD) Len() int {
|
||||
return len(cfs)
|
||||
}
|
||||
|
||||
func (cfs CloudflareIPDataSetD) Less(i, j int) bool {
|
||||
return cfs[i].downloadSpeed > cfs[j].downloadSpeed
|
||||
}
|
||||
|
||||
func (cfs CloudflareIPDataSetD) Swap(i, j int) {
|
||||
cfs[i], cfs[j] = cfs[j], cfs[i]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user