mirror of
https://github.com/XIU2/CloudflareSpeedTest.git
synced 2026-03-07 15:15:52 +08:00
Compare commits
118 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e5804b7ba | ||
|
|
cee772547b | ||
|
|
1a939f752b | ||
|
|
5b45f400a2 | ||
|
|
3b43b21b83 | ||
|
|
2f284efddd | ||
|
|
f960ce4a4b | ||
|
|
8ae1d495af | ||
|
|
3b957cb1a4 | ||
|
|
faee85edb8 | ||
|
|
b901003dd1 | ||
|
|
873200de80 | ||
|
|
ac29c9666e | ||
|
|
a540789180 | ||
|
|
012865f02d | ||
|
|
74c1744ca6 | ||
|
|
e66d44882e | ||
|
|
0d3af2d5f8 | ||
|
|
70d11e3bc1 | ||
|
|
ec650d3084 | ||
|
|
4038a5b0aa | ||
|
|
1ff4c24c59 | ||
|
|
ffbb161f79 | ||
|
|
3efabb5661 | ||
|
|
e2f23aeb48 | ||
|
|
97bbb8b5e9 | ||
|
|
5d75cb861e | ||
|
|
eed4f4fa0b | ||
|
|
e8537fb0ae | ||
|
|
f1147c5cbf | ||
|
|
8a7b0f12f5 | ||
|
|
6a3504e98c | ||
|
|
78a8f9c6c4 | ||
|
|
aa11024026 | ||
|
|
cf1a01b614 | ||
|
|
b9159db975 | ||
|
|
157b89d88e | ||
|
|
d1c304ff59 | ||
|
|
ceff5971d2 | ||
|
|
32d544184b | ||
|
|
f82471671c | ||
|
|
638273b7e7 | ||
|
|
dc68529244 | ||
|
|
29c927d3cd | ||
|
|
fb190c661d | ||
|
|
9e39be140a | ||
|
|
09a578decf | ||
|
|
8ef6b3b7c2 | ||
|
|
cc6b5dd7a6 | ||
|
|
6c1166fc5e | ||
|
|
f9ac05a072 | ||
|
|
976dd79913 | ||
|
|
c8ef175207 | ||
|
|
31dc7aed3c | ||
|
|
e3a6f80a14 | ||
|
|
38e1d26341 | ||
|
|
166d9abe7c | ||
|
|
f9c310bfb4 | ||
|
|
0d54b65f33 | ||
|
|
9f2e5b5b5e | ||
|
|
7b4f6944be | ||
|
|
00b569d649 | ||
|
|
4b4426c195 | ||
|
|
ff3a6d1d56 | ||
|
|
a78c6e6270 | ||
|
|
6b52fbf5ea | ||
|
|
25fa4b65d8 | ||
|
|
dca761ec72 | ||
|
|
0f5b18b305 | ||
|
|
4e1678edc3 | ||
|
|
65b451ec4d | ||
|
|
72ecee9e26 | ||
|
|
1d9f64a4a2 | ||
|
|
40b22f660a | ||
|
|
12039f4850 | ||
|
|
129deeaf71 | ||
|
|
3de6b38e00 | ||
|
|
8c0e8732cc | ||
|
|
8820c5f982 | ||
|
|
d50c4806a6 | ||
|
|
306ce709c9 | ||
|
|
956a35cab0 | ||
|
|
3d49bb13ed | ||
|
|
3ddd66b3c1 | ||
|
|
9aa64db555 | ||
|
|
9654cb8ea6 | ||
|
|
13bae9c6f8 | ||
|
|
f0fa3e4d0a | ||
|
|
b83734b426 | ||
|
|
c1348df16e | ||
|
|
c52750ad9c | ||
|
|
0e9461f3b7 | ||
|
|
07e20028cc | ||
|
|
4c92eae311 | ||
|
|
efdbc8f08e | ||
|
|
e85a03c651 | ||
|
|
5d00d7c5ff | ||
|
|
92d5216259 | ||
|
|
63e2bc00ee | ||
|
|
5664055c84 | ||
|
|
c6bfd01dd5 | ||
|
|
383f4c979b | ||
|
|
31743a8138 | ||
|
|
479629b84e | ||
|
|
03a1b44e88 | ||
|
|
963dfa68ed | ||
|
|
6417339312 | ||
|
|
edf5dee039 | ||
|
|
12a9fd2ffb | ||
|
|
d73b1374bc | ||
|
|
68899b53fb | ||
|
|
df0b12f018 | ||
|
|
31dbf635a8 | ||
|
|
8e3ee6f2d0 | ||
|
|
4af4d4d3c8 | ||
|
|
7290f04a6b | ||
|
|
9ea310a4d8 | ||
|
|
2dfea884a2 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
dist
|
||||
Releases
|
||||
|
||||
@@ -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:'
|
||||
206
IPRangeLoader.go
206
IPRangeLoader.go
@@ -1,39 +1,167 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
)
|
||||
|
||||
func loadFirstIPOfRangeFromFile() []net.IPAddr {
|
||||
file, err := os.Open("ip.txt")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
firstIPs := make([]net.IPAddr, 0)
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
for scanner.Scan() {
|
||||
IPString := scanner.Text()
|
||||
firstIP, IPRange, err := net.ParseCIDR(IPString)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
firstIP[15] = ipEndWith
|
||||
for IPRange.Contains(firstIP) {
|
||||
firstIPCopy := make([]byte, len(firstIP))
|
||||
copy(firstIPCopy, firstIP)
|
||||
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
|
||||
firstIP[14]++
|
||||
if firstIP[14] == 0 {
|
||||
firstIP[13]++
|
||||
if firstIP[13] == 0 {
|
||||
firstIP[12]++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return firstIPs
|
||||
}
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 根据子网掩码获取主机数量
|
||||
func getCidrHostNum(maskLen int) int {
|
||||
cidrIPNum := int(0)
|
||||
if maskLen < 32 {
|
||||
var i int = int(32 - maskLen - 1)
|
||||
for ; i >= 1; i-- {
|
||||
cidrIPNum += 1 << i
|
||||
}
|
||||
cidrIPNum += 2
|
||||
} else {
|
||||
cidrIPNum = 1
|
||||
}
|
||||
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 {
|
||||
file, err := os.Open(ipFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
firstIPs := make([]net.IPAddr, 0)
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
for scanner.Scan() {
|
||||
IPString := scanner.Text()
|
||||
firstIP, IPRange, err := net.ParseCIDR(IPString)
|
||||
//fmt.Println(firstIP)
|
||||
//fmt.Println(IPRange)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
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
|
||||
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(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(255)
|
||||
if firstIP[13] < tempIP {
|
||||
tempIP = firstIP[12]
|
||||
firstIP[12] += randipEndWith(255)
|
||||
if firstIP[12] < tempIP {
|
||||
tempIP = firstIP[11]
|
||||
firstIP[11] += randipEndWith(255)
|
||||
if firstIP[11] < tempIP {
|
||||
tempIP = firstIP[10]
|
||||
firstIP[10] += randipEndWith(255)
|
||||
if firstIP[10] < tempIP {
|
||||
tempIP = firstIP[9]
|
||||
firstIP[9] += randipEndWith(255)
|
||||
if firstIP[9] < tempIP {
|
||||
tempIP = firstIP[8]
|
||||
firstIP[8] += randipEndWith(255)
|
||||
if firstIP[8] < tempIP {
|
||||
tempIP = firstIP[7]
|
||||
firstIP[7] += randipEndWith(255)
|
||||
if firstIP[7] < tempIP {
|
||||
tempIP = firstIP[6]
|
||||
firstIP[6] += randipEndWith(255)
|
||||
if firstIP[6] < tempIP {
|
||||
tempIP = firstIP[5]
|
||||
firstIP[5] += randipEndWith(255)
|
||||
if firstIP[5] < tempIP {
|
||||
tempIP = firstIP[4]
|
||||
firstIP[4] += randipEndWith(255)
|
||||
if firstIP[4] < tempIP {
|
||||
tempIP = firstIP[3]
|
||||
firstIP[3] += randipEndWith(255)
|
||||
if firstIP[3] < tempIP {
|
||||
tempIP = firstIP[2]
|
||||
firstIP[2] += randipEndWith(255)
|
||||
if firstIP[2] < tempIP {
|
||||
tempIP = firstIP[1]
|
||||
firstIP[1] += randipEndWith(255)
|
||||
if firstIP[1] < tempIP {
|
||||
tempIP = firstIP[0]
|
||||
firstIP[0] += randipEndWith(255)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return firstIPs
|
||||
}
|
||||
|
||||
311
README.md
311
README.md
@@ -1,10 +1,301 @@
|
||||
# CloudflareScanner
|
||||
|
||||
本项目可以测试Cloudflare节点接入速度
|
||||
|
||||
程序运行完毕后,结果会保存在当前目录的`result.csv`下
|
||||
|
||||
## 注意事项
|
||||
#### 协程数请不要调过1000,否则容易出现较大误差
|
||||
#### 编译选项为 `go build`
|
||||
#### 您可在release界面下载或编译运行
|
||||
# XIU2/CloudflareSpeedTest
|
||||
|
||||
[](https://github.com/XIU2/CloudflareSpeedTest/blob/master/go.mod)
|
||||
[](https://github.com/XIU2/CloudflareSpeedTest/releases/latest)
|
||||
[](https://github.com/XIU2/CloudflareSpeedTest/blob/master/LICENSE)
|
||||
[](https://github.com/XIU2/CloudflareSpeedTest/stargazers)
|
||||
[](https://github.com/XIU2/CloudflareSpeedTest/network/members)
|
||||
|
||||
[国外很多网站](https://github.com/XIU2/CloudflareSpeedTest/discussions/62)都在使用 Cloudflare CDN,但分配给中国访客的 IP 并不友好(高延迟/高丢包/速度慢等)。
|
||||
虽然 Cloudflare 公开了所有 [IP 段](https://www.cloudflare.com/ips/) ,但想要在这么多 IP 中找到适合自己的,怕是要累死,所以就有了这个软件。
|
||||
|
||||
该软件可以**测试 Cloudflare CDN 延迟和速度,获取最快 IP (IPv4+IPv6)**!觉得好用请**点个⭐鼓励一下下~**
|
||||
|
||||
> _本项目也**适用于其他 CDN**,但是需要自行寻找 **CDN IP 段及下载测速地址**(否则只能延迟测速)。_
|
||||
|
||||
> _我另一个开源项目: **[一个 \[油猴脚本\] 轻松解决「Github」文件下载速度慢的问题!](https://github.com/XIU2/UserScript)**_
|
||||
|
||||
****
|
||||
## 快速使用
|
||||
|
||||
### 下载运行
|
||||
|
||||
1. 下载编译好的可执行文件 [蓝奏云](https://xiu.lanzoux.com/b0742hkxe) / [Github](https://github.com/XIU2/CloudflareSpeedTest/releases) 并解压。
|
||||
2. 双击运行 `CloudflareST.exe`文件(Windows),等待测速完成...
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 点击查看 Linux 下载运行命令示例 」</strong></code></summary>
|
||||
|
||||
****
|
||||
|
||||
以下命令仅为示例,版本号和文件名请前往 [**Releases**](https://github.com/XIU2/CloudflareSpeedTest/releases) 查看。
|
||||
|
||||
``` bash
|
||||
# 如果是第一次使用,则建议创建新文件夹(后续更新请跳过该步骤)
|
||||
mkdir CloudflareST
|
||||
|
||||
# 进入文件夹(后续更新,只需要从这里重复下面的下载、解压命令即可)
|
||||
cd CloudflareST
|
||||
|
||||
# 下载 CloudflareST 压缩包(自行根据需求替换 URL 中版本号和文件名)
|
||||
wget -N https://github.com/XIU2/CloudflareSpeedTest/releases/download/v1.4.7/CloudflareST_linux_amd64.tar.gz
|
||||
|
||||
# 解压(不需要删除旧文件,会直接覆盖,自行根据需求替换 文件名)
|
||||
tar -zxf CloudflareST_linux_amd64.tar.gz
|
||||
|
||||
# 赋予执行权限
|
||||
chmod +x CloudflareST
|
||||
|
||||
# 运行
|
||||
./CloudflareST
|
||||
```
|
||||
|
||||
> 如果平**均延迟非常低**(如 0.xx),则说明 CloudflareST **测速时走了代理**,请先关闭代理软件后再测速。
|
||||
> 如果在**路由器**上运行(如 OpenWrt),请先关闭路由器内的代理,否则测速结果会**不准确且无法使用**。
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
> [_**在 Android 手机上运行 CloudflareST 测速的简单教程 ...**_](https://github.com/XIU2/CloudflareSpeedTest/discussions/61)
|
||||
> _**建议测速时避开晚上高峰期(20:00~24:00)**,否则测速结果会与其他时间**相差很大...**_
|
||||
|
||||
### 结果示例
|
||||
|
||||
测速完毕后,默认会显示**最快的 20 个 IP**,示例(我联通白天测速结果):
|
||||
|
||||
``` bash
|
||||
IP 地址 已发送 已接收 丢包率 平均延迟 下载速度 (MB/s)
|
||||
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
|
||||
...
|
||||
|
||||
# 如果平均延迟非常低(如 0.xx),则说明 CloudflareST 测速时走了代理,请先关闭代理软件后再测速。
|
||||
# 如果在路由器上运行(如 OpenWrt),请先关闭路由器内的代理,否则测速结果会不准确且无法使用。
|
||||
# 因为每次测速都是在每个 IP 段中随机 IP,所以每次的测速结果都不可能相同,这是正常的!
|
||||
# 软件是先 延迟测速并按从低到高排序后,再从 最低延迟的 IP 开始下载测速的,所以:
|
||||
```
|
||||
|
||||
测速结果第一行就是**既下载速度最快、又平均延迟最低的最快 IP**!至于拿来干嘛?取决于你~
|
||||
|
||||
完整结果保存在当前目录下的 `result.csv` 文件中,用**记事本/表格软件**打开,格式如下:
|
||||
|
||||
```
|
||||
IP 地址, 已发送, 已接收, 丢包率, 平均延迟, 下载速度 (MB/s)
|
||||
104.27.200.69, 4, 4, 0.00, 146.23, 28.64
|
||||
```
|
||||
|
||||
> _大家可以按自己需求,对完整结果**进一步筛选处理**,或者去看一看进阶使用**指定过滤条件**!_
|
||||
|
||||
****
|
||||
## 进阶使用
|
||||
|
||||
直接运行使用的是默认参数,如果想要测速结果更全面、更符合自己的要求,可以自定义参数。
|
||||
|
||||
``` cmd
|
||||
C:\>CloudflareST.exe -h
|
||||
|
||||
CloudflareSpeedTest vX.X.X
|
||||
测试 Cloudflare CDN 所有 IP 的延迟和速度,获取最快 IP (IPv4+IPv6)!
|
||||
https://github.com/XIU2/CloudflareSpeedTest
|
||||
|
||||
参数:
|
||||
-n 500
|
||||
测速线程数量;越多测速越快,性能弱的设备 (如路由器) 请适当调低;(默认 500 最多 1000 )
|
||||
-t 4
|
||||
延迟测速次数;单个 IP 延迟测速次数,为 1 时将过滤丢包的IP,TCP协议;(默认 4 )
|
||||
-tp 443
|
||||
延迟测速端口;延迟测速 TCP 协议的端口;(默认 443 )
|
||||
-dn 20
|
||||
下载测速数量;延迟测速并排序后,从最低延迟起下载测速的数量;(默认 20 )
|
||||
-dt 10
|
||||
下载测速时间;单个 IP 下载测速最长时间,单位:秒;(默认 10 )
|
||||
-url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
|
||||
下载测速地址;用来下载测速的 Cloudflare CDN 文件地址,如地址含有空格请加上引号;
|
||||
-tl 200
|
||||
平均延迟上限;只输出低于指定平均延迟的 IP,可单独使用也可搭配下载速度下限;(默认 9999.00 ms)
|
||||
-sl 5
|
||||
下载速度下限;只输出高于指定下载速度的 IP,凑够指定数量 [-dn] 才会停止测速;(默认 0.00 MB/s )
|
||||
-p 20
|
||||
显示结果数量;测速后直接显示指定数量的结果,为 0 时不显示结果直接退出;(默认 20 )
|
||||
-f ip.txt
|
||||
IP段数据文件;如路径含有空格请加上引号;支持其他 CDN IP段;(默认 ip.txt )
|
||||
-o result.csv
|
||||
输出结果文件;如路径含有空格请加上引号;值为空格时不输出 [-o " "];(默认 result.csv )
|
||||
-dd
|
||||
禁用下载测速;禁用后测速结果会按延迟排序 (默认按下载速度排序);(默认 启用 )
|
||||
-ipv6
|
||||
IPv6测速模式;确保 IP 段数据文件内只包含 IPv6 IP段,软件不支持同时测速 IPv4+IPv6;(默认 IPv4 )
|
||||
-allip
|
||||
测速全部的IP;对 IP 段中的每个 IP (仅支持 IPv4) 进行测速;(默认 每个 IP 段随机测速一个 IP )
|
||||
-v
|
||||
打印程序版本+检查版本更新
|
||||
-h
|
||||
打印帮助说明
|
||||
```
|
||||
|
||||
### 使用示例
|
||||
|
||||
Windows 是在 CMD 中运行,或者把相关参数添加到快捷方式目标中。
|
||||
|
||||
> **注意**:各参数均有**默认值**,使用默认值的参数是可以省略的(**按需选择**),参数**不分前后顺序**。
|
||||
> **提示**:Linux 系统只需要把下面命令中的 `CloudflareST.exe` 改为 `./CloudflareST` 即可。
|
||||
|
||||
#### \# IPv4/IPv6
|
||||
|
||||
``` bash
|
||||
# 指定 IPv4 数据文件(-f 默认值就是 ip.txt,所以该参数可以省略)
|
||||
CloudflareST.exe -f ip.txt
|
||||
|
||||
# 指定 IPv6 数据文件( ipv6.txt ),需要加上 -ipv6 参数
|
||||
CloudflareST.exe -f ipv6.txt -ipv6
|
||||
```
|
||||
****
|
||||
#### \# 文件相对/绝对路径
|
||||
|
||||
``` bash
|
||||
# 指定 IPv4 数据文件,不显示结果直接退出,输出结果到文件(-p 值为 0)
|
||||
CloudflareST.exe -f ip.txt -p 0 -dd
|
||||
|
||||
# 指定 IPv4 数据文件,不输出结果到文件,直接显示结果(-p 值为 10 条,-o 值为空格)
|
||||
CloudflareST.exe -f ip.txt -o " " -p 10 -dd
|
||||
|
||||
# 指定 IPv4 数据文件 及 输出结果到文件(相对路径,即当前目录下,如含空格请加上引号)
|
||||
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
|
||||
```
|
||||
****
|
||||
#### \# 自定义下载测速地址
|
||||
|
||||
``` bash
|
||||
# 地址要求:可以直接下载、文件大小超过 200MB、用的是 Cloudflare CDN
|
||||
CloudflareST.exe -url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
|
||||
```
|
||||
****
|
||||
#### \# 自定义测速条件
|
||||
|
||||
- 仅指定 **[平均延迟上限]** 条件
|
||||
|
||||
``` bash
|
||||
# 平均延迟上限:200 ms,下载速度下限:0 MB/s,数量:10 个
|
||||
# 即找到平均延迟低于 200 ms 的 IP,然后再按延迟从低到高进行 10 次下载测速
|
||||
CloudflareST.exe -tl 200 -dn 10
|
||||
```
|
||||
|
||||
> 如果没有一个 IP **平均延迟低于 200ms**,那么不会输出任何内容。
|
||||
|
||||
- 仅指定 **[平均延迟上限]** 条件,且**只延迟测速,不下载测速**
|
||||
|
||||
``` bash
|
||||
# 平均延迟上限:200 ms,下载速度下限:0 MB/s,数量:无限 个
|
||||
# 即只输出低于 200ms 的 IP,且不再下载测速(因为不再下载测速,所以 -dn 参数就无效了)
|
||||
CloudflareST.exe -tl 200 -dd
|
||||
```
|
||||
|
||||
- 仅指定 **[下载速度下限]** 条件
|
||||
|
||||
``` bash
|
||||
# 平均延迟上限:9999 ms,下载速度下限:5 MB/s,数量:10 个( 可选)
|
||||
# 即需要找到 10 个平均延迟低于 9999 ms 且下载速度高于 5 MB/s 的 IP 才会停止测速
|
||||
CloudflareST.exe -sl 5 -dn 10
|
||||
```
|
||||
|
||||
> 没有指定平均延迟上限时,如果一直**凑不够**满足条件的 IP 数量,就会**一直测速**下去。
|
||||
> 所以建议**同时指定 下载速度下限 和 平均延迟上限**,这样测速到指定延迟上限还没凑够数量,就会终止测速。
|
||||
|
||||
- 同时指定 **[平均延迟上限] + [下载速度下限]** 条件
|
||||
|
||||
``` bash
|
||||
# 平均延迟上限、下载速度下限均支持小数(如 -sl 0.5)
|
||||
# 平均延迟上限:200 ms,下载速度下限:5.6 MB/s,数量:10 个( 可选)
|
||||
# 即需要找到 10 个平均延迟低于 200 ms 且下载速度高于 5 .6MB/s 的 IP 才会停止测速
|
||||
CloudflareST.exe -tl 200 -sl 5.6 -dn 10
|
||||
```
|
||||
|
||||
> 如果没有一个 IP **平均延迟低于 200ms**,那么不会输出任何内容。
|
||||
> 如果没有一个 IP **下载速度高于 5.6 MB/s**,那么就会**和不指定 [下载速度下限] 条件一样**输出结果。
|
||||
> 所以建议先不指定条件测速一遍,看看平均延迟和下载速度大概在什么范围,避免指定条件**过低/过高**!
|
||||
|
||||
> 因为 Cloudflare 公开的 IP 段是**回源 IP+任播 IP**,而**回源 IP**是无法用来指向网站的,所以下载测速是 0.00。
|
||||
> 运行时可以加上 `-sl 0.01`(下载速度下限,最小值 0.01),过滤掉**回源 IP**(下载测速小于 0.01MB/s 的结果)。
|
||||
|
||||
****
|
||||
#### \# Windows 快捷方式
|
||||
|
||||
``` bash
|
||||
## 右键快捷方式 - 目标
|
||||
# 如果要不输出结果文件,那么请加上 -o " ",引号里的是空格(没有空格会导致该参数被省略)。
|
||||
D:\ABC\CloudflareST\CloudflareST.exe -n 500 -t 4 -dn 20 -dt 5 -o " "
|
||||
|
||||
# 如果文件路径包含引号,则需要把启动参数放在引号外面,记得引号和 - 之间有空格。
|
||||
"D:\Program Files\CloudflareST\CloudflareST.exe" -n 500 -t 4 -dn 20 -dt 5 -o " "
|
||||
```
|
||||
****
|
||||
#### \# 单独测速 IP
|
||||
|
||||
如果要单独**对一个或多个 IP 进行测速**,只需要把这些 IP 按如下格式写入到任意文本文件中,例如:`1.txt`
|
||||
|
||||
``` json
|
||||
1.1.1.1/32
|
||||
1.0.0.1/24
|
||||
|
||||
// 子网掩码 /32 指的是这个 IP 本身,即 1.1.1.1
|
||||
// 子网掩码 /24 指的是这个 IP 最后一段,即 1.0.0.1~1.0.0.255
|
||||
```
|
||||
|
||||
然后运行 CloudflareST 时加上启动参数 `-f 1.txt` 即可。
|
||||
|
||||
``` bash
|
||||
# 先进入 CloudflareST 所在目录,然后运行:
|
||||
# Windows(在 CMD 中运行)
|
||||
CloudflareST.exe -f 1.txt
|
||||
|
||||
# Linux
|
||||
./CloudflareST -f 1.txt
|
||||
|
||||
# 对于 IP 段 1.0.0.1/24 软件只会随机最后一段(1.0.0.1~255),如果要测速该 IP 段中的所有 IP,需要加上 -allip 参数。
|
||||
```
|
||||
|
||||
****
|
||||
#### \# 自动更新 Hosts
|
||||
|
||||
考虑到很多人获得最快 Cloudflare CDN IP 后,需要替换 Hosts 文件中的 IP。
|
||||
|
||||
可以看这个 [**Issues**](https://github.com/XIU2/CloudflareSpeedTest/issues/42) 获取 **Windows/Linux 自动更新 Hosts 脚本**!
|
||||
|
||||
****
|
||||
#### \# 自动更新域名解析记录
|
||||
|
||||
如果你的域名托管在 Cloudflare,则可以通过 Cloudflare 官方提供的 API 来自动更新域名解析记录。
|
||||
|
||||
可以看这个 [**Issues**](https://github.com/XIU2/CloudflareSpeedTest/issues/40) 获取**手动教程**或 **Windows/Linux 自动更新脚本**!
|
||||
|
||||
****
|
||||
## 问题反馈
|
||||
|
||||
如果你遇到什么问题,可以先去 [**Issues**](https://github.com/XIU2/CloudflareSpeedTest/issues) 里看看是否有别人问过了(记得去看下 [**Closed**](https://github.com/XIU2/CloudflareSpeedTest/issues?q=is%3Aissue+is%3Aclosed) 的)。
|
||||
如果没找到类似问题,请新开个 [**Issues**](https://github.com/XIU2/CloudflareSpeedTest/issues/new) 来告诉我!
|
||||
|
||||
> _有问题请**大胆告诉我**,描述越详细越好(必要时可远程协助),如果不说那我怎么去完善功能或~~修复 BUG~~ 呢?!_
|
||||
|
||||
****
|
||||
## 感谢项目
|
||||
|
||||
* https://github.com/Spedoske/CloudflareScanner
|
||||
|
||||
> _因为该项目已经很长时间没更新了,而我又产生了很多功能需求,所以我临时学了下 Go 语言就上手了(菜)..._
|
||||
> _本软件基于该项目制作,但**已添加大量功能及修复 BUG**,并根据大家的使用反馈积极添加、优化功能(闲)..._
|
||||
|
||||
****
|
||||
## License
|
||||
The GPL-3.0 License.
|
||||
|
||||
4
ip.txt
4
ip.txt
@@ -1,3 +1,7 @@
|
||||
1.1.1.0/24
|
||||
1.0.0.0/24
|
||||
1.1.1.1/32
|
||||
1.0.0.1/32
|
||||
173.245.48.0/20
|
||||
103.21.244.0/22
|
||||
103.22.200.0/22
|
||||
|
||||
42
ipv6.txt
Normal file
42
ipv6.txt
Normal file
@@ -0,0 +1,42 @@
|
||||
2606:4700:10::6814:0/112
|
||||
2606:4700:10::ac43:0/112
|
||||
2606:4700:3000::/48
|
||||
2606:4700:3001::/48
|
||||
2606:4700:3002::/48
|
||||
2606:4700:3003::/48
|
||||
2606:4700:3004::/48
|
||||
2606:4700:3005::/48
|
||||
2606:4700:3006::/48
|
||||
2606:4700:3007::/48
|
||||
2606:4700:3008::/48
|
||||
2606:4700:3009::/48
|
||||
2606:4700:3010::/48
|
||||
2606:4700:3011::/48
|
||||
2606:4700:3012::/48
|
||||
2606:4700:3013::/48
|
||||
2606:4700:3014::/48
|
||||
2606:4700:3015::/48
|
||||
2606:4700:3016::/48
|
||||
2606:4700:3017::/48
|
||||
2606:4700:3018::/48
|
||||
2606:4700:3019::/48
|
||||
2606:4700:3020::/48
|
||||
2606:4700:3021::/48
|
||||
2606:4700:3022::/48
|
||||
2606:4700:3023::/48
|
||||
2606:4700:3024::/48
|
||||
2606:4700:3025::/48
|
||||
2606:4700:3026::/48
|
||||
2606:4700:3027::/48
|
||||
2606:4700:3028::/48
|
||||
2606:4700:3029::/48
|
||||
2606:4700:3030::/48
|
||||
2606:4700:3031::/48
|
||||
2606:4700:3032::/48
|
||||
2606:4700:3033::/48
|
||||
2606:4700:3034::/48
|
||||
2606:4700:3035::/48
|
||||
2606:4700:3036::/48
|
||||
2606:4700:3037::/48
|
||||
2606:4700:3038::/48
|
||||
2606:4700:3039::/48
|
||||
345
main.go
345
main.go
@@ -1,67 +1,278 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/cheggaaa/pb/v3"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func handleUserInput() {
|
||||
fmt.Println("请输入扫描协程数(数字越大越快,默认400):")
|
||||
fmt.Scanln(&pingRoutine)
|
||||
if pingRoutine <= 0 {
|
||||
pingRoutine = 400
|
||||
}
|
||||
fmt.Println("请输入tcping次数(默认10):")
|
||||
fmt.Scanln(&pingTime)
|
||||
if pingTime <= 0 {
|
||||
pingTime = 10
|
||||
}
|
||||
fmt.Println("请输入要测试的下载节点个数(默认10):")
|
||||
fmt.Scanln(&downloadTestCount)
|
||||
if downloadTestCount <= 0 {
|
||||
downloadTestCount = 10
|
||||
}
|
||||
fmt.Println("请输入下载测试时间(默认10,单位为秒):")
|
||||
var downloadSecond int64
|
||||
fmt.Scanln(&downloadSecond)
|
||||
if downloadSecond <= 0 {
|
||||
downloadSecond = 10
|
||||
}
|
||||
downloadTestTime = time.Duration(downloadSecond) * time.Second
|
||||
}
|
||||
|
||||
func main() {
|
||||
initipEndWith()
|
||||
handleUserInput()
|
||||
ips := loadFirstIPOfRangeFromFile()
|
||||
pingCount := len(ips) * pingTime
|
||||
bar := pb.StartNew(pingCount)
|
||||
var wg sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
var data = make([]CloudflareIPData, 0)
|
||||
|
||||
fmt.Println("开始tcping")
|
||||
|
||||
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)
|
||||
}
|
||||
wg.Wait()
|
||||
bar.Finish()
|
||||
bar = pb.StartNew(downloadTestCount)
|
||||
sort.Sort(CloudflareIPDataSet(data))
|
||||
fmt.Println("开始下载测速")
|
||||
for i := 0; i < downloadTestCount; i++ {
|
||||
_, speed := DownloadSpeedHandler(data[i].ip)
|
||||
data[i].downloadSpeed = speed
|
||||
bar.Add(1)
|
||||
}
|
||||
bar.Finish()
|
||||
ExportCsv("./result.csv", data)
|
||||
}
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/cheggaaa/pb/v3"
|
||||
)
|
||||
|
||||
var version, ipFile, outputFile, versionNew string
|
||||
var disableDownload, ipv6Mode, allip bool
|
||||
var tcpPort, printResultNum, downloadSecond int
|
||||
var timeLimit, speedLimit float64
|
||||
|
||||
func init() {
|
||||
var printVersion bool
|
||||
var help = `
|
||||
CloudflareSpeedTest ` + version + `
|
||||
测试 Cloudflare CDN 所有 IP 的延迟和速度,获取最快 IP (IPv4+IPv6)!
|
||||
https://github.com/XIU2/CloudflareSpeedTest
|
||||
|
||||
参数:
|
||||
-n 500
|
||||
测速线程数量;越多测速越快,性能弱的设备 (如路由器) 请适当调低;(默认 500 最多 1000)
|
||||
-t 4
|
||||
延迟测速次数;单个 IP 延迟测速次数,为 1 时将过滤丢包的IP,TCP协议;(默认 4)
|
||||
-tp 443
|
||||
延迟测速端口;延迟测速 TCP 协议的端口;(默认 443)
|
||||
-dn 20
|
||||
下载测速数量;延迟测速并排序后,从最低延迟起下载测速的数量;(默认 20)
|
||||
-dt 10
|
||||
下载测速时间;单个 IP 下载测速最长时间,单位:秒;(默认 10)
|
||||
-url https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png
|
||||
下载测速地址;用来下载测速的 Cloudflare CDN 文件地址,如地址含有空格请加上引号;
|
||||
-tl 200
|
||||
平均延迟上限;只输出低于指定平均延迟的 IP,可单独使用也可搭配下载速度下限;(默认 9999.00 ms)
|
||||
-sl 5
|
||||
下载速度下限;只输出高于指定下载速度的 IP,凑够指定数量 [-dn] 才会停止测速;(默认 0.00 MB/s)
|
||||
-p 20
|
||||
显示结果数量;测速后直接显示指定数量的结果,为 0 时不显示结果直接退出;(默认 20)
|
||||
-f ip.txt
|
||||
IP段数据文件;如路径含有空格请加上引号;支持其他 CDN IP段;(默认 ip.txt)
|
||||
-o result.csv
|
||||
输出结果文件;如路径含有空格请加上引号;值为空格时不输出 [-o " "];(默认 result.csv)
|
||||
-dd
|
||||
禁用下载测速;禁用后测速结果会按延迟排序 (默认按下载速度排序);(默认 启用)
|
||||
-ipv6
|
||||
IPv6测速模式;确保 IP 段数据文件内只包含 IPv6 IP段,软件不支持同时测速 IPv4+IPv6;(默认 IPv4)
|
||||
-allip
|
||||
测速全部的IP;对 IP 段中的每个 IP (仅支持 IPv4) 进行测速;(默认 每个 IP 段随机测速一个 IP)
|
||||
-v
|
||||
打印程序版本+检查版本更新
|
||||
-h
|
||||
打印帮助说明
|
||||
`
|
||||
|
||||
flag.IntVar(&pingRoutine, "n", 500, "测速线程数量")
|
||||
flag.IntVar(&pingTime, "t", 4, "延迟测速次数")
|
||||
flag.IntVar(&tcpPort, "tp", 443, "延迟测速端口")
|
||||
flag.IntVar(&downloadTestCount, "dn", 20, "下载测速数量")
|
||||
flag.IntVar(&downloadSecond, "dt", 10, "下载测速时间")
|
||||
flag.StringVar(&url, "url", "https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png", "下载测速地址")
|
||||
flag.Float64Var(&timeLimit, "tl", 9999, "平均延迟上限")
|
||||
flag.Float64Var(&speedLimit, "sl", 0, "下载速度下限")
|
||||
flag.IntVar(&printResultNum, "p", 20, "显示结果数量")
|
||||
flag.BoolVar(&disableDownload, "dd", false, "禁用下载测速")
|
||||
flag.BoolVar(&ipv6Mode, "ipv6", false, "禁用下载测速")
|
||||
flag.BoolVar(&allip, "allip", false, "测速全部 IP")
|
||||
flag.StringVar(&ipFile, "f", "ip.txt", "IP 数据文件")
|
||||
flag.StringVar(&outputFile, "o", "result.csv", "输出结果文件")
|
||||
flag.BoolVar(&printVersion, "v", false, "打印程序版本")
|
||||
|
||||
flag.Usage = func() { fmt.Print(help) }
|
||||
flag.Parse()
|
||||
if printVersion {
|
||||
println(version)
|
||||
fmt.Println("检查版本更新中...")
|
||||
checkUpdate()
|
||||
if versionNew != "" {
|
||||
fmt.Println("发现新版本 [" + versionNew + "]!请前往 [https://github.com/XIU2/CloudflareSpeedTest] 更新!")
|
||||
} else {
|
||||
fmt.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 url == "" {
|
||||
url = "https://cf.xiu2.xyz/Github/CloudflareSpeedTest.png"
|
||||
}
|
||||
if timeLimit <= 0 {
|
||||
timeLimit = 9999
|
||||
}
|
||||
if speedLimit < 0 {
|
||||
speedLimit = 0
|
||||
}
|
||||
if printResultNum < 0 {
|
||||
printResultNum = 20
|
||||
}
|
||||
if ipFile == "" {
|
||||
ipFile = "ip.txt"
|
||||
}
|
||||
if outputFile == " " {
|
||||
outputFile = ""
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
go checkUpdate() // 检查版本更新
|
||||
initRandSeed() // 置随机数种子
|
||||
failTime = pingTime // 设置接收次数
|
||||
ips := loadFirstIPOfRangeFromFile(ipFile) // 读入IP
|
||||
pingCount := len(ips) * pingTime // 计算进度条总数(IP*测试次数)
|
||||
bar := pb.Simple.Start(pingCount) // 进度条总数
|
||||
var wg sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
var data = make([]CloudflareIPData, 0)
|
||||
var data2 = make([]CloudflareIPData, 0)
|
||||
downloadTestTime = time.Duration(downloadSecond) * time.Second
|
||||
|
||||
// 开始延迟测速
|
||||
fmt.Println("# XIU2/CloudflareSpeedTest " + version + "\n")
|
||||
if ipv6Mode { // IPv6 模式判断
|
||||
fmt.Println("开始延迟测速(模式:TCP IPv6,端口:" + strconv.Itoa(tcpPort) + ",平均延迟上限:" + fmt.Sprintf("%.2f", timeLimit) + " ms):")
|
||||
} else {
|
||||
fmt.Println("开始延迟测速(模式:TCP IPv4,端口:" + strconv.Itoa(tcpPort) + ",平均延迟上限:" + fmt.Sprintf("%.2f", timeLimit) + " ms):")
|
||||
}
|
||||
control := make(chan bool, pingRoutine)
|
||||
for _, ip := range ips {
|
||||
wg.Add(1)
|
||||
control <- false
|
||||
handleProgress := handleProgressGenerator(bar) // 多线程进度条
|
||||
go tcpingGoroutine(&wg, &mu, ip, tcpPort, pingTime, &data, control, handleProgress)
|
||||
}
|
||||
wg.Wait()
|
||||
bar.Finish()
|
||||
|
||||
sort.Sort(CloudflareIPDataSet(data)) // 排序(按延迟,从低到高,不同丢包率会分开单独按延迟和丢包率排序)
|
||||
|
||||
// 延迟测速完毕后,以 [平均延迟上限] 条件过滤结果
|
||||
if timeLimit < 9999 && timeLimit > 0 {
|
||||
for i := 0; i < len(data); i++ {
|
||||
if float64(data[i].pingTime) <= timeLimit {
|
||||
data2 = append(data2, data[i]) // 延迟满足条件时,添加到新数组中
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
data = data2
|
||||
data2 = []CloudflareIPData{}
|
||||
}
|
||||
|
||||
// 开始下载测速
|
||||
if !disableDownload { // 如果禁用下载测速就跳过
|
||||
if len(data) > 0 { // IP数组长度(IP数量) 大于 0 时才会继续下载测速
|
||||
if len(data) < downloadTestCount { // 如果IP数组长度(IP数量) 小于下载测速数量(-dn),则次数修正为IP数
|
||||
downloadTestCount = len(data)
|
||||
}
|
||||
var downloadTestCount2 int // 临时的下载测速次数,即实际的下载测速数量
|
||||
if speedLimit > 0 {
|
||||
downloadTestCount2 = len(data) // 如果指定了 [下载速度下限] 条件,则临时变量改为总数量(即一直测速下去,直到凑够下载测速数量 -dn)
|
||||
} else {
|
||||
downloadTestCount2 = downloadTestCount // 如果没有指定 [下载速度下限] 条件,则临时变量为下载测速数量(-dn)
|
||||
}
|
||||
fmt.Println("开始下载测速(下载速度下限:" + fmt.Sprintf("%.2f", speedLimit) + " MB/s,下载测速数量:" + strconv.Itoa(downloadTestCount) + ",下载测速队列:" + strconv.Itoa(downloadTestCount2) + "):")
|
||||
bar = pb.Simple.Start(downloadTestCount)
|
||||
for i := 0; i < downloadTestCount2; i++ {
|
||||
_, speed := DownloadSpeedHandler(data[i].ip)
|
||||
data[i].downloadSpeed = speed
|
||||
// 在每个 IP 下载测速后,以 [下载速度下限] 条件过滤结果
|
||||
if float64(speed)/1024/1024 >= speedLimit {
|
||||
data2 = append(data2, data[i]) // 高于下载速度下限时,添加到新数组中
|
||||
bar.Add(1)
|
||||
if len(data2) == downloadTestCount { // 凑够满足条件的 IP 时(下载测速数量 -dn),就跳出循环
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
bar.Finish()
|
||||
} else {
|
||||
fmt.Println("\n[信息] 延迟测速结果 IP 数量为 0,跳过下载测速。")
|
||||
}
|
||||
}
|
||||
|
||||
if len(data2) > 0 { // 如果该数组有内容,说明指定了 [下载测速下限] 条件,且最少有 1 个满足条件的 IP
|
||||
data = data2
|
||||
}
|
||||
sort.Sort(CloudflareIPDataSetD(data)) // 排序(按下载速度,从高到低)
|
||||
if outputFile != "" {
|
||||
ExportCsv(outputFile, data) // 输出结果到文件
|
||||
}
|
||||
printResult(data) // 显示最快结果
|
||||
}
|
||||
|
||||
// 显示最快结果
|
||||
func printResult(data []CloudflareIPData) {
|
||||
sysType := runtime.GOOS
|
||||
if printResultNum > 0 { // 如果禁止直接输出结果就跳过
|
||||
dateString := convertToString(data) // 转为多维数组 [][]String
|
||||
if len(dateString) > 0 { // IP数组长度(IP数量) 大于 0 时继续
|
||||
if len(dateString) < printResultNum { // 如果IP数组长度(IP数量) 小于 打印次数,则次数改为IP数量
|
||||
printResultNum = len(dateString)
|
||||
}
|
||||
if ipv6Mode { // IPv6 太长了,所以需要调整一下间隔
|
||||
fmt.Printf("%-40s%-5s%-5s%-5s%-6s%-11s\n", "IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)")
|
||||
for i := 0; i < printResultNum; i++ {
|
||||
fmt.Printf("%-42s%-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])
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("%-16s%-5s%-5s%-5s%-6s%-11s\n", "IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)")
|
||||
for i := 0; i < printResultNum; 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 versionNew != "" {
|
||||
fmt.Println("\n发现新版本 [" + versionNew + "]!请前往 [https://github.com/XIU2/CloudflareSpeedTest] 更新!")
|
||||
}
|
||||
|
||||
if sysType == "windows" { // 如果是 Windows 系统,则需要按下 回车键 或 Ctrl+C 退出(避免通过双击运行时,测速完毕后直接关闭)
|
||||
if outputFile != "" {
|
||||
fmt.Printf("\n完整测速结果已写入 %v 文件,请使用记事本/表格软件查看。\n按下 回车键 或 Ctrl+C 退出。", outputFile)
|
||||
} else {
|
||||
fmt.Printf("\n按下 回车键 或 Ctrl+C 退出。")
|
||||
}
|
||||
var pause int
|
||||
fmt.Scanln(&pause)
|
||||
} else { // 其它系统直接退出
|
||||
if outputFile != "" {
|
||||
fmt.Println("\n完整测速结果已写入 " + outputFile + " 文件,请使用记事本/表格软件查看。")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fmt.Println("\n[信息] 完整测速结果 IP 数量为 0,跳过输出结果。")
|
||||
}
|
||||
} else {
|
||||
fmt.Println("\n完整测速结果已写入 " + outputFile + " 文件,请使用记事本/表格软件查看。")
|
||||
}
|
||||
}
|
||||
|
||||
// 检查更新
|
||||
func checkUpdate() {
|
||||
timeout := time.Duration(10 * time.Second)
|
||||
client := http.Client{Timeout: timeout}
|
||||
res, err := client.Get("https://api.xiuer.pw/ver/cloudflarespeedtest.txt")
|
||||
if err == nil {
|
||||
// 读取资源数据 body: []byte
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
// 关闭资源流
|
||||
res.Body.Close()
|
||||
if err == nil {
|
||||
if string(body) != version {
|
||||
versionNew = string(body)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
331
tcping.go
331
tcping.go
@@ -1,159 +1,172 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/VividCortex/ewma"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
//bool connectionSucceed float32 time
|
||||
func tcping(ip net.IPAddr) (bool, float32) {
|
||||
startTime := time.Now()
|
||||
conn, err := net.DialTimeout("tcp", ip.String()+":"+strconv.Itoa(defaultTcpPort), tcpConnectTimeout)
|
||||
if err != nil {
|
||||
return false, 0
|
||||
} else {
|
||||
var endTime = time.Since(startTime)
|
||||
var duration = float32(endTime.Microseconds()) / 1000.0
|
||||
_ = conn.Close()
|
||||
return true, duration
|
||||
}
|
||||
}
|
||||
|
||||
//pingReceived pingTotalTime
|
||||
func checkConnection(ip net.IPAddr) (int, float32) {
|
||||
pingRecv := 0
|
||||
var pingTime float32 = 0.0
|
||||
for i := 1; i <= failTime; i++ {
|
||||
pingSucceed, pingTimeCurrent := tcping(ip)
|
||||
if pingSucceed {
|
||||
pingRecv++
|
||||
pingTime += pingTimeCurrent
|
||||
}
|
||||
}
|
||||
return pingRecv, pingTime
|
||||
}
|
||||
|
||||
//return Success packetRecv averagePingTime specificIPAddr
|
||||
func tcpingHandler(ip net.IPAddr, 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)
|
||||
if pingRecvCurrent != 0 {
|
||||
ipCanConnect = true
|
||||
pingRecv = pingRecvCurrent
|
||||
pingTime = pingTimeCurrent
|
||||
} else {
|
||||
ip.IP[15]++
|
||||
if ip.IP[15] == 0 {
|
||||
break
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if ipCanConnect {
|
||||
progressHandler(AvailableIPFound)
|
||||
for i := failTime; i < pingCount; i++ {
|
||||
pingSuccess, pingTimeCurrent := tcping(ip)
|
||||
progressHandler(NormalPing)
|
||||
if pingSuccess {
|
||||
pingRecv++
|
||||
pingTime += pingTimeCurrent
|
||||
}
|
||||
}
|
||||
return true, pingRecv, pingTime / float32(pingRecv), ip
|
||||
} else {
|
||||
progressHandler(NoAvailableIPFound)
|
||||
return false, 0, 0, net.IPAddr{}
|
||||
}
|
||||
}
|
||||
|
||||
func tcpingGoroutine(wg *sync.WaitGroup, mutex *sync.Mutex, ip net.IPAddr, pingCount int, csv *[]CloudflareIPData, control chan bool, progressHandler func(e progressEvent)) {
|
||||
defer wg.Done()
|
||||
success, pingRecv, pingTimeAvg, currentIP := tcpingHandler(ip, pingCount, progressHandler)
|
||||
if success {
|
||||
mutex.Lock()
|
||||
var cfdata CloudflareIPData
|
||||
cfdata.ip = currentIP
|
||||
cfdata.pingReceived = pingRecv
|
||||
cfdata.pingTime = pingTimeAvg
|
||||
cfdata.pingCount = pingCount
|
||||
*csv = append(*csv, cfdata)
|
||||
mutex.Unlock()
|
||||
}
|
||||
<-control
|
||||
}
|
||||
|
||||
func GetDialContextByAddr(fakeSourceAddr string) func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
c, e := (&net.Dialer{}).DialContext(ctx, network, fakeSourceAddr)
|
||||
return c, e
|
||||
}
|
||||
}
|
||||
|
||||
//bool : can download,float32 downloadSpeed
|
||||
func DownloadSpeedHandler(ip net.IPAddr) (bool, float32) {
|
||||
var client = http.Client{
|
||||
Transport: nil,
|
||||
CheckRedirect: nil,
|
||||
Jar: nil,
|
||||
Timeout: 0,
|
||||
}
|
||||
client.Transport = &http.Transport{
|
||||
DialContext: GetDialContextByAddr(ip.String() + ":443"),
|
||||
}
|
||||
response, err := client.Get(url)
|
||||
|
||||
if err != nil {
|
||||
return false, 0
|
||||
} else {
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
if response.StatusCode == 200 {
|
||||
timeStart := time.Now()
|
||||
timeEnd := timeStart.Add(downloadTestTime)
|
||||
|
||||
contentLength := response.ContentLength
|
||||
buffer := make([]byte, downloadBufferSize)
|
||||
|
||||
var contentRead int64 = 0
|
||||
var timeSlice = downloadTestTime / 100
|
||||
var timeCounter = 1
|
||||
var lastContentRead int64 = 0
|
||||
|
||||
var nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
|
||||
e := ewma.NewMovingAverage()
|
||||
|
||||
for ; contentLength != contentRead; {
|
||||
var currentTime = time.Now()
|
||||
if currentTime.After(nextTime) {
|
||||
timeCounter += 1
|
||||
nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
|
||||
e.Add(float64(contentRead - lastContentRead))
|
||||
lastContentRead = contentRead
|
||||
}
|
||||
if currentTime.After(timeEnd) {
|
||||
break
|
||||
}
|
||||
bufferRead, err := response.Body.Read(buffer)
|
||||
contentRead += int64(bufferRead)
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
break
|
||||
} else {
|
||||
e.Add(float64(contentRead-lastContentRead) / (float64(nextTime.Sub(currentTime)) / float64(timeSlice)))
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, float32(e.Value()) / (float32(downloadTestTime.Seconds()) / 100)
|
||||
} else {
|
||||
return false, 0
|
||||
}
|
||||
}
|
||||
}
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/VividCortex/ewma"
|
||||
)
|
||||
|
||||
//bool connectionSucceed float32 time
|
||||
func tcping(ip net.IPAddr, tcpPort int) (bool, float32) {
|
||||
startTime := time.Now()
|
||||
var fullAddress string
|
||||
//fmt.Println(ip.String())
|
||||
if ipv6Mode { // IPv6 需要加上 []
|
||||
fullAddress = "[" + ip.String() + "]:" + strconv.Itoa(tcpPort)
|
||||
} else {
|
||||
fullAddress = ip.String() + ":" + strconv.Itoa(tcpPort)
|
||||
}
|
||||
conn, err := net.DialTimeout("tcp", fullAddress, tcpConnectTimeout)
|
||||
if err != nil {
|
||||
return false, 0
|
||||
} else {
|
||||
var endTime = time.Since(startTime)
|
||||
var duration = float32(endTime.Microseconds()) / 1000.0
|
||||
_ = conn.Close()
|
||||
return true, duration
|
||||
}
|
||||
}
|
||||
|
||||
//pingReceived pingTotalTime
|
||||
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, tcpPort)
|
||||
if pingSucceed {
|
||||
pingRecv++
|
||||
pingTime += pingTimeCurrent
|
||||
}
|
||||
}
|
||||
return pingRecv, pingTime
|
||||
}
|
||||
|
||||
//return Success packetRecv averagePingTime specificIPAddr
|
||||
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, tcpPort)
|
||||
if pingRecvCurrent != 0 {
|
||||
ipCanConnect = true
|
||||
pingRecv = pingRecvCurrent
|
||||
pingTime = pingTimeCurrent
|
||||
} else {
|
||||
ip.IP[15]++
|
||||
if ip.IP[15] == 0 {
|
||||
break
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if ipCanConnect {
|
||||
progressHandler(AvailableIPFound)
|
||||
for i := failTime; i < pingCount; i++ {
|
||||
pingSuccess, pingTimeCurrent := tcping(ip, tcpPort)
|
||||
progressHandler(NormalPing)
|
||||
if pingSuccess {
|
||||
pingRecv++
|
||||
pingTime += pingTimeCurrent
|
||||
}
|
||||
}
|
||||
return true, pingRecv, pingTime / float32(pingRecv), ip
|
||||
} else {
|
||||
progressHandler(NoAvailableIPFound)
|
||||
return false, 0, 0, net.IPAddr{}
|
||||
}
|
||||
}
|
||||
|
||||
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, tcpPort, pingCount, progressHandler)
|
||||
if success {
|
||||
mutex.Lock()
|
||||
var cfdata CloudflareIPData
|
||||
cfdata.ip = currentIP
|
||||
cfdata.pingReceived = pingRecv
|
||||
cfdata.pingTime = pingTimeAvg
|
||||
cfdata.pingCount = pingCount
|
||||
*csv = append(*csv, cfdata)
|
||||
mutex.Unlock()
|
||||
}
|
||||
<-control
|
||||
}
|
||||
|
||||
func GetDialContextByAddr(fakeSourceAddr string) func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
c, e := (&net.Dialer{}).DialContext(ctx, network, fakeSourceAddr)
|
||||
return c, e
|
||||
}
|
||||
}
|
||||
|
||||
//bool : can download,float32 downloadSpeed
|
||||
func DownloadSpeedHandler(ip net.IPAddr) (bool, float32) {
|
||||
var client = http.Client{
|
||||
Transport: nil,
|
||||
CheckRedirect: nil,
|
||||
Jar: nil,
|
||||
Timeout: downloadTestTime,
|
||||
}
|
||||
var fullAddress string
|
||||
if ipv6Mode { // IPv6 需要加上 []
|
||||
fullAddress = "[" + ip.String() + "]:443"
|
||||
} else {
|
||||
fullAddress = ip.String() + ":443"
|
||||
}
|
||||
client.Transport = &http.Transport{
|
||||
DialContext: GetDialContextByAddr(fullAddress),
|
||||
}
|
||||
response, err := client.Get(url)
|
||||
if err != nil {
|
||||
return false, 0
|
||||
} else {
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
if response.StatusCode == 200 {
|
||||
timeStart := time.Now()
|
||||
timeEnd := timeStart.Add(downloadTestTime)
|
||||
|
||||
contentLength := response.ContentLength
|
||||
buffer := make([]byte, downloadBufferSize)
|
||||
|
||||
var contentRead int64 = 0
|
||||
var timeSlice = downloadTestTime / 100
|
||||
var timeCounter = 1
|
||||
var lastContentRead int64 = 0
|
||||
|
||||
var nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
|
||||
e := ewma.NewMovingAverage()
|
||||
|
||||
for contentLength != contentRead {
|
||||
var currentTime = time.Now()
|
||||
if currentTime.After(nextTime) {
|
||||
timeCounter += 1
|
||||
nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
|
||||
e.Add(float64(contentRead - lastContentRead))
|
||||
lastContentRead = contentRead
|
||||
}
|
||||
if currentTime.After(timeEnd) {
|
||||
break
|
||||
}
|
||||
bufferRead, err := response.Body.Read(buffer)
|
||||
contentRead += int64(bufferRead)
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
break
|
||||
} else {
|
||||
e.Add(float64(contentRead-lastContentRead) / (float64(nextTime.Sub(currentTime)) / float64(timeSlice)))
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, float32(e.Value()) / (float32(downloadTestTime.Seconds()) / 120)
|
||||
} else {
|
||||
return false, 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
286
util.go
286
util.go
@@ -1,122 +1,164 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"github.com/cheggaaa/pb/v3"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CloudflareIPData struct {
|
||||
ip net.IPAddr
|
||||
pingCount int
|
||||
pingReceived int
|
||||
recvRate float32
|
||||
downloadSpeed float32
|
||||
pingTime float32
|
||||
}
|
||||
|
||||
func (cf *CloudflareIPData) getRecvRate() float32 {
|
||||
if cf.recvRate == 0 {
|
||||
cf.recvRate = float32(cf.pingReceived) / float32(cf.pingCount)
|
||||
}
|
||||
return cf.recvRate
|
||||
}
|
||||
|
||||
func ExportCsv(filePath string, data []CloudflareIPData) {
|
||||
fp, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
log.Fatalf("创建文件["+filePath+"]句柄失败,%v", err)
|
||||
return
|
||||
}
|
||||
defer fp.Close()
|
||||
w := csv.NewWriter(fp) //创建一个新的写入文件流
|
||||
w.Write([]string{"IP Address", "Ping count", "Ping received", "Ping received rate", "Ping time", "Download Speed (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()
|
||||
result[1] = strconv.Itoa(cf.pingCount)
|
||||
result[2] = strconv.Itoa(cf.pingReceived)
|
||||
result[3] = strconv.FormatFloat(float64(cf.getRecvRate()), 'f', 4, 32)
|
||||
result[4] = strconv.FormatFloat(float64(cf.pingTime), 'f', 4, 32)
|
||||
result[5] = strconv.FormatFloat(float64(cf.downloadSpeed)/1024/1024, 'f', 4, 32)
|
||||
return result
|
||||
}
|
||||
|
||||
func convertToString(data []CloudflareIPData) [][]string {
|
||||
result := make([][]string, 0)
|
||||
for _, v := range data {
|
||||
result = append(result, v.toString())
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
var pingTime int
|
||||
var pingRoutine int
|
||||
|
||||
var ipEndWith uint8 = 0
|
||||
|
||||
type progressEvent int
|
||||
|
||||
const (
|
||||
NoAvailableIPFound progressEvent = iota
|
||||
AvailableIPFound
|
||||
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 downloadTestTime time.Duration
|
||||
|
||||
const downloadBufferSize = 1024
|
||||
|
||||
var downloadTestCount int
|
||||
|
||||
const defaultTcpPort = 443
|
||||
const tcpConnectTimeout = time.Second * 1
|
||||
const failTime = 4
|
||||
|
||||
type CloudflareIPDataSet []CloudflareIPData
|
||||
|
||||
func initipEndWith() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
ipEndWith = uint8(rand.Intn(254) + 1)
|
||||
}
|
||||
|
||||
func handleProgressGenerator(pb *pb.ProgressBar) func(e progressEvent) {
|
||||
return func(e progressEvent) {
|
||||
switch e {
|
||||
case NoAvailableIPFound:
|
||||
pb.Add(pingTime)
|
||||
case AvailableIPFound:
|
||||
pb.Add(failTime)
|
||||
case NormalPing:
|
||||
pb.Increment()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (cfs CloudflareIPDataSet) Len() int {
|
||||
return len(cfs)
|
||||
}
|
||||
|
||||
func (cfs CloudflareIPDataSet) Less(i, j int) bool {
|
||||
if (cfs)[i].getRecvRate() != cfs[j].getRecvRate() {
|
||||
return cfs[i].getRecvRate() > cfs[j].getRecvRate()
|
||||
}
|
||||
return cfs[i].pingTime < cfs[j].pingTime
|
||||
}
|
||||
|
||||
func (cfs CloudflareIPDataSet) Swap(i, j int) {
|
||||
cfs[i], cfs[j] = cfs[j], cfs[i]
|
||||
}
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/cheggaaa/pb/v3"
|
||||
)
|
||||
|
||||
type CloudflareIPData struct {
|
||||
ip net.IPAddr
|
||||
pingCount int
|
||||
pingReceived int
|
||||
recvRate float32
|
||||
downloadSpeed float32
|
||||
pingTime float32
|
||||
}
|
||||
|
||||
func (cf *CloudflareIPData) getRecvRate() float32 {
|
||||
if cf.recvRate == 0 {
|
||||
pingLost := cf.pingCount - cf.pingReceived
|
||||
cf.recvRate = float32(pingLost) / float32(cf.pingCount)
|
||||
}
|
||||
return cf.recvRate
|
||||
}
|
||||
|
||||
func ExportCsv(filePath string, data []CloudflareIPData) {
|
||||
fp, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
log.Fatalf("创建文件["+filePath+"]句柄失败,%v", err)
|
||||
return
|
||||
}
|
||||
defer fp.Close()
|
||||
w := csv.NewWriter(fp) //创建一个新的写入文件流
|
||||
w.Write([]string{"IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)"})
|
||||
w.WriteAll(convertToString(data))
|
||||
w.Flush()
|
||||
}
|
||||
|
||||
func (cf *CloudflareIPData) toString() []string {
|
||||
result := make([]string, 6)
|
||||
result[0] = cf.ip.String()
|
||||
result[1] = strconv.Itoa(cf.pingCount)
|
||||
result[2] = strconv.Itoa(cf.pingReceived)
|
||||
result[3] = strconv.FormatFloat(float64(cf.getRecvRate()), 'f', 2, 32)
|
||||
result[4] = strconv.FormatFloat(float64(cf.pingTime), 'f', 2, 32)
|
||||
result[5] = strconv.FormatFloat(float64(cf.downloadSpeed)/1024/1024, 'f', 2, 32)
|
||||
return result
|
||||
}
|
||||
|
||||
func convertToString(data []CloudflareIPData) [][]string {
|
||||
result := make([][]string, 0)
|
||||
for _, v := range data {
|
||||
result = append(result, v.toString())
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
var pingTime int
|
||||
var pingRoutine int
|
||||
|
||||
type progressEvent int
|
||||
|
||||
const (
|
||||
NoAvailableIPFound progressEvent = iota
|
||||
AvailableIPFound
|
||||
NormalPing
|
||||
)
|
||||
|
||||
var url string
|
||||
|
||||
var downloadTestTime time.Duration
|
||||
|
||||
const downloadBufferSize = 1024
|
||||
|
||||
var downloadTestCount int
|
||||
|
||||
//const defaultTcpPort = 443
|
||||
const tcpConnectTimeout = time.Second * 1
|
||||
|
||||
var failTime int
|
||||
|
||||
// 平均延迟排序(丢包另算)
|
||||
type CloudflareIPDataSet []CloudflareIPData
|
||||
|
||||
// 下载速度排序
|
||||
type CloudflareIPDataSetD []CloudflareIPData
|
||||
|
||||
func initRandSeed() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
func randipEndWith(num int) uint8 {
|
||||
return uint8(rand.Intn(num))
|
||||
}
|
||||
|
||||
func GetRandomString() string {
|
||||
str := "0123456789abcdef"
|
||||
bytes := []byte(str)
|
||||
result := []byte{}
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
for i := 0; i < 4; i++ {
|
||||
result = append(result, bytes[r.Intn(len(bytes))])
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
|
||||
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) {
|
||||
return func(e progressEvent) {
|
||||
switch e {
|
||||
case NoAvailableIPFound:
|
||||
pb.Add(pingTime)
|
||||
case AvailableIPFound:
|
||||
pb.Add(failTime)
|
||||
case NormalPing:
|
||||
pb.Increment()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (cfs CloudflareIPDataSet) Len() int {
|
||||
return len(cfs)
|
||||
}
|
||||
|
||||
func (cfs CloudflareIPDataSet) Less(i, j int) bool {
|
||||
if (cfs)[i].getRecvRate() != cfs[j].getRecvRate() {
|
||||
return cfs[i].getRecvRate() < cfs[j].getRecvRate()
|
||||
}
|
||||
return cfs[i].pingTime < cfs[j].pingTime
|
||||
}
|
||||
|
||||
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