mirror of
https://github.com/XIU2/CloudflareSpeedTest.git
synced 2026-03-07 23:25:53 +08:00
Compare commits
276 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3125b49b63 | ||
|
|
729631deef | ||
|
|
fe0721f077 | ||
|
|
201619ecf7 | ||
|
|
12effc301f | ||
|
|
fede8a04e6 | ||
|
|
b63f368837 | ||
|
|
c9a8c2b8cf | ||
|
|
e80d7fff50 | ||
|
|
f61a94886b | ||
|
|
f3f2bafbe7 | ||
|
|
5cf3b4bb42 | ||
|
|
6667e82c84 | ||
|
|
af4e8cf8b2 | ||
|
|
f2ec0a447c | ||
|
|
45b604103e | ||
|
|
05ba005960 | ||
|
|
a0b194f1d0 | ||
|
|
7e21b2f46b | ||
|
|
efafc38db8 | ||
|
|
b14b3657b7 | ||
|
|
2634fe43ff | ||
|
|
b26fa89447 | ||
|
|
a6b08bb4b8 | ||
|
|
bd6fbb9f1f | ||
|
|
465a83462b | ||
|
|
c045b914a4 | ||
|
|
d8db541c1f | ||
|
|
65018cd415 | ||
|
|
f5ce273688 | ||
|
|
ed1d512d65 | ||
|
|
00d3e21e1b | ||
|
|
2662b1affb | ||
|
|
fa1940fe91 | ||
|
|
0a9fb30671 | ||
|
|
741f080c54 | ||
|
|
2c4d115b83 | ||
|
|
bd16488cfb | ||
|
|
a1ae4f8e45 | ||
|
|
f4f1fdcd80 | ||
|
|
562789aa15 | ||
|
|
c54fb92537 | ||
|
|
07ef3fbb8f | ||
|
|
12066eec6c | ||
|
|
7079a6c7b7 | ||
|
|
a2fa9dadb3 | ||
|
|
f47ba5d06f | ||
|
|
93430a08ef | ||
|
|
295af17fc8 | ||
|
|
25e8ad170b | ||
|
|
b6e5438ec1 | ||
|
|
5b7791b892 | ||
|
|
0beea5df99 | ||
|
|
882678e1eb | ||
|
|
ffb2e54a7b | ||
|
|
154cf5564d | ||
|
|
c6449f6a4a | ||
|
|
021914f975 | ||
|
|
ce22f6b2a4 | ||
|
|
81c6a70a5c | ||
|
|
ce15d4741a | ||
|
|
32b5afaa88 | ||
|
|
73362a1d4c | ||
|
|
82cfd0123f | ||
|
|
a6f627f5fb | ||
|
|
12695563d7 | ||
|
|
d8316525ff | ||
|
|
3c8b51db36 | ||
|
|
1ed7fb7726 | ||
|
|
e8c847d2c9 | ||
|
|
eaaaa53c10 | ||
|
|
270dcab04b | ||
|
|
c1ba72b3a3 | ||
|
|
01b105d42d | ||
|
|
e3c0a3a742 | ||
|
|
7f6e374ae4 | ||
|
|
b22e7fd4af | ||
|
|
2e20a7c279 | ||
|
|
195f95ed80 | ||
|
|
8c720eb137 | ||
|
|
d412f8645d | ||
|
|
04ed493e9d | ||
|
|
794c387b50 | ||
|
|
20708746c3 | ||
|
|
cace4572f1 | ||
|
|
80c3c7bdad | ||
|
|
a6f72ade0a | ||
|
|
0f721fc44b | ||
|
|
8d8a8df123 | ||
|
|
1173bcda06 | ||
|
|
ce6b494996 | ||
|
|
a24d393a11 | ||
|
|
01d796fb90 | ||
|
|
78513f977a | ||
|
|
9878cb56d4 | ||
|
|
aeb1474e5a | ||
|
|
c88577a183 | ||
|
|
60c9902177 | ||
|
|
b2422ca12d | ||
|
|
91e6ae5add | ||
|
|
a08c2c35c4 | ||
|
|
6629fdbdea | ||
|
|
81de6c28ed | ||
|
|
70ce3f8cbb | ||
|
|
f8230942a3 | ||
|
|
638e5ffaba | ||
|
|
533b140c9c | ||
|
|
3895479b35 | ||
|
|
8264138dd8 | ||
|
|
6cb94db59e | ||
|
|
85923cf335 | ||
|
|
60631a958f | ||
|
|
ea46fc5e04 | ||
|
|
9471094d27 | ||
|
|
be4634ee9d | ||
|
|
e5bbd07aea | ||
|
|
445ca36877 | ||
|
|
f6288f4e52 | ||
|
|
cfe30c2f6a | ||
|
|
115bd2af03 | ||
|
|
272eb40d5b | ||
|
|
2c46cfcd0f | ||
|
|
8eea8cdd0e | ||
|
|
f82425bbb6 | ||
|
|
2fa023c7f3 | ||
|
|
ed0a8bbfea | ||
|
|
7301d32cbe | ||
|
|
ab6390a4a4 | ||
|
|
fdbf9ca131 | ||
|
|
49c14f3e0e | ||
|
|
80eadc8df2 | ||
|
|
fbaa312622 | ||
|
|
48012f513a | ||
|
|
28d5d89e85 | ||
|
|
f1a9b5c966 | ||
|
|
4d64abb94d | ||
|
|
71671ebe66 | ||
|
|
83c63e975e | ||
|
|
6f74e60444 | ||
|
|
921bb5ed62 | ||
|
|
337de75d11 | ||
|
|
169af2afe9 | ||
|
|
ccfca867d3 | ||
|
|
897977a11d | ||
|
|
7212110dde | ||
|
|
d651fdeb54 | ||
|
|
d453124943 | ||
|
|
e92badf8be | ||
|
|
01dc5df491 | ||
|
|
b428775cc5 | ||
|
|
5202f73108 | ||
|
|
1bb45af3f1 | ||
|
|
3736d81dda | ||
|
|
a42059737b | ||
|
|
46da45b25f | ||
|
|
acd47ee96d | ||
|
|
698108f453 | ||
|
|
2963b61910 | ||
|
|
9bab2944b1 | ||
|
|
fa5e4f34f0 | ||
|
|
db9d092010 | ||
|
|
180097b044 | ||
|
|
c115249811 | ||
|
|
3b7851f77c | ||
|
|
80c201f160 | ||
|
|
85546abb23 | ||
|
|
1d46334a6b | ||
|
|
afd8736268 | ||
|
|
17ff85954f | ||
|
|
7ece9d6cda | ||
|
|
d67b9bc86d | ||
|
|
73ae874645 | ||
|
|
0441c27cec | ||
|
|
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 |
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@@ -0,0 +1 @@
|
||||
script/*.* linguist-language=None
|
||||
33
.github/ISSUE_TEMPLATE/01-bugReport.yml
vendored
Normal file
33
.github/ISSUE_TEMPLATE/01-bugReport.yml
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
name: 反馈问题 (Bug report)
|
||||
description: 软件报错等异常情况,或遇到预期之外的问题...
|
||||
labels: 反馈问题
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
发之前,请先搜下有没有类似的 [**Issues**](https://github.com/XIU2/CloudflareSpeedTest/issues)、[**Discussions**](https://github.com/XIU2/CloudflareSpeedTest/discussions) 问题(包括[**已关闭**](https://github.com/XIU2/CloudflareSpeedTest/issues?q=is%3Aissue+is%3Aclosed)的),请勿重复发起!
|
||||
|
||||
注意!如果你反馈的问题和 CloudflareST **软件本身功能无关**(如关于 Cloudflare CDN 的网络问题),请**前往讨论区** (💬 [**Discussions**](https://github.com/XIU2/CloudflareSpeedTest/discussions)) 发帖交流!(不合适的 Issues 会被转过去
|
||||
****
|
||||
- type: textarea
|
||||
id: description
|
||||
attributes:
|
||||
label: 问题描述
|
||||
description: 必填,最好写上 复现问题 的步骤,越详细越好,特别是一些复杂的问题
|
||||
placeholder: 请输入...
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
id: version
|
||||
attributes:
|
||||
label: 软件版本
|
||||
description: 必填,可通过运行软件来获取版本信息(例如 v2.2.2)
|
||||
placeholder: 请输入...
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: screenshots
|
||||
attributes:
|
||||
label: 附加截图
|
||||
description: 可选,也可以是一些错误代码
|
||||
placeholder: 可在此粘贴图片,或点击下方 [Attach files by dragging & dropping, selecting or pasting them.] 文字来选择图片...
|
||||
29
.github/ISSUE_TEMPLATE/02-featureRequest.yml
vendored
Normal file
29
.github/ISSUE_TEMPLATE/02-featureRequest.yml
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
name: 功能建议 (Feature request)
|
||||
description: 有什么建议,或希望添加、完善某个功能...
|
||||
labels: 功能建议
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
发之前,请先搜下有没有类似的 [**Issues**](https://github.com/XIU2/CloudflareSpeedTest/issues)、[**Discussions**](https://github.com/XIU2/CloudflareSpeedTest/discussions) 问题(包括[**已关闭**](https://github.com/XIU2/CloudflareSpeedTest/issues?q=is%3Aissue+is%3Aclosed)的),请勿重复发起!
|
||||
|
||||
注意!如果你提的功能建议和 CloudflareST **软件本身功能无关**,请**前往讨论区** (💬 [**Discussions**](https://github.com/XIU2/CloudflareSpeedTest/discussions)) 发帖交流!
|
||||
|
||||
> 另外,不接受**个性化**的功能请求(即 **很少人** 或 **只有你自己** 才会用到的功能)
|
||||
****
|
||||
- type: textarea
|
||||
id: description
|
||||
attributes:
|
||||
label: 功能需求
|
||||
description: 必填,你要什么样的功能?
|
||||
placeholder: 请输入...
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: anticipation
|
||||
attributes:
|
||||
label: 预期目标
|
||||
description: 必填,你希望该功能具体是什么样子的?如果能提供 示例/截图/代码 就更好了
|
||||
placeholder: 请输入...
|
||||
validations:
|
||||
required: true
|
||||
5
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
5
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
blank_issues_enabled: false
|
||||
contact_links:
|
||||
- name: 前往讨论区 (💬 Discussions)
|
||||
url: https://github.com/XIU2/CloudflareSpeedTest/discussions
|
||||
about: Issues 仅用于反馈问题、功能建议,其他话题请到 💬 Discussions 发帖讨论(不合适的 Issues 会被转过去
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
dist
|
||||
Releases
|
||||
*.exe
|
||||
*.csv
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
)
|
||||
|
||||
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)
|
||||
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
|
||||
}
|
||||
740
README.md
740
README.md
@@ -1,86 +1,654 @@
|
||||
# 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)
|
||||
|
||||
国外很多网站都在使用 Cloudflare CDN,但分配给中国访客的 IP 并不友好。
|
||||
虽然 Cloudflare 公开了所有 [IP 段](https://www.cloudflare.com/ips/) ,但想要在这么多 IP 中找到适合自己的,怕是要累死,所以就有了这个软件。
|
||||
|
||||
该软件可以**测试 Cloudflare CDN 所有 IP 的延迟和速度,获得最快 IP**!
|
||||
你可以将 IP 添加到 `Hosts` 文件中,以提高访问使用 Cloudflare CDN 服务的国外网站速度!
|
||||
|
||||
****
|
||||
### 快速使用
|
||||
|
||||
1. 下载编译好的可执行文件 [蓝奏云](https://www.lanzoux.com/b0742hkxe) / [Github](https://github.com/XIU2/CloudflareSpeedTest/releases) 并解压。
|
||||
2. 双击运行 `CloudflareST.exe`文件(Windows系统),等待测速...
|
||||
|
||||
测速完毕后,会把结果保存在当前目录下的 `result.csv` 文件中(只输出丢包率 50% 以下的),用记事本打开,排序为**延迟由低到高**,每一列用逗号分隔,分别是:
|
||||
```
|
||||
IP 地址, 测试次数, 成功次数, 成功比率, 平均延迟, 下载速度 (MB/s)
|
||||
104.27.70.18, 4, 4, 1.00, 150.79, 12.89
|
||||
```
|
||||
选择一个平均延迟与下载速度都不错的 IP 放到 `Hosts` 文件中(指向域名)。
|
||||
|
||||
****
|
||||
### 进阶使用
|
||||
|
||||
直接双击运行使用的是默认参数,如果想要测试速度更快、测试结果更全面,可以自定义参数。
|
||||
``` cmd
|
||||
C:\>CloudflareST.exe -h
|
||||
|
||||
CloudflareSpeedTest
|
||||
测试 Cloudflare CDN 所有 IP 的延迟和速度,获取最快 IP!
|
||||
https://github.com/XIU2/CloudflareSpeedTest
|
||||
|
||||
参数:
|
||||
-n 500
|
||||
测速线程数量;数值越大速度越快,请勿超过1000(结果误差大);(默认 500)
|
||||
-t 4
|
||||
延迟测速次数;单个 IP 测速次数,为 1 时将过滤丢包的IP,TCP协议;(默认 4)
|
||||
-dn 20
|
||||
下载测速数量;延迟测速并排序后,从最低延迟起测试下载速度的数量,请勿太多(速度慢);(默认 20)
|
||||
-dt 10
|
||||
下载测速时间;单个 IP 测速最长时间,单位:秒;(默认 10)
|
||||
-f ip.txt
|
||||
IP 数据文件;支持相对路径和绝对路径,如果包含空格请前后加上引号;(默认 ip.txt)
|
||||
-dd
|
||||
禁用下载测速;如果带上该参数就是禁用下载测速;(默认 启用)
|
||||
-v
|
||||
打印程序版本
|
||||
-h
|
||||
打印帮助说明
|
||||
|
||||
示例:
|
||||
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
|
||||
```
|
||||
|
||||
#### 使用示例
|
||||
|
||||
在 CMD 中运行,或者把启动参数添加到快捷方式中。
|
||||
> **注意:** 不需要通顺加上所有参数,按需选择,参数前后顺序随意。
|
||||
|
||||
``` cmd
|
||||
# CMD 示例
|
||||
CloudflareST.exe -n 500 -t 4 -dn 20 -dt 10
|
||||
```
|
||||
|
||||
``` cmd
|
||||
# 快捷方式示例(右键快捷方式 - 目标)
|
||||
## 如果有引号就放在引号外面,记得引号和 - 之间有空格。
|
||||
"D:\Program Files\CloudflareST\CloudflareST.exe" -n 500 -t 4 -dn 20 -dt 10
|
||||
```
|
||||
|
||||
****
|
||||
### 感谢项目
|
||||
* https://github.com/Spedoske/CloudflareScanner
|
||||
|
||||
意外发现了这个项目,看了之后发现正好解决了我的问题,但是我更喜欢用户命令行方式运行,这样会更方便、有更多使用姿势,于是我临时学了下 Golang 并 Fork 修改了一份命令行方式交互的版本,如果有什么问题可以告诉我,虽然我不一定会~
|
||||
|
||||
****
|
||||
### 许可证
|
||||
The GPL-3.0 License.
|
||||
# XIU2/CloudflareSpeedTest
|
||||
|
||||
[](https://github.com/XIU2/CloudflareSpeedTest/)
|
||||
[](https://github.com/XIU2/CloudflareSpeedTest/releases/latest)
|
||||
[](https://github.com/XIU2/CloudflareSpeedTest/)
|
||||
[](https://github.com/XIU2/CloudflareSpeedTest/)
|
||||
[](https://github.com/XIU2/CloudflareSpeedTest/)
|
||||
|
||||
国外很多网站都在使用 Cloudflare CDN,但分配给中国内地访客的 IP 并不友好(延迟高、丢包多、速度慢)。
|
||||
虽然 Cloudflare 公开了所有 [IP 段](https://www.cloudflare.com/zh-cn/ips/) ,但想要在这么多 IP 中找到适合自己的,怕是要累死,于是就有了这个软件。
|
||||
|
||||
**「自选优选 IP」测试 Cloudflare CDN 延迟和速度,获取最快 IP (IPv4+IPv6)**!好用的话**点个`⭐`鼓励一下叭~**
|
||||
|
||||
> _分享我其他开源项目:[**TrackersList.com** - 全网热门 BT Tracker 列表!有效提高 BT 下载速度~](https://github.com/XIU2/TrackersListCollection) <img src="https://img.shields.io/github/stars/XIU2/TrackersListCollection.svg?style=flat-square&label=Star&color=4285dd&logo=github" height="16px" />_
|
||||
> _[**UserScript** - 🐵 Github 高速下载、知乎增强、自动无缝翻页、护眼模式 等十几个**油猴脚本**!](https://github.com/XIU2/UserScript)<img src="https://img.shields.io/github/stars/XIU2/UserScript.svg?style=flat-square&label=Star&color=4285dd&logo=github" height="16px" />_
|
||||
|
||||
> 本项目也支持对**其他 CDN / 网站 IP** 延迟测速(如:[CloudFront](https://github.com/XIU2/CloudflareSpeedTest/discussions/304)、[Gcore](https://github.com/XIU2/CloudflareSpeedTest/discussions/303) CDN),但下载测速需自行寻找地址
|
||||
|
||||
> 对于**代理套 Cloudflare CDN** 的用户,须知这应为**备用方案**,而不应该是**唯一方案**,请勿过度依赖 [#217](https://github.com/XIU2/CloudflareSpeedTest/issues/217) [#188](https://github.com/XIU2/CloudflareSpeedTest/issues/188)
|
||||
|
||||
****
|
||||
## \# 快速使用
|
||||
|
||||
### 下载运行
|
||||
|
||||
1. 下载编译好的可执行文件 [蓝奏云](https://pan.lanzouf.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) 查看。
|
||||
|
||||
``` yaml
|
||||
# 如果是第一次使用,则建议创建新文件夹(后续更新时,跳过该步骤)
|
||||
mkdir CloudflareST
|
||||
|
||||
# 进入文件夹(后续更新,只需要从这里重复下面的下载、解压命令即可)
|
||||
cd CloudflareST
|
||||
|
||||
# 下载 CloudflareST 压缩包(自行根据需求替换 URL 中 [版本号] 和 [文件名])
|
||||
wget -N https://github.com/XIU2/CloudflareSpeedTest/releases/download/v2.2.2/CloudflareST_linux_amd64.tar.gz
|
||||
# 如果你是在国内服务器上下载,那么请使用下面这几个镜像加速:
|
||||
# wget -N https://download.fastgit.org/XIU2/CloudflareSpeedTest/releases/download/v2.2.2/CloudflareST_linux_amd64.tar.gz
|
||||
# wget -N https://ghproxy.com/https://github.com/XIU2/CloudflareSpeedTest/releases/download/v2.2.2/CloudflareST_linux_amd64.tar.gz
|
||||
# 如果下载失败的话,尝试删除 -N 参数(如果是为了更新,则记得提前删除旧压缩包 rm CloudflareST_linux_amd64.tar.gz )
|
||||
|
||||
# 解压(不需要删除旧文件,会直接覆盖,自行根据需求替换 文件名)
|
||||
tar -zxf CloudflareST_linux_amd64.tar.gz
|
||||
|
||||
# 赋予执行权限
|
||||
chmod +x CloudflareST
|
||||
|
||||
# 运行(不带参数)
|
||||
./CloudflareST
|
||||
|
||||
# 运行(带参数示例)
|
||||
./CloudflareST -dd -tll 90
|
||||
```
|
||||
|
||||
> 如果平**均延迟非常低**(如 0.xx),则说明 CloudflareST **测速时走了代理**,请先关闭代理软件后再测速。
|
||||
> 如果在**路由器**上运行,建议先关闭路由器内的代理(或将其排除),否则测速结果可能会**不准确/无法使用**。
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
> _在**手机**上独立运行 CloudflareST 测速的简单教程:**[Android](https://github.com/XIU2/CloudflareSpeedTest/discussions/61)、[Android APP](https://github.com/xianshenglu/cloudflare-ip-tester-app)、[IOS](https://github.com/XIU2/CloudflareSpeedTest/discussions/321)**_
|
||||
|
||||
### 结果示例
|
||||
|
||||
测速完毕后,默认会显示**最快的 10 个 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 测速时走了代理,请先关闭代理软件后再测速。
|
||||
# 如果在路由器上运行,请先关闭路由器内的代理(或将其排除),否则测速结果可能会不准确/无法使用。
|
||||
|
||||
# 因为每次测速都是在每个 IP 段中随机 IP,所以每次的测速结果都不可能相同,这是正常的!
|
||||
|
||||
# 注意!我发现电脑开机后第一次测速延迟会明显偏高(手动 TCPing 也一样),后续测速都正常
|
||||
# 因此建议大家开机后第一次正式测速前,先随便测几个 IP(无需等待延迟测速完成,只要进度条动了就可以直接关了)
|
||||
|
||||
# 软件在 默认参数 下的整个流程大概步骤:
|
||||
# 1. 延迟测速(默认 TCPing 模式,HTTPing 模式需要手动加上参数)
|
||||
# 2. 延迟排序(延迟从低到高排序,不同丢包率的会分开独立排序,因此可能会有一些延迟低但丢包的 IP 被排到后面)
|
||||
# 3. 下载测速(从延迟最低的 IP 开始依次下载测速,默认测够 10 个就会停止)
|
||||
# 4. 速度排序(速度从高到低排序)
|
||||
# 5. 输出结果(可依靠参数控制是否输出到命令行(-p 0)/文件(-o ""))
|
||||
```
|
||||
|
||||
测速结果第一行就是**既下载速度最快、又平均延迟最低的最快 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 200
|
||||
延迟测速线程;越多延迟测速越快,性能弱的设备 (如路由器) 请勿太高;(默认 200 最多 1000)
|
||||
-t 4
|
||||
延迟测速次数;单个 IP 延迟测速次数,为 1 时将过滤丢包的IP;(默认 4 次)
|
||||
-dn 10
|
||||
下载测速数量;延迟测速并排序后,从最低延迟起下载测速的数量;(默认 10 个)
|
||||
-dt 10
|
||||
下载测速时间;单个 IP 下载测速最长时间,不能太短;(默认 10 秒)
|
||||
-tp 443
|
||||
指定测速端口;延迟测速/下载测速时使用的端口;(默认 443 端口)
|
||||
-url https://cf.xiu2.xyz/url
|
||||
指定测速地址;延迟测速(HTTPing)/下载测速时使用的地址,默认地址不保证可用性,建议自建;
|
||||
|
||||
-httping
|
||||
切换测速模式;延迟测速模式改为 HTTP 协议,所用测试地址为 [-url] 参数;(默认 TCPing)
|
||||
注意:HTTPing 本质上也算一种 网络扫描 行为,因此如果你在服务器上面运行,需要降低并发(-n),否则可能会被一些严格的商家暂停服务。
|
||||
如果你遇到 HTTPing 首次测速可用 IP 数量正常,后续测速越来越少甚至直接为 0,但停一段时间后又恢复了的情况,那么也可能是被 运营商、Cloudflare CDN 认为你在网络扫描而 触发临时限制机制,因此才会过一会儿就恢复了,建议降低并发(-n)减少这种情况的发生。
|
||||
-httping-code 200
|
||||
有效状态代码;HTTPing 延迟测速时网页返回的有效 HTTP 状态码,仅限一个;(默认 200 301 302)
|
||||
-cfcolo HKG,KHH,NRT,LAX,SEA,SJC,FRA,MAD
|
||||
匹配指定地区;地区名为当地机场三字码,英文逗号分隔,支持小写,支持 Cloudflare、AWS CloudFront,仅 HTTPing 模式可用;(默认 所有地区)
|
||||
|
||||
-tl 200
|
||||
平均延迟上限;只输出低于指定平均延迟的 IP,可与其他上限/下限搭配;(默认 9999 ms)
|
||||
-tll 40
|
||||
平均延迟下限;只输出高于指定平均延迟的 IP,可与其他上限/下限搭配;(默认 0 ms)
|
||||
-sl 5
|
||||
下载速度下限;只输出高于指定下载速度的 IP,凑够指定数量 [-dn] 才会停止测速;(默认 0.00 MB/s)
|
||||
|
||||
-p 10
|
||||
显示结果数量;测速后直接显示指定数量的结果,为 0 时不显示结果直接退出;(默认 10 个)
|
||||
-f ip.txt
|
||||
IP段数据文件;如路径含有空格请加上引号;支持其他 CDN IP段;(默认 ip.txt)
|
||||
-ip 1.1.1.1,2.2.2.2/24,2606:4700::/32
|
||||
指定IP段数据;直接通过参数指定要测速的 IP 段数据,英文逗号分隔;(默认 空)
|
||||
-o result.csv
|
||||
写入结果文件;如路径含有空格请加上引号;值为空时不写入文件 [-o ""];(默认 result.csv)
|
||||
|
||||
-dd
|
||||
禁用下载测速;禁用后测速结果会按延迟排序 (默认按下载速度排序);(默认 启用)
|
||||
-allip
|
||||
测速全部的IP;对 IP 段中的每个 IP (仅支持 IPv4) 进行测速;(默认 每个 /24 段随机测速一个 IP)
|
||||
|
||||
-v
|
||||
打印程序版本 + 检查版本更新
|
||||
-h
|
||||
打印帮助说明
|
||||
```
|
||||
|
||||
### 使用示例
|
||||
|
||||
Windows 要指定参数需要在 CMD 中运行,或者把参数添加到快捷方式目标中。
|
||||
|
||||
> **注意**:各参数均有**默认值**,使用默认值的参数是可以省略的(**按需选择**),参数**不分前后顺序**。
|
||||
> **提示**:Windows **PowerShell** 只需把下面命令中的 `CloudflareST.exe` 改为 `.\CloudflareST.exe` 即可。
|
||||
> **提示**:Linux 系统只需要把下面命令中的 `CloudflareST.exe` 改为 `./CloudflareST` 即可。
|
||||
|
||||
****
|
||||
|
||||
#### \# CMD 带参数运行 CloudflareST
|
||||
|
||||
对命令行程序不熟悉的人,可能不知道该如何带参数运行,我就简单说一下。
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 点击展开 查看内容 」</strong></code></summary>
|
||||
|
||||
****
|
||||
|
||||
很多人打开 CMD 以**绝对路径**运行 CloudflareST 会报错,这是因为默认的 `-f ip.txt` 参数是相对路径,需要指定绝对路径的 ip.txt 才行,但这样毕竟太麻烦了,因此还是建议进入 CloudflareST 程序目录下,以**相对路径**方式运行:
|
||||
|
||||
**方式 一**:
|
||||
1. 打开 CloudflareST 程序所在目录
|
||||
2. 空白处按下 <kbd>Shift + 鼠标右键</kbd> 显示右键菜单
|
||||
3. 选择 **\[在此处打开命令窗口\]** 来打开 CMD 窗口,此时默认就位于当前目录下
|
||||
4. 输入带参数的命令,如:`CloudflareST.exe -tll 50 -tl 200`即可运行
|
||||
|
||||
**方式 二**:
|
||||
1. 打开 CloudflareST 程序所在目录
|
||||
2. 直接在文件夹地址栏中全选并输入 `cmd` 回车来打开 CMD 窗口,此时默认就位于当前目录下
|
||||
4. 输入带参数的命令,如:`CloudflareST.exe -tll 50 -tl 200`即可运行
|
||||
|
||||
> 当然你也可以随便打开一个 CMD 窗口,然后输入如 `cd /d "D:\Program Files\CloudflareST"` 来进入程序目录
|
||||
|
||||
> **提示**:如果用的是 **PowerShell** 只需把命令中的 `CloudflareST.exe` 改为 `.\CloudflareST.exe` 即可。
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
#### \# Windows 快捷方式带参数运行 CloudflareST
|
||||
|
||||
如果不经常修改运行参数(比如平时都是直接双击运行)的人,建议使用快捷方式,更方便点。
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 点击展开 查看内容 」</strong></code></summary>
|
||||
|
||||
****
|
||||
|
||||
右键 `CloudflareST.exe` 文件 - **\[创建快捷方式\]**,然后右键该快捷方式 - **\[属性\]**,修改其**目标**:
|
||||
|
||||
``` 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.txt 文件
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
#### \# IPv4/IPv6
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 点击展开 查看内容 」</strong></code></summary>
|
||||
|
||||
****
|
||||
``` bash
|
||||
# 测速 IPv4 时,需要指定 IPv4 数据文件(-f 默认值就是 ip.txt,所以该参数可省略)
|
||||
CloudflareST.exe -f ip.txt
|
||||
|
||||
# 测速 IPv6 时,需要指定 IPv6 数据文件(v2.1.0 版本后支持 IPv4+IPv6 混合测速并移除了 -ipv6 参数)
|
||||
CloudflareST.exe -f ipv6.txt
|
||||
|
||||
# 当然你也可以将 IPv4 IPv6 混合在一起测速,也可以直接通过参数指定要测速的 IP
|
||||
CloudflareST.exe -ip 1.1.1.1,2606:4700::/32
|
||||
```
|
||||
|
||||
> 测速 IPv6 时,可能会注意到每次测速数量都不一样,了解原因: [#120](https://github.com/XIU2/CloudflareSpeedTest/issues/120)
|
||||
> 因为 IPv6 太多(以亿为单位),且绝大部分 IP 段压根未启用,所以我只扫了一部分可用的 IPv6 段写到 `ipv6.txt` 文件中,有兴趣的可以自行扫描增删,ASN 数据源来自:[bgp.he.net](https://bgp.he.net/AS13335#_prefixes6)
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
#### \# HTTPing
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 点击展开 查看内容 」</strong></code></summary>
|
||||
|
||||
****
|
||||
|
||||
目前有两种延迟测速模式,分别为 **TCP 协议、HTTP 协议**。
|
||||
TCP 协议耗时更短、消耗资源更少,超时时间为 1 秒,该协议为默认模式。
|
||||
HTTP 协议适用于快速测试某域名指向某 IP 时是否可以访问,超时时间为 2 秒。
|
||||
同一个 IP,各协议去 Ping 得到的延迟一般为:**ICMP < TCP < HTTP**,越靠右对丢包等网络波动越敏感。
|
||||
|
||||
> 注意:HTTPing 本质上也算一种**网络扫描**行为,因此如果你在服务器上面运行,需要**降低并发**(`-n`),否则可能会被一些严格的商家暂停服务。如果你遇到 HTTPing 首次测速可用 IP 数量正常,后续测速越来越少甚至直接为 0,但停一段时间后又恢复了的情况,那么也可能是被 运营商、Cloudflare CDN 认为你在网络扫描而**触发临时限制机制**,因此才会过一会儿就恢复了,建议**降低并发**(`-n`)减少这种情况的发生。
|
||||
|
||||
``` bash
|
||||
# 只需加上 -httping 参数即可切换到 HTTP 协议延迟测速模式
|
||||
CloudflareST.exe -httping
|
||||
|
||||
# 软件会根据访问时网页返回的有效 HTTP 状态码来判断可用性(当然超时也算),默认对返回 200 301 302 这三个 HTTP 状态码的视为有效,可以手动指定认为有效的 HTTP 状态码,但只能指定一个(你需要提前确定测试地址正常情况下会返回哪个状态码)
|
||||
CloudflareST.exe -httping -httping-code 200
|
||||
|
||||
# 通过 -url 参数来指定 HTTPing 测试地址(可以是任意网页 URL,不局限于具体文件地址)
|
||||
CloudflareST.exe -httping -url https://cf.xiu2.xyz/url
|
||||
|
||||
# 注意:如果测速地址为 HTTP 协议,记得加上 -tp 80(这个参数会影响 延迟测速/下载测速 时使用的端口)
|
||||
# 同理,如果要测速 80 端口,那么也需要加上 -url 参数来指定一个 http:// 协议的地址才行(且该地址不会强制重定向至 HTTPS),如果是非 80 443 端口,那么需要确定该下载测速地址是否支持通过该端口访问。
|
||||
CloudflareST.exe -httping -tp 80 -url http://cdn.cloudflare.steamstatic.com/steam/apps/5952/movie_max.webm
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
#### \# 匹配指定地区(colo 机场三字码)
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 点击展开 查看内容 」</strong></code></summary>
|
||||
|
||||
****
|
||||
|
||||
``` bash
|
||||
# 该功能支持 Cloudflare CDN、AWS CloudFront CDN,且这两个 CDN 的机场三字码是通用的
|
||||
# 指定地区名后,延迟测速后得到的结果就都是指定地区的 IP 了(也可以继续进行下载测速)
|
||||
# 节点地区名为当地 机场三字码,指定多个时用英文逗号分隔,v2.2.3 版本后支持小写
|
||||
|
||||
CloudflareST.exe -cfcolo HKG,KHH,NRT,LAX,SEA,SJC,FRA,MAD
|
||||
|
||||
# 注意,该参数只有在 HTTPing 延迟测速模式下才可用(因为要访问网页来获得)
|
||||
```
|
||||
|
||||
> 两个 CDN 机场三字码通用,各地区见:https://www.cloudflarestatus.com/
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
#### \# 文件相对/绝对路径
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 点击展开 查看内容 」</strong></code></summary>
|
||||
|
||||
****
|
||||
|
||||
``` bash
|
||||
# 指定 IPv4 数据文件,不显示结果直接退出,输出结果到文件(-p 值为 0)
|
||||
CloudflareST.exe -f 1.txt -p 0 -dd
|
||||
|
||||
# 指定 IPv4 数据文件,不输出结果到文件,直接显示结果(-p 值为 10 条,-o 值为空但引号不能少)
|
||||
CloudflareST.exe -f 2.txt -o "" -p 10 -dd
|
||||
|
||||
# 指定 IPv4 数据文件 及 输出结果到文件(相对路径,即当前目录下,如含空格请加上引号)
|
||||
CloudflareST.exe -f 3.txt -o result.txt -dd
|
||||
|
||||
|
||||
# 指定 IPv4 数据文件 及 输出结果到文件(相对路径,即当前目录内的 abc 文件夹下,如含空格请加上引号)
|
||||
# Linux(CloudflareST 程序所在目录内的 abc 文件夹下)
|
||||
./CloudflareST -f abc/3.txt -o abc/result.txt -dd
|
||||
|
||||
# Windows(注意是反斜杠)
|
||||
CloudflareST.exe -f abc\3.txt -o abc\result.txt -dd
|
||||
|
||||
|
||||
# 指定 IPv4 数据文件 及 输出结果到文件(绝对路径,即 C:\abc\ 目录下,如含空格请加上引号)
|
||||
# Linux(/abc/ 目录下)
|
||||
./CloudflareST -f /abc/4.txt -o /abc/result.csv -dd
|
||||
|
||||
# Windows(注意是反斜杠)
|
||||
CloudflareST.exe -f C:\abc\4.txt -o C:\abc\result.csv -dd
|
||||
|
||||
|
||||
# 如果要以【绝对路径】运行 CloudflareST,那么 -f / -o 参数中的文件名也必须是【绝对路径】,否则会报错找不到文件!
|
||||
# Linux(/abc/ 目录下)
|
||||
/abc/CloudflareST -f /abc/4.txt -o /abc/result.csv -dd
|
||||
|
||||
# Windows(注意是反斜杠)
|
||||
C:\abc\CloudflareST.exe -f C:\abc\4.txt -o C:\abc\result.csv -dd
|
||||
```
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
#### \# 测速其他端口
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 点击展开 查看内容 」</strong></code></summary>
|
||||
|
||||
****
|
||||
|
||||
``` bash
|
||||
# 如果你想要测速非默认 443 的其他端口,则需要通过 -tp 参数指定(该参数会影响 延迟测速/下载测速 时使用的端口)
|
||||
|
||||
# 如果要延迟测速 80 端口+下载测速(如果 -dd 禁用了下载测速则不需要),那么还需要指定 http:// 协议的下载测速地址才行(且该地址不会强制重定向至 HTTPS,因为那样就变成 443 端口了)
|
||||
CloudflareST.exe -tp 80 -url http://cdn.cloudflare.steamstatic.com/steam/apps/5952/movie_max.webm
|
||||
|
||||
# 如果是非 80 443 的其他端口,那么需要确定你使用的下载测速地址是否支持通过该非标端口访问。
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
#### \# 自定义测速地址
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 点击展开 查看内容 」</strong></code></summary>
|
||||
|
||||
****
|
||||
|
||||
``` bash
|
||||
# 该参数适用于下载测速 及 HTTP 协议的延迟测速,对于后者该地址可以是任意网页 URL(不局限于具体文件地址)
|
||||
|
||||
# 地址要求:可以直接下载、文件大小超过 200MB、用的是 Cloudflare CDN
|
||||
CloudflareST.exe -url https://cf.xiu2.xyz/url
|
||||
|
||||
# 注意:如果测速地址为 HTTP 协议(该地址不能强制重定向至 HTTPS),记得加上 -tp 80(这个参数会影响 延迟测速/下载测速 时使用的端口),如果是非 80 443 端口,那么需要确定下载测速地址是否支持通过该端口访问。
|
||||
CloudflareST.exe -tp 80 -url http://cdn.cloudflare.steamstatic.com/steam/apps/5952/movie_max.webm
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
#### \# 自定义测速条件(指定 延迟/下载速度 的目标范围)
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 点击展开 查看内容 」</strong></code></summary>
|
||||
|
||||
****
|
||||
|
||||
> 注意:延迟测速进度条右边的**可用数量**,仅指延迟测速过程中**未超时的 IP 数量**,和延迟上下限条件无关。
|
||||
|
||||
- 指定 **[平均延迟下限]** 条件
|
||||
|
||||
``` bash
|
||||
# 平均延迟下限:40 ms (一般除了移动直连香港外,几乎不存在低于 100ms 的,自行测试适合的下限延迟)
|
||||
# 平均延迟下限和其他的上下限参数一样,都可以单独使用、互相搭配使用!
|
||||
CloudflareST.exe -tll 40
|
||||
```
|
||||
|
||||
- 仅指定 **[平均延迟上限]** 条件
|
||||
|
||||
``` bash
|
||||
# 平均延迟上限:200 ms,下载速度下限:0 MB/s,数量:10 个(可选)
|
||||
# 即找到平均延迟低于 200 ms 的 IP,然后再按延迟从低到高进行 10 次下载测速
|
||||
CloudflareST.exe -tl 200 -dn 10
|
||||
```
|
||||
|
||||
> 如果**没有找到一个满足延迟**条件的 IP,那么不会输出任何内容。
|
||||
|
||||
****
|
||||
|
||||
- 仅指定 **[平均延迟上限]** 条件,且**只延迟测速,不下载测速**
|
||||
|
||||
``` 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,那么会**忽略条件输出所有 IP 测速结果**(方便你下次测速时调整条件)。
|
||||
|
||||
> 没有指定平均延迟上限时,如果一直**凑不够**满足条件的 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,那么不会输出任何内容。
|
||||
> 如果**没有找到一个满足速度**条件的 IP,那么会忽略条件输出所有 IP 测速结果(方便你下次测速时调整条件)。
|
||||
> 所以建议先不指定条件测速一遍,看看平均延迟和下载速度大概在什么范围,避免指定条件**过低/过高**!
|
||||
|
||||
> 因为Cloudflare 公开的 IP 段是**回源 IP+任播 IP**,而**回源 IP**是无法使用的,所以下载测速是 0.00。
|
||||
> 运行时可以加上 `-sl 0.01`(下载速度下限),过滤掉**回源 IP**(下载测速低于 0.01MB/s 的结果)。
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
#### \# 单独对一个或多个 IP 测速
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 点击展开 查看内容 」</strong></code></summary>
|
||||
|
||||
****
|
||||
|
||||
**方式 一**:
|
||||
直接通过参数指定要测速的 IP 段数据。
|
||||
``` bash
|
||||
# 先进入 CloudflareST 所在目录,然后运行:
|
||||
# Windows 系统(在 CMD 中运行)
|
||||
CloudflareST.exe -ip 1.1.1.1,2.2.2.2/24,2606:4700::/32
|
||||
|
||||
# Linux 系统
|
||||
./CloudflareST -ip 1.1.1.1,2.2.2.2/24,2606:4700::/32
|
||||
```
|
||||
|
||||
****
|
||||
|
||||
**方式 二**:
|
||||
或者把这些 IP 按如下格式写入到任意文本文件中,例如:`1.txt`
|
||||
|
||||
```
|
||||
1.1.1.1
|
||||
1.1.1.200
|
||||
1.0.0.1/24
|
||||
2606:4700::/32
|
||||
```
|
||||
|
||||
> 单个 IP 的话可以省略 `/32` 子网掩码了(即 `1.1.1.1`等同于 `1.1.1.1/32`)。
|
||||
> 子网掩码 `/24` 指的是这个 IP 最后一段,即 `1.0.0.1~1.0.0.255`。
|
||||
|
||||
|
||||
然后运行 CloudflareST 时加上启动参数 `-f 1.txt` 来指定 IP 段数据文件。
|
||||
|
||||
``` bash
|
||||
# 先进入 CloudflareST 所在目录,然后运行:
|
||||
# Windows 系统(在 CMD 中运行)
|
||||
CloudflareST.exe -f 1.txt
|
||||
|
||||
# Linux 系统
|
||||
./CloudflareST -f 1.txt
|
||||
|
||||
# 对于 1.0.0.1/24 这样的 IP 段只会随机最后一段(1.0.0.1~255),如果要测速该 IP 段中的所有 IP,请加上 -allip 参数。
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
#### \# 一劳永逸加速所有使用 Cloudflare CDN 的网站(不需要再一个个添加域名到 Hosts 了)
|
||||
|
||||
我以前说过,开发该软件项目的目的就是为了通过**改 Hosts 的方式来加速访问使用 Cloudflare CDN 的网站**。
|
||||
|
||||
但就如 [**#8**](https://github.com/XIU2/CloudflareSpeedTest/issues/8) 所说,一个个添加域名到 Hosts 实在**太麻烦**了,于是我就找到了个**一劳永逸**的办法!可以看这个 [**还在一个个添加 Hosts?完美本地加速所有使用 Cloudflare CDN 的网站方法来了!**](https://github.com/XIU2/CloudflareSpeedTest/discussions/71) 和另一个[依靠本地 DNS 服务来修改域名解析 IP 为自选 IP](https://github.com/XIU2/CloudflareSpeedTest/discussions/317) 的教程。
|
||||
|
||||
****
|
||||
|
||||
#### \# 自动更新 Hosts
|
||||
|
||||
考虑到很多人获得最快 Cloudflare CDN IP 后,需要替换 Hosts 文件中的 IP。
|
||||
|
||||
可以看这个 [**Issues**](https://github.com/XIU2/CloudflareSpeedTest/discussions/312) 获取 **Windows/Linux 自动更新 Hosts 脚本**!
|
||||
|
||||
****
|
||||
|
||||
## 问题反馈
|
||||
|
||||
如果你遇到什么问题,可以先去 [**Issues**](https://github.com/XIU2/CloudflareSpeedTest/issues)、[Discussions](https://github.com/XIU2/CloudflareSpeedTest/discussions) 里看看是否有别人问过了(记得去看下 [**Closed**](https://github.com/XIU2/CloudflareSpeedTest/issues?q=is%3Aissue+is%3Aclosed) 的)。
|
||||
如果没找到类似问题,请新开个 [**Issues**](https://github.com/XIU2/CloudflareSpeedTest/issues/new) 来告诉我!
|
||||
|
||||
> **注意**!_与 `反馈问题、功能建议` 无关的,请前往项目内部 论坛 讨论(上面的 `💬 Discussions`_
|
||||
|
||||
****
|
||||
|
||||
## 赞赏支持
|
||||
|
||||

|
||||
|
||||
****
|
||||
|
||||
## 衍生项目
|
||||
|
||||
- _https://github.com/xianshenglu/cloudflare-ip-tester-app_
|
||||
_**CloudflareST 安卓版 APP [#202](https://github.com/XIU2/CloudflareSpeedTest/discussions/320)**_
|
||||
|
||||
- _https://github.com/mingxiaoyu/luci-app-cloudflarespeedtest_
|
||||
_**CloudflareST OpenWrt 路由器插件版 [#174](https://github.com/XIU2/CloudflareSpeedTest/discussions/319)**_
|
||||
|
||||
- _https://github.com/immortalwrt-collections/openwrt-cdnspeedtest_
|
||||
_**CloudflareST OpenWrt 原生编译版本 [#64](https://github.com/XIU2/CloudflareSpeedTest/discussions/64)**_
|
||||
|
||||
- _https://github.com/hoseinnikkhah/CloudflareSpeedTest-English_
|
||||
_**English language version of CloudflareST (Text language differences only) [#64](https://github.com/XIU2/CloudflareSpeedTest/issues/68)**_
|
||||
|
||||
> _此处仅收集了在本项目中宣传过的部分 CloudflareST 相关衍生项目,如果有遗漏可以告诉我~_
|
||||
|
||||
****
|
||||
|
||||
## 感谢项目
|
||||
|
||||
- _https://github.com/Spedoske/CloudflareScanner_
|
||||
|
||||
> _因为该项目已经很长时间没更新了,而我又产生了很多功能需求,所以我临时学了下 Go 语言就上手了(菜)..._
|
||||
> _本软件基于该项目制作,但**已添加大量功能及修复 BUG**,并根据大家的使用反馈积极添加、优化功能(闲)..._
|
||||
|
||||
****
|
||||
|
||||
## 手动编译
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 点击展开 查看内容 」</strong></code></summary>
|
||||
|
||||
****
|
||||
|
||||
为了方便,我是在编译的时候将版本号写入代码中的 version 变量,因此你手动编译时,需要像下面这样在 `go build` 命令后面加上 `-ldflags` 参数来指定版本号:
|
||||
|
||||
```bash
|
||||
go build -ldflags "-s -w -X main.version=v2.3.3"
|
||||
# 在 CloudflareSpeedTest 目录中通过命令行(例如 CMD、Bat 脚本)运行该命令,即可编译一个可在和当前设备同样系统、位数、架构的环境下运行的二进制程序(Go 会自动检测你的系统位数、架构)且版本号为 v2.3.3
|
||||
```
|
||||
|
||||
如果想要在 Windows 64位系统下编译**其他系统、架构、位数**,那么需要指定 **GOOS** 和 **GOARCH** 变量。
|
||||
|
||||
例如在 Windows 系统下编译一个适用于 **Linux 系统 amd 架构 64 位**的二进制程序:
|
||||
|
||||
```bat
|
||||
SET GOOS=linux
|
||||
SET GOARCH=amd64
|
||||
go build -ldflags "-s -w -X main.version=v2.3.3"
|
||||
```
|
||||
|
||||
例如在 Linux 系统下编译一个适用于 **Windows 系统 amd 架构 32 位**的二进制程序:
|
||||
|
||||
```bash
|
||||
GOOS=windows
|
||||
GOARCH=386
|
||||
go build -ldflags "-s -w -X main.version=v2.3.3"
|
||||
```
|
||||
|
||||
> 可以运行 `go tool dist list` 来查看当前 Go 版本支持编译哪些组合。
|
||||
|
||||
****
|
||||
|
||||
当然,为了方便批量编译,我会专门指定一个变量为版本号,后续编译直接调用该版本号变量即可。
|
||||
同时,批量编译的话,还需要分开放到不同文件夹才行(或者文件名不同),需要加上 `-o` 参数指定。
|
||||
|
||||
```bat
|
||||
:: Windows 系统下是这样:
|
||||
SET version=v2.3.3
|
||||
SET GOOS=linux
|
||||
SET GOARCH=amd64
|
||||
go build -o Releases\CloudflareST_linux_amd64\CloudflareST -ldflags "-s -w -X main.version=%version%"
|
||||
```
|
||||
|
||||
```bash
|
||||
# Linux 系统下是这样:
|
||||
version=v2.3.3
|
||||
GOOS=windows
|
||||
GOARCH=386
|
||||
go build -o Releases/CloudflareST_windows_386/CloudflareST.exe -ldflags "-s -w -X main.version=${version}"
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
## License
|
||||
|
||||
The GPL-3.0 License.
|
||||
2
go.mod
2
go.mod
@@ -1,4 +1,4 @@
|
||||
module CloudflareIPScanner
|
||||
module github.com/XIU2/CloudflareSpeedTest
|
||||
|
||||
go 1.14
|
||||
|
||||
|
||||
13
ip.txt
13
ip.txt
@@ -10,5 +10,16 @@
|
||||
198.41.128.0/17
|
||||
162.158.0.0/15
|
||||
104.16.0.0/12
|
||||
172.64.0.0/13
|
||||
172.64.0.0/17
|
||||
172.64.128.0/18
|
||||
172.64.192.0/19
|
||||
172.64.224.0/22
|
||||
172.64.229.0/24
|
||||
172.64.230.0/23
|
||||
172.64.232.0/21
|
||||
172.64.240.0/21
|
||||
172.64.248.0/21
|
||||
172.65.0.0/16
|
||||
172.66.0.0/16
|
||||
172.67.0.0/16
|
||||
131.0.72.0/22
|
||||
97
ipv6.txt
Normal file
97
ipv6.txt
Normal file
@@ -0,0 +1,97 @@
|
||||
2400:cb00:2049::/48
|
||||
2400:cb00:f00e::/48
|
||||
2606:4700::/32
|
||||
2606:4700:10::/48
|
||||
2606:4700:130::/48
|
||||
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
|
||||
2606:4700:a0::/48
|
||||
2606:4700:a1::/48
|
||||
2606:4700:a8::/48
|
||||
2606:4700:a9::/48
|
||||
2606:4700:a::/48
|
||||
2606:4700:b::/48
|
||||
2606:4700:c::/48
|
||||
2606:4700:d0::/48
|
||||
2606:4700:d1::/48
|
||||
2606:4700:d::/48
|
||||
2606:4700:e0::/48
|
||||
2606:4700:e1::/48
|
||||
2606:4700:e2::/48
|
||||
2606:4700:e3::/48
|
||||
2606:4700:e4::/48
|
||||
2606:4700:e5::/48
|
||||
2606:4700:e6::/48
|
||||
2606:4700:e7::/48
|
||||
2606:4700:e::/48
|
||||
2606:4700:f1::/48
|
||||
2606:4700:f2::/48
|
||||
2606:4700:f3::/48
|
||||
2606:4700:f4::/48
|
||||
2606:4700:f5::/48
|
||||
2606:4700:f::/48
|
||||
2803:f800:50::/48
|
||||
2803:f800:51::/48
|
||||
2a06:98c1:3100::/48
|
||||
2a06:98c1:3101::/48
|
||||
2a06:98c1:3102::/48
|
||||
2a06:98c1:3103::/48
|
||||
2a06:98c1:3104::/48
|
||||
2a06:98c1:3105::/48
|
||||
2a06:98c1:3106::/48
|
||||
2a06:98c1:3107::/48
|
||||
2a06:98c1:3108::/48
|
||||
2a06:98c1:3109::/48
|
||||
2a06:98c1:310a::/48
|
||||
2a06:98c1:310b::/48
|
||||
2a06:98c1:310c::/48
|
||||
2a06:98c1:310d::/48
|
||||
2a06:98c1:310e::/48
|
||||
2a06:98c1:310f::/48
|
||||
2a06:98c1:3120::/48
|
||||
2a06:98c1:3121::/48
|
||||
2a06:98c1:3122::/48
|
||||
2a06:98c1:3123::/48
|
||||
2a06:98c1:3200::/48
|
||||
2a06:98c1:50::/48
|
||||
2a06:98c1:51::/48
|
||||
2a06:98c1:54::/48
|
||||
2a06:98c1:58::/48
|
||||
191
main.go
191
main.go
@@ -3,98 +3,167 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"sync"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/cheggaaa/pb/v3"
|
||||
"github.com/XIU2/CloudflareSpeedTest/task"
|
||||
"github.com/XIU2/CloudflareSpeedTest/utils"
|
||||
)
|
||||
|
||||
var version string
|
||||
var disableDownload bool
|
||||
var ipFile string
|
||||
var (
|
||||
version, versionNew string
|
||||
)
|
||||
|
||||
func init() {
|
||||
var downloadSecond int64
|
||||
var printVersion bool
|
||||
const help = `
|
||||
CloudflareSpeedTest
|
||||
测试 Cloudflare CDN 所有 IP 的延迟和速度,获取最快 IP!
|
||||
var help = `
|
||||
CloudflareSpeedTest ` + version + `
|
||||
测试 Cloudflare CDN 所有 IP 的延迟和速度,获取最快 IP (IPv4+IPv6)!
|
||||
https://github.com/XIU2/CloudflareSpeedTest
|
||||
|
||||
参数:
|
||||
-n 500
|
||||
测速线程数量;数值越大速度越快,请勿超过1000(结果误差大);(默认 500)
|
||||
-n 200
|
||||
延迟测速线程;越多延迟测速越快,性能弱的设备 (如路由器) 请勿太高;(默认 200 最多 1000)
|
||||
-t 4
|
||||
延迟测速次数;单个 IP 测速次数,为 1 时将过滤丢包的IP,TCP协议;(默认 4)
|
||||
-dn 20
|
||||
下载测速数量;延迟测速并排序后,从最低延迟起测试下载速度的数量,请勿太多(速度慢);(默认 20)
|
||||
延迟测速次数;单个 IP 延迟测速次数,为 1 时将过滤丢包的IP;(默认 4 次)
|
||||
-dn 10
|
||||
下载测速数量;延迟测速并排序后,从最低延迟起下载测速的数量;(默认 10 个)
|
||||
-dt 10
|
||||
下载测速时间;单个 IP 测速最长时间,单位:秒;(默认 10)
|
||||
下载测速时间;单个 IP 下载测速最长时间,不能太短;(默认 10 秒)
|
||||
-tp 443
|
||||
指定测速端口;延迟测速/下载测速时使用的端口;(默认 443 端口)
|
||||
-url https://cf.xiu2.xyz/url
|
||||
指定测速地址;延迟测速(HTTPing)/下载测速时使用的地址,默认地址不保证可用性,建议自建;
|
||||
|
||||
-httping
|
||||
切换测速模式;延迟测速模式改为 HTTP 协议,所用测试地址为 [-url] 参数;(默认 TCPing)
|
||||
-httping-code 200
|
||||
有效状态代码;HTTPing 延迟测速时网页返回的有效 HTTP 状态码,仅限一个;(默认 200 301 302)
|
||||
-cfcolo HKG,KHH,NRT,LAX,SEA,SJC,FRA,MAD
|
||||
匹配指定地区;地区名为当地机场三字码,英文逗号分隔,仅 HTTPing 模式可用;(默认 所有地区)
|
||||
|
||||
-tl 200
|
||||
平均延迟上限;只输出低于指定平均延迟的 IP,可与其他上限/下限搭配;(默认 9999 ms)
|
||||
-tll 40
|
||||
平均延迟下限;只输出高于指定平均延迟的 IP,可与其他上限/下限搭配;(默认 0 ms)
|
||||
-sl 5
|
||||
下载速度下限;只输出高于指定下载速度的 IP,凑够指定数量 [-dn] 才会停止测速;(默认 0.00 MB/s)
|
||||
|
||||
-p 10
|
||||
显示结果数量;测速后直接显示指定数量的结果,为 0 时不显示结果直接退出;(默认 10 个)
|
||||
-f ip.txt
|
||||
IP 数据文件;支持相对路径和绝对路径,如果包含空格请前后加上引号;(默认 ip.txt)
|
||||
IP段数据文件;如路径含有空格请加上引号;支持其他 CDN IP段;(默认 ip.txt)
|
||||
-ip 1.1.1.1,2.2.2.2/24,2606:4700::/32
|
||||
指定IP段数据;直接通过参数指定要测速的 IP 段数据,英文逗号分隔;(默认 空)
|
||||
-o result.csv
|
||||
写入结果文件;如路径含有空格请加上引号;值为空时不写入文件 [-o ""];(默认 result.csv)
|
||||
|
||||
-dd
|
||||
禁用下载测速;如果带上该参数就是禁用下载测速;(默认 启用)
|
||||
禁用下载测速;禁用后测速结果会按延迟排序 (默认按下载速度排序);(默认 启用)
|
||||
-allip
|
||||
测速全部的IP;对 IP 段中的每个 IP (仅支持 IPv4) 进行测速;(默认 每个 /24 段随机测速一个 IP)
|
||||
|
||||
-v
|
||||
打印程序版本
|
||||
打印程序版本 + 检查版本更新
|
||||
-h
|
||||
打印帮助说明
|
||||
`
|
||||
var minDelay, maxDelay, downloadTime int
|
||||
flag.IntVar(&task.Routines, "n", 200, "延迟测速线程")
|
||||
flag.IntVar(&task.PingTimes, "t", 4, "延迟测速次数")
|
||||
flag.IntVar(&task.TestCount, "dn", 10, "下载测速数量")
|
||||
flag.IntVar(&downloadTime, "dt", 10, "下载测速时间")
|
||||
flag.IntVar(&task.TCPPort, "tp", 443, "指定测速端口")
|
||||
flag.StringVar(&task.URL, "url", "https://cf.xiu2.xyz/url", "指定测速地址")
|
||||
|
||||
示例:
|
||||
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`
|
||||
flag.BoolVar(&task.Httping, "httping", false, "切换测速模式")
|
||||
flag.IntVar(&task.HttpingStatusCode, "httping-code", 0, "有效状态代码")
|
||||
flag.StringVar(&task.HttpingCFColo, "cfcolo", "", "匹配指定地区")
|
||||
|
||||
flag.IntVar(&maxDelay, "tl", 9999, "平均延迟上限")
|
||||
flag.IntVar(&minDelay, "tll", 0, "平均延迟下限")
|
||||
flag.Float64Var(&task.MinSpeed, "sl", 0, "下载速度下限")
|
||||
|
||||
flag.IntVar(&utils.PrintNum, "p", 10, "显示结果数量")
|
||||
flag.StringVar(&task.IPFile, "f", "ip.txt", "IP段数据文件")
|
||||
flag.StringVar(&task.IPText, "ip", "", "指定IP段数据")
|
||||
flag.StringVar(&utils.Output, "o", "result.csv", "输出结果文件")
|
||||
|
||||
flag.BoolVar(&task.Disable, "dd", false, "禁用下载测速")
|
||||
flag.BoolVar(&task.TestAll, "allip", false, "测速全部 IP")
|
||||
|
||||
flag.IntVar(&pingRoutine, "n", 500, "测速线程数量")
|
||||
flag.IntVar(&pingTime, "t", 4, "延迟测速次数")
|
||||
flag.IntVar(&downloadTestCount, "dn", 20, "下载测速数量")
|
||||
flag.Int64Var(&downloadSecond, "dt", 10, "下载测速时间")
|
||||
flag.BoolVar(&disableDownload, "dd", false, "禁用下载测速")
|
||||
flag.StringVar(&ipFile, "f", "ip.txt", "IP 数据文件")
|
||||
flag.BoolVar(&printVersion, "v", false, "打印程序版本")
|
||||
|
||||
downloadTestTime = time.Duration(downloadSecond) * time.Second
|
||||
|
||||
flag.Usage = func() { fmt.Print(help) }
|
||||
flag.Parse()
|
||||
|
||||
if task.MinSpeed > 0 && time.Duration(maxDelay)*time.Millisecond == utils.InputMaxDelay {
|
||||
fmt.Println("[小提示] 在使用 [-sl] 参数时,建议搭配 [-tl] 参数,以避免因凑不够 [-dn] 数量而一直测速...")
|
||||
}
|
||||
utils.InputMaxDelay = time.Duration(maxDelay) * time.Millisecond
|
||||
utils.InputMinDelay = time.Duration(minDelay) * time.Millisecond
|
||||
task.Timeout = time.Duration(downloadTime) * time.Second
|
||||
task.HttpingCFColomap = task.MapColoMap()
|
||||
|
||||
if printVersion {
|
||||
println(version)
|
||||
fmt.Println("检查版本更新中...")
|
||||
checkUpdate()
|
||||
if versionNew != "" {
|
||||
fmt.Printf("*** 发现新版本 [%s]!请前往 [https://github.com/XIU2/CloudflareSpeedTest] 更新! ***", versionNew)
|
||||
} else {
|
||||
fmt.Println("当前为最新版本 [" + version + "]!")
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
initipEndWith() // 随机数
|
||||
failTime = pingTime // 设置接收次数
|
||||
ips := loadFirstIPOfRangeFromFile(ipFile) // 读入IP
|
||||
pingCount := len(ips) * pingTime // 计算进度条总数(IP*测试次数)
|
||||
bar := pb.Full.Start(pingCount) // 进度条总数
|
||||
var wg sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
var data = make([]CloudflareIPData, 0)
|
||||
task.InitRandSeed() // 置随机数种子
|
||||
|
||||
fmt.Println("开始延迟测速(TCP):")
|
||||
fmt.Printf("# XIU2/CloudflareSpeedTest %s \n\n", version)
|
||||
|
||||
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)
|
||||
// 开始延迟测速
|
||||
pingData := task.NewPing().Run().FilterDelay()
|
||||
// 开始下载测速
|
||||
speedData := task.TestDownloadSpeed(pingData)
|
||||
utils.ExportCsv(speedData) // 输出文件
|
||||
speedData.Print() // 打印结果
|
||||
|
||||
if versionNew != "" {
|
||||
fmt.Printf("\n*** 发现新版本 [%s]!请前往 [https://github.com/XIU2/CloudflareSpeedTest] 更新! ***\n", versionNew)
|
||||
}
|
||||
endPrint()
|
||||
}
|
||||
|
||||
func endPrint() {
|
||||
if utils.NoPrintResult() {
|
||||
return
|
||||
}
|
||||
if runtime.GOOS == "windows" { // 如果是 Windows 系统,则需要按下 回车键 或 Ctrl+C 退出(避免通过双击运行时,测速完毕后直接关闭)
|
||||
fmt.Printf("按下 回车键 或 Ctrl+C 退出。")
|
||||
fmt.Scanln()
|
||||
}
|
||||
}
|
||||
|
||||
// 检查更新
|
||||
func checkUpdate() {
|
||||
timeout := 10 * time.Second
|
||||
client := http.Client{Timeout: timeout}
|
||||
res, err := client.Get("https://api.xiu2.xyz/ver/cloudflarespeedtest.txt")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 读取资源数据 body: []byte
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 关闭资源流
|
||||
defer res.Body.Close()
|
||||
if string(body) != version {
|
||||
versionNew = string(body)
|
||||
}
|
||||
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)
|
||||
}
|
||||
bar.Finish()
|
||||
}
|
||||
ExportCsv("./result.csv", data) // 输出结果
|
||||
}
|
||||
|
||||
109
script/README.md
Normal file
109
script/README.md
Normal file
@@ -0,0 +1,109 @@
|
||||
# XIU2/CloudflareSpeedTest - Script
|
||||
|
||||
这里都是一些基于 **XIU2/CloudflareSpeedTest** 并**扩展更多功能**的脚本。
|
||||
有什么现有脚本功能上的建议可以告诉我,如果你有一些自用好用的脚本也可以通过 [**Issues**](https://github.com/XIU2/CloudflareSpeedTest/issues) 或 Pull requests 发给我添加到这里让更多人用到(会标注作者的~
|
||||
|
||||
> 小提示:点击↖左上角的三横杠图标按钮即可查看目录~
|
||||
|
||||
****
|
||||
## 📑 cfst_hosts.sh / cfst_hosts.bat (已内置)
|
||||
|
||||
运行 CloudflareST 获得最快 IP 后,脚本会替换 Hosts 文件中的旧 CDN IP。
|
||||
|
||||
> **使用说明:https://github.com/XIU2/CloudflareSpeedTest/issues/42**
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 更新日志」</strong></code></summary>
|
||||
|
||||
****
|
||||
|
||||
#### 2021年12月17日,版本 v1.0.6
|
||||
- **1. 优化** [找不到满足条件的 IP 就一直循环测速] 功能,在指定下载测速下限时没有重新测速的问题(默认注释)
|
||||
|
||||
#### 2021年12月17日,版本 v1.0.3
|
||||
- **1. 新增** 找不到满足条件的 IP 就一直循环测速功能(默认注释)
|
||||
- **2. 优化** 代码
|
||||
|
||||
#### 2021年09月29日,版本 v1.0.2
|
||||
- **1. 修复** 当测速结果 IP 数量为 0 时,脚本没有退出的问题
|
||||
|
||||
#### 2021年04月29日,版本 v1.0.1
|
||||
- **1. 优化** 不再需要加上 -p 0 参数来避免回车键退出了(现在可以即显示结果,又不用担心回车键退出程序)
|
||||
|
||||
#### 2021年01月28日,版本 v1.0.0
|
||||
- **1. 发布** 第一个版本
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
## 📑 cfst_3proxy.bat (已内置)
|
||||
|
||||
该脚本的作用为 CloudflareST 测速后获取最快 IP 并替换 3Proxy 配置文件中的 Cloudflare CDN IP。
|
||||
可以把所有 Cloudflare CDN IP 都重定向至最快 IP,实现一劳永逸的加速所有使用 Cloudflare CDN 的网站(不需要一个个添加域名到 Hosts 了)。
|
||||
|
||||
> **使用说明:https://github.com/XIU2/CloudflareSpeedTest/discussions/71**
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 更新日志」</strong></code></summary>
|
||||
|
||||
****
|
||||
|
||||
#### 2021年12月17日,版本 v1.0.5
|
||||
- **1. 优化** [找不到满足条件的 IP 就一直循环测速] 功能,在指定下载测速下限时没有重新测速的问题(默认注释)
|
||||
|
||||
#### 2021年12月17日,版本 v1.0.4
|
||||
- **1. 新增** 找不到满足条件的 IP 就一直循环测速功能(默认注释)
|
||||
- **2. 优化** 代码
|
||||
|
||||
#### 2021年09月29日,版本 v1.0.3
|
||||
- **1. 修复** 当测速结果 IP 数量为 0 时,脚本没有退出的问题
|
||||
|
||||
#### 2021年04月29日,版本 v1.0.2
|
||||
- **1. 优化** 不再需要加上 -p 0 参数来避免回车键退出了(现在可以即显示结果,又不用担心回车键退出程序)
|
||||
|
||||
#### 2021年03月16日,版本 v1.0.1
|
||||
- **1. 优化** 代码及注释内容
|
||||
|
||||
#### 2021年03月13日,版本 v1.0.0
|
||||
- **1. 发布** 第一个版本
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
## 📑 cfst_ddns.sh / cfst_ddns.bat
|
||||
|
||||
如果你的域名托管在 Cloudflare,则可以通过 Cloudflare 官方提供的 API 来自动更新域名解析记录!
|
||||
|
||||
> **使用说明:https://github.com/XIU2/CloudflareSpeedTest/issues/40**
|
||||
|
||||
<details>
|
||||
<summary><code><strong>「 更新日志」</strong></code></summary>
|
||||
|
||||
****
|
||||
|
||||
#### 2021年12月17日,版本 v1.0.4
|
||||
- **1. 新增** 找不到满足条件的 IP 就一直循环测速功能(默认注释)
|
||||
- **2. 优化** 代码
|
||||
|
||||
#### 2021年09月29日,版本 v1.0.3
|
||||
- **1. 修复** 当测速结果 IP 数量为 0 时,脚本没有退出的问题
|
||||
|
||||
#### 2021年04月29日,版本 v1.0.2
|
||||
- **1. 优化** 不再需要加上 -p 0 参数来避免回车键退出了(现在可以即显示结果,又不用担心回车键退出程序)
|
||||
|
||||
#### 2021年01月27日,版本 v1.0.1
|
||||
- **1. 优化** 配置从文件中读取
|
||||
|
||||
#### 2021年01月26日,版本 v1.0.0
|
||||
- **1. 发布** 第一个版本
|
||||
|
||||
</details>
|
||||
|
||||
****
|
||||
|
||||
## 功能建议/问题反馈
|
||||
|
||||
如果你遇到什么问题,可以先去 [**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) 来告诉我!
|
||||
136
script/cfst_3proxy.bat
Normal file
136
script/cfst_3proxy.bat
Normal file
@@ -0,0 +1,136 @@
|
||||
:: --------------------------------------------------------------
|
||||
:: <09><>Ŀ: CloudflareSpeedTest <20>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD> 3Proxy
|
||||
:: <09>汾: 1.0.5
|
||||
:: <09><><EFBFBD><EFBFBD>: XIU2
|
||||
:: <09><>Ŀ: https://github.com/XIU2/CloudflareSpeedTest
|
||||
:: --------------------------------------------------------------
|
||||
@echo off
|
||||
Setlocal Enabledelayedexpansion
|
||||
|
||||
::<3A>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7>ѻ<EFBFBD><D1BB>ù<EFBFBD><C3B9><EFBFBD>ԱȨ<D4B1><C8A8>
|
||||
|
||||
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
|
||||
|
||||
if '%errorlevel%' NEQ '0' (
|
||||
goto UACPrompt
|
||||
) else ( goto gotAdmin )
|
||||
|
||||
::д<><D0B4> vbs <20>ű<EFBFBD><C5B1>Թ<EFBFBD><D4B9><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1>ű<EFBFBD><C5B1><EFBFBD>bat<61><74>
|
||||
|
||||
:UACPrompt
|
||||
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
|
||||
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
|
||||
"%temp%\getadmin.vbs"
|
||||
exit /B
|
||||
|
||||
::<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ vbs <20>ű<EFBFBD><C5B1><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD>ɾ<EFBFBD><C9BE>
|
||||
|
||||
:gotAdmin
|
||||
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
|
||||
pushd "%CD%"
|
||||
CD /D "%~dp0"
|
||||
|
||||
|
||||
::<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7>Ի<EFBFBD><D4BB>ù<EFBFBD><C3B9><EFBFBD>ԱȨ<D4B1>ޣ<EFBFBD><DEA3><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>о<EFBFBD>ȥ<EFBFBD><C8A5>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DZ<EFBFBD><C7B1>ű<EFBFBD><C5B1><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
|
||||
|
||||
|
||||
::<3A><><EFBFBD><EFBFBD> nowip_3proxy.txt <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>˵<EFBFBD><CBB5><EFBFBD>ǵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>иýű<C3BD>
|
||||
if not exist "nowip_3proxy.txt" (
|
||||
echo <20>ýű<C3BD><C5B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ CloudflareST <20><><EFBFBD>ٺ<EFBFBD><D9BA><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD> IP <20><><EFBFBD>滻 3Proxy <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>е<EFBFBD> Cloudflare CDN IP<49><50>
|
||||
echo <20><><EFBFBD><EFBFBD><D4B0><EFBFBD><EFBFBD><EFBFBD> Cloudflare CDN IP <20><><EFBFBD>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP<49><50>ʵ<EFBFBD><CAB5>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ݵļ<DDB5><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> Cloudflare CDN <20><><EFBFBD><EFBFBD>վ<EFBFBD><D5BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫһ<D2AA><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Hosts <20>ˣ<EFBFBD><CBA3><EFBFBD>
|
||||
echo ʹ<><CAB9>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>https://github.com/XIU2/CloudflareSpeedTest/discussions/71
|
||||
echo.
|
||||
set /p nowip="<EFBFBD><EFBFBD><EFBFBD>뵱ǰ 3Proxy <20><><EFBFBD><EFBFBD>ʹ<EFBFBD>õ<EFBFBD> Cloudflare CDN IP <20><><EFBFBD>س<EFBFBD><D8B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ò<EFBFBD><C3B2>裩:"
|
||||
echo !nowip!>nowip_3proxy.txt
|
||||
echo.
|
||||
)
|
||||
|
||||
::<3A><> nowip_3proxy.txt <20>ļ<EFBFBD><C4BC><EFBFBD>ȡ<EFBFBD><C8A1>ǰʹ<C7B0>õ<EFBFBD> Cloudflare CDN IP
|
||||
set /p nowip=<nowip_3proxy.txt
|
||||
echo <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>...
|
||||
|
||||
|
||||
:: <20><><EFBFBD><EFBFBD> RESET <20>Ǹ<EFBFBD><C7B8><EFBFBD>Ҫ "<22>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP <20><>һֱѭ<D6B1><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ" <20><><EFBFBD>ܵ<EFBFBD><DCB5><EFBFBD><EFBFBD><D7BC><EFBFBD><EFBFBD>
|
||||
:: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܾͰ<DCBE><CDB0><EFBFBD><EFBFBD><EFBFBD> 3 <20><> goto :STOP <20><>Ϊ goto :RESET <20><><EFBFBD><EFBFBD>
|
||||
:RESET
|
||||
|
||||
|
||||
:: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD> CloudflareST <20><><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD>echo.| <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6>س<EFBFBD><D8B3>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><F2A3A8B2><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD> -p 0 <20><><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>
|
||||
echo.|CloudflareST.exe -o "result_3proxy.txt"
|
||||
|
||||
|
||||
:: <20>жϽ<D0B6><CFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 0
|
||||
if not exist result_3proxy.txt (
|
||||
echo.
|
||||
echo CloudflareST <20><><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD> IP <20><><EFBFBD><EFBFBD>Ϊ 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>沽<EFBFBD><E6B2BD>...
|
||||
goto :STOP
|
||||
)
|
||||
|
||||
:: <20><>ȡ<EFBFBD><C8A1>һ<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> IP
|
||||
for /f "tokens=1 delims=," %%i in (result_3proxy.txt) do (
|
||||
set /a n+=1
|
||||
If !n!==2 (
|
||||
set bestip=%%i
|
||||
goto :END
|
||||
)
|
||||
)
|
||||
:END
|
||||
|
||||
:: <20>жϸոջ<D5B8>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP <20>Ƿ<EFBFBD>Ϊ<EFBFBD>գ<EFBFBD><D5A3>Լ<EFBFBD><D4BC>Ƿ<EFBFBD><C7B7>;<EFBFBD> IP һ<><D2BB>
|
||||
if "%bestip%"=="" (
|
||||
echo.
|
||||
echo CloudflareST <20><><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD> IP <20><><EFBFBD><EFBFBD>Ϊ 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>沽<EFBFBD><E6B2BD>...
|
||||
goto :STOP
|
||||
)
|
||||
if "%bestip%"=="%nowip%" (
|
||||
echo.
|
||||
echo CloudflareST <20><><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD> IP <20><><EFBFBD><EFBFBD>Ϊ 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>沽<EFBFBD><E6B2BD>...
|
||||
goto :STOP
|
||||
)
|
||||
|
||||
|
||||
:: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD> "<22>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP <20><>һֱѭ<D6B1><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ" <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
|
||||
:: <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD><D9B6><EFBFBD><EFBFBD>ޣ<EFBFBD><DEA3><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP <20><>û<EFBFBD>ҵ<EFBFBD>ʱ<EFBFBD><CAB1>CloudflareST <20>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP <20><><EFBFBD><EFBFBD>
|
||||
:: <20><><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>ָ<EFBFBD><D6B8> -sl <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4>뿪ͷ<EBBFAA><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD> :: ð<><C3B0>ע<EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϣ<D0B6><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>10 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 11<31><31>
|
||||
::set /a v=0
|
||||
::for /f %%a in ('type result_3proxy.txt') do set /a v+=1
|
||||
::if %v% GTR 11 (
|
||||
:: echo.
|
||||
:: echo CloudflareST <20><><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ҵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP<49><50><EFBFBD><EFBFBD><EFBFBD>²<EFBFBD><C2B2><EFBFBD>...
|
||||
:: goto :RESET
|
||||
::)
|
||||
|
||||
|
||||
echo %bestip%>nowip_3proxy.txt
|
||||
echo.
|
||||
echo <20><> IP Ϊ %nowip%
|
||||
echo <20><> IP Ϊ %bestip%
|
||||
|
||||
|
||||
|
||||
:: <20>뽫<EFBFBD><EBBDAB><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> D:\Program Files\3Proxy <20><>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD> 3Proxy <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼
|
||||
CD /d "D:\Program Files\3Proxy"
|
||||
:: <20><>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD><EFBFBD>иýű<C3BD>ǰ<EFBFBD><C7B0><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD>Թ<EFBFBD> 3Proxy <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD>ʹ<EFBFBD>ã<EFBFBD>
|
||||
|
||||
|
||||
|
||||
echo.
|
||||
echo <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD> 3proxy.cfg <20>ļ<EFBFBD><C4BC><EFBFBD>3proxy.cfg_backup<75><70>...
|
||||
copy 3proxy.cfg 3proxy.cfg_backup
|
||||
echo.
|
||||
echo <20><>ʼ<EFBFBD>滻...
|
||||
(
|
||||
for /f "tokens=*" %%i in (3proxy.cfg_backup) do (
|
||||
set s=%%i
|
||||
set s=!s:%nowip%=%bestip%!
|
||||
echo !s!
|
||||
)
|
||||
)>3proxy.cfg
|
||||
|
||||
net stop 3proxy
|
||||
net start 3proxy
|
||||
|
||||
echo <20><><EFBFBD><EFBFBD>...
|
||||
echo.
|
||||
:STOP
|
||||
pause
|
||||
38
script/cfst_ddns.bat
Normal file
38
script/cfst_ddns.bat
Normal file
@@ -0,0 +1,38 @@
|
||||
:: --------------------------------------------------------------
|
||||
:: 项目: CloudflareSpeedTest 自动更新域名解析记录
|
||||
:: 版本: 1.0.4
|
||||
:: 作者: XIU2
|
||||
:: 项目: https://github.com/XIU2/CloudflareSpeedTest
|
||||
:: --------------------------------------------------------------
|
||||
@echo off
|
||||
Setlocal Enabledelayedexpansion
|
||||
|
||||
:: 这里可以自己添加、修改 CloudflareST 的运行参数,echo.| 的作用是自动回车退出程序(不再需要加上 -p 0 参数了)
|
||||
echo.|CloudflareST.exe -o "result_ddns.txt"
|
||||
|
||||
:: 判断结果文件是否存在,如果不存在说明结果为 0
|
||||
if not exist result_ddns.txt (
|
||||
echo.
|
||||
echo CloudflareST 测速结果 IP 数量为 0,跳过下面步骤...
|
||||
goto :END
|
||||
)
|
||||
|
||||
for /f "tokens=1 delims=," %%i in (result_ddns.txt) do (
|
||||
Set /a n+=1
|
||||
If !n!==2 (
|
||||
Echo %%i
|
||||
if "%%i"=="" (
|
||||
echo.
|
||||
echo CloudflareST 测速结果 IP 数量为 0,跳过下面步骤...
|
||||
goto :END
|
||||
)
|
||||
curl -X PUT "https://api.cloudflare.com/client/v4/zones/域名ID/dns_records/域名解析记录ID" ^
|
||||
-H "X-Auth-Email: 账号邮箱" ^
|
||||
-H "X-Auth-Key: 前面获取的 API 令牌" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
--data "{\"type\":\"A\",\"name\":\"完整域名\",\"content\":\"%%i\",\"ttl\":1,\"proxied\":true}"
|
||||
goto :END
|
||||
)
|
||||
)
|
||||
:END
|
||||
pause
|
||||
55
script/cfst_ddns.sh
Normal file
55
script/cfst_ddns.sh
Normal file
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env bash
|
||||
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
|
||||
export PATH
|
||||
# --------------------------------------------------------------
|
||||
# 项目: CloudflareSpeedTest 自动更新域名解析记录
|
||||
# 版本: 1.0.4
|
||||
# 作者: XIU2
|
||||
# 项目: https://github.com/XIU2/CloudflareSpeedTest
|
||||
# --------------------------------------------------------------
|
||||
|
||||
_READ() {
|
||||
[[ ! -e "cfst_ddns.conf" ]] && echo -e "[错误] 配置文件不存在 [cfst_ddns.conf] !" && exit 1
|
||||
CONFIG=$(cat "cfst_ddns.conf")
|
||||
FOLDER=$(echo "${CONFIG}"|grep 'FOLDER='|awk -F '=' '{print $NF}')
|
||||
[[ -z "${FOLDER}" ]] && echo -e "[错误] 缺少配置项 [FOLDER] !" && exit 1
|
||||
ZONE_ID=$(echo "${CONFIG}"|grep 'ZONE_ID='|awk -F '=' '{print $NF}')
|
||||
[[ -z "${ZONE_ID}" ]] && echo -e "[错误] 缺少配置项 [ZONE_ID] !" && exit 1
|
||||
DNS_RECORDS_ID=$(echo "${CONFIG}"|grep 'DNS_RECORDS_ID='|awk -F '=' '{print $NF}')
|
||||
[[ -z "${DNS_RECORDS_ID}" ]] && echo -e "[错误] 缺少配置项 [DNS_RECORDS_ID] !" && exit 1
|
||||
EMAIL=$(echo "${CONFIG}"|grep 'EMAIL='|awk -F '=' '{print $NF}')
|
||||
[[ -z "${EMAIL}" ]] && echo -e "[错误] 缺少配置项 [EMAIL] !" && exit 1
|
||||
KEY=$(echo "${CONFIG}"|grep 'KEY='|awk -F '=' '{print $NF}')
|
||||
[[ -z "${KEY}" ]] && echo -e "[错误] 缺少配置项 [KEY] !" && exit 1
|
||||
TYPE=$(echo "${CONFIG}"|grep 'TYPE='|awk -F '=' '{print $NF}')
|
||||
[[ -z "${TYPE}" ]] && echo -e "[错误] 缺少配置项 [TYPE] !" && exit 1
|
||||
NAME=$(echo "${CONFIG}"|grep 'NAME='|awk -F '=' '{print $NF}')
|
||||
[[ -z "${NAME}" ]] && echo -e "[错误] 缺少配置项 [NAME] !" && exit 1
|
||||
TTL=$(echo "${CONFIG}"|grep 'TTL='|awk -F '=' '{print $NF}')
|
||||
[[ -z "${TTL}" ]] && echo -e "[错误] 缺少配置项 [TTL] !" && exit 1
|
||||
PROXIED=$(echo "${CONFIG}"|grep 'PROXIED='|awk -F '=' '{print $NF}')
|
||||
[[ -z "${PROXIED}" ]] && echo -e "[错误] 缺少配置项 [PROXIED] !" && exit 1
|
||||
}
|
||||
|
||||
_UPDATE() {
|
||||
# 这里可以自己添加、修改 CloudflareST 的运行参数
|
||||
./CloudflareST -o "result_ddns.txt"
|
||||
|
||||
# 判断结果文件是否存在,如果不存在说明结果为 0
|
||||
[[ ! -e "result_ddns.txt" ]] && echo "CloudflareST 测速结果 IP 数量为 0,跳过下面步骤..." && exit 0
|
||||
|
||||
CONTENT=$(sed -n "2,1p" result_ddns.txt | awk -F, '{print $1}')
|
||||
if [[ -z "${CONTENT}" ]]; then
|
||||
echo "CloudflareST 测速结果 IP 数量为 0,跳过下面步骤..."
|
||||
exit 0
|
||||
fi
|
||||
curl -X PUT "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${DNS_RECORDS_ID}" \
|
||||
-H "X-Auth-Email: ${EMAIL}" \
|
||||
-H "X-Auth-Key: ${KEY}" \
|
||||
-H "Content-Type: application/json" \
|
||||
--data "{\"type\":\"${TYPE}\",\"name\":\"${NAME}\",\"content\":\"${CONTENT}\",\"ttl\":${TTL},\"proxied\":${PROXIED}}"
|
||||
}
|
||||
|
||||
_READ
|
||||
cd "${FOLDER}"
|
||||
_UPDATE
|
||||
126
script/cfst_hosts.bat
Normal file
126
script/cfst_hosts.bat
Normal file
@@ -0,0 +1,126 @@
|
||||
:: --------------------------------------------------------------
|
||||
:: <09><>Ŀ: CloudflareSpeedTest <20>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD> Hosts
|
||||
:: <09>汾: 1.0.4
|
||||
:: <09><><EFBFBD><EFBFBD>: XIU2
|
||||
:: <09><>Ŀ: https://github.com/XIU2/CloudflareSpeedTest
|
||||
:: --------------------------------------------------------------
|
||||
@echo off
|
||||
Setlocal Enabledelayedexpansion
|
||||
|
||||
::<3A>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7>ѻ<EFBFBD><D1BB>ù<EFBFBD><C3B9><EFBFBD>ԱȨ<D4B1><C8A8>
|
||||
|
||||
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
|
||||
|
||||
if '%errorlevel%' NEQ '0' (
|
||||
goto UACPrompt
|
||||
) else ( goto gotAdmin )
|
||||
|
||||
::д<><D0B4> vbs <20>ű<EFBFBD><C5B1>Թ<EFBFBD><D4B9><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1>ű<EFBFBD><C5B1><EFBFBD>bat<61><74>
|
||||
|
||||
:UACPrompt
|
||||
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
|
||||
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
|
||||
"%temp%\getadmin.vbs"
|
||||
exit /B
|
||||
|
||||
::<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ vbs <20>ű<EFBFBD><C5B1><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD>ɾ<EFBFBD><C9BE>
|
||||
|
||||
:gotAdmin
|
||||
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
|
||||
pushd "%CD%"
|
||||
CD /D "%~dp0"
|
||||
|
||||
|
||||
::<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7>Ի<EFBFBD><D4BB>ù<EFBFBD><C3B9><EFBFBD>ԱȨ<D4B1>ޣ<EFBFBD><DEA3><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>о<EFBFBD>ȥ<EFBFBD><C8A5>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DZ<EFBFBD><C7B1>ű<EFBFBD><C5B1><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
|
||||
|
||||
|
||||
::<3A><><EFBFBD><EFBFBD> nowip_hosts.txt <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>˵<EFBFBD><CBB5><EFBFBD>ǵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>иýű<C3BD>
|
||||
if not exist "nowip_hosts.txt" (
|
||||
echo <20>ýű<C3BD><C5B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ CloudflareST <20><><EFBFBD>ٺ<EFBFBD><D9BA><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD> IP <20><><EFBFBD>滻 Hosts <20>е<EFBFBD> Cloudflare CDN IP<49><50>
|
||||
echo ʹ<><CAB9>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>https://github.com/XIU2/CloudflareSpeedTest/issues/42#issuecomment-768273768
|
||||
echo.
|
||||
echo <20><>һ<EFBFBD><D2BB>ʹ<EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD>Ƚ<EFBFBD> Hosts <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Cloudflare CDN IP ͳһ<CDB3><D2BB>Ϊһ<CEAA><D2BB> IP<49><50>
|
||||
set /p nowip="<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Cloudflare CDN IP <20><><EFBFBD>س<EFBFBD><D8B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ò<EFBFBD><C3B2>裩:"
|
||||
echo !nowip!>nowip_hosts.txt
|
||||
echo.
|
||||
)
|
||||
|
||||
::<3A><> nowip_hosts.txt <20>ļ<EFBFBD><C4BC><EFBFBD>ȡ<EFBFBD><C8A1>ǰ Hosts <20><>ʹ<EFBFBD>õ<EFBFBD> Cloudflare CDN IP
|
||||
set /p nowip=<nowip_hosts.txt
|
||||
echo <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>...
|
||||
|
||||
|
||||
:: <20><><EFBFBD><EFBFBD> RESET <20>Ǹ<EFBFBD><C7B8><EFBFBD>Ҫ "<22>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP <20><>һֱѭ<D6B1><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ" <20><><EFBFBD>ܵ<EFBFBD><DCB5><EFBFBD><EFBFBD><D7BC><EFBFBD><EFBFBD>
|
||||
:: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܾͰ<DCBE><CDB0><EFBFBD><EFBFBD><EFBFBD> 3 <20><> goto :STOP <20><>Ϊ goto :RESET <20><><EFBFBD><EFBFBD>
|
||||
:RESET
|
||||
|
||||
|
||||
:: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD> CloudflareST <20><><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD>echo.| <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6>س<EFBFBD><D8B3>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><F2A3A8B2><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD> -p 0 <20><><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>
|
||||
echo.|CloudflareST.exe -o "result_hosts.txt"
|
||||
|
||||
|
||||
:: <20>жϽ<D0B6><CFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 0
|
||||
if not exist result_hosts.txt (
|
||||
echo.
|
||||
echo CloudflareST <20><><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD> IP <20><><EFBFBD><EFBFBD>Ϊ 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>沽<EFBFBD><E6B2BD>...
|
||||
goto :STOP
|
||||
)
|
||||
|
||||
:: <20><>ȡ<EFBFBD><C8A1>һ<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> IP
|
||||
for /f "tokens=1 delims=," %%i in (result_hosts.txt) do (
|
||||
SET /a n+=1
|
||||
If !n!==2 (
|
||||
SET bestip=%%i
|
||||
goto :END
|
||||
)
|
||||
)
|
||||
:END
|
||||
|
||||
:: <20>жϸոջ<D5B8>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP <20>Ƿ<EFBFBD>Ϊ<EFBFBD>գ<EFBFBD><D5A3>Լ<EFBFBD><D4BC>Ƿ<EFBFBD><C7B7>;<EFBFBD> IP һ<><D2BB>
|
||||
if "%bestip%"=="" (
|
||||
echo.
|
||||
echo CloudflareST <20><><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD> IP <20><><EFBFBD><EFBFBD>Ϊ 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>沽<EFBFBD><E6B2BD>...
|
||||
goto :STOP
|
||||
)
|
||||
if "%bestip%"=="%nowip%" (
|
||||
echo.
|
||||
echo CloudflareST <20><><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD> IP <20><><EFBFBD><EFBFBD>Ϊ 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>沽<EFBFBD><E6B2BD>...
|
||||
goto :STOP
|
||||
)
|
||||
|
||||
|
||||
:: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD> "<22>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP <20><>һֱѭ<D6B1><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ" <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
|
||||
:: <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD><D9B6><EFBFBD><EFBFBD>ޣ<EFBFBD><DEA3><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP <20><>û<EFBFBD>ҵ<EFBFBD>ʱ<EFBFBD><CAB1>CloudflareST <20>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP <20><><EFBFBD><EFBFBD>
|
||||
:: <20><><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>ָ<EFBFBD><D6B8> -sl <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4>뿪ͷ<EBBFAA><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD> :: ð<><C3B0>ע<EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϣ<D0B6><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>10 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 11<31><31>
|
||||
::set /a v=0
|
||||
::for /f %%a in ('type result_hosts.txt') do set /a v+=1
|
||||
::if %v% GTR 11 (
|
||||
:: echo.
|
||||
:: echo CloudflareST <20><><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ҵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP<49><50><EFBFBD><EFBFBD><EFBFBD>²<EFBFBD><C2B2><EFBFBD>...
|
||||
:: goto :RESET
|
||||
::)
|
||||
|
||||
|
||||
echo %bestip%>nowip_hosts.txt
|
||||
echo.
|
||||
echo <20><> IP Ϊ %nowip%
|
||||
echo <20><> IP Ϊ %bestip%
|
||||
|
||||
CD /d "C:\Windows\System32\drivers\etc"
|
||||
echo.
|
||||
echo <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD> Hosts <20>ļ<EFBFBD><C4BC><EFBFBD>hosts_backup<75><70>...
|
||||
copy hosts hosts_backup
|
||||
echo.
|
||||
echo <20><>ʼ<EFBFBD>滻...
|
||||
(
|
||||
for /f "tokens=*" %%i in (hosts_backup) do (
|
||||
set s=%%i
|
||||
set s=!s:%nowip%=%bestip%!
|
||||
echo !s!
|
||||
)
|
||||
)>hosts
|
||||
|
||||
echo <20><><EFBFBD><EFBFBD>...
|
||||
echo.
|
||||
:STOP
|
||||
pause
|
||||
63
script/cfst_hosts.sh
Normal file
63
script/cfst_hosts.sh
Normal file
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env bash
|
||||
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
|
||||
export PATH
|
||||
# --------------------------------------------------------------
|
||||
# 项目: CloudflareSpeedTest 自动更新 Hosts
|
||||
# 版本: 1.0.4
|
||||
# 作者: XIU2
|
||||
# 项目: https://github.com/XIU2/CloudflareSpeedTest
|
||||
# --------------------------------------------------------------
|
||||
|
||||
_CHECK() {
|
||||
while true
|
||||
do
|
||||
if [[ ! -e "nowip_hosts.txt" ]]; then
|
||||
echo -e "该脚本的作用为 CloudflareST 测速后获取最快 IP 并替换 Hosts 中的 Cloudflare CDN IP。\n使用前请先阅读:https://github.com/XIU2/CloudflareSpeedTest/issues/42#issuecomment-768273848"
|
||||
echo -e "第一次使用,请先将 Hosts 中所有 Cloudflare CDN IP 统一改为一个 IP。"
|
||||
read -e -p "输入该 Cloudflare CDN IP 并回车(后续不再需要该步骤):" NOWIP
|
||||
if [[ ! -z "${NOWIP}" ]]; then
|
||||
echo ${NOWIP} > nowip_hosts.txt
|
||||
break
|
||||
else
|
||||
echo "该 IP 不能是空!"
|
||||
fi
|
||||
else
|
||||
break
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
_UPDATE() {
|
||||
echo -e "开始测速..."
|
||||
NOWIP=$(head -1 nowip_hosts.txt)
|
||||
|
||||
# 这里可以自己添加、修改 CloudflareST 的运行参数
|
||||
./CloudflareST -o "result_hosts.txt"
|
||||
|
||||
# 如果需要 "找不到满足条件的 IP 就一直循环测速下去",那么可以将下面的两个 exit 0 改为 _UPDATE 即可
|
||||
[[ ! -e "result_hosts.txt" ]] && echo "CloudflareST 测速结果 IP 数量为 0,跳过下面步骤..." && exit 0
|
||||
|
||||
# 下面这行代码是 "找不到满足条件的 IP 就一直循环测速下去" 才需要的代码
|
||||
# 考虑到当指定了下载速度下限,但一个满足全部条件的 IP 都没找到时,CloudflareST 就会输出所有 IP 结果
|
||||
# 因此当你指定 -sl 参数时,需要移除下面这段代码开头的 # 井号注释符,来做文件行数判断(比如下载测速数量:10 个,那么下面的值就设在为 11)
|
||||
#[[ $(cat result_hosts.txt|wc -l) > 11 ]] && echo "CloudflareST 测速结果没有找到一个完全满足条件的 IP,重新测速..." && _UPDATE
|
||||
|
||||
|
||||
BESTIP=$(sed -n "2,1p" result_hosts.txt | awk -F, '{print $1}')
|
||||
if [[ -z "${BESTIP}" ]]; then
|
||||
echo "CloudflareST 测速结果 IP 数量为 0,跳过下面步骤..."
|
||||
exit 0
|
||||
fi
|
||||
echo ${BESTIP} > nowip_hosts.txt
|
||||
echo -e "\n旧 IP 为 ${NOWIP}\n新 IP 为 ${BESTIP}\n"
|
||||
|
||||
echo "开始备份 Hosts 文件(hosts_backup)..."
|
||||
\cp -f /etc/hosts /etc/hosts_backup
|
||||
|
||||
echo -e "开始替换..."
|
||||
sed -i 's/'${NOWIP}'/'${BESTIP}'/g' /etc/hosts
|
||||
echo -e "完成..."
|
||||
}
|
||||
|
||||
_CHECK
|
||||
_UPDATE
|
||||
63
script/cfst_hosts_mac.sh
Normal file
63
script/cfst_hosts_mac.sh
Normal file
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env bash
|
||||
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
|
||||
export PATH
|
||||
# --------------------------------------------------------------
|
||||
# 项目: CloudflareSpeedTest 自动更新 Hosts
|
||||
# 版本: 1.0.4
|
||||
# 作者: XIU2
|
||||
# 项目: https://github.com/XIU2/CloudflareSpeedTest
|
||||
# --------------------------------------------------------------
|
||||
|
||||
_CHECK() {
|
||||
while true
|
||||
do
|
||||
if [[ ! -e "nowip_hosts.txt" ]]; then
|
||||
echo -e "该脚本的作用为 CloudflareST 测速后获取最快 IP 并替换 Hosts 中的 Cloudflare CDN IP。\n使用前请先阅读:https://github.com/XIU2/CloudflareSpeedTest/issues/42#issuecomment-768273848"
|
||||
echo -e "第一次使用,请先将 Hosts 中所有 Cloudflare CDN IP 统一改为一个 IP。"
|
||||
read -e -p "输入该 Cloudflare CDN IP 并回车(后续不再需要该步骤):" NOWIP
|
||||
if [[ ! -z "${NOWIP}" ]]; then
|
||||
echo ${NOWIP} > nowip_hosts.txt
|
||||
break
|
||||
else
|
||||
echo "该 IP 不能是空!"
|
||||
fi
|
||||
else
|
||||
break
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
_UPDATE() {
|
||||
echo -e "开始测速..."
|
||||
NOWIP=$(head -1 nowip_hosts.txt)
|
||||
|
||||
# 这里可以自己添加、修改 CloudflareST 的运行参数
|
||||
./CloudflareST -o "result_hosts.txt"
|
||||
|
||||
# 如果需要 "找不到满足条件的 IP 就一直循环测速下去",那么可以将下面的两个 exit 0 改为 _UPDATE 即可
|
||||
[[ ! -e "result_hosts.txt" ]] && echo "CloudflareST 测速结果 IP 数量为 0,跳过下面步骤..." && exit 0
|
||||
|
||||
# 下面这行代码是 "找不到满足条件的 IP 就一直循环测速下去" 才需要的代码
|
||||
# 考虑到当指定了下载速度下限,但一个满足全部条件的 IP 都没找到时,CloudflareST 就会输出所有 IP 结果
|
||||
# 因此当你指定 -sl 参数时,需要移除下面这段代码开头的 # 井号注释符,来做文件行数判断(比如下载测速数量:10 个,那么下面的值就设在为 11)
|
||||
#[[ $(cat result_hosts.txt|wc -l) > 11 ]] && echo "CloudflareST 测速结果没有找到一个完全满足条件的 IP,重新测速..." && _UPDATE
|
||||
|
||||
|
||||
BESTIP=$(sed -n "2,1p" result_hosts.txt | awk -F, '{print $1}')
|
||||
if [[ -z "${BESTIP}" ]]; then
|
||||
echo "CloudflareST 测速结果 IP 数量为 0,跳过下面步骤..."
|
||||
exit 0
|
||||
fi
|
||||
echo ${BESTIP} > nowip_hosts.txt
|
||||
echo -e "\n旧 IP 为 ${NOWIP}\n新 IP 为 ${BESTIP}\n"
|
||||
|
||||
echo "开始备份 Hosts 文件(hosts_backup)..."
|
||||
\cp -f /etc/hosts /etc/hosts_backup
|
||||
|
||||
echo -e "开始替换..."
|
||||
sed -i '' 's/'${NOWIP}'/'${BESTIP}'/g' /etc/hosts
|
||||
echo -e "完成..."
|
||||
}
|
||||
|
||||
_CHECK
|
||||
_UPDATE
|
||||
183
task/download.go
Normal file
183
task/download.go
Normal file
@@ -0,0 +1,183 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/XIU2/CloudflareSpeedTest/utils"
|
||||
|
||||
"github.com/VividCortex/ewma"
|
||||
)
|
||||
|
||||
const (
|
||||
bufferSize = 1024
|
||||
defaultURL = "https://cf.xiu2.xyz/url"
|
||||
defaultTimeout = 10 * time.Second
|
||||
defaultDisableDownload = false
|
||||
defaultTestNum = 10
|
||||
defaultMinSpeed float64 = 0.0
|
||||
)
|
||||
|
||||
var (
|
||||
URL = defaultURL
|
||||
Timeout = defaultTimeout
|
||||
Disable = defaultDisableDownload
|
||||
|
||||
TestCount = defaultTestNum
|
||||
MinSpeed = defaultMinSpeed
|
||||
)
|
||||
|
||||
func checkDownloadDefault() {
|
||||
if URL == "" {
|
||||
URL = defaultURL
|
||||
}
|
||||
if Timeout <= 0 {
|
||||
Timeout = defaultTimeout
|
||||
}
|
||||
if TestCount <= 0 {
|
||||
TestCount = defaultTestNum
|
||||
}
|
||||
if MinSpeed <= 0.0 {
|
||||
MinSpeed = defaultMinSpeed
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownloadSpeed(ipSet utils.PingDelaySet) (speedSet utils.DownloadSpeedSet) {
|
||||
checkDownloadDefault()
|
||||
if Disable {
|
||||
return utils.DownloadSpeedSet(ipSet)
|
||||
}
|
||||
if len(ipSet) <= 0 { // IP数组长度(IP数量) 大于 0 时才会继续下载测速
|
||||
fmt.Println("\n[信息] 延迟测速结果 IP 数量为 0,跳过下载测速。")
|
||||
return
|
||||
}
|
||||
testNum := TestCount
|
||||
if len(ipSet) < TestCount || MinSpeed > 0 { // 如果IP数组长度(IP数量) 小于下载测速数量(-dn),则次数修正为IP数
|
||||
testNum = len(ipSet)
|
||||
}
|
||||
if testNum < TestCount {
|
||||
TestCount = testNum
|
||||
}
|
||||
|
||||
fmt.Printf("开始下载测速(下载速度下限:%.2f MB/s,下载测速数量:%d,下载测速队列:%d):\n", MinSpeed, TestCount, testNum)
|
||||
// 控制 下载测速进度条 与 延迟测速进度条 长度一致(强迫症)
|
||||
bar_a := len(strconv.Itoa(len(ipSet)))
|
||||
bar_b := " "
|
||||
for i := 0; i < bar_a; i++ {
|
||||
bar_b += " "
|
||||
}
|
||||
bar := utils.NewBar(TestCount, bar_b, "")
|
||||
for i := 0; i < testNum; i++ {
|
||||
speed := downloadHandler(ipSet[i].IP)
|
||||
ipSet[i].DownloadSpeed = speed
|
||||
// 在每个 IP 下载测速后,以 [下载速度下限] 条件过滤结果
|
||||
if speed >= MinSpeed*1024*1024 {
|
||||
bar.Grow(1, "")
|
||||
speedSet = append(speedSet, ipSet[i]) // 高于下载速度下限时,添加到新数组中
|
||||
if len(speedSet) == TestCount { // 凑够满足条件的 IP 时(下载测速数量 -dn),就跳出循环
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
bar.Done()
|
||||
if len(speedSet) == 0 { // 没有符合速度限制的数据,返回所有测试数据
|
||||
speedSet = utils.DownloadSpeedSet(ipSet)
|
||||
}
|
||||
// 按速度排序
|
||||
sort.Sort(speedSet)
|
||||
return
|
||||
}
|
||||
|
||||
func getDialContext(ip *net.IPAddr) func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
var fakeSourceAddr string
|
||||
if isIPv4(ip.String()) {
|
||||
fakeSourceAddr = fmt.Sprintf("%s:%d", ip.String(), TCPPort)
|
||||
} else {
|
||||
fakeSourceAddr = fmt.Sprintf("[%s]:%d", ip.String(), TCPPort)
|
||||
}
|
||||
return func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return (&net.Dialer{}).DialContext(ctx, network, fakeSourceAddr)
|
||||
}
|
||||
}
|
||||
|
||||
// return download Speed
|
||||
func downloadHandler(ip *net.IPAddr) float64 {
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{DialContext: getDialContext(ip)},
|
||||
Timeout: Timeout,
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
if len(via) > 10 { // 限制最多重定向 10 次
|
||||
return http.ErrUseLastResponse
|
||||
}
|
||||
if req.Header.Get("Referer") == defaultURL { // 当使用默认下载测速地址时,重定向不携带 Referer
|
||||
req.Header.Del("Referer")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
req, err := http.NewRequest("GET", URL, nil)
|
||||
if err != nil {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36")
|
||||
|
||||
response, err := client.Do(req)
|
||||
if err != nil {
|
||||
return 0.0
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != 200 {
|
||||
return 0.0
|
||||
}
|
||||
timeStart := time.Now() // 开始时间(当前)
|
||||
timeEnd := timeStart.Add(Timeout) // 加上下载测速时间得到的结束时间
|
||||
|
||||
contentLength := response.ContentLength // 文件大小
|
||||
buffer := make([]byte, bufferSize)
|
||||
|
||||
var (
|
||||
contentRead int64 = 0
|
||||
timeSlice = Timeout / 100
|
||||
timeCounter = 1
|
||||
lastContentRead int64 = 0
|
||||
)
|
||||
|
||||
var nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
|
||||
e := ewma.NewMovingAverage()
|
||||
|
||||
// 循环计算,如果文件下载完了(两者相等),则退出循环(终止测速)
|
||||
for contentLength != contentRead {
|
||||
currentTime := time.Now()
|
||||
if currentTime.After(nextTime) {
|
||||
timeCounter++
|
||||
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)
|
||||
if err != nil {
|
||||
if err != io.EOF { // 如果文件下载过程中遇到报错(如 Timeout),且并不是因为文件下载完了,则退出循环(终止测速)
|
||||
break
|
||||
} else if contentLength == -1 { // 文件下载完成 且 文件大小未知,则退出循环(终止测速),例如:https://speed.cloudflare.com/__down?bytes=200000000 这样的,如果在 10 秒内就下载完成了,会导致测速结果明显偏低甚至显示为 0.00(下载速度太快时)
|
||||
break
|
||||
}
|
||||
// 获取上个时间片
|
||||
last_time_slice := timeStart.Add(timeSlice * time.Duration(timeCounter-1))
|
||||
// 下载数据量 / (用当前时间 - 上个时间片/ 时间片)
|
||||
e.Add(float64(contentRead-lastContentRead) / (float64(currentTime.Sub(last_time_slice)) / float64(timeSlice)))
|
||||
}
|
||||
contentRead += int64(bufferRead)
|
||||
}
|
||||
return e.Value() / (Timeout.Seconds() / 120)
|
||||
}
|
||||
141
task/httping.go
Normal file
141
task/httping.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
//"crypto/tls"
|
||||
//"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
Httping bool
|
||||
HttpingStatusCode int
|
||||
HttpingCFColo string
|
||||
HttpingCFColomap *sync.Map
|
||||
OutRegexp = regexp.MustCompile(`[A-Z]{3}`)
|
||||
)
|
||||
|
||||
// pingReceived pingTotalTime
|
||||
func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration) {
|
||||
hc := http.Client{
|
||||
Timeout: time.Second * 2,
|
||||
Transport: &http.Transport{
|
||||
DialContext: getDialContext(ip),
|
||||
//TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // 跳过证书验证
|
||||
},
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse // 阻止重定向
|
||||
},
|
||||
}
|
||||
|
||||
// 先访问一次获得 HTTP 状态码 及 Cloudflare Colo
|
||||
{
|
||||
requ, err := http.NewRequest(http.MethodHead, URL, nil)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
requ.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36")
|
||||
resp, err := hc.Do(requ)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
//fmt.Println("IP:", ip, "StatusCode:", resp.StatusCode, resp.Request.URL)
|
||||
// 如果未指定的 HTTP 状态码,或指定的状态码不合规,则默认只认为 200、301、302 才算 HTTPing 通过
|
||||
if HttpingStatusCode == 0 || HttpingStatusCode < 100 && HttpingStatusCode > 599 {
|
||||
if resp.StatusCode != 200 && resp.StatusCode != 301 && resp.StatusCode != 302 {
|
||||
return 0, 0
|
||||
}
|
||||
} else {
|
||||
if resp.StatusCode != HttpingStatusCode {
|
||||
return 0, 0
|
||||
}
|
||||
}
|
||||
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
|
||||
// 只有指定了地区才匹配机场三字码
|
||||
if HttpingCFColo != "" {
|
||||
// 通过头部 Server 值判断是 Cloudflare 还是 AWS CloudFront 并设置 cfRay 为各自的机场三字码完整内容
|
||||
cfRay := func() string {
|
||||
if resp.Header.Get("Server") == "cloudflare" {
|
||||
return resp.Header.Get("CF-RAY") // 示例 cf-ray: 7bd32409eda7b020-SJC
|
||||
}
|
||||
return resp.Header.Get("x-amz-cf-pop") // 示例 X-Amz-Cf-Pop: SIN52-P1
|
||||
}()
|
||||
colo := p.getColo(cfRay)
|
||||
if colo == "" { // 没有匹配到三字码或不符合指定地区则直接结束该 IP 测试
|
||||
return 0, 0
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 循环测速计算延迟
|
||||
success := 0
|
||||
var delay time.Duration
|
||||
for i := 0; i < PingTimes; i++ {
|
||||
requ, err := http.NewRequest(http.MethodHead, URL, nil)
|
||||
if err != nil {
|
||||
log.Fatal("意外的错误,情报告:", err)
|
||||
return 0, 0
|
||||
}
|
||||
requ.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36")
|
||||
if i == PingTimes-1 {
|
||||
requ.Header.Set("Connection", "close")
|
||||
}
|
||||
startTime := time.Now()
|
||||
resp, err := hc.Do(requ)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
success++
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
duration := time.Since(startTime)
|
||||
delay += duration
|
||||
|
||||
}
|
||||
|
||||
return success, delay
|
||||
|
||||
}
|
||||
|
||||
func MapColoMap() *sync.Map {
|
||||
if HttpingCFColo == "" {
|
||||
return nil
|
||||
}
|
||||
// 将参数指定的地区三字码转为大写并格式化
|
||||
colos := strings.Split(strings.ToUpper(HttpingCFColo), ",")
|
||||
colomap := &sync.Map{}
|
||||
for _, colo := range colos {
|
||||
colomap.Store(colo, colo)
|
||||
}
|
||||
return colomap
|
||||
}
|
||||
|
||||
func (p *Ping) getColo(b string) string {
|
||||
if b == "" {
|
||||
return ""
|
||||
}
|
||||
// 正则匹配并返回 机场三字码
|
||||
out := OutRegexp.FindString(b)
|
||||
|
||||
if HttpingCFColomap == nil {
|
||||
return out
|
||||
}
|
||||
// 匹配 机场三字码 是否为指定的地区
|
||||
_, ok := HttpingCFColomap.Load(out)
|
||||
if ok {
|
||||
return out
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
172
task/ip.go
Normal file
172
task/ip.go
Normal file
@@ -0,0 +1,172 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const defaultInputFile = "ip.txt"
|
||||
|
||||
var (
|
||||
// TestAll test all ip
|
||||
TestAll = false
|
||||
// IPFile is the filename of IP Rangs
|
||||
IPFile = defaultInputFile
|
||||
IPText string
|
||||
)
|
||||
|
||||
func InitRandSeed() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
func isIPv4(ip string) bool {
|
||||
return strings.Contains(ip, ".")
|
||||
}
|
||||
|
||||
func randIPEndWith(num byte) byte {
|
||||
if num == 0 { // 对于 /32 这种单独的 IP
|
||||
return byte(0)
|
||||
}
|
||||
return byte(rand.Intn(int(num)))
|
||||
}
|
||||
|
||||
type IPRanges struct {
|
||||
ips []*net.IPAddr
|
||||
mask string
|
||||
firstIP net.IP
|
||||
ipNet *net.IPNet
|
||||
}
|
||||
|
||||
func newIPRanges() *IPRanges {
|
||||
return &IPRanges{
|
||||
ips: make([]*net.IPAddr, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *IPRanges) fixIP(ip string) string {
|
||||
// 如果不含有 '/' 则代表不是 IP 段,而是一个单独的 IP,因此需要加上 /32 /128 子网掩码
|
||||
if i := strings.IndexByte(ip, '/'); i < 0 {
|
||||
if isIPv4(ip) {
|
||||
r.mask = "/32"
|
||||
} else {
|
||||
r.mask = "/128"
|
||||
}
|
||||
ip += r.mask
|
||||
} else {
|
||||
r.mask = ip[i:]
|
||||
}
|
||||
return ip
|
||||
}
|
||||
|
||||
func (r *IPRanges) parseCIDR(ip string) {
|
||||
var err error
|
||||
if r.firstIP, r.ipNet, err = net.ParseCIDR(r.fixIP(ip)); err != nil {
|
||||
log.Fatalln("ParseCIDR err", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *IPRanges) appendIPv4(d byte) {
|
||||
r.appendIP(net.IPv4(r.firstIP[12], r.firstIP[13], r.firstIP[14], d))
|
||||
}
|
||||
|
||||
func (r *IPRanges) appendIP(ip net.IP) {
|
||||
r.ips = append(r.ips, &net.IPAddr{IP: ip})
|
||||
}
|
||||
|
||||
// 返回第四段 ip 的最小值及可用数目
|
||||
func (r *IPRanges) getIPRange() (minIP, hosts byte) {
|
||||
minIP = r.firstIP[15] & r.ipNet.Mask[3] // IP 第四段最小值
|
||||
|
||||
// 根据子网掩码获取主机数量
|
||||
m := net.IPv4Mask(255, 255, 255, 255)
|
||||
for i, v := range r.ipNet.Mask {
|
||||
m[i] ^= v
|
||||
}
|
||||
total, _ := strconv.ParseInt(m.String(), 16, 32) // 总可用 IP 数
|
||||
if total > 255 { // 矫正 第四段 可用 IP 数
|
||||
hosts = 255
|
||||
return
|
||||
}
|
||||
hosts = byte(total)
|
||||
return
|
||||
}
|
||||
|
||||
func (r *IPRanges) chooseIPv4() {
|
||||
minIP, hosts := r.getIPRange()
|
||||
for r.ipNet.Contains(r.firstIP) {
|
||||
if TestAll { // 如果是测速全部 IP
|
||||
for i := 0; i <= int(hosts); i++ { // 遍历 IP 最后一段最小值到最大值
|
||||
r.appendIPv4(byte(i) + minIP)
|
||||
}
|
||||
} else { // 随机 IP 的最后一段 0.0.0.X
|
||||
r.appendIPv4(minIP + randIPEndWith(hosts))
|
||||
}
|
||||
r.firstIP[14]++ // 0.0.(X+1).X
|
||||
if r.firstIP[14] == 0 {
|
||||
r.firstIP[13]++ // 0.(X+1).X.X
|
||||
if r.firstIP[13] == 0 {
|
||||
r.firstIP[12]++ // (X+1).X.X.X
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *IPRanges) chooseIPv6() {
|
||||
var tempIP uint8
|
||||
for r.ipNet.Contains(r.firstIP) {
|
||||
if r.mask != "/128" {
|
||||
r.firstIP[15] = randIPEndWith(255) // 随机 IP 的最后一段
|
||||
r.firstIP[14] = randIPEndWith(255) // 随机 IP 的最后一段
|
||||
}
|
||||
targetIP := make([]byte, len(r.firstIP))
|
||||
copy(targetIP, r.firstIP)
|
||||
r.appendIP(targetIP)
|
||||
for i := 13; i >= 0; i-- {
|
||||
tempIP = r.firstIP[i]
|
||||
r.firstIP[i] += randIPEndWith(255)
|
||||
if r.firstIP[i] >= tempIP {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func loadIPRanges() []*net.IPAddr {
|
||||
ranges := newIPRanges()
|
||||
if IPText != "" { // 从参数中获取 IP 段数据
|
||||
IPs := strings.Split(IPText, ",")
|
||||
for _, IP := range IPs {
|
||||
ranges.parseCIDR(IP)
|
||||
if isIPv4(IP) {
|
||||
ranges.chooseIPv4()
|
||||
} else {
|
||||
ranges.chooseIPv6()
|
||||
}
|
||||
}
|
||||
} else { // 从文件中获取 IP 段数据
|
||||
if IPFile == "" {
|
||||
IPFile = defaultInputFile
|
||||
}
|
||||
file, err := os.Open(IPFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
ranges.parseCIDR(scanner.Text())
|
||||
if isIPv4(scanner.Text()) {
|
||||
ranges.chooseIPv4()
|
||||
} else {
|
||||
ranges.chooseIPv6()
|
||||
}
|
||||
}
|
||||
}
|
||||
return ranges.ips
|
||||
}
|
||||
147
task/tcping.go
Normal file
147
task/tcping.go
Normal file
@@ -0,0 +1,147 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/XIU2/CloudflareSpeedTest/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
tcpConnectTimeout = time.Second * 1
|
||||
maxRoutine = 1000
|
||||
defaultRoutines = 200
|
||||
defaultPort = 443
|
||||
defaultPingTimes = 4
|
||||
)
|
||||
|
||||
var (
|
||||
Routines = defaultRoutines
|
||||
TCPPort int = defaultPort
|
||||
PingTimes int = defaultPingTimes
|
||||
)
|
||||
|
||||
type Ping struct {
|
||||
wg *sync.WaitGroup
|
||||
m *sync.Mutex
|
||||
ips []*net.IPAddr
|
||||
csv utils.PingDelaySet
|
||||
control chan bool
|
||||
bar *utils.Bar
|
||||
}
|
||||
|
||||
func checkPingDefault() {
|
||||
if Routines <= 0 {
|
||||
Routines = defaultRoutines
|
||||
}
|
||||
if TCPPort <= 0 || TCPPort >= 65535 {
|
||||
TCPPort = defaultPort
|
||||
}
|
||||
if PingTimes <= 0 {
|
||||
PingTimes = defaultPingTimes
|
||||
}
|
||||
}
|
||||
|
||||
func NewPing() *Ping {
|
||||
checkPingDefault()
|
||||
ips := loadIPRanges()
|
||||
return &Ping{
|
||||
wg: &sync.WaitGroup{},
|
||||
m: &sync.Mutex{},
|
||||
ips: ips,
|
||||
csv: make(utils.PingDelaySet, 0),
|
||||
control: make(chan bool, Routines),
|
||||
bar: utils.NewBar(len(ips), "可用:", ""),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Ping) Run() utils.PingDelaySet {
|
||||
if len(p.ips) == 0 {
|
||||
return p.csv
|
||||
}
|
||||
if Httping {
|
||||
fmt.Printf("开始延迟测速(模式:HTTP,端口:%d,平均延迟上限:%v ms,平均延迟下限:%v ms)\n", TCPPort, utils.InputMaxDelay.Milliseconds(), utils.InputMinDelay.Milliseconds())
|
||||
} else {
|
||||
fmt.Printf("开始延迟测速(模式:TCP,端口:%d,平均延迟上限:%v ms,平均延迟下限:%v ms)\n", TCPPort, utils.InputMaxDelay.Milliseconds(), utils.InputMinDelay.Milliseconds())
|
||||
}
|
||||
for _, ip := range p.ips {
|
||||
p.wg.Add(1)
|
||||
p.control <- false
|
||||
go p.start(ip)
|
||||
}
|
||||
p.wg.Wait()
|
||||
p.bar.Done()
|
||||
sort.Sort(p.csv)
|
||||
return p.csv
|
||||
}
|
||||
|
||||
func (p *Ping) start(ip *net.IPAddr) {
|
||||
defer p.wg.Done()
|
||||
p.tcpingHandler(ip)
|
||||
<-p.control
|
||||
}
|
||||
|
||||
// bool connectionSucceed float32 time
|
||||
func (p *Ping) tcping(ip *net.IPAddr) (bool, time.Duration) {
|
||||
startTime := time.Now()
|
||||
var fullAddress string
|
||||
if isIPv4(ip.String()) {
|
||||
fullAddress = fmt.Sprintf("%s:%d", ip.String(), TCPPort)
|
||||
} else {
|
||||
fullAddress = fmt.Sprintf("[%s]:%d", ip.String(), TCPPort)
|
||||
}
|
||||
conn, err := net.DialTimeout("tcp", fullAddress, tcpConnectTimeout)
|
||||
if err != nil {
|
||||
return false, 0
|
||||
}
|
||||
defer conn.Close()
|
||||
duration := time.Since(startTime)
|
||||
return true, duration
|
||||
}
|
||||
|
||||
// pingReceived pingTotalTime
|
||||
func (p *Ping) checkConnection(ip *net.IPAddr) (recv int, totalDelay time.Duration) {
|
||||
if Httping {
|
||||
recv, totalDelay = p.httping(ip)
|
||||
return
|
||||
}
|
||||
for i := 0; i < PingTimes; i++ {
|
||||
if ok, delay := p.tcping(ip); ok {
|
||||
recv++
|
||||
totalDelay += delay
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Ping) appendIPData(data *utils.PingData) {
|
||||
p.m.Lock()
|
||||
defer p.m.Unlock()
|
||||
p.csv = append(p.csv, utils.CloudflareIPData{
|
||||
PingData: data,
|
||||
})
|
||||
}
|
||||
|
||||
// handle tcping
|
||||
func (p *Ping) tcpingHandler(ip *net.IPAddr) {
|
||||
recv, totalDlay := p.checkConnection(ip)
|
||||
nowAble := len(p.csv)
|
||||
if recv != 0 {
|
||||
nowAble++
|
||||
}
|
||||
p.bar.Grow(1, strconv.Itoa(nowAble))
|
||||
if recv == 0 {
|
||||
return
|
||||
}
|
||||
data := &utils.PingData{
|
||||
IP: ip,
|
||||
Sended: PingTimes,
|
||||
Received: recv,
|
||||
Delay: totalDlay / time.Duration(recv),
|
||||
}
|
||||
p.appendIPData(data)
|
||||
}
|
||||
159
tcping.go
159
tcping.go
@@ -1,159 +0,0 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
124
util.go
124
util.go
@@ -1,124 +0,0 @@
|
||||
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 {
|
||||
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 地址", "Ping 发送次数", "Ping 接收次数", "Ping 接收率", "平均延迟", "下载速度 (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', 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
|
||||
|
||||
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
|
||||
|
||||
var failTime int
|
||||
|
||||
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]
|
||||
}
|
||||
169
utils/csv.go
Normal file
169
utils/csv.go
Normal file
@@ -0,0 +1,169 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultOutput = "result.csv"
|
||||
maxDelay = 9999 * time.Millisecond
|
||||
minDelay = 0 * time.Millisecond
|
||||
)
|
||||
|
||||
var (
|
||||
InputMaxDelay = maxDelay
|
||||
InputMinDelay = minDelay
|
||||
Output = defaultOutput
|
||||
PrintNum = 10
|
||||
)
|
||||
|
||||
// 是否打印测试结果
|
||||
func NoPrintResult() bool {
|
||||
return PrintNum == 0
|
||||
}
|
||||
|
||||
// 是否输出到文件
|
||||
func noOutput() bool {
|
||||
return Output == "" || Output == " "
|
||||
}
|
||||
|
||||
type PingData struct {
|
||||
IP *net.IPAddr
|
||||
Sended int
|
||||
Received int
|
||||
Delay time.Duration
|
||||
}
|
||||
|
||||
type CloudflareIPData struct {
|
||||
*PingData
|
||||
recvRate float32
|
||||
DownloadSpeed float64
|
||||
}
|
||||
|
||||
func (cf *CloudflareIPData) getRecvRate() float32 {
|
||||
if cf.recvRate == 0 {
|
||||
pingLost := cf.Sended - cf.Received
|
||||
cf.recvRate = float32(pingLost) / float32(cf.Sended)
|
||||
}
|
||||
return cf.recvRate
|
||||
}
|
||||
|
||||
func (cf *CloudflareIPData) toString() []string {
|
||||
result := make([]string, 6)
|
||||
result[0] = cf.IP.String()
|
||||
result[1] = strconv.Itoa(cf.Sended)
|
||||
result[2] = strconv.Itoa(cf.Received)
|
||||
result[3] = strconv.FormatFloat(float64(cf.getRecvRate()), 'f', 2, 32)
|
||||
result[4] = strconv.FormatFloat(cf.Delay.Seconds()*1000, 'f', 2, 32)
|
||||
result[5] = strconv.FormatFloat(cf.DownloadSpeed/1024/1024, 'f', 2, 32)
|
||||
return result
|
||||
}
|
||||
|
||||
func ExportCsv(data []CloudflareIPData) {
|
||||
if noOutput() || len(data) == 0 {
|
||||
return
|
||||
}
|
||||
fp, err := os.Create(Output)
|
||||
if err != nil {
|
||||
log.Fatalf("创建文件[%s]失败:%v", Output, err)
|
||||
return
|
||||
}
|
||||
defer fp.Close()
|
||||
w := csv.NewWriter(fp) //创建一个新的写入文件流
|
||||
_ = w.Write([]string{"IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)"})
|
||||
_ = w.WriteAll(convertToString(data))
|
||||
w.Flush()
|
||||
}
|
||||
|
||||
func convertToString(data []CloudflareIPData) [][]string {
|
||||
result := make([][]string, 0)
|
||||
for _, v := range data {
|
||||
result = append(result, v.toString())
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
type PingDelaySet []CloudflareIPData
|
||||
|
||||
func (s PingDelaySet) FilterDelay() (data PingDelaySet) {
|
||||
if InputMaxDelay > maxDelay || InputMinDelay < minDelay {
|
||||
return s
|
||||
}
|
||||
for _, v := range s {
|
||||
if v.Delay > InputMaxDelay { // 平均延迟上限
|
||||
break
|
||||
}
|
||||
if v.Delay < InputMinDelay { // 平均延迟下限
|
||||
continue
|
||||
}
|
||||
data = append(data, v) // 延迟满足条件时,添加到新数组中
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s PingDelaySet) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
|
||||
func (s PingDelaySet) Less(i, j int) bool {
|
||||
iRate, jRate := s[i].getRecvRate(), s[j].getRecvRate()
|
||||
if iRate != jRate {
|
||||
return iRate < jRate
|
||||
}
|
||||
return s[i].Delay < s[j].Delay
|
||||
}
|
||||
|
||||
func (s PingDelaySet) Swap(i, j int) {
|
||||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
|
||||
// 下载速度排序
|
||||
type DownloadSpeedSet []CloudflareIPData
|
||||
|
||||
func (s DownloadSpeedSet) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
|
||||
func (s DownloadSpeedSet) Less(i, j int) bool {
|
||||
return s[i].DownloadSpeed > s[j].DownloadSpeed
|
||||
}
|
||||
|
||||
func (s DownloadSpeedSet) Swap(i, j int) {
|
||||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
|
||||
func (s DownloadSpeedSet) Print() {
|
||||
if NoPrintResult() {
|
||||
return
|
||||
}
|
||||
if len(s) <= 0 { // IP数组长度(IP数量) 大于 0 时继续
|
||||
fmt.Println("\n[信息] 完整测速结果 IP 数量为 0,跳过输出结果。")
|
||||
return
|
||||
}
|
||||
dateString := convertToString(s) // 转为多维数组 [][]String
|
||||
if len(dateString) < PrintNum { // 如果IP数组长度(IP数量) 小于 打印次数,则次数改为IP数量
|
||||
PrintNum = len(dateString)
|
||||
}
|
||||
headFormat := "%-16s%-5s%-5s%-5s%-6s%-11s\n"
|
||||
dataFormat := "%-18s%-8s%-8s%-8s%-10s%-15s\n"
|
||||
for i := 0; i < PrintNum; i++ { // 如果要输出的 IP 中包含 IPv6,那么就需要调整一下间隔
|
||||
if len(dateString[i][0]) > 15 {
|
||||
headFormat = "%-40s%-5s%-5s%-5s%-6s%-11s\n"
|
||||
dataFormat = "%-42s%-8s%-8s%-8s%-10s%-15s\n"
|
||||
break
|
||||
}
|
||||
}
|
||||
fmt.Printf(headFormat, "IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)")
|
||||
for i := 0; i < PrintNum; i++ {
|
||||
fmt.Printf(dataFormat, dateString[i][0], dateString[i][1], dateString[i][2], dateString[i][3], dateString[i][4], dateString[i][5])
|
||||
}
|
||||
if !noOutput() {
|
||||
fmt.Printf("\n完整测速结果已写入 %v 文件,可使用记事本/表格软件查看。\n", Output)
|
||||
}
|
||||
}
|
||||
25
utils/progress.go
Normal file
25
utils/progress.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cheggaaa/pb/v3"
|
||||
)
|
||||
|
||||
type Bar struct {
|
||||
pb *pb.ProgressBar
|
||||
}
|
||||
|
||||
func NewBar(count int, MyStrStart, MyStrEnd string) *Bar {
|
||||
tmpl := fmt.Sprintf(`{{counters . }} {{ bar . "[" "-" (cycle . "↖" "↗" "↘" "↙" ) "_" "]"}} %s {{string . "MyStr" | green}} %s `, MyStrStart, MyStrEnd)
|
||||
bar := pb.ProgressBarTemplate(tmpl).Start(count)
|
||||
return &Bar{pb: bar}
|
||||
}
|
||||
|
||||
func (b *Bar) Grow(num int, MyStrVal string) {
|
||||
b.pb.Set("MyStr", MyStrVal).Add(num)
|
||||
}
|
||||
|
||||
func (b *Bar) Done() {
|
||||
b.pb.Finish()
|
||||
}
|
||||
Reference in New Issue
Block a user